|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* notify_self.c - example of how you can use overflow notifications
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* Copyright (c) 2002-2006 Hewlett-Packard Development Company, L.P.
|
|
Packit |
577717 |
* Contributed by Stephane Eranian <eranian@hpl.hp.com>
|
|
Packit |
577717 |
* Modified by Phil Mucci <mucci@cs.utk.edu> to add the fork()
|
|
Packit |
577717 |
* Adapted to v2.0 interface by Stephane Eranian <eranian@gmail.com>
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
Packit |
577717 |
* of this software and associated documentation files (the "Software"), to deal
|
|
Packit |
577717 |
* in the Software without restriction, including without limitation the rights
|
|
Packit |
577717 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
Packit |
577717 |
* of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
Packit |
577717 |
* subject to the following conditions:
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* The above copyright notice and this permission notice shall be included in all
|
|
Packit |
577717 |
* copies or substantial portions of the Software.
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
Packit |
577717 |
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
Packit |
577717 |
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
Packit |
577717 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
Packit |
577717 |
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
Packit |
577717 |
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* This file is part of libpfm, a performance monitoring support library for
|
|
Packit |
577717 |
* applications on Linux/ia64.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
#include <sys/types.h>
|
|
Packit |
577717 |
#include <inttypes.h>
|
|
Packit |
577717 |
#include <stdio.h>
|
|
Packit |
577717 |
#include <stdlib.h>
|
|
Packit |
577717 |
#include <stdarg.h>
|
|
Packit |
577717 |
#include <errno.h>
|
|
Packit |
577717 |
#include <unistd.h>
|
|
Packit |
577717 |
#include <string.h>
|
|
Packit |
577717 |
#include <signal.h>
|
|
Packit |
577717 |
#include <fcntl.h>
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#include <perfmon/perfmon.h>
|
|
Packit |
577717 |
#include <perfmon/pfmlib.h>
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#define SMPL_PERIOD 1000000000UL
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static volatile unsigned long notification_received;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#define NUM_PMCS PFMLIB_MAX_PMCS
|
|
Packit |
577717 |
#define NUM_PMDS PFMLIB_MAX_PMDS
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static pfarg_reg_t pd[NUM_PMDS];
|
|
Packit |
577717 |
static int ctx_fd;
|
|
Packit |
577717 |
static char *event1_name;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void fatal_error(char *fmt,...) __attribute__((noreturn));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void
|
|
Packit |
577717 |
fatal_error(char *fmt, ...)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
va_list ap;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
va_start(ap, fmt);
|
|
Packit |
577717 |
vfprintf(stderr, fmt, ap);
|
|
Packit |
577717 |
va_end(ap);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
exit(1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void
|
|
Packit |
577717 |
sigio_handler(int n, struct siginfo *info, struct sigcontext *sc)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
pfm_msg_t msg;
|
|
Packit |
577717 |
int fd = ctx_fd;
|
|
Packit |
577717 |
int r;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (fd != ctx_fd) {
|
|
Packit |
577717 |
fatal_error("handler does not get valid file descriptor\n");
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (event1_name && perfmonctl(fd, PFM_READ_PMDS, pd+1, 1) == -1) {
|
|
Packit |
577717 |
fatal_error("PFM_READ_PMDS: %s", strerror(errno));
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
r = read(fd, &msg, sizeof(msg));
|
|
Packit |
577717 |
if (r != sizeof(msg)) {
|
|
Packit |
577717 |
fatal_error("cannot read overflow message: %s\n", strerror(errno));
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (msg.type != PFM_MSG_OVFL) {
|
|
Packit |
577717 |
fatal_error("unexpected msg type: %d\n",msg.type);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* XXX: risky to do printf() in signal handler!
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (event1_name)
|
|
Packit |
577717 |
printf("Notification %lu: %"PRIu64" %s\n", notification_received, pd[1].reg_value, event1_name);
|
|
Packit |
577717 |
else
|
|
Packit |
577717 |
printf("Notification %lu\n", notification_received);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* At this point, the counter used for the sampling period has already
|
|
Packit |
577717 |
* be reset by the kernel because we are in non-blocking mode, self-monitoring.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* increment our notification counter
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
notification_received++;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* And resume monitoring
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (perfmonctl(fd, PFM_RESTART,NULL, 0) == -1) {
|
|
Packit |
577717 |
fatal_error("PFM_RESTART: %s", strerror(errno));
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* infinite loop waiting for notification to get out
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
void
|
|
Packit |
577717 |
busyloop(void)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* busy loop to burn CPU cycles
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
for(;notification_received < 3;) ;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* forking causes the context to be shared with the child
|
|
Packit |
577717 |
* When the child terminates, it closes its descriptor.
|
|
Packit |
577717 |
* The parent's remains and notification keep on coming.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (fork() == 0) {
|
|
Packit |
577717 |
printf("child terminates\n");
|
|
Packit |
577717 |
fflush(stdout);
|
|
Packit |
577717 |
exit(0);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
printf("after fork\n");
|
|
Packit |
577717 |
fflush(stdout);
|
|
Packit |
577717 |
for(;notification_received < 6;) ;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
int
|
|
Packit |
577717 |
main(int argc, char **argv)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
pfarg_context_t ctx[1];
|
|
Packit |
577717 |
pfmlib_input_param_t inp;
|
|
Packit |
577717 |
pfmlib_output_param_t outp;
|
|
Packit |
577717 |
pfarg_reg_t pc[NUM_PMCS];
|
|
Packit |
577717 |
pfarg_load_t load_args;
|
|
Packit |
577717 |
pfmlib_options_t pfmlib_options;
|
|
Packit |
577717 |
struct sigaction act;
|
|
Packit |
577717 |
unsigned int i, num_counters;
|
|
Packit |
577717 |
size_t len;
|
|
Packit |
577717 |
int ret;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Initialize pfm library (required before we can use it)
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (pfm_initialize() != PFMLIB_SUCCESS) {
|
|
Packit |
577717 |
printf("Can't initialize library\n");
|
|
Packit |
577717 |
exit(1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Install the signal handler (SIGIO)
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
memset(&act, 0, sizeof(act));
|
|
Packit |
577717 |
act.sa_handler = (sig_t)sigio_handler;
|
|
Packit |
577717 |
sigaction (SIGIO, &act, 0);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* pass options to library (optional)
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
memset(&pfmlib_options, 0, sizeof(pfmlib_options));
|
|
Packit |
577717 |
pfmlib_options.pfm_debug = 0; /* set to 1 for debug */
|
|
Packit |
577717 |
pfm_set_options(&pfmlib_options);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
memset(pc, 0, sizeof(pc));
|
|
Packit |
577717 |
memset(ctx, 0, sizeof(ctx));
|
|
Packit |
577717 |
memset(&load_args, 0, sizeof(load_args));
|
|
Packit |
577717 |
memset(&inp,0, sizeof(inp));
|
|
Packit |
577717 |
memset(&outp,0, sizeof(outp));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
pfm_get_num_counters(&num_counters);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (pfm_get_cycle_event(&inp.pfp_events[0]) != PFMLIB_SUCCESS)
|
|
Packit |
577717 |
fatal_error("cannot find cycle event\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (pfm_get_inst_retired_event(&inp.pfp_events[1]) != PFMLIB_SUCCESS)
|
|
Packit |
577717 |
fatal_error("cannot find inst retired event\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
i = 2;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* set the default privilege mode for all counters:
|
|
Packit |
577717 |
* PFM_PLM3 : user level only
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
inp.pfp_dfl_plm = PFM_PLM3;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (i > num_counters) {
|
|
Packit |
577717 |
i = num_counters;
|
|
Packit |
577717 |
printf("too many events provided (max=%d events), using first %d event(s)\n", num_counters, i);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* how many counters we use
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
inp.pfp_event_count = i;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
pfm_get_max_event_name_len(&len;;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
event1_name = malloc(len+1);
|
|
Packit |
577717 |
if (event1_name == NULL)
|
|
Packit |
577717 |
fatal_error("cannot allocate event name\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
pfm_get_full_event_name(&inp.pfp_events[1], event1_name, len+1);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* let the library figure out the values for the PMCS
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if ((ret=pfm_dispatch_events(&inp, NULL, &outp, NULL)) != PFMLIB_SUCCESS) {
|
|
Packit |
577717 |
fatal_error("Cannot configure events: %s\n", pfm_strerror(ret));
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* now create the context for self monitoring/per-task
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (perfmonctl(0, PFM_CREATE_CONTEXT, ctx, 1) == -1 ) {
|
|
Packit |
577717 |
if (errno == ENOSYS) {
|
|
Packit |
577717 |
fatal_error("Your kernel does not have performance monitoring support!\n");
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
fatal_error("Can't create PFM context %s\n", strerror(errno));
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
ctx_fd = ctx->ctx_fd;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Now prepare the argument to initialize the PMDs and PMCS.
|
|
Packit |
577717 |
* We use pfp_pmc_count to determine the number of registers to
|
|
Packit |
577717 |
* setup. Note that this field can be >= pfp_event_count.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
|
|
Packit |
577717 |
for (i=0; i < outp.pfp_pmc_count; i++) {
|
|
Packit |
577717 |
pc[i].reg_num = outp.pfp_pmcs[i].reg_num;
|
|
Packit |
577717 |
pc[i].reg_value = outp.pfp_pmcs[i].reg_value;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
for (i=0; i < inp.pfp_event_count; i++) {
|
|
Packit |
577717 |
pd[i].reg_num = pc[i].reg_num;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* We want to get notified when the counter used for our first
|
|
Packit |
577717 |
* event overflows
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
pc[0].reg_flags |= PFM_REGFL_OVFL_NOTIFY;
|
|
Packit |
577717 |
pc[0].reg_reset_pmds[0] |= 1UL << outp.pfp_pmcs[1].reg_num;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* we arm the first counter, such that it will overflow
|
|
Packit |
577717 |
* after SMPL_PERIOD events have been observed
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
pd[0].reg_value = (~0UL) - SMPL_PERIOD + 1;
|
|
Packit |
577717 |
pd[0].reg_long_reset = (~0UL) - SMPL_PERIOD + 1;
|
|
Packit |
577717 |
pd[0].reg_short_reset = (~0UL) - SMPL_PERIOD + 1;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Now program the registers
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* We don't use the save variable to indicate the number of elements passed to
|
|
Packit |
577717 |
* the kernel because, as we said earlier, pc may contain more elements than
|
|
Packit |
577717 |
* the number of events we specified, i.e., contains more than counting monitors.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (perfmonctl(ctx_fd, PFM_WRITE_PMCS, pc, outp.pfp_pmc_count) == -1) {
|
|
Packit |
577717 |
fatal_error("perfmonctl error PFM_WRITE_PMCS errno %d\n",errno);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (perfmonctl(ctx_fd, PFM_WRITE_PMDS, pd, inp.pfp_event_count) == -1) {
|
|
Packit |
577717 |
fatal_error("perfmonctl error PFM_WRITE_PMDS errno %d\n",errno);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* we want to monitor ourself
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
load_args.load_pid = getpid();
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (perfmonctl(ctx_fd, PFM_LOAD_CONTEXT, &load_args, 1) == -1) {
|
|
Packit |
577717 |
fatal_error("perfmonctl error PFM_WRITE_PMDS errno %d\n",errno);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* setup asynchronous notification on the file descriptor
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ret = fcntl(ctx_fd, F_SETFL, fcntl(ctx_fd, F_GETFL, 0) | O_ASYNC);
|
|
Packit |
577717 |
if (ret == -1) {
|
|
Packit |
577717 |
fatal_error("cannot set ASYNC: %s\n", strerror(errno));
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* get ownership of the descriptor
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ret = fcntl(ctx_fd, F_SETOWN, getpid());
|
|
Packit |
577717 |
if (ret == -1) {
|
|
Packit |
577717 |
fatal_error("cannot setown: %s\n", strerror(errno));
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Let's roll now
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
pfm_self_start(ctx_fd);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
busyloop();
|
|
Packit |
577717 |
|
|
Packit |
577717 |
pfm_self_stop(ctx_fd);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* free our context
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
close(ctx_fd);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
return 0;
|
|
Packit |
577717 |
}
|