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

Packit 577717
/*
Packit 577717
 * ita2_opcode.c - example of how to use the opcode matcher with the Itanium2 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_itanium2.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
/*
Packit 577717
 * we don't use static to make sure the compiler does not inline the function
Packit 577717
 */
Packit 577717
int
Packit 577717
do_test(unsigned long loop)
Packit 577717
{
Packit 577717
	unsigned long sum = 0;
Packit 577717
	while(loop--) sum += loop;
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
int
Packit 577717
main(void)
Packit 577717
{
Packit 577717
	pfmlib_input_param_t inp;
Packit 577717
	pfmlib_output_param_t outp;
Packit 577717
	pfmlib_ita2_input_param_t ita2_inp;
Packit 577717
	pfarg_reg_t pd[NUM_PMDS];
Packit 577717
	pfarg_reg_t pc[NUM_PMCS];
Packit 577717
	pfarg_context_t ctx[1];
Packit 577717
	pfarg_load_t load_args;
Packit 577717
	pfmlib_options_t pfmlib_options;
Packit 577717
	int ret;
Packit 577717
	int type = 0;
Packit 577717
	int id;
Packit 577717
	unsigned int i;
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
	/*
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_ITANIUM2_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 the %s PMU\n", model);
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 verbose */
Packit 577717
	pfm_set_options(&pfmlib_options);
Packit 577717
Packit 577717
	memset(pd, 0, sizeof(pd));
Packit 577717
	memset(pc, 0, sizeof(pc));
Packit 577717
	memset(ctx, 0, sizeof(ctx));
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(&ita2_inp,0, sizeof(ita2_inp));
Packit 577717
Packit 577717
	/*
Packit 577717
	 * We indicate that we are using the PMC8 opcode matcher. This is required
Packit 577717
	 * otherwise the library add PMC8 to the list of PMC to pogram during
Packit 577717
	 * pfm_dispatch_events().
Packit 577717
	 */
Packit 577717
	ita2_inp.pfp_ita2_pmc8.opcm_used = 1;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * We want to match all the br.cloop in our test function.
Packit 577717
	 * This branch is an IP-relative branch for which the major
Packit 577717
	 * opcode (bits [40-37]=4) and the btype field is 5 (which represents
Packit 577717
	 * bits[6-8]) so it is included in the match/mask fields of PMC8.
Packit 577717
	 * It is necessarily in a B slot.
Packit 577717
	 *
Packit 577717
	 * We don't care which operands are used with br.cloop therefore
Packit 577717
	 * the mask field of pmc8 is set such that only the 4 bits of the
Packit 577717
	 * opcode and 3 bits of btype must match exactly. This is accomplished by
Packit 577717
	 * clearing the top 4 bits and bits [6-8] of the mask field and setting the
Packit 577717
	 * remaining bits.  Similarly, the match field only has the opcode value  and btype
Packit 577717
	 * set according to the encoding of br.cloop, the
Packit 577717
	 * remaining bits are zero. Bit 60 of PMC8 is set to indicate
Packit 577717
	 * that we look only in B slots  (this is the only possibility for
Packit 577717
	 * this instruction anyway).
Packit 577717
	 *
Packit 577717
	 * So the binary representation of the value for PMC8 is as follows:
Packit 577717
	 *
Packit 577717
	 * 6666555555555544444444443333333333222222222211111111110000000000
Packit 577717
	 * 3210987654321098765432109876543210987654321098765432109876543210
Packit 577717
	 * ----------------------------------------------------------------
Packit 577717
	 * 0001010000000000000000101000000000000011111111111111000111111000
Packit 577717
	 *
Packit 577717
	 * which yields a value of 0x1400028003fff1f8.
Packit 577717
	 *
Packit 577717
	 * Depending on the level of optimization to compile this code, it may
Packit 577717
	 * be that the count reported could be zero, if the compiler uses a br.cond
Packit 577717
	 * instead of br.cloop.
Packit 577717
	 *
Packit 577717
	 *
Packit 577717
	 * The 0x1 sets the ig_ad field to make sure we ignore any range restriction.
Packit 577717
	 * Also bit 2 must always be set
Packit 577717
	 */
Packit 577717
	ita2_inp.pfp_ita2_pmc8.pmc_val = 0x1400028003fff1fa;
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("IA64_TAGGED_INST_RETIRED_IBRP0_PMC8", &inp.pfp_events[0]) != PFMLIB_SUCCESS) {
Packit 577717
		fatal_error("cannot find event IA64_TAGGED_INST_RETIRED_IBRP0_PMC8\n");
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 = 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, &ita2_inp, &outp, NULL)) != PFMLIB_SUCCESS) {
Packit 577717
		fatal_error("cannot configure events: %s\n", pfm_strerror(ret));
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
	 * extract the unique identifier for our context, a regular file descriptor
Packit 577717
	 */
Packit 577717
	id = ctx[0].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 causes 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
	printf("event_count=%d id=%d\n",  inp.pfp_event_count, id);
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
	if (perfmonctl(id, PFM_WRITE_PMDS, pd, inp.pfp_event_count) == -1) {
Packit 577717
		fatal_error("perfmonctl error PFM_WRITE_PMDS errno %d\n",errno);
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
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
	 * Let's roll now.
Packit 577717
	 */
Packit 577717
	pfm_self_start(id);
Packit 577717
Packit 577717
	do_test(100UL);
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("perfmonctl error READ_PMDS errno %d\n",errno);
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * print the results
Packit 577717
	 */
Packit 577717
	pfm_get_full_event_name(&inp.pfp_events[0], name, MAX_EVT_NAME_LEN);
Packit 577717
	printf("PMD%u %20lu %s\n",
Packit 577717
			pd[0].reg_num,
Packit 577717
			pd[0].reg_value,
Packit 577717
			name);
Packit 577717
Packit 577717
	if (pd[0].reg_value != 0)
Packit 577717
		printf("compiler used br.cloop\n");
Packit 577717
	else
Packit 577717
		printf("compiler did not use br.cloop\n");
Packit 577717
Packit 577717
	/*
Packit 577717
	 * let's stop this now
Packit 577717
	 */
Packit 577717
	close(id);
Packit 577717
	return 0;
Packit 577717
}