|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* mont_opcode.c - example of how to use the opcode matcher 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 OPCM_EVENT "IA64_TAGGED_INST_RETIRED_IBRP0_PMC32_33"
|
|
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 NLOOP 200UL
|
|
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_mont_input_param_t mont_inp;
|
|
Packit |
577717 |
pfarg_reg_t pd[NUM_PMDS];
|
|
Packit |
577717 |
pfarg_reg_t pc[NUM_PMCS];
|
|
Packit |
577717 |
pfarg_context_t ctx;
|
|
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 |
* 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_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 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 = 1; /* 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(&mont_inp,0, sizeof(mont_inp));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* We indicate that we are using the first opcode matcher (PMC32/PMC33).
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
mont_inp.pfp_mont_opcm1.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]) is 4 and the btype field (bits[6-8]) is 5.
|
|
Packit |
577717 |
* We ignore all the other fields in the opcode.
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* On Montecito, the opcode matcher covers the full 41 bits of each
|
|
Packit |
577717 |
* instruction but we'll ignore them in this example. Hence the
|
|
Packit |
577717 |
* match value is:
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* match = (4<<37)| (5<<6) = 0x8000000140
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* On Montecito, the match field covers the full 41 bits of each instruction.
|
|
Packit |
577717 |
* But for this example, we only care about the major and btype field,
|
|
Packit |
577717 |
* and we ignore all other bits. When a bit is set in the mask it means
|
|
Packit |
577717 |
* that the corresponding match bit value is a "don't care". A bit
|
|
Packit |
577717 |
* with value of zero indicates that the corresponding match bit
|
|
Packit |
577717 |
* must match. Hence we build the following mask:
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* mask = ~((0xf<<37) | (0x3<<6)) = 0x1fffffff3f;
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* The 0xf comes from the fact that major opcode is 4-bit wide.
|
|
Packit |
577717 |
* The 0x3 comes from the fact that btype is 3-bit wide.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
mont_inp.pfp_mont_opcm1.opcm_b = 1;
|
|
Packit |
577717 |
mont_inp.pfp_mont_opcm1.opcm_match = 0x8000000140;
|
|
Packit |
577717 |
mont_inp.pfp_mont_opcm1.opcm_mask = 0x1fffffff3f;
|
|
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(OPCM_EVENT, &inp.pfp_events[0]) != PFMLIB_SUCCESS)
|
|
Packit |
577717 |
fatal_error("cannot find event %s\n", OPCM_EVENT);
|
|
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 = 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, &mont_inp, &outp, NULL)) != PFMLIB_SUCCESS)
|
|
Packit |
577717 |
fatal_error("cannot configure events: %s\n", pfm_strerror(ret));
|
|
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.ctx_fd;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Now prepare the argument to initialize the PMDs and PMCS.
|
|
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 |
* 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))
|
|
Packit |
577717 |
fatal_error("pfm_write_pmcs error errno %d\n",errno);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (perfmonctl(id, PFM_WRITE_PMDS, pd, outp.pfp_pmd_count))
|
|
Packit |
577717 |
fatal_error("pfm_write_pmds error 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))
|
|
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 |
pfm_self_start(id);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
do_test(NLOOP);
|
|
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))
|
|
Packit |
577717 |
fatal_error("pfm_read_pmds error errno %d\n",errno);
|
|
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 |
|
|
Packit |
577717 |
printf("PMD%-3u %20lu %s (expected %lu)\n",
|
|
Packit |
577717 |
pd[0].reg_num,
|
|
Packit |
577717 |
pd[0].reg_value,
|
|
Packit |
577717 |
name, NLOOP);
|
|
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 |
}
|