Blame src/libpfm-3.y/examples_v2.x/notify_self.c

Packit Service a1973e
/*
Packit Service a1973e
 * notify_self.c - example of how you can use overflow notifications
Packit Service a1973e
 *
Packit Service a1973e
 * Copyright (c) 2002-2006 Hewlett-Packard Development Company, L.P.
Packit Service a1973e
 * Contributed by Stephane Eranian <eranian@hpl.hp.com>
Packit Service a1973e
 *
Packit Service a1973e
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit Service a1973e
 * of this software and associated documentation files (the "Software"), to deal
Packit Service a1973e
 * in the Software without restriction, including without limitation the rights
Packit Service a1973e
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
Packit Service a1973e
 * of the Software, and to permit persons to whom the Software is furnished to do so,
Packit Service a1973e
 * subject to the following conditions:
Packit Service a1973e
 *
Packit Service a1973e
 * The above copyright notice and this permission notice shall be included in all
Packit Service a1973e
 * copies or substantial portions of the Software.
Packit Service a1973e
 *
Packit Service a1973e
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
Packit Service a1973e
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
Packit Service a1973e
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
Packit Service a1973e
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Packit Service a1973e
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
Packit Service a1973e
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit Service a1973e
 */
Packit Service a1973e
#include <sys/types.h>
Packit Service a1973e
#include <inttypes.h>
Packit Service a1973e
#include <stdio.h>
Packit Service a1973e
#include <stdlib.h>
Packit Service a1973e
#include <stdarg.h>
Packit Service a1973e
#include <errno.h>
Packit Service a1973e
#include <unistd.h>
Packit Service a1973e
#include <string.h>
Packit Service a1973e
#include <signal.h>
Packit Service a1973e
#include <fcntl.h>
Packit Service a1973e
Packit Service a1973e
Packit Service a1973e
#include <perfmon/perfmon.h>
Packit Service a1973e
#include <perfmon/pfmlib.h>
Packit Service a1973e
Packit Service a1973e
#include "detect_pmcs.h"
Packit Service a1973e
Packit Service a1973e
#define SMPL_PERIOD	1000000000ULL
Packit Service a1973e
Packit Service a1973e
static volatile unsigned long notification_received;
Packit Service a1973e
Packit Service a1973e
#define NUM_PMCS PFMLIB_MAX_PMCS
Packit Service a1973e
#define NUM_PMDS PFMLIB_MAX_PMDS
Packit Service a1973e
Packit Service a1973e
static pfarg_pmd_t pd[NUM_PMDS];
Packit Service a1973e
static int ctx_fd;
Packit Service a1973e
static char *event1_name;
Packit Service a1973e
Packit Service a1973e
static void fatal_error(char *fmt,...) __attribute__((noreturn));
Packit Service a1973e
Packit Service a1973e
static void
Packit Service a1973e
fatal_error(char *fmt, ...)
Packit Service a1973e
{
Packit Service a1973e
	va_list ap;
Packit Service a1973e
Packit Service a1973e
	va_start(ap, fmt);
Packit Service a1973e
	vfprintf(stderr, fmt, ap);
Packit Service a1973e
	va_end(ap);
Packit Service a1973e
Packit Service a1973e
	exit(1);
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
static void
Packit Service a1973e
warning(char *fmt, ...)
Packit Service a1973e
{
Packit Service a1973e
	va_list ap;
Packit Service a1973e
Packit Service a1973e
	va_start(ap, fmt);
Packit Service a1973e
	vfprintf(stderr, fmt, ap);
Packit Service a1973e
	va_end(ap);
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
static void
Packit Service a1973e
sigio_handler(int n)
Packit Service a1973e
{
Packit Service a1973e
	pfarg_msg_t msg;
Packit Service a1973e
	int fd = ctx_fd;
Packit Service a1973e
	int r;
Packit Service a1973e
Packit Service a1973e
	if (event1_name && pfm_read_pmds(fd, pd+1, 1) == -1)
Packit Service a1973e
		fatal_error("pfm_read_pmds: %s", strerror(errno));
Packit Service a1973e
Packit Service a1973e
retry:
Packit Service a1973e
	r = read(fd, &msg, sizeof(msg));
Packit Service a1973e
	if (r != sizeof(msg)) {
Packit Service a1973e
		if(r == -1 && errno == EINTR) {
Packit Service a1973e
			warning("read interrupted, retrying\n");
Packit Service a1973e
			goto retry;
Packit Service a1973e
		}
Packit Service a1973e
		fatal_error("cannot read overflow message: %s\n", strerror(errno));
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	if (msg.type != PFM_MSG_OVFL)
Packit Service a1973e
		fatal_error("unexpected msg type: %d\n",msg.type);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * increment our notification counter
Packit Service a1973e
	 */
Packit Service a1973e
	notification_received++;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * XXX: risky to do printf() in signal handler!
Packit Service a1973e
	 */
Packit Service a1973e
	if (event1_name)
Packit Service a1973e
		printf("Notification %lu: %"PRIu64" %s ip=0x%llx\n",
Packit Service a1973e
			notification_received, pd[1].reg_value,
Packit Service a1973e
			event1_name,
Packit Service a1973e
			(unsigned long long)msg.pfm_ovfl_msg.msg_ovfl_ip);
Packit Service a1973e
	else
Packit Service a1973e
		printf("Notification %lu ip=0x%llx\n",
Packit Service a1973e
			notification_received,
Packit Service a1973e
			(unsigned long long)msg.pfm_ovfl_msg.msg_ovfl_ip);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * And resume monitoring
Packit Service a1973e
	 */
Packit Service a1973e
	if (pfm_restart(fd) == -1)
Packit Service a1973e
		fatal_error("pfm_restart: %d\n", errno);
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
/*
Packit Service a1973e
 * infinite loop waiting for notification to get out
Packit Service a1973e
 */
Packit Service a1973e
void
Packit Service a1973e
busyloop(void)
Packit Service a1973e
{
Packit Service a1973e
	/*
Packit Service a1973e
	 * busy loop to burn CPU cycles
Packit Service a1973e
	 */
Packit Service a1973e
	for(;notification_received < 3;) ;
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
#define BPL (sizeof(uint64_t)<<3)
Packit Service a1973e
#define LBPL	6
Packit Service a1973e
Packit Service a1973e
static inline void pfm_bv_set(uint64_t *bv, uint16_t rnum)
Packit Service a1973e
{
Packit Service a1973e
	bv[rnum>>LBPL] |= 1UL << (rnum&(BPL-1));
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
int
Packit Service a1973e
main(int argc, char **argv)
Packit Service a1973e
{
Packit Service a1973e
	pfarg_ctx_t ctx;
Packit Service a1973e
	pfmlib_input_param_t inp;
Packit Service a1973e
	pfmlib_output_param_t outp;
Packit Service a1973e
	pfarg_pmc_t pc[NUM_PMCS];
Packit Service a1973e
	pfarg_load_t load_args;
Packit Service a1973e
	pfmlib_options_t pfmlib_options;
Packit Service a1973e
	struct sigaction act;
Packit Service a1973e
	unsigned int i, num_counters;
Packit Service a1973e
	size_t len;
Packit Service a1973e
	int ret;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * pass options to library (optional)
Packit Service a1973e
	 */
Packit Service a1973e
	memset(&pfmlib_options, 0, sizeof(pfmlib_options));
Packit Service a1973e
	pfmlib_options.pfm_debug = 0; /* set to 1 for debug */
Packit Service a1973e
	pfmlib_options.pfm_verbose = 1; /* set to 1 for verbose */
Packit Service a1973e
	pfm_set_options(&pfmlib_options);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * Initialize pfm library (required before we can use it)
Packit Service a1973e
	 */
Packit Service a1973e
	ret = pfm_initialize();
Packit Service a1973e
	if (ret != PFMLIB_SUCCESS)
Packit Service a1973e
		fatal_error("Cannot initialize library: %s\n", pfm_strerror(ret));
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * Install the signal handler (SIGIO)
Packit Service a1973e
	 */
Packit Service a1973e
	memset(&act, 0, sizeof(act));
Packit Service a1973e
	act.sa_handler = sigio_handler;
Packit Service a1973e
	sigaction (SIGIO, &act, 0);
Packit Service a1973e
Packit Service a1973e
	memset(pc, 0, sizeof(pc));
Packit Service a1973e
	memset(&ctx, 0, sizeof(ctx));
Packit Service a1973e
	memset(&load_args, 0, sizeof(load_args));
Packit Service a1973e
	memset(&inp,0, sizeof(inp));
Packit Service a1973e
	memset(&outp,0, sizeof(outp));
Packit Service a1973e
Packit Service a1973e
	pfm_get_num_counters(&num_counters);
Packit Service a1973e
Packit Service a1973e
	if (pfm_get_cycle_event(&inp.pfp_events[0]) != PFMLIB_SUCCESS)
Packit Service a1973e
		fatal_error("cannot find cycle event\n");
Packit Service a1973e
Packit Service a1973e
	if (pfm_get_inst_retired_event(&inp.pfp_events[1]) != PFMLIB_SUCCESS)
Packit Service a1973e
		fatal_error("cannot find inst retired event\n");
Packit Service a1973e
Packit Service a1973e
	i = 2;
Packit Service a1973e
	
Packit Service a1973e
	/*
Packit Service a1973e
	 * set the default privilege mode for all counters:
Packit Service a1973e
	 * 	PFM_PLM3 : user level only
Packit Service a1973e
	 */
Packit Service a1973e
	inp.pfp_dfl_plm = PFM_PLM3;
Packit Service a1973e
	if (i > num_counters) {
Packit Service a1973e
		i = num_counters;
Packit Service a1973e
		printf("too many events provided (max=%d events), using first %d event(s)\n", num_counters, i);
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	inp.pfp_event_count = i;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * how many counters we use
Packit Service a1973e
	 */
Packit Service a1973e
	if (i > 1) {
Packit Service a1973e
Packit Service a1973e
		pfm_get_max_event_name_len(&len;;
Packit Service a1973e
Packit Service a1973e
		event1_name = malloc(len+1);
Packit Service a1973e
		if (event1_name == NULL)
Packit Service a1973e
			fatal_error("cannot allocate event name\n");
Packit Service a1973e
Packit Service a1973e
		pfm_get_full_event_name(&inp.pfp_events[1], event1_name, len+1);
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * now create the context for self monitoring/per-task
Packit Service a1973e
	 */
Packit Service a1973e
	ctx_fd = pfm_create_context(&ctx, NULL, NULL, 0);
Packit Service a1973e
	if (ctx_fd == -1) {
Packit Service a1973e
		if (errno == ENOSYS) {
Packit Service a1973e
			fatal_error("Your kernel does not have performance monitoring support!\n");
Packit Service a1973e
		}
Packit Service a1973e
		fatal_error("Can't create PFM context %s\n", strerror(errno));
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * build the pfp_unavail_pmcs bitmask by looking
Packit Service a1973e
	 * at what perfmon has available. It is not always
Packit Service a1973e
	 * the case that all PMU registers are actually available
Packit Service a1973e
	 * to applications. For instance, on IA-32 platforms, some
Packit Service a1973e
	 * registers may be reserved for the NMI watchdog timer.
Packit Service a1973e
	 *
Packit Service a1973e
	 * With this bitmap, the library knows which registers NOT to
Packit Service a1973e
	 * use. Of source, it is possible that no valid assignement may
Packit Service a1973e
	 * be possible if certina PMU registers  are not available.
Packit Service a1973e
	 */
Packit Service a1973e
	detect_unavail_pmcs(ctx_fd, &inp.pfp_unavail_pmcs);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * let the library figure out the values for the PMCS
Packit Service a1973e
	 */
Packit Service a1973e
	if ((ret=pfm_dispatch_events(&inp, NULL, &outp, NULL)) != PFMLIB_SUCCESS)
Packit Service a1973e
		fatal_error("Cannot configure events: %s\n", pfm_strerror(ret));
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * Now prepare the argument to initialize the PMDs and PMCS.
Packit Service a1973e
	 */
Packit Service a1973e
Packit Service a1973e
	for (i=0; i < outp.pfp_pmc_count; i++) {
Packit Service a1973e
		pc[i].reg_num   = outp.pfp_pmcs[i].reg_num;
Packit Service a1973e
		pc[i].reg_value = outp.pfp_pmcs[i].reg_value;
Packit Service a1973e
	}
Packit Service a1973e
	for (i=0; i < outp.pfp_pmd_count; i++)
Packit Service a1973e
		pd[i].reg_num = outp.pfp_pmds[i].reg_num;
Packit Service a1973e
	/*
Packit Service a1973e
	 * We want to get notified when the counter used for our first
Packit Service a1973e
	 * event overflows
Packit Service a1973e
	 */
Packit Service a1973e
	pd[0].reg_flags |= PFM_REGFL_OVFL_NOTIFY;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * nothing to sample when only one counter
Packit Service a1973e
	 */
Packit Service a1973e
	if (inp.pfp_event_count > 1)
Packit Service a1973e
		pfm_bv_set(pd[0].reg_reset_pmds, pd[1].reg_num);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * we arm the first counter, such that it will overflow
Packit Service a1973e
	 * after SMPL_PERIOD events have been observed
Packit Service a1973e
	 */
Packit Service a1973e
	pd[0].reg_value       = - SMPL_PERIOD;
Packit Service a1973e
	pd[0].reg_long_reset  = - SMPL_PERIOD;
Packit Service a1973e
	pd[0].reg_short_reset = - SMPL_PERIOD;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * Now program the registers
Packit Service a1973e
	 */
Packit Service a1973e
	if (pfm_write_pmcs(ctx_fd, pc, outp.pfp_pmc_count))
Packit Service a1973e
		fatal_error("pfm_write_pmcs error errno %d\n",errno);
Packit Service a1973e
Packit Service a1973e
	if (pfm_write_pmds(ctx_fd, pd, outp.pfp_pmd_count))
Packit Service a1973e
		fatal_error("pfm_write_pmds error errno %d\n",errno);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * we want to monitor ourself
Packit Service a1973e
	 */
Packit Service a1973e
	load_args.load_pid = getpid();
Packit Service a1973e
Packit Service a1973e
	if (pfm_load_context(ctx_fd, &load_args))
Packit Service a1973e
		fatal_error("pfm_load_context error errno %d\n",errno);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * setup asynchronous notification on the file descriptor
Packit Service a1973e
	 */
Packit Service a1973e
	ret = fcntl(ctx_fd, F_SETFL, fcntl(ctx_fd, F_GETFL, 0) | O_ASYNC);
Packit Service a1973e
	if (ret == -1)
Packit Service a1973e
		fatal_error("cannot set ASYNC: %s\n", strerror(errno));
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * get ownership of the descriptor
Packit Service a1973e
	 */
Packit Service a1973e
	ret = fcntl(ctx_fd, F_SETOWN, getpid());
Packit Service a1973e
	if (ret == -1)
Packit Service a1973e
		fatal_error("cannot setown: %s\n", strerror(errno));
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * Let's roll now
Packit Service a1973e
	 */
Packit Service a1973e
	pfm_self_start(ctx_fd);
Packit Service a1973e
Packit Service a1973e
	busyloop();
Packit Service a1973e
Packit Service a1973e
	pfm_self_stop(ctx_fd);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * free our context
Packit Service a1973e
	 */
Packit Service a1973e
	close(ctx_fd);
Packit Service a1973e
Packit Service a1973e
	if (event1_name)
Packit Service a1973e
		free(event1_name);
Packit Service a1973e
Packit Service a1973e
	return 0;
Packit Service a1973e
}