Blame src/libpfm-3.y/examples_v2.x/x86/smpl_nhm_lbr.c

Packit 577717
/*
Packit 577717
 * smpl_nhm_lbr.c - Intel Nehalem LBR sampling
Packit 577717
 *
Packit 577717
 * Copyright (c) 2008 Google, Inc
Packit 577717
 * Contributed by Stephane Eranian <eranian@gmail.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 <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 <stdarg.h>
Packit 577717
#include <stdint.h>
Packit 577717
#include <sys/ptrace.h>
Packit 577717
#include <sys/wait.h>
Packit 577717
#include <sys/mman.h>
Packit 577717
#include <sys/resource.h>
Packit 577717
#include <setjmp.h>
Packit 577717
Packit 577717
#include <perfmon/pfmlib.h>
Packit 577717
#include <perfmon/pfmlib_intel_nhm.h>
Packit 577717
#include <perfmon/perfmon.h>
Packit 577717
#include <perfmon/perfmon_dfl_smpl.h>
Packit 577717
Packit 577717
#include "../detect_pmcs.h"
Packit 577717
Packit 577717
#define SAMPLING_PERIOD	100000
Packit 577717
#define EVENT_NAME	"br_inst_retired:all_branches"
Packit 577717
Packit 577717
typedef pfm_dfl_smpl_arg_t		smpl_fmt_arg_t;
Packit 577717
typedef pfm_dfl_smpl_hdr_t		smpl_hdr_t;
Packit 577717
typedef pfm_dfl_smpl_entry_t		smpl_entry_t;
Packit 577717
typedef pfm_dfl_smpl_arg_t		smpl_arg_t;
Packit 577717
#define FMT_NAME			PFM_DFL_SMPL_NAME
Packit 577717
Packit 577717
#define NUM_PMCS PFMLIB_MAX_PMCS
Packit 577717
#define NUM_PMDS PFMLIB_MAX_PMDS
Packit 577717
Packit 577717
static jmp_buf jbuf;
Packit 577717
Packit 577717
static uint64_t collected_samples, collected_partial, ovfl_count;
Packit 577717
Packit 577717
static void fatal_error(char *fmt,...) __attribute__((noreturn));
Packit 577717
Packit 577717
#define BPL (sizeof(uint64_t)<<3)
Packit 577717
#define LBPL	6
Packit 577717
Packit 577717
static void handler (int n)
Packit 577717
{
Packit 577717
	longjmp(jbuf, 1);
Packit 577717
}
Packit 577717
Packit 577717
static inline void pfm_bv_set(uint64_t *bv, uint16_t rnum)
Packit 577717
{
Packit 577717
	bv[rnum>>LBPL] |= 1UL << (rnum&(BPL-1));
Packit 577717
}
Packit 577717
Packit 577717
static void
Packit 577717
warning(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
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
child(char **arg)
Packit 577717
{
Packit 577717
	/*
Packit 577717
	 * force the task to stop before executing the first
Packit 577717
	 * user level instruction
Packit 577717
	 */
Packit 577717
	ptrace(PTRACE_TRACEME, 0, NULL, NULL);
Packit 577717
Packit 577717
	execvp(arg[0], arg);
Packit 577717
	/* not reached */
Packit 577717
	exit(1);
Packit 577717
}
Packit 577717
Packit 577717
static void
Packit 577717
process_smpl_buf(smpl_hdr_t *hdr)
Packit 577717
{
Packit 577717
	static uint64_t last_overflow = ~0; /* initialize to biggest value possible */
Packit 577717
	static uint64_t last_count;
Packit 577717
	smpl_entry_t *ent;
Packit 577717
	size_t pos, count, entry_size;
Packit 577717
	uint64_t entry, *reg;
Packit 577717
	uint64_t tos, i;
Packit 577717
	
Packit 577717
	if (hdr->hdr_overflows == last_overflow && hdr->hdr_count == last_count) {
Packit 577717
		warning("skipping identical set of samples %"PRIu64" = %"PRIu64"\n",
Packit 577717
			hdr->hdr_overflows, last_overflow);
Packit 577717
		return;	
Packit 577717
	}
Packit 577717
	/*
Packit 577717
 	 * 33 = 32 LBR registers + LBR_TOS
Packit 577717
 	 */
Packit 577717
	entry_size = sizeof(smpl_entry_t) + 33 * sizeof(uint64_t);
Packit 577717
Packit 577717
	count = hdr->hdr_count;
Packit 577717
Packit 577717
	ent   = (smpl_entry_t *)(hdr+1);
Packit 577717
	pos   = (unsigned long)ent;
Packit 577717
	entry = collected_samples;
Packit 577717
Packit 577717
	while(count--) {
Packit 577717
		printf("entry %"PRIu64" PID:%d TID:%d CPU:%d LAST_VAL:%"PRIu64" OVFL:%u IIP:0x%llx\n",
Packit 577717
			entry,
Packit 577717
			ent->tgid,
Packit 577717
			ent->pid,
Packit 577717
			ent->cpu,
Packit 577717
			-ent->last_reset_val,
Packit 577717
			ent->ovfl_pmd,
Packit 577717
			(unsigned long long)ent->ip);
Packit 577717
		/*
Packit 577717
		 * TOS is pmd31 and comes first
Packit 577717
		 * TOs points to most recent entry
Packit 577717
		 */
Packit 577717
		reg = (uint64_t *)(ent+1);
Packit 577717
		tos = reg[0] * 2;
Packit 577717
Packit 577717
		/*
Packit 577717
 		 * i points to oldest entry, the one to print first
Packit 577717
 		 */
Packit 577717
		i = (tos + 2) % 32;
Packit 577717
		/*
Packit 577717
 		 * iterate over the 16 branches printing src -> dst
Packit 577717
 		 */
Packit 577717
		while (i != tos) {
Packit 577717
			printf("0x%016"PRIx64" -> 0x%016"PRIx64"\n",
Packit 577717
				reg[1+i],
Packit 577717
				reg[1+i+1]);
Packit 577717
			i = (i + 2) % 32;
Packit 577717
		}
Packit 577717
		pos += entry_size;
Packit 577717
		ent = (smpl_entry_t *)pos;
Packit 577717
		entry++;
Packit 577717
	}
Packit 577717
	collected_samples = entry;
Packit 577717
	last_overflow = hdr->hdr_overflows;
Packit 577717
	if (last_count != hdr->hdr_count && (last_count || last_overflow == 0))
Packit 577717
		collected_partial += hdr->hdr_count;
Packit 577717
	last_count = hdr->hdr_count;
Packit 577717
}
Packit 577717
Packit 577717
int
Packit 577717
mainloop(char **arg)
Packit 577717
{
Packit 577717
	smpl_hdr_t *hdr;
Packit 577717
	pfarg_ctx_t ctx;
Packit 577717
	smpl_arg_t buf_arg;
Packit 577717
	pfmlib_input_param_t inp;
Packit 577717
	pfmlib_nhm_input_param_t mod_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_load_t load_args;
Packit 577717
	pfarg_msg_t msg;
Packit 577717
	void *buf_addr;
Packit 577717
	pid_t pid;
Packit 577717
	int i, status, ret, fd;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * intialize all locals
Packit 577717
	 */
Packit 577717
	memset(&ctx, 0, sizeof(ctx));
Packit 577717
	memset(&buf_arg, 0, sizeof(buf_arg));
Packit 577717
	memset(&inp,0, sizeof(inp));
Packit 577717
	memset(&outp,0, sizeof(outp));
Packit 577717
	memset(pd, 0, sizeof(pd));
Packit 577717
	memset(pc, 0, sizeof(pc));
Packit 577717
	memset(&load_args, 0, sizeof(load_args));
Packit 577717
	memset(&mod_inp, 0, sizeof(mod_inp));
Packit 577717
Packit 577717
	ret = pfm_find_full_event(EVENT_NAME, &inp.pfp_events[0]);
Packit 577717
	if (ret != PFMLIB_SUCCESS)
Packit 577717
		fatal_error("cannot find event %s\n", EVENT_NAME);
Packit 577717
	/*
Packit 577717
	 * set the privilege mode:
Packit 577717
	 * 	PFM_PLM3 : user level
Packit 577717
	 * 	PFM_PLM0 : kernel level
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
	mod_inp.pfp_nhm_lbr.lbr_used = 1;
Packit 577717
	mod_inp.pfp_nhm_lbr.lbr_plm = 0; /* inherit from pfp_dfl_plm */
Packit 577717
Packit 577717
	/*
Packit 577717
	 * setup LBR filter
Packit 577717
	 *
Packit 577717
	 * By default all types of branches are captured
Packit 577717
	 *
Packit 577717
	 * it is possible to filter out some types of branches using
Packit 577717
	 * the macros in pfmlib_intel_nhm.c.
Packit 577717
	 *
Packit 577717
	 * for instance, to only capture nears calls, you do:
Packit 577717
	 *
Packit 577717
	 * lbr_filter = PFM_NHM_LBR_NEAR_REL_CALL
Packit 577717
	 */
Packit 577717
	mod_inp.pfp_nhm_lbr.lbr_filter = 0;
Packit 577717
	
Packit 577717
	/*
Packit 577717
	 * the size of the buffer is indicated in bytes (not entries).
Packit 577717
	 *
Packit 577717
	 * The kernel will record into the buffer up to a certain point.
Packit 577717
	 * No partial samples are ever recorded.
Packit 577717
	 */
Packit 577717
	buf_arg.buf_size = 4 * getpagesize();
Packit 577717
Packit 577717
	/*
Packit 577717
	 * now create our perfmon context.
Packit 577717
	 */
Packit 577717
	fd = pfm_create_context(&ctx, FMT_NAME, &buf_arg, sizeof(buf_arg));
Packit 577717
	if (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("cannot create PFM context %s\n", strerror(errno));
Packit 577717
	}
Packit 577717
Packit 577717
	detect_unavail_pmcs(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, &mod_inp, &outp, NULL)) != PFMLIB_SUCCESS)
Packit 577717
		fatal_error("cannot configure events: %s\n", pfm_strerror(ret));
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
 	 * we use only one counter
Packit 577717
 	 */
Packit 577717
	pd[0].reg_num = outp.pfp_pmds[0].reg_num;
Packit 577717
	pd[0].reg_flags = PFM_REGFL_OVFL_NOTIFY;
Packit 577717
	/*
Packit 577717
 	 * add 2 x 16 LBR entries + LBR_TOS to smpl_pmds
Packit 577717
 	 */
Packit 577717
	for(i=31; i < 64; i++)
Packit 577717
		pfm_bv_set(pd[0].reg_smpl_pmds, i);
Packit 577717
Packit 577717
	/*
Packit 577717
 	 * we need to reset LBR after each sample to be able to determine
Packit 577717
 	 * whether or not we get new data
Packit 577717
 	 *
Packit 577717
 	 * LBR_TOS(PMD31) is read-only, it is not included in reset_pmds
Packit 577717
	 */
Packit 577717
	for(i=32; i < 64; i++)
Packit 577717
		pfm_bv_set(pd[0].reg_reset_pmds, i);
Packit 577717
	/*
Packit 577717
	 * set sampling periods
Packit 577717
	 */
Packit 577717
	pd[0].reg_value       = - SAMPLING_PERIOD;
Packit 577717
	pd[0].reg_short_reset = - SAMPLING_PERIOD;
Packit 577717
	pd[0].reg_long_reset  = - SAMPLING_PERIOD;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * prepare context structure.
Packit 577717
	 */
Packit 577717
	/*
Packit 577717
	 * retrieve the virtual address at which the sampling
Packit 577717
	 * buffer has been mapped
Packit 577717
	 */
Packit 577717
	buf_addr = mmap(NULL, (size_t)buf_arg.buf_size, PROT_READ, MAP_PRIVATE, fd, 0);
Packit 577717
	if (buf_addr == MAP_FAILED)
Packit 577717
		fatal_error("cannot mmap sampling buffer: %s\n", strerror(errno));
Packit 577717
Packit 577717
	printf("buffer mapped @%p\n", buf_addr);
Packit 577717
Packit 577717
	hdr = (smpl_hdr_t *)buf_addr;
Packit 577717
Packit 577717
	printf("hdr_cur_offs=%llu version=%u.%u\n",
Packit 577717
		(unsigned long long)hdr->hdr_cur_offs,
Packit 577717
		PFM_VERSION_MAJOR(hdr->hdr_version),
Packit 577717
		PFM_VERSION_MINOR(hdr->hdr_version));
Packit 577717
Packit 577717
	if (PFM_VERSION_MAJOR(hdr->hdr_version) < 1)
Packit 577717
		fatal_error("invalid buffer format version\n");
Packit 577717
Packit 577717
	/*
Packit 577717
	 * program the PMCs
Packit 577717
	 */
Packit 577717
	if (pfm_write_pmcs(fd, pc, outp.pfp_pmc_count))
Packit 577717
		fatal_error("pfm_write_pmcs error errno %d\n",errno);
Packit 577717
Packit 577717
	/*
Packit 577717
	 *  program our counter
Packit 577717
	 */
Packit 577717
	if (pfm_write_pmds(fd, pd, 1))
Packit 577717
		fatal_error("pfm_write_pmds error errno %d\n",errno);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * create the child task
Packit 577717
	 */
Packit 577717
	if ((pid=fork()) == -1)
Packit 577717
		fatal_error("Cannot fork process\n");
Packit 577717
Packit 577717
	/*
Packit 577717
	 * create child
Packit 577717
	 * make sure child does not inherit the file descriptor
Packit 577717
	 */
Packit 577717
	if (pid == 0) {
Packit 577717
		close(fd);
Packit 577717
		child(arg);
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * wait for the child to exec
Packit 577717
	 */
Packit 577717
	waitpid(pid, &status, WUNTRACED);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * process is stopped at this point
Packit 577717
	 */
Packit 577717
	if (WIFEXITED(status)) {
Packit 577717
		warning("task %s [%d] exited already status %d\n", arg[0], pid, WEXITSTATUS(status));
Packit 577717
		goto terminate_session;
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * attach context to stopped task
Packit 577717
	 */
Packit 577717
	load_args.load_pid = pid;
Packit 577717
	if (pfm_load_context (fd, &load_args))
Packit 577717
		fatal_error("pfm_load_context error errno %d\n",errno);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * activate monitoring for stopped task.
Packit 577717
	 * (nothing will be measured at this point
Packit 577717
	 */
Packit 577717
	if (pfm_start(fd, NULL))
Packit 577717
		fatal_error("pfm_start error errno %d\n",errno);
Packit 577717
	/*
Packit 577717
	 * detach child. Side effect includes
Packit 577717
	 * activation of monitoring.
Packit 577717
	 */
Packit 577717
	ptrace(PTRACE_DETACH, pid, NULL, 0);
Packit 577717
Packit 577717
	signal(SIGCHLD, handler);
Packit 577717
Packit 577717
	if (setjmp(jbuf) == 1)
Packit 577717
		goto terminate_session;
Packit 577717
	/*
Packit 577717
	 * core loop
Packit 577717
	 */
Packit 577717
	for(;;) {
Packit 577717
		ret = read(fd, &msg, sizeof(msg));
Packit 577717
		if (ret == -1) {
Packit 577717
			if(ret == -1 && errno == EINTR) {
Packit 577717
				warning("read interrupted, retrying\n");
Packit 577717
				continue;
Packit 577717
			}
Packit 577717
			fatal_error("cannot read perfmon msg: %s\n", strerror(errno));
Packit 577717
		}
Packit 577717
		if (msg.type == PFM_MSG_OVFL) {
Packit 577717
			process_smpl_buf(hdr);
Packit 577717
			ovfl_count++;
Packit 577717
			/*
Packit 577717
			 * reactivate monitoring once we are done with the samples
Packit 577717
			 *
Packit 577717
			 * Note that this call can fail with EBUSY in non-blocking mode
Packit 577717
			 * as the task may have disappeared while we were processing
Packit 577717
			 * the samples.
Packit 577717
			 */
Packit 577717
			if (pfm_restart(fd)) {
Packit 577717
				if (errno != EBUSY)
Packit 577717
					fatal_error("pfm_restart error errno %d\n",errno);
Packit 577717
			}
Packit 577717
		}
Packit 577717
	}
Packit 577717
terminate_session:
Packit 577717
	/*
Packit 577717
	 * cleanup child
Packit 577717
	 */
Packit 577717
	wait4(pid, &status, 0, NULL);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * check for any leftover samples
Packit 577717
	 */
Packit 577717
	process_smpl_buf(hdr);
Packit 577717
Packit 577717
	close(fd);
Packit 577717
Packit 577717
	ret = munmap(hdr, (size_t)buf_arg.buf_size);
Packit 577717
	if (ret)
Packit 577717
		fatal_error("cannot unmap buffer: %s\n", strerror(errno));
Packit 577717
Packit 577717
	printf("%"PRIu64" samples (%"PRIu64" in partial buffer) collected in %"PRIu64" buffer overflows\n",
Packit 577717
		collected_samples,
Packit 577717
		collected_partial,
Packit 577717
		ovfl_count);
Packit 577717
Packit 577717
	return 0;
Packit 577717
}
Packit 577717
Packit 577717
int
Packit 577717
main(int argc, char **argv)
Packit 577717
{
Packit 577717
	pfmlib_options_t pfmlib_options;
Packit 577717
	int ret;
Packit 577717
Packit 577717
	if (argc == 1)
Packit 577717
		fatal_error("You must specify a command to execute\n");
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
	/*
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
	return mainloop(argv+1);
Packit 577717
}