Blame src/libpfm-3.y/examples_v2.x/ia64/mont_opcode.c

Packit Service a1973e
/*
Packit Service a1973e
 * mont_opcode.c - example of how to use the opcode matcher with the Dual-Core Itanium 2 PMU
Packit Service a1973e
 *
Packit Service a1973e
 * Copyright (c) 2005-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
 * This file is part of libpfm, a performance monitoring support library for
Packit Service a1973e
 * applications on Linux.
Packit Service a1973e
 */
Packit Service a1973e
#include <sys/types.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
Packit Service a1973e
#include <perfmon/perfmon.h>
Packit Service a1973e
#include <perfmon/pfmlib_montecito.h>
Packit Service a1973e
Packit Service a1973e
#define OPCM_EVENT "IA64_TAGGED_INST_RETIRED_IBRP0_PMC32_33"
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
#define MAX_EVT_NAME_LEN	128
Packit Service a1973e
#define MAX_PMU_NAME_LEN	32
Packit Service a1973e
Packit Service a1973e
#define NLOOP 			200UL
Packit Service a1973e
Packit Service a1973e
/*
Packit Service a1973e
 * we don't use static to make sure the compiler does not inline the function
Packit Service a1973e
 */
Packit Service a1973e
int
Packit Service a1973e
do_test(unsigned long loop)
Packit Service a1973e
{
Packit Service a1973e
	unsigned long sum = 0;
Packit Service a1973e
	while(loop--) sum += loop;
Packit Service a1973e
	return sum;
Packit Service a1973e
}
Packit Service a1973e
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
int
Packit Service a1973e
main(void)
Packit Service a1973e
{
Packit Service a1973e
	pfmlib_input_param_t inp;
Packit Service a1973e
	pfmlib_output_param_t outp;
Packit Service a1973e
	pfmlib_mont_input_param_t mont_inp;
Packit Service a1973e
	pfarg_pmd_t pd[NUM_PMDS];
Packit Service a1973e
	pfarg_pmc_t pc[NUM_PMCS];
Packit Service a1973e
	pfarg_ctx_t ctx;
Packit Service a1973e
	pfarg_load_t load_args;
Packit Service a1973e
	pfmlib_options_t pfmlib_options;
Packit Service a1973e
	int ret;
Packit Service a1973e
	int type = 0;
Packit Service a1973e
	int id;
Packit Service a1973e
	unsigned int i;
Packit Service a1973e
	char name[MAX_EVT_NAME_LEN];
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * Initialize pfm library (required before we can use it)
Packit Service a1973e
	 */
Packit Service a1973e
	if (pfm_initialize() != PFMLIB_SUCCESS)
Packit Service a1973e
		fatal_error("Can't initialize library\n");
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * Let's make sure we run this on the right CPU
Packit Service a1973e
	 */
Packit Service a1973e
	pfm_get_pmu_type(&type);
Packit Service a1973e
	if (type != PFMLIB_MONTECITO_PMU) {
Packit Service a1973e
		char model[MAX_PMU_NAME_LEN];
Packit Service a1973e
		pfm_get_pmu_name(model, MAX_PMU_NAME_LEN);
Packit Service a1973e
		fatal_error("this program does not work with the %s PMU\n", model);
Packit Service a1973e
	}
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
	memset(pd, 0, sizeof(pd));
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
Packit Service a1973e
	memset(&inp,0, sizeof(inp));
Packit Service a1973e
	memset(&outp,0, sizeof(outp));
Packit Service a1973e
	memset(&mont_inp,0, sizeof(mont_inp));
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * We indicate that we are using the first opcode matcher (PMC32/PMC33).
Packit Service a1973e
	 */
Packit Service a1973e
	mont_inp.pfp_mont_opcm1.opcm_used = 1;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * We want to match all the br.cloop in our test function.
Packit Service a1973e
	 * This branch is an IP-relative branch for which the major
Packit Service a1973e
	 * opcode (bits [40-37]) is 4 and the btype field (bits[6-8]) is 5.
Packit Service a1973e
	 * We ignore all the other fields in the opcode.
Packit Service a1973e
	 *
Packit Service a1973e
	 * On Montecito, the opcode matcher covers the full 41 bits of each
Packit Service a1973e
	 * instruction but we'll ignore them in this example. Hence the
Packit Service a1973e
	 * match value is:
Packit Service a1973e
	 *
Packit Service a1973e
	 * 	match = (4<<37)| (5<<6) = 0x8000000140
Packit Service a1973e
	 *
Packit Service a1973e
	 * On Montecito, the match field covers the full 41 bits of each instruction.
Packit Service a1973e
	 * But for this example, we only care about the major and btype field,
Packit Service a1973e
	 * and we ignore all other bits. When a bit is set in the mask it means
Packit Service a1973e
	 * that the corresponding match bit value is a "don't care". A bit
Packit Service a1973e
	 * with value of zero indicates that the corresponding match bit
Packit Service a1973e
	 * must match. Hence we build the following mask:
Packit Service a1973e
	 *
Packit Service a1973e
	 *	mask = ~((0xf<<37) | (0x3<<6)) = 0x1fffffff3f;
Packit Service a1973e
	 *
Packit Service a1973e
	 * The 0xf comes from the fact that major opcode is 4-bit wide.
Packit Service a1973e
	 * The 0x3 comes from the fact that btype is 3-bit wide.
Packit Service a1973e
	 */
Packit Service a1973e
	mont_inp.pfp_mont_opcm1.opcm_b     = 1;
Packit Service a1973e
	mont_inp.pfp_mont_opcm1.opcm_match = 0x8000000140;
Packit Service a1973e
	mont_inp.pfp_mont_opcm1.opcm_mask  = 0x1fffffff3f;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * To count the number of occurence of this instruction, we must
Packit Service a1973e
	 * program a counting monitor with the IA64_TAGGED_INST_RETIRED_PMC8
Packit Service a1973e
	 * event.
Packit Service a1973e
	 */
Packit Service a1973e
	if (pfm_find_full_event(OPCM_EVENT, &inp.pfp_events[0]) != PFMLIB_SUCCESS)
Packit Service a1973e
		fatal_error("cannot find event %s\n", OPCM_EVENT);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * set the privilege mode:
Packit Service a1973e
	 * 	PFM_PLM3 : user level only
Packit Service a1973e
	 */
Packit Service a1973e
	inp.pfp_dfl_plm   = PFM_PLM3;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * how many counters we use
Packit Service a1973e
	 */
Packit Service a1973e
	inp.pfp_event_count = 1;
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, &mont_inp, &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 create the context for self monitoring/per-task
Packit Service a1973e
	 */
Packit Service a1973e
	id = pfm_create_context(&ctx, NULL, NULL, 0);
Packit Service a1973e
	if (id == -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
	 * 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
Packit Service a1973e
	/*
Packit Service a1973e
	 * figure out pmd mapping from output pmc
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
	/*
Packit Service a1973e
	 * Now program the registers
Packit Service a1973e
	 *
Packit Service a1973e
	 * We don't use the save variable to indicate the number of elements passed to
Packit Service a1973e
	 * the kernel because, as we said earlier, pc may contain more elements than
Packit Service a1973e
	 * the number of events we specified, i.e., contains more thann coutning monitors.
Packit Service a1973e
	 */
Packit Service a1973e
	if (pfm_write_pmcs(id, 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(id, 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
	 * now we load (i.e., attach) the context to ourself
Packit Service a1973e
	 */
Packit Service a1973e
	load_args.load_pid = getpid();
Packit Service a1973e
Packit Service a1973e
	if (pfm_load_context(id, &load_args))
Packit Service a1973e
		fatal_error("pfm_load_context error errno %d\n",errno);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * Let's roll now.
Packit Service a1973e
	 */
Packit Service a1973e
	pfm_self_start(id);
Packit Service a1973e
Packit Service a1973e
	do_test(NLOOP);
Packit Service a1973e
Packit Service a1973e
	pfm_self_stop(id);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * now read the results
Packit Service a1973e
	 */
Packit Service a1973e
	if (pfm_read_pmds(id, pd, inp.pfp_event_count))
Packit Service a1973e
		fatal_error("pfm_read_pmds error errno %d\n",errno);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * print the results
Packit Service a1973e
	 */
Packit Service a1973e
	pfm_get_full_event_name(&inp.pfp_events[0], name, MAX_EVT_NAME_LEN);
Packit Service a1973e
Packit Service a1973e
	printf("PMD%-3u %20lu %s (expected %lu)\n",
Packit Service a1973e
		pd[0].reg_num,
Packit Service a1973e
		pd[0].reg_value,
Packit Service a1973e
		name, NLOOP);
Packit Service a1973e
Packit Service a1973e
	if (pd[0].reg_value != 0)
Packit Service a1973e
		printf("compiler used br.cloop\n");
Packit Service a1973e
	else
Packit Service a1973e
		printf("compiler did not use br.cloop\n");
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * let's stop this now
Packit Service a1973e
	 */
Packit Service a1973e
	close(id);
Packit Service a1973e
	return 0;
Packit Service a1973e
}