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

Packit 577717
/*
Packit 577717
 * ita_rr.c - example of how to use data 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 N_LOOP 100000000U
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 clear_psr_ac()	__rum(1UL<<3)
Packit 577717
Packit 577717
#elif defined(__GNUC__)
Packit 577717
Packit 577717
static inline void
Packit 577717
clear_psr_ac(void)
Packit 577717
{
Packit 577717
	__asm__ __volatile__("rum psr.ac;;" ::: "memory" );
Packit 577717
}
Packit 577717
#else
Packit 577717
#error "You need to define clear_psr_ac() for your compiler"
Packit 577717
#endif
Packit 577717
Packit 577717
#define TEST_DATA_COUNT	16
Packit 577717
Packit 577717
#define NUM_PMCS PFMLIB_MAX_PMCS
Packit 577717
#define NUM_PMDS PFMLIB_MAX_PMDS
Packit 577717
Packit 577717
#define MAX_PMU_NAME_LEN	32
Packit 577717
#define MAX_EVT_NAME_LEN	128
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
Packit 577717
static event_desc_t event_list[]={
Packit 577717
	{ "misaligned_loads_retired", N_LOOP },
Packit 577717
	{ "misaligned_stores_retired", N_LOOP },
Packit 577717
	{ NULL, 0UL}
Packit 577717
};
Packit 577717
Packit 577717
Packit 577717
typedef union {
Packit 577717
	unsigned long   l_tab[2];
Packit 577717
	unsigned int    i_tab[4];
Packit 577717
	unsigned short  s_tab[8];
Packit 577717
	unsigned char   c_tab[16];
Packit 577717
} test_data_t;
Packit 577717
Packit 577717
static int
Packit 577717
do_test(test_data_t *data)
Packit 577717
{
Packit 577717
	unsigned int *l, v;
Packit 577717
Packit 577717
	l = (unsigned int *)(data->c_tab+1);
Packit 577717
Packit 577717
	if (((unsigned long)l & 0x1) == 0) {
Packit 577717
		printf("Data is not unaligned, can't run test\n");
Packit 577717
		return  -1;
Packit 577717
	}
Packit 577717
Packit 577717
	v = *l;
Packit 577717
	v++;
Packit 577717
	*l = v;
Packit 577717
Packit 577717
	return 0;
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
int
Packit 577717
main(int argc, char **argv)
Packit 577717
{
Packit 577717
	event_desc_t *p;
Packit 577717
	test_data_t *test_data, *test_data_fake;
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_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 dbrs[8];
Packit 577717
	pfmlib_options_t pfmlib_options;
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 = 0; /* set to 1 for debug */
Packit 577717
	pfm_set_options(&pfmlib_options);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * now let's allocate the data structure we will be monitoring
Packit 577717
	 */
Packit 577717
	test_data = (test_data_t *)malloc(sizeof(test_data_t)*TEST_DATA_COUNT);
Packit 577717
	if (test_data == NULL) {
Packit 577717
		fatal_error("cannot allocate test data structure");
Packit 577717
	}
Packit 577717
	test_data_fake = (test_data_t *)malloc(sizeof(test_data_t)*TEST_DATA_COUNT);
Packit 577717
	if (test_data_fake == NULL) {
Packit 577717
		fatal_error("cannot allocate test data structure");
Packit 577717
	}
Packit 577717
	/*
Packit 577717
	 * Compute the range we are interested in
Packit 577717
	 */
Packit 577717
	range_start = (unsigned long)test_data;
Packit 577717
	range_end   = range_start + sizeof(test_data_t)*TEST_DATA_COUNT;
Packit 577717
	
Packit 577717
	memset(pd, 0, sizeof(pd));
Packit 577717
	memset(pc, 0, sizeof(pc));
Packit 577717
	memset(dbrs,0, sizeof(dbrs));
Packit 577717
Packit 577717
	/*
Packit 577717
	 * prepare parameters to library. we don't use any Itanium
Packit 577717
	 * specific features here. so the pfp_model is NULL.
Packit 577717
	 */
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
	/*
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
	/*
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 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, the actual
Packit 577717
	 * start and end offsets of the generated range by checking the rr_soff and rr_eoff fields
Packit 577717
	 * in the pfmlib_ita_output_param_t structure when coming back from the library call.
Packit 577717
	 *
Packit 577717
	 * Upon return, the pfmlib_ita_output_param_t.pfp_ita_drange.rr_dbr array is programmed and
Packit 577717
	 * the number of entries used to cover the range is in rr_nbr_used.
Packit 577717
	 */
Packit 577717
Packit 577717
	/*
Packit 577717
	 * We indicate that we are using a Data Range Restriction feature.
Packit 577717
	 * In this particular case this will cause, pfm_dispatch_events() to
Packit 577717
	 * add pmc13 to the list of PMC registers to initialize and the
Packit 577717
	 */
Packit 577717
Packit 577717
	ita_inp.pfp_ita_drange.rr_used = 1;
Packit 577717
	ita_inp.pfp_ita_drange.rr_limits[0].rr_start = range_start;
Packit 577717
	ita_inp.pfp_ita_drange.rr_limits[0].rr_end   = range_end;
Packit 577717
Packit 577717
Packit 577717
	/*
Packit 577717
	 * use the library to find the monitors to use
Packit 577717
	 *
Packit 577717
	 * upon return, cnt contains the number of entries
Packit 577717
	 * used in pc[].
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
	printf("data range  : [0x%016lx-0x%016lx): %d pair of debug registers used\n"
Packit 577717
	       "start_offset:-0x%lx end_offset:+0x%lx\n",
Packit 577717
			range_start,
Packit 577717
			range_end,
Packit 577717
			ita_outp.pfp_ita_drange.rr_nbr_used >> 1,
Packit 577717
			ita_outp.pfp_ita_drange.rr_infos[0].rr_soff,
Packit 577717
			ita_outp.pfp_ita_drange.rr_infos[0].rr_eoff);
Packit 577717
Packit 577717
	printf("fake data range: [0x%016lx-0x%016lx)\n",
Packit 577717
			(unsigned long)test_data_fake,
Packit 577717
			(unsigned long)test_data_fake+sizeof(test_data_t)*TEST_DATA_COUNT);
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
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
	 * 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. DBRS are ampped at PMC264+PMC271
Packit 577717
	 */
Packit 577717
	for (i=0; i < ita_outp.pfp_ita_drange.rr_nbr_used; i++) {
Packit 577717
		dbrs[i].reg_num   = 264+ita_outp.pfp_ita_drange.rr_br[i].reg_num;
Packit 577717
		dbrs[i].reg_value = ita_outp.pfp_ita_drange.rr_br[i].reg_value;
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * Program the data debug registers.
Packit 577717
	 */
Packit 577717
	if (pfm_write(id, 0, PFM_RW_PMC, dbrs, ita_outp.pfp_ita_drange.rr_nbr_used * sizeof(*dbrs)) == -1)
Packit 577717
		fatal_error("pfm_write_pmrs 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, inp.pfp_event_count * sizeof(*pd)) == -1)
Packit 577717
		fatal_error("pfm_write(PMD) error errno %d\n",errno);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * now we 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 make sure that the hardware does the unaligned accesses (do not use the
Packit 577717
	 * kernel software handler otherwise the PMU won't see the unaligned fault).
Packit 577717
	 */
Packit 577717
	clear_psr_ac();
Packit 577717
Packit 577717
	/*
Packit 577717
	 * Let's roll now.
Packit 577717
	 *
Packit 577717
	 * The idea behind this test is to have two dynamically allocated data structures
Packit 577717
	 * which are access in a unaligned fashion. But we want to capture only the unaligned
Packit 577717
	 * accesses on one of the two. So the debug registers are programmed to cover the
Packit 577717
	 * first one ONLY. Then we activate monotoring and access the two data structures.
Packit 577717
	 * This is an artificial example just to demonstrate how to use data address range
Packit 577717
	 * restrictions.
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
	for (i=0; i < N_LOOP; i++) {
Packit 577717
		do_test(test_data);
Packit 577717
		do_test(test_data_fake);
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 this example, we expect to see a value of 1 for both misaligned loads
Packit 577717
	 * and misaligned stores. But it can be two when the test_data and test_data_fake
Packit 577717
	 * are allocate very close from each other and the range created with the debug
Packit 577717
	 * registers is larger then test_data.
Packit 577717
	 *
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%u %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
		if (pd[i].reg_value != event_list[i].expected_value) {
Packit 577717
			printf("error: Result should be %lu for %s\n", event_list[i].expected_value, name);
Packit 577717
			break;
Packit 577717
		}
Packit 577717
	}
Packit 577717
	/*
Packit 577717
	 * let's stop this now
Packit 577717
	 */
Packit 577717
	close(id);
Packit 577717
Packit 577717
	free(test_data);
Packit 577717
	free(test_data_fake);
Packit 577717
Packit 577717
	return 0;
Packit 577717
}