Blame src/libpfm-3.y/examples_v2.x/self_view.c

Packit 577717
/*
Packit 577717
 * self_view.c - example of self-monitoring with PMD access via remapping
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
#include <sys/types.h>
Packit 577717
#include <inttypes.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
#include <sys/mman.h>
Packit 577717
Packit 577717
#include <perfmon/pfmlib.h>
Packit 577717
#include <perfmon/perfmon.h>
Packit 577717
Packit 577717
#include "detect_pmcs.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
Packit 577717
typedef struct {
Packit 577717
	uint64_t	val;
Packit 577717
	unsigned int	reg_num;
Packit 577717
	char		name[MAX_EVT_NAME_LEN];
Packit 577717
} pmd_val_t;
Packit 577717
Packit 577717
Packit 577717
static pfm_set_view_t	*view0;
Packit 577717
static pmd_val_t	pmdv[NUM_PMDS];
Packit 577717
static unsigned int	num_pmdv;
Packit 577717
static uint64_t		ovfl_mask;
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
#ifdef __ia64__
Packit 577717
#define get_pmd(n)	ia64_get_pmd(n)
Packit 577717
#elif defined(__x86_64__) || defined(__i386__)
Packit 577717
/*
Packit 577717
 * IMPORTANT:
Packit 577717
 *  there is an implicit assumption here that the perfmon-2 
Packit 577717
 *  PMD mapping and the rdpmc mapping correspond. As such
Packit 577717
 *  pmd0 -> rdpmc(0)
Packit 577717
 *  pmd1 -> rdpmc(1)
Packit 577717
 *  ...
Packit 577717
 */
Packit 577717
#define rdpmc(counter,low,high) \
Packit 577717
	__asm__ __volatile__("rdpmc" \
Packit 577717
		: "=a" (low), "=d" (high) \
Packit 577717
		: "c" (counter))
Packit 577717
Packit 577717
uint64_t
Packit 577717
get_pmd(unsigned int n)
Packit 577717
{
Packit 577717
	uint32_t high, low;
Packit 577717
	uint64_t tmp;
Packit 577717
	rdpmc(n, low, high);
Packit 577717
	tmp = (uint64_t)high <<32 | low;
Packit 577717
	return tmp;
Packit 577717
}
Packit 577717
#elif defined(__mips__) || defined(__powerpc__)
Packit 577717
/*
Packit 577717
 * XXX: MIPS does not have an instruction to read a counter at the user level
Packit 577717
 */
Packit 577717
uint64_t
Packit 577717
get_pmd(unsigned int n)
Packit 577717
{
Packit 577717
  return(0ULL);
Packit 577717
}
Packit 577717
#else
Packit 577717
#error "you need to define get_pmd for your architecture"
Packit 577717
#endif
Packit 577717
Packit 577717
static void
Packit 577717
show_view_self(int is_loaded)
Packit 577717
{
Packit 577717
	unsigned int i, is_active;
Packit 577717
	uint64_t val, hw_val;
Packit 577717
	uint64_t mask = ovfl_mask;
Packit 577717
	unsigned long retries = 0;
Packit 577717
	unsigned long start_seq, end_seq;
Packit 577717
Packit 577717
	retries = -1;
Packit 577717
	/*
Packit 577717
	 * print the results
Packit 577717
	 */
Packit 577717
retry:
Packit 577717
	retries++;
Packit 577717
	/*
Packit 577717
	 * get initial sequence number, if the number changes
Packit 577717
	 * while we are scanning, it means the view was updated
Packit 577717
	 * and we need to retry
Packit 577717
	 */
Packit 577717
	start_seq = view0->set_seq;
Packit 577717
	/*
Packit 577717
	 * active is true if the set is the active set
Packit 577717
	 */
Packit 577717
	is_active = view0->set_status & PFM_SETVFL_ACTIVE;
Packit 577717
Packit 577717
	printf("retries=%lu active=%d view_seq=%lu set_runs=%"PRIu64"\n",
Packit 577717
		retries,
Packit 577717
		view0->set_status,
Packit 577717
		view0->set_seq,
Packit 577717
		view0->set_runs);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * extract information directly from view
Packit 577717
	 */
Packit 577717
	for (i=0; i < num_pmdv; i++) {
Packit 577717
		val = view0->set_pmds[pmdv[i].reg_num];
Packit 577717
		/*
Packit 577717
		 * is context is attached and set is active then
Packit 577717
		 * we need to complement the software value with 
Packit 577717
		 * the current hardware value. For self-monitoring
Packit 577717
		 * with simply need to read the PMD
Packit 577717
		 */
Packit 577717
		if (is_loaded && is_active) {
Packit 577717
			hw_val = get_pmd(pmdv[i].reg_num);
Packit 577717
			/*
Packit 577717
			 * update the lower portion of the 64-bit
Packit 577717
			 * value with the hardware value
Packit 577717
			 */
Packit 577717
			val    = (val & ~mask) | (hw_val & mask);
Packit 577717
		}
Packit 577717
		pmdv[i].val  = val;
Packit 577717
Packit 577717
	}
Packit 577717
	/*
Packit 577717
	 * check if sequence number is still the same
Packit 577717
	 */
Packit 577717
	end_seq = view0->set_seq;
Packit 577717
	if (end_seq != start_seq) goto retry;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * print results
Packit 577717
	 */
Packit 577717
	for (i=0; i < num_pmdv; i++) {
Packit 577717
		printf("%20"PRIu64" %s\n", pmdv[i].val, pmdv[i].name);
Packit 577717
	}
Packit 577717
}
Packit 577717
Packit 577717
uint64_t
Packit 577717
noploop(uint64_t loop)
Packit 577717
{
Packit 577717
Packit 577717
	while (loop--) {
Packit 577717
		if ((loop % 10000) == 0) show_view_self(1);
Packit 577717
	}
Packit 577717
	return loop;
Packit 577717
}
Packit 577717
Packit 577717
Packit 577717
Packit 577717
int
Packit 577717
main(int argc, char **argv)
Packit 577717
{
Packit 577717
	char **p;
Packit 577717
	unsigned int i;
Packit 577717
	int ret, ctx_fd;
Packit 577717
	pfmlib_input_param_t inp;
Packit 577717
	pfmlib_output_param_t outp;
Packit 577717
	pfarg_pmd_t pd[NUM_PMDS];
Packit 577717
	pfarg_pmc_t pc[NUM_PMCS];
Packit 577717
	pfarg_ctx_t ctx[1];
Packit 577717
	pfarg_setinfo_t setinfo;
Packit 577717
	pfarg_load_t load_args;
Packit 577717
	pfmlib_options_t pfmlib_options;
Packit 577717
	unsigned int num_counters, width;
Packit 577717
Packit 577717
#ifdef __mips__
Packit 577717
	printf("<<WARNING: MIPS does not have an instruction to read a counter at the user level. Results are wrong>>\n");
Packit 577717
#endif
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 debug */
Packit 577717
	pfm_set_options(&pfmlib_options);
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
	pfm_get_num_counters(&num_counters);
Packit 577717
Packit 577717
	pfm_get_hw_counter_width(&width);
Packit 577717
Packit 577717
	ovfl_mask = (1ULL << width)-1;
Packit 577717
	printf("width=%u ovfl_mask=0x%"PRIx64"\n", width, ovfl_mask);	
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
	memset(&setinfo, 0, sizeof(setinfo));
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
Packit 577717
	/*
Packit 577717
	 * be nice to user!
Packit 577717
	 */
Packit 577717
	if (argc > 1) {
Packit 577717
		p = argv+1;
Packit 577717
		for (i=0; *p ; i++, p++) {
Packit 577717
			if (pfm_find_full_event(*p, &inp.pfp_events[i]) != PFMLIB_SUCCESS) {
Packit 577717
				fatal_error("Cannot find %s event\n", *p);
Packit 577717
			}
Packit 577717
		}
Packit 577717
	} else {
Packit 577717
		if (pfm_get_cycle_event(&inp.pfp_events[0]) != PFMLIB_SUCCESS) {
Packit 577717
			fatal_error("cannot find cycle event\n");
Packit 577717
		}
Packit 577717
		if (pfm_get_inst_retired_event(&inp.pfp_events[1]) != PFMLIB_SUCCESS) {
Packit 577717
			fatal_error("cannot find inst retired event\n");
Packit 577717
		}
Packit 577717
		i = 2;
Packit 577717
	}
Packit 577717
	/*
Packit 577717
	 * set the default privilege mode for all counters:
Packit 577717
	 * 	PFM_PLM3 : user level only
Packit 577717
	 */
Packit 577717
	inp.pfp_dfl_plm   = PFM_PLM3;
Packit 577717
Packit 577717
	if (i > num_counters) {
Packit 577717
		i = num_counters;
Packit 577717
		printf("too many events provided (max=%d events), using first %d event(s)\n", num_counters, i);
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * how many counters we use
Packit 577717
	 */
Packit 577717
	inp.pfp_event_count = i;
Packit 577717
Packit 577717
	ctx[0].ctx_flags = PFM_FL_MAP_SETS;
Packit 577717
	/*
Packit 577717
	 * now create a new context, per process context.
Packit 577717
	 * This just creates a new context with some initial state, it is not
Packit 577717
	 * active nor attached to any process.
Packit 577717
	 */
Packit 577717
	ctx_fd = pfm_create_context(ctx, NULL, NULL, 0);
Packit 577717
	if (ctx_fd == -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
	/*
Packit 577717
	 * build the pfp_unavail_pmcs bitmask by looking
Packit 577717
	 * at what perfmon has available. It is not always
Packit 577717
	 * the case that all PMU registers are actually available
Packit 577717
	 * to applications. For instance, on IA-32 platforms, some
Packit 577717
	 * registers may be reserved for the NMI watchdog timer.
Packit 577717
	 *
Packit 577717
	 * With this bitmap, the library knows which registers NOT to
Packit 577717
	 * use. Of source, it is possible that no valid assignement may
Packit 577717
	 * be possible if certina PMU registers  are not available.
Packit 577717
	 */
Packit 577717
	detect_unavail_pmcs(ctx_fd, &inp.pfp_unavail_pmcs);
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, NULL, &outp, NULL)) != PFMLIB_SUCCESS) {
Packit 577717
		fatal_error("cannot configure events: %s\n", pfm_strerror(ret));
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
	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
	 * Now program the registers
Packit 577717
	 *
Packit 577717
	 * We don't use the same 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 (pmd) we specified, i.e., contains more than counting
Packit 577717
	 * monitors.
Packit 577717
	 */
Packit 577717
	if (pfm_write_pmcs(ctx_fd, pc, outp.pfp_pmc_count))
Packit 577717
		fatal_error("pfm_write_pmcs error errno %d\n",errno);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * To be read, each PMD must be either written or declared
Packit 577717
	 * as being part of a sample (reg_smpl_pmds)
Packit 577717
	 */
Packit 577717
	if (pfm_write_pmds(ctx_fd, 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 (pfm_load_context(ctx_fd, &load_args))
Packit 577717
		fatal_error("pfm_load_context error errno %d\n",errno);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * retrieve set0 information
Packit 577717
	 */
Packit 577717
	if (pfm_getinfo_evtsets(ctx_fd, &setinfo, 1))
Packit 577717
		fatal_error("pfm_getinfo_evtsets error errno %d\n",errno);
Packit 577717
Packit 577717
	printf("set_id=0 mmap_offset=%llu\n", (unsigned long long)setinfo.set_mmap_offset);
Packit 577717
Packit 577717
	view0 = (pfm_set_view_t *)mmap(NULL, getpagesize(), PROT_READ, MAP_PRIVATE, ctx_fd, setinfo.set_mmap_offset);
Packit 577717
	if (view0 == MAP_FAILED) {
Packit 577717
		fatal_error("cannot mmap set view errno %d\n", errno);
Packit 577717
	}
Packit 577717
	printf("view=%p set_id=0 set_status=%u\n", view0, view0->set_status);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * Let's roll now
Packit 577717
	 */
Packit 577717
	pfm_self_start(ctx_fd);
Packit 577717
Packit 577717
	noploop(10000000);
Packit 577717
Packit 577717
	pfm_self_stop(ctx_fd);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * unload context (stop + unload)
Packit 577717
	 */
Packit 577717
	if (pfm_unload_context(ctx_fd))
Packit 577717
		fatal_error( "pfm_unload_context error errno %d\n",errno);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * now read the results
Packit 577717
	 */
Packit 577717
	if (pfm_read_pmds(ctx_fd, pd, outp.pfp_pmd_count))
Packit 577717
		fatal_error( "pfm_read_pmds error errno %d\n",errno);
Packit 577717
	
Packit 577717
	printf("results using pfm_read_pmds:\n");
Packit 577717
	for (i=0; i < num_pmdv; i++) {
Packit 577717
		printf("%20"PRIu64" %s\n", pd[i].reg_value, pmdv[i].name);
Packit 577717
	}
Packit 577717
Packit 577717
	show_view_self(0);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * and destroy our context
Packit 577717
	 */
Packit 577717
	munmap(view0, getpagesize());
Packit 577717
	close(ctx_fd);
Packit 577717
Packit 577717
	return 0;
Packit 577717
}