Blame src/libpfm-3.y/examples_ia64_v2.0/ita_btb.c

Packit 577717
/*
Packit 577717
 * ita_btb.c - example of how use the BTB with the Itanium PMU
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
 *
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 <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
#include <perfmon/perfmon.h>
Packit 577717
#include <perfmon/perfmon_default_smpl.h>
Packit 577717
#include <perfmon/pfmlib_itanium.h>
Packit 577717
Packit 577717
typedef pfm_default_smpl_hdr_t	btb_hdr_t;
Packit 577717
typedef pfm_default_smpl_entry_t	btb_entry_t;
Packit 577717
typedef pfm_default_smpl_ctx_arg_t	btb_ctx_arg_t;
Packit 577717
#define BTB_FMT_UUID	        	PFM_DEFAULT_SMPL_UUID
Packit 577717
Packit 577717
static pfm_uuid_t buf_fmt_id = BTB_FMT_UUID;
Packit 577717
Packit 577717
#define NUM_PMCS PFMLIB_MAX_PMCS
Packit 577717
#define NUM_PMDS PFMLIB_MAX_PMDS
Packit 577717
Packit 577717
#define MAX_EVT_NAME_LEN	128
Packit 577717
#define MAX_PMU_NAME_LEN	32
Packit 577717
Packit 577717
/*
Packit 577717
 * The BRANCH_EVENT is increment by 1 for each branch event. Such event is composed of
Packit 577717
 * two entries in the BTB: a source and a target entry. The BTB is full after 4 branch
Packit 577717
 * events.
Packit 577717
 */
Packit 577717
#define SMPL_PERIOD	(4UL*256)
Packit 577717
Packit 577717
/*
Packit 577717
 * We use a small buffer size to exercise the overflow handler
Packit 577717
 */
Packit 577717
#define SMPL_BUF_NENTRIES	64
Packit 577717
Packit 577717
#define M_PMD(x)		(1UL<<(x))
Packit 577717
#define BTB_REGS_MASK		(M_PMD(8)|M_PMD(9)|M_PMD(10)|M_PMD(11)|M_PMD(12)|M_PMD(13)|M_PMD(14)|M_PMD(15)|M_PMD(16))
Packit 577717
Packit 577717
static void *smpl_vaddr;
Packit 577717
static unsigned int entry_size;
Packit 577717
static int id;
Packit 577717
Packit 577717
#if defined(__ECC) && defined(__INTEL_COMPILER)
Packit 577717
Packit 577717
/* if you do not have this file, your compiler is too old */
Packit 577717
#include <ia64intrin.h>
Packit 577717
Packit 577717
#define hweight64(x)	_m64_popcnt(x)
Packit 577717
Packit 577717
#elif defined(__GNUC__)
Packit 577717
Packit 577717
static __inline__ int
Packit 577717
hweight64 (unsigned long x)
Packit 577717
{
Packit 577717
	unsigned long result;
Packit 577717
	__asm__ ("popcnt %0=%1" : "=r" (result) : "r" (x));
Packit 577717
	return (int)result;
Packit 577717
}
Packit 577717
Packit 577717
#else
Packit 577717
#error "you need to provide inline assembly from your compiler"
Packit 577717
#endif
Packit 577717
Packit 577717
/*
Packit 577717
 * we don't use static to make sure the compiler does not inline the function
Packit 577717
 */
Packit 577717
long func1(void) { return 0;}
Packit 577717
Packit 577717
long
Packit 577717
do_test(unsigned long loop)
Packit 577717
{
Packit 577717
	long sum  = 0;
Packit 577717
Packit 577717
	while(loop--) {
Packit 577717
		if (loop & 0x1)
Packit 577717
			sum += func1();
Packit 577717
		else
Packit 577717
			sum += loop;
Packit 577717
	}
Packit 577717
	return sum;
Packit 577717
}
Packit 577717
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
/*
Packit 577717
 * print content of sampling buffer
Packit 577717
 *
Packit 577717
 * XXX: using stdio to print from a signal handler is not safe with multi-threaded
Packit 577717
 * applications
Packit 577717
 */
Packit 577717
#define safe_printf	printf
Packit 577717
Packit 577717
static int
Packit 577717
show_btb_reg(int j, pfm_ita_pmd_reg_t reg)
Packit 577717
{
Packit 577717
	int ret;
Packit 577717
	int is_valid = reg.pmd8_15_ita_reg.btb_b == 0 && reg.pmd8_15_ita_reg.btb_mp == 0 ? 0 :1;
Packit 577717
Packit 577717
	ret = safe_printf("\tPMD%-2d: 0x%016lx b=%d mp=%d valid=%c\n",
Packit 577717
			j,
Packit 577717
			reg.pmd_val,
Packit 577717
			 reg.pmd8_15_ita_reg.btb_b,
Packit 577717
			 reg.pmd8_15_ita_reg.btb_mp,
Packit 577717
			is_valid ? 'Y' : 'N');
Packit 577717
Packit 577717
	if (!is_valid) return ret;
Packit 577717
Packit 577717
	if (reg.pmd8_15_ita_reg.btb_b) {
Packit 577717
		unsigned long addr;
Packit 577717
Packit 577717
		addr = 	reg.pmd8_15_ita_reg.btb_addr<<4;
Packit 577717
		addr |= reg.pmd8_15_ita_reg.btb_slot < 3 ?  reg.pmd8_15_ita_reg.btb_slot : 0;
Packit 577717
Packit 577717
		ret = safe_printf("\t       Source Address: 0x%016lx\n"
Packit 577717
				  "\t       Taken=%c Prediction: %s\n\n",
Packit 577717
			 addr,
Packit 577717
			 reg.pmd8_15_ita_reg.btb_slot < 3 ? 'Y' : 'N',
Packit 577717
			 reg.pmd8_15_ita_reg.btb_mp ? "Failure" : "Success");
Packit 577717
	} else {
Packit 577717
		ret = safe_printf("\t       Target Address: 0x%016lx\n\n",
Packit 577717
			 (unsigned long)reg.pmd8_15_ita_reg.btb_addr<<4);
Packit 577717
	}
Packit 577717
	return ret;
Packit 577717
}
Packit 577717
Packit 577717
static void
Packit 577717
show_btb(pfm_ita_pmd_reg_t *btb, pfm_ita_pmd_reg_t *pmd16)
Packit 577717
{
Packit 577717
	int i, last;
Packit 577717
Packit 577717
Packit 577717
	i    = (pmd16->pmd16_ita_reg.btbi_full) ? pmd16->pmd16_ita_reg.btbi_bbi : 0;
Packit 577717
	last = pmd16->pmd16_ita_reg.btbi_bbi;
Packit 577717
Packit 577717
	safe_printf("btb_trace: i=%d last=%d bbi=%d full=%d\n", i, last,pmd16->pmd16_ita_reg.btbi_bbi, pmd16->pmd16_ita_reg.btbi_full);
Packit 577717
	do {
Packit 577717
		show_btb_reg(i+8, btb[i]);
Packit 577717
		i = (i+1) % 8;
Packit 577717
	} while (i != last);
Packit 577717
}
Packit 577717
Packit 577717
Packit 577717
static void
Packit 577717
process_smpl_buffer(void)
Packit 577717
{
Packit 577717
	btb_hdr_t	*hdr;
Packit 577717
	btb_entry_t	*ent;
Packit 577717
	unsigned long pos;
Packit 577717
	unsigned long smpl_entry = 0;
Packit 577717
	pfm_ita_pmd_reg_t *reg, *pmd16;
Packit 577717
	unsigned long i;
Packit 577717
	int ret;
Packit 577717
	static unsigned long last_ovfl = ~0UL;
Packit 577717
Packit 577717
Packit 577717
	hdr = (btb_hdr_t *)smpl_vaddr;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * check that we are not diplaying the previous set of samples again.
Packit 577717
	 * Required to take care of the last batch of samples.
Packit 577717
	 */
Packit 577717
	if (hdr->hdr_overflows <= last_ovfl && last_ovfl != ~0UL) {
Packit 577717
		printf("skipping identical set of samples %lu <= %lu\n", hdr->hdr_overflows, last_ovfl);
Packit 577717
		return;
Packit 577717
	}
Packit 577717
Packit 577717
	pos = (unsigned long)(hdr+1);
Packit 577717
	/*
Packit 577717
	 * walk through all the entries recored in the buffer
Packit 577717
	 */
Packit 577717
	for(i=0; i < hdr->hdr_count; i++) {
Packit 577717
Packit 577717
		ret = 0;
Packit 577717
Packit 577717
		ent = (btb_entry_t *)pos;
Packit 577717
		/*
Packit 577717
		 * print entry header
Packit 577717
		 */
Packit 577717
		safe_printf("Entry %ld PID:%d CPU:%d STAMP:0x%lx IIP:0x%016lx\n",
Packit 577717
			smpl_entry++,
Packit 577717
			ent->pid,
Packit 577717
			ent->cpu,
Packit 577717
			ent->tstamp,
Packit 577717
			ent->ip);
Packit 577717
Packit 577717
		/*
Packit 577717
		 * point to first recorded register (always contiguous with entry header)
Packit 577717
		 */
Packit 577717
		reg = (pfm_ita_pmd_reg_t*)(ent+1);
Packit 577717
Packit 577717
		/*
Packit 577717
		 * in this particular example, we have pmd8-pmd15 has the BTB. We have also
Packit 577717
		 * included pmd16 (BTB index) has part of the registers to record. This trick
Packit 577717
		 * allows us to get the index to decode the sequential order of the BTB.
Packit 577717
		 *
Packit 577717
		 * Recorded registers are always recorded in increasing order. So we know
Packit 577717
		 * that pmd16 is at a fixed offset (+8*sizeof(unsigned long)) from pmd8.
Packit 577717
		 */
Packit 577717
		pmd16 = reg+8;
Packit 577717
		show_btb(reg, pmd16);
Packit 577717
Packit 577717
		/*
Packit 577717
		 * move to next entry
Packit 577717
		 */
Packit 577717
		pos += entry_size;
Packit 577717
	}
Packit 577717
}
Packit 577717
Packit 577717
static void
Packit 577717
overflow_handler(int n, struct siginfo *info, struct sigcontext *sc)
Packit 577717
{
Packit 577717
	/* dangerous */
Packit 577717
	printf("Notification received\n");
Packit 577717
Packit 577717
	process_smpl_buffer();
Packit 577717
Packit 577717
	/*
Packit 577717
	 * And resume monitoring
Packit 577717
	 */
Packit 577717
	if (perfmonctl(id, PFM_RESTART,NULL, 0) == -1) {
Packit 577717
		perror("PFM_RESTART");
Packit 577717
		exit(1);
Packit 577717
	}
Packit 577717
}
Packit 577717
Packit 577717
Packit 577717
int
Packit 577717
main(void)
Packit 577717
{
Packit 577717
	int ret;
Packit 577717
	int type = 0;
Packit 577717
	pfmlib_input_param_t inp;
Packit 577717
	pfmlib_output_param_t outp;
Packit 577717
	pfmlib_ita_input_param_t ita_inp;
Packit 577717
	pfarg_reg_t pd[NUM_PMDS];
Packit 577717
	pfarg_reg_t pc[NUM_PMCS];
Packit 577717
	btb_ctx_arg_t  ctx[1];
Packit 577717
	pfarg_load_t load_args;
Packit 577717
	pfmlib_options_t pfmlib_options;
Packit 577717
	struct sigaction act;
Packit 577717
	unsigned int i;
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
		fatal_error("Can't initialize library\n");
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * Let's make sure we run this on the right CPU
Packit 577717
	 */
Packit 577717
	pfm_get_pmu_type(&type);
Packit 577717
	if (type != PFMLIB_ITANIUM_PMU) {
Packit 577717
		char model[MAX_PMU_NAME_LEN];
Packit 577717
		pfm_get_pmu_name(model, MAX_PMU_NAME_LEN);
Packit 577717
		fatal_error("this program does not work with %s PMU\n", model);
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * Install the overflow handler (SIGIO)
Packit 577717
	 */
Packit 577717
	memset(&act, 0, sizeof(act));
Packit 577717
	act.sa_handler = (sig_t)overflow_handler;
Packit 577717
	sigaction (SIGIO, &act, 0);
Packit 577717
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
	pfmlib_options.pfm_verbose = 0; /* set to 1 for debug */
Packit 577717
	pfm_set_options(&pfmlib_options);
Packit 577717
Packit 577717
Packit 577717
Packit 577717
	memset(pd, 0, sizeof(pd));
Packit 577717
	memset(ctx, 0, sizeof(ctx));
Packit 577717
	memset(&inp, 0, sizeof(inp));
Packit 577717
	memset(&outp, 0, sizeof(outp));
Packit 577717
	memset(&ita_inp,0, sizeof(ita_inp));
Packit 577717
Packit 577717
Packit 577717
	/*
Packit 577717
	 * Before calling pfm_find_dispatch(), we must specify what kind
Packit 577717
	 * of branches we want to capture. We are interesteed in all the mispredicted branches,
Packit 577717
	 * therefore we program we set the various fields of the BTB config to:
Packit 577717
	 */
Packit 577717
	ita_inp.pfp_ita_btb.btb_used = 1;
Packit 577717
Packit 577717
	ita_inp.pfp_ita_btb.btb_tar = 0x1;
Packit 577717
	ita_inp.pfp_ita_btb.btb_tm  = 0x2;
Packit 577717
	ita_inp.pfp_ita_btb.btb_ptm = 0x3;
Packit 577717
	ita_inp.pfp_ita_btb.btb_tac = 0x1;
Packit 577717
	ita_inp.pfp_ita_btb.btb_bac = 0x1;
Packit 577717
	ita_inp.pfp_ita_btb.btb_ppm = 0x3;
Packit 577717
	ita_inp.pfp_ita_btb.btb_plm = PFM_PLM3;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * To count the number of occurence of this instruction, we must
Packit 577717
	 * program a counting monitor with the IA64_TAGGED_INST_RETIRED_PMC8
Packit 577717
	 * event.
Packit 577717
	 */
Packit 577717
	if (pfm_find_full_event("BRANCH_EVENT", &inp.pfp_events[0]) != PFMLIB_SUCCESS) {
Packit 577717
		fatal_error("cannot find event BRANCH_EVENT\n");
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * set the (global) privilege mode:
Packit 577717
	 * 	PFM_PLM3 : user level only
Packit 577717
	 */
Packit 577717
	inp.pfp_dfl_plm   = PFM_PLM3;
Packit 577717
	/*
Packit 577717
	 * how many counters we use
Packit 577717
	 */
Packit 577717
	inp.pfp_event_count = 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, &ita_inp, &outp, NULL)) != PFMLIB_SUCCESS) {
Packit 577717
		fatal_error("cannot configure events: %s\n", pfm_strerror(ret));
Packit 577717
	}
Packit 577717
	 /*
Packit 577717
	  * We initialize the format specific information.
Packit 577717
	  * The format is identified by its UUID which must be copied
Packit 577717
	  * into the ctx_buf_fmt_id field.
Packit 577717
	  */
Packit 577717
	memcpy(ctx[0].ctx_arg.ctx_smpl_buf_id, buf_fmt_id, sizeof(pfm_uuid_t));
Packit 577717
Packit 577717
	/*
Packit 577717
	 * the size of the buffer is indicated in bytes (not entries).
Packit 577717
	 *
Packit 577717
	 * The kernel will record into the buffer up to a certain point.
Packit 577717
	 * No partial samples are ever recorded.
Packit 577717
	 */
Packit 577717
	ctx[0].buf_arg.buf_size = 8192;
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
Packit 577717
	printf("Sampling buffer mapped at %p\n", ctx[0].ctx_arg.ctx_smpl_vaddr);
Packit 577717
Packit 577717
	smpl_vaddr = ctx[0].ctx_arg.ctx_smpl_vaddr;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * extract our file descriptor
Packit 577717
	 */
Packit 577717
	id = ctx[0].ctx_arg.ctx_fd;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * Now prepare the argument to initialize the PMDs and PMCS.
Packit 577717
	 * We must pfp_pmc_count to determine the number of PMC to intialize.
Packit 577717
	 * We must use pfp_event_count to determine the number of PMD to initialize.
Packit 577717
	 * Some events cause extra PMCs to be used, so  pfp_pmc_count may be >= pfp_event_count.
Packit 577717
	 *
Packit 577717
	 * This step is new compared to libpfm-2.x. It is necessary because the library no
Packit 577717
	 * longer knows about the kernel data structures.
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
	/*
Packit 577717
	 * the PMC controlling the event ALWAYS come first, that's why this loop
Packit 577717
	 * is safe even when extra PMC are needed to support a particular event.
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
	/*
Packit 577717
	 * indicate we want notification when buffer is full
Packit 577717
	 */
Packit 577717
	pc[0].reg_flags |= PFM_REGFL_OVFL_NOTIFY;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * Now prepare the argument to initialize the PMD and the sampling period
Packit 577717
	 * We know we use only one PMD in this case, therefore pmd[0] corresponds
Packit 577717
	 * to our first event which is our sampling period.
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
	 * indicate PMD to collect in each sample
Packit 577717
	 */
Packit 577717
	pc[0].reg_smpl_pmds[0] = BTB_REGS_MASK;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * compute size of each sample: fixed-size header + all our BTB regs
Packit 577717
	 */
Packit 577717
	entry_size = sizeof(btb_entry_t)+(hweight64(BTB_REGS_MASK)<<3);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * When our counter overflows, we want to BTB index to be reset, so that we keep
Packit 577717
	 * in sync. This is required to make it possible to interpret pmd16 on overflow
Packit 577717
	 * to avoid repeating the same branch several times.
Packit 577717
	 */
Packit 577717
	pc[0].reg_reset_pmds[0] = M_PMD(16);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * reset pmd16 (BTB index), short and long reset value are set to zero as well
Packit 577717
	 *
Packit 577717
	 * We use slot 1 of our pd[] array for this.
Packit 577717
	 */
Packit 577717
	pd[1].reg_num         = 16;
Packit 577717
	pd[1].reg_value       = 0UL;
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 thann coutning monitors.
Packit 577717
	 */
Packit 577717
	if (perfmonctl(id, 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
	 * we use 2 = 1 for the branch_event + 1 for the reset of PMD16.
Packit 577717
	 */
Packit 577717
	if (perfmonctl(id, PFM_WRITE_PMDS, pd, 2) == -1) {
Packit 577717
		fatal_error("perfmonctl error PFM_WRITE_PMDS errno %d\n",errno);
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * now we load (i.e., attach) the context to ourself
Packit 577717
	 */
Packit 577717
	load_args.load_pid = getpid();
Packit 577717
	if (perfmonctl(id, PFM_LOAD_CONTEXT, &load_args, 1) == -1) {
Packit 577717
		fatal_error("perfmonctl error PFM_LOAD_CONTEXT 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(id, F_SETFL, fcntl(id, 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(id, 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(id);
Packit 577717
Packit 577717
	do_test(100000);
Packit 577717
Packit 577717
	pfm_self_stop(id);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * We must call the processing routine to cover the last entries recorded
Packit 577717
	 * in the sampling buffer. Note that the buffer may not be full at this point.
Packit 577717
	 *
Packit 577717
	 */
Packit 577717
Packit 577717
	process_smpl_buffer();
Packit 577717
Packit 577717
	/*
Packit 577717
	 * let's stop this now
Packit 577717
	 */
Packit 577717
	close(id);
Packit 577717
Packit 577717
	return 0;
Packit 577717
}