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

Packit 577717
/*
Packit 577717
 * mont_irr.c - example of how to use code range restriction with the Dual-Core Itanium 2 PMU
Packit 577717
 *
Packit 577717
 * Copyright (c) 2005-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.
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
Packit 577717
#include <perfmon/perfmon.h>
Packit 577717
#include <perfmon/pfmlib_montecito.h>
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
#define VECTOR_SIZE	1000000UL
Packit 577717
Packit 577717
typedef struct {
Packit 577717
	char *event_name;
Packit 577717
	unsigned long expected_value;
Packit 577717
}  event_desc_t;
Packit 577717
Packit 577717
static event_desc_t event_list[]={
Packit 577717
	{ "fp_ops_retired", VECTOR_SIZE<<1 },
Packit 577717
	{ NULL, 0UL }
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
void
Packit 577717
saxpy(double *a, double *b, double *c, unsigned long size)
Packit 577717
{
Packit 577717
	unsigned long i;
Packit 577717
Packit 577717
	for(i=0; i < size; i++) {
Packit 577717
		c[i] = 2*a[i] + b[i];
Packit 577717
	}
Packit 577717
}
Packit 577717
Packit 577717
void
Packit 577717
saxpy2(double *a, double *b, double *c, unsigned long size)
Packit 577717
{
Packit 577717
	unsigned long i;
Packit 577717
Packit 577717
	for(i=0; i < size; i++) {
Packit 577717
		c[i] = 2*a[i] + b[i];
Packit 577717
	}
Packit 577717
}
Packit 577717
Packit 577717
Packit 577717
Packit 577717
static int
Packit 577717
do_test(void)
Packit 577717
{
Packit 577717
	unsigned long size;
Packit 577717
	double *a, *b, *c;
Packit 577717
Packit 577717
	size = VECTOR_SIZE;
Packit 577717
Packit 577717
	a = malloc(size*sizeof(double));
Packit 577717
	b = malloc(size*sizeof(double));
Packit 577717
	c = malloc(size*sizeof(double));
Packit 577717
Packit 577717
	if (a == NULL || b == NULL || c == NULL) fatal_error("Cannot allocate vectors\n");
Packit 577717
Packit 577717
	memset(a, 0, size*sizeof(double));
Packit 577717
	memset(b, 0, size*sizeof(double));
Packit 577717
	memset(c, 0, size*sizeof(double));
Packit 577717
Packit 577717
	saxpy(a,b,c, size);
Packit 577717
	saxpy2(a,b,c, size);
Packit 577717
Packit 577717
	return 0;
Packit 577717
}
Packit 577717
Packit 577717
int
Packit 577717
main(int argc, char **argv)
Packit 577717
{
Packit 577717
	event_desc_t *p;
Packit 577717
	unsigned long range_start, range_end;
Packit 577717
	int ret, type = 0;
Packit 577717
	pfmlib_input_param_t inp;
Packit 577717
	pfmlib_output_param_t outp;
Packit 577717
	pfmlib_mont_input_param_t mont_inp;
Packit 577717
	pfmlib_mont_output_param_t mont_outp;
Packit 577717
	pfarg_reg_t pd[NUM_PMDS];
Packit 577717
	pfarg_reg_t pc[NUM_PMCS];
Packit 577717
	pfarg_dbreg_t ibrs[8];
Packit 577717
	pfarg_context_t ctx;
Packit 577717
	pfarg_load_t load_args;
Packit 577717
	pfmlib_options_t pfmlib_options;
Packit 577717
	struct fd {			/* function descriptor */
Packit 577717
		unsigned long addr;
Packit 577717
		unsigned long gp;
Packit 577717
	} *fd;
Packit 577717
	unsigned int i;
Packit 577717
	int id;
Packit 577717
	char name[MAX_EVT_NAME_LEN];
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
	 * Let's make sure we run this on the right CPU family
Packit 577717
	 */
Packit 577717
	pfm_get_pmu_type(&type);
Packit 577717
	if (type != PFMLIB_MONTECITO_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
	 * pass options to library (optional)
Packit 577717
	 */
Packit 577717
	memset(&pfmlib_options, 0, sizeof(pfmlib_options));
Packit 577717
	pfmlib_options.pfm_debug   = 1; /* set to 1 for debug */
Packit 577717
	pfmlib_options.pfm_verbose = 1; /* set to 1 for verbose */
Packit 577717
	pfm_set_options(&pfmlib_options);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * Compute the range we are interested in
Packit 577717
	 *
Packit 577717
	 * On IA-64, the function pointer does not point directly
Packit 577717
	 * to the function but to a descriptor which contains two
Packit 577717
	 * unsigned long: the first one is the actual start address
Packit 577717
	 * of the function, the second is the gp (global pointer)
Packit 577717
	 * to load into r1 before jumping into the function. Unlesss
Packit 577717
	 * we're jumping into a shared library the gp is the same as
Packit 577717
	 * the current gp.
Packit 577717
	 *
Packit 577717
	 * In the artificial example, we also rely on the compiler/linker
Packit 577717
	 * NOT reordering code layout. We depend on saxpy2() being just
Packit 577717
	 * after saxpy().
Packit 577717
	 *
Packit 577717
	 */
Packit 577717
	fd = (struct fd *)saxpy;
Packit 577717
	range_start = fd->addr;
Packit 577717
Packit 577717
	fd = (struct fd *)saxpy2;
Packit 577717
	range_end   =  fd->addr;
Packit 577717
	
Packit 577717
	memset(pc, 0, sizeof(pc));
Packit 577717
	memset(pd, 0, sizeof(pd));
Packit 577717
	memset(&ctx, 0, sizeof(ctx));
Packit 577717
	memset(ibrs,0, sizeof(ibrs));
Packit 577717
	memset(&load_args,0, sizeof(load_args));
Packit 577717
Packit 577717
	memset(&inp,0, sizeof(inp));
Packit 577717
	memset(&outp,0, sizeof(outp));
Packit 577717
	memset(&mont_inp,0, sizeof(mont_inp));
Packit 577717
	memset(&mont_outp,0, sizeof(mont_outp));
Packit 577717
Packit 577717
	/*
Packit 577717
	 * find requested event
Packit 577717
	 */
Packit 577717
	p = event_list;
Packit 577717
	for (i=0; p->event_name ; i++, p++) {
Packit 577717
		if (pfm_find_event(p->event_name, &inp.pfp_events[i].event) != PFMLIB_SUCCESS) {
Packit 577717
			fatal_error("cannot find %s event\n", p->event_name);
Packit 577717
		}
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * set the 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 = i;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * We use the library to figure out how to program the debug registers
Packit 577717
	 * to cover the data range we are interested in. The rr_end parameter
Packit 577717
	 * must point to the byte after the last element of the range (C-style range).
Packit 577717
	 *
Packit 577717
	 * Because of the masking mechanism and therefore alignment constraints used to implement
Packit 577717
	 * this feature, it may not be possible to exactly cover a given range. It may be that
Packit 577717
	 * the coverage exceeds the desired range. So it is possible to capture noise if
Packit 577717
	 * the surrounding addresses are also heavily used. You can figure out by how much the
Packit 577717
	 * actual range is off compared to the requested range by checking the rr_soff and rr_eoff
Packit 577717
	 * fields on return from the library call.
Packit 577717
	 *
Packit 577717
	 * Upon return, the rr_dbr array is programmed and the number of debug registers (not pairs)
Packit 577717
	 * used to cover the range is in rr_nbr_used.
Packit 577717
	 *
Packit 577717
	 * In the case of code range restriction on Itanium 2, the library will try to use the fine
Packit 577717
	 * mode first and then it will default to using multiple pairs to cover the range.
Packit 577717
	 */
Packit 577717
Packit 577717
	mont_inp.pfp_mont_irange.rr_used = 1;	/* indicate we use code range restriction */
Packit 577717
	mont_inp.pfp_mont_irange.rr_limits[0].rr_start = range_start;
Packit 577717
	mont_inp.pfp_mont_irange.rr_limits[0].rr_end   = range_end;
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, &mont_inp, &outp, &mont_outp)) != PFMLIB_SUCCESS)
Packit 577717
		fatal_error("cannot configure events: %s\n", pfm_strerror(ret));
Packit 577717
Packit 577717
	/*
Packit 577717
	 * print offsets
Packit 577717
	 */
Packit 577717
	printf("code range  : [0x%016lx-0x%016lx)\n"
Packit 577717
	       "start_offset:-0x%lx end_offset:+0x%lx\n"
Packit 577717
		"%d pairs of debug registers used\n",
Packit 577717
			range_start,
Packit 577717
			range_end,
Packit 577717
			mont_outp.pfp_mont_irange.rr_infos[0].rr_soff,
Packit 577717
			mont_outp.pfp_mont_irange.rr_infos[0].rr_eoff,
Packit 577717
			mont_outp.pfp_mont_irange.rr_nbr_used >> 1);
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
	 * extract our file descriptor
Packit 577717
	 */
Packit 577717
	id = ctx.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
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
	 * figure out pmd mapping from output pmc
Packit 577717
	 */
Packit 577717
	for (i=0; i < outp.pfp_pmd_count; i++)
Packit 577717
		pd[i].reg_num   = outp.pfp_pmds[i].reg_num;
Packit 577717
	
Packit 577717
	/*
Packit 577717
	 * propagate IBR settings. IBRS are mapped to PMC256-PMC263
Packit 577717
	 */
Packit 577717
	for (i=0; i < mont_outp.pfp_mont_irange.rr_nbr_used; i++) {
Packit 577717
		ibrs[i].dbreg_num   = mont_outp.pfp_mont_irange.rr_br[i].reg_num;
Packit 577717
		ibrs[i].dbreg_value = mont_outp.pfp_mont_irange.rr_br[i].reg_value;
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 coutning monitors.
Packit 577717
	 */
Packit 577717
	if (perfmonctl(id, PFM_WRITE_IBRS, ibrs, mont_outp.pfp_mont_irange.rr_nbr_used) == -1)
Packit 577717
		fatal_error("child: perfmonctl error PFM_WRITE_IBRS errno %d\n",errno);
Packit 577717
Packit 577717
	if (perfmonctl(id, PFM_WRITE_PMCS, pc, outp.pfp_pmc_count))
Packit 577717
		fatal_error("child: pfm_write_pmcs error errno %d\n",errno);
Packit 577717
Packit 577717
	if (perfmonctl(id, PFM_WRITE_PMDS, pd, outp.pfp_pmd_count) == -1)
Packit 577717
		fatal_error("child: pfm_write_pmds error errno %d\n",errno);
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))
Packit 577717
		fatal_error("pfm_load_context error errno %d\n",errno);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * Let's roll now.
Packit 577717
	 *
Packit 577717
	 * We run two distinct copies of the same function but we restrict measurement
Packit 577717
	 * to the first one (saxpy). Therefore the expected count is half what you would
Packit 577717
	 * get if code range restriction was not used. The core loop in both case uses
Packit 577717
	 * two floating point operation per iteration.
Packit 577717
	 */
Packit 577717
	pfm_self_start(id);
Packit 577717
Packit 577717
	do_test();
Packit 577717
Packit 577717
	pfm_self_stop(id);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * now read the results
Packit 577717
	 */
Packit 577717
	if (perfmonctl(id, PFM_READ_PMDS, pd, inp.pfp_event_count) == -1) {
Packit 577717
		fatal_error( "pfm_read_pmds error errno %d\n",errno);
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * print the results
Packit 577717
	 *
Packit 577717
	 * It is important to realize, that the first event we specified may not
Packit 577717
	 * be in PMD4. Not all events can be measured by any monitor. That's why
Packit 577717
	 * we need to use the pc[] array to figure out where event i was allocated.
Packit 577717
	 */
Packit 577717
	for (i=0; i < inp.pfp_event_count; i++) {
Packit 577717
		pfm_get_full_event_name(&inp.pfp_events[i], name, MAX_EVT_NAME_LEN);
Packit 577717
		printf("PMD%-3u %20lu %s (expected %lu)\n",
Packit 577717
			pd[i].reg_num,
Packit 577717
			pd[i].reg_value,
Packit 577717
			name, event_list[i].expected_value);
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
}