Blame src/libpfm-3.y/examples_v3.x/ia64/ita_irr.c

Packit 577717
/*
Packit 577717
 * ita_irr.c - example of how to use code range restriction 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
Packit 577717
#include <perfmon/perfmon.h>
Packit 577717
#include <perfmon/pfmlib_itanium.h>
Packit 577717
Packit 577717
#define VECTOR_SIZE	1000000UL
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
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_hi", 0UL} ,
Packit 577717
	{ "fp_ops_retired_lo", 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
	pfmlib_input_param_t inp;
Packit 577717
	pfmlib_output_param_t outp;
Packit 577717
	pfmlib_ita_input_param_t ita_inp;
Packit 577717
	pfmlib_ita_output_param_t ita_outp;
Packit 577717
	pfarg_pmr_t pd[NUM_PMDS];
Packit 577717
	pfarg_pmr_t pc[NUM_PMCS];
Packit 577717
	pfarg_pmr_t ibrs[8];
Packit 577717
	unsigned long range_start, range_end;
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
	int ret, type = 0;
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
	ret = pfm_initialize();
Packit 577717
	if (ret != PFMLIB_SUCCESS)
Packit 577717
		fatal_error("Cannot initialize library: %s\n", pfm_strerror(ret));
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_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
	 * 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 = 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
	/*
Packit 577717
	 * linker may reorder saxpy() and saxpy2()
Packit 577717
	 */
Packit 577717
	if (range_end < range_start) {
Packit 577717
		unsigned long tmp;
Packit 577717
		tmp = range_start;
Packit 577717
		range_start = range_end;
Packit 577717
		range_end = tmp;
Packit 577717
	}
Packit 577717
Packit 577717
	memset(pc, 0, sizeof(pc));
Packit 577717
	memset(pd, 0, sizeof(pd));
Packit 577717
	memset(ibrs,0, sizeof(ibrs));
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
	memset(&ita_outp,0, sizeof(ita_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
	/*
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 of rr_infos 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
	 */
Packit 577717
Packit 577717
	ita_inp.pfp_ita_irange.rr_used = 1;	/* indicate we use code range restriction */
Packit 577717
	ita_inp.pfp_ita_irange.rr_limits[0].rr_start = range_start;
Packit 577717
	ita_inp.pfp_ita_irange.rr_limits[0].rr_end   = range_end;
Packit 577717
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, &ita_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
			ita_outp.pfp_ita_irange.rr_infos[0].rr_soff,
Packit 577717
			ita_outp.pfp_ita_irange.rr_infos[0].rr_eoff,
Packit 577717
			ita_outp.pfp_ita_irange.rr_nbr_used >> 1);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * now create the session
Packit 577717
	 */
Packit 577717
	id = pfm_create(0, NULL);
Packit 577717
	if (id == -1) {
Packit 577717
		if (errno == ENOSYS)
Packit 577717
			fatal_error("Your kernel does not have performance monitoring support!\n");
Packit 577717
		fatal_error("cannot create session %s\n", strerror(errno));
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
	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
	/*
Packit 577717
	 * propagate the setup for the debug registers from the library to the arguments
Packit 577717
	 * to the syscall. The library does not know the type of the syscall
Packit 577717
	 * anymore. IBRs are mapped to PMC256-PMC263
Packit 577717
	 */
Packit 577717
	for (i=0; i < ita_outp.pfp_ita_drange.rr_nbr_used; i++) {
Packit 577717
		ibrs[i].reg_num   = 256+ita_outp.pfp_ita_irange.rr_br[i].reg_num;
Packit 577717
		ibrs[i].reg_value = ita_outp.pfp_ita_irange.rr_br[i].reg_value;
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * Program the code debug registers.
Packit 577717
	 *
Packit 577717
	 * IMPORTANT: programming the debug register MUST always be done before the PMCs
Packit 577717
	 * otherwise the kernel will fail on PFM_WRITE_PMCS. This is for security reasons.
Packit 577717
	 */
Packit 577717
	if (pfm_write(id, 0, PFM_RW_PMC, ibrs, ita_outp.pfp_ita_irange.rr_nbr_used * sizeof(*ibrs)) == -1)
Packit 577717
		fatal_error("pfm_write error errno %d\n",errno);
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 (pfm_write(id, 0, PFM_RW_PMC, pc, outp.pfp_pmc_count * sizeof(*pc)) == -1)
Packit 577717
		fatal_error("pfm_write error errno %d\n",errno);
Packit 577717
Packit 577717
	if (pfm_write(id, 0, PFM_RW_PMD, pd, outp.pfp_pmd_count * sizeof(*pd)) == -1)
Packit 577717
		fatal_error("pfm_write(PMD) error errno %d\n",errno);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * now attach session
Packit 577717
	 */
Packit 577717
	if (pfm_attach(id, 0, getpid()) == -1)
Packit 577717
		fatal_error("pfm_attach 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
	if (pfm_set_state(id, 0, PFM_ST_START))
Packit 577717
		fatal_error("pfm_set_state error errno %d\n",errno);
Packit 577717
Packit 577717
	do_test();
Packit 577717
Packit 577717
	if (pfm_set_state(id, 0, PFM_ST_STOP))
Packit 577717
		fatal_error("pfm_set_state error errno %d\n",errno);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * now read the results
Packit 577717
	 */
Packit 577717
	if (pfm_read(id, 0, PFM_RW_PMD, pd, inp.pfp_event_count * sizeof(*pd)) == -1)
Packit 577717
		fatal_error("pfm_read error errno %d\n",errno);
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,
Packit 577717
			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
}