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

Packit 577717
/*
Packit 577717
 * smpl_core_pebs.c - Intel Core processor PEBS example
Packit 577717
 *
Packit 577717
 * Copyright (c) 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 <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 <syscall.h>
Packit 577717
#include <unistd.h>
Packit 577717
#include <sys/wait.h>
Packit 577717
#include <sys/ptrace.h>
Packit 577717
#include <sys/mman.h>
Packit 577717
#include <perfmon/perfmon.h>
Packit 577717
#include <perfmon/perfmon_pebs_core_smpl.h>
Packit 577717
Packit 577717
#include <perfmon/pfmlib.h>
Packit 577717
#include <perfmon/pfmlib_core.h>
Packit 577717
#include <perfmon/pfmlib_intel_atom.h>
Packit 577717
Packit 577717
#include "../detect_pmcs.h"
Packit 577717
Packit 577717
#define SMPL_EVENT	"INST_RETIRED:ANY_P" /* not all events support PEBS */
Packit 577717
Packit 577717
#define NUM_PMCS	16
Packit 577717
#define NUM_PMDS	16
Packit 577717
Packit 577717
#define SMPL_PERIOD	100000ULL	 /* must not use more bits than actual HW counter width */
Packit 577717
Packit 577717
typedef pfm_pebs_core_smpl_hdr_t	smpl_hdr_t;
Packit 577717
typedef pfm_pebs_core_smpl_entry_t	smpl_entry_t;
Packit 577717
typedef pfm_pebs_core_smpl_arg_t	smpl_arg_t;
Packit 577717
#define FMT_NAME			PFM_PEBS_CORE_SMPL_NAME
Packit 577717
Packit 577717
static uint64_t collected_samples;
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
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
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
	uint64_t entry;
Packit 577717
	unsigned long count;
Packit 577717
Packit 577717
	count = (hdr->ds.pebs_index - hdr->ds.pebs_buf_base)/sizeof(*ent);
Packit 577717
Packit 577717
	if (hdr->overflows == last_overflow && last_count == count) {
Packit 577717
		warning("skipping identical set of samples %"PRIu64" = %"PRIu64"\n",
Packit 577717
			hdr->overflows, last_overflow);
Packit 577717
		return;	
Packit 577717
	}
Packit 577717
	last_count = count;
Packit 577717
	last_overflow = hdr->overflows;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * the beginning of the buffer does not necessarily follow the header
Packit 577717
	 * due to alignement.
Packit 577717
	 */
Packit 577717
	ent   = (smpl_entry_t *)((unsigned long)(hdr+1)+ hdr->start_offs);
Packit 577717
	entry = collected_samples;
Packit 577717
Packit 577717
	while(count--) {
Packit 577717
		/*
Packit 577717
		 * print some of the machine registers of each sample
Packit 577717
		 */
Packit 577717
		printf("entry %06"PRIu64" eflags:0x%08llx EAX:0x%08llx ESP:0x%08llx IP:0x%08llx\n",
Packit 577717
			entry,
Packit 577717
			(unsigned long long)ent->eflags,
Packit 577717
			(unsigned long long)ent->eax,
Packit 577717
			(unsigned long long)ent->esp,
Packit 577717
			(unsigned long long)ent->ip);
Packit 577717
		ent++;
Packit 577717
		entry++;
Packit 577717
	}
Packit 577717
	collected_samples = entry;
Packit 577717
}
Packit 577717
Packit 577717
Packit 577717
int
Packit 577717
main(int argc, char **argv)
Packit 577717
{
Packit 577717
	pfmlib_input_param_t inp;
Packit 577717
	pfmlib_output_param_t outp;
Packit 577717
	pfmlib_core_input_param_t mod_inp;
Packit 577717
	pfmlib_options_t pfmlib_options;
Packit 577717
	pfarg_pmd_t pd[NUM_PMDS];
Packit 577717
	pfarg_pmc_t pc[NUM_PMCS];
Packit 577717
	pfarg_ctx_t ctx;
Packit 577717
	smpl_arg_t buf_arg;
Packit 577717
	pfarg_load_t load_args;
Packit 577717
	pfarg_msg_t msg;
Packit 577717
	smpl_hdr_t *hdr;
Packit 577717
	void *buf_addr;
Packit 577717
	pid_t pid;
Packit 577717
	int ret, fd, status, type;
Packit 577717
	unsigned int i;
Packit 577717
Packit 577717
	if (argc < 2)
Packit 577717
		fatal_error("you need to pass a program to sample\n");
Packit 577717
Packit 577717
	if (pfm_initialize() != PFMLIB_SUCCESS)
Packit 577717
		fatal_error("libpfm intialization failed\n");
Packit 577717
Packit 577717
	/*
Packit 577717
	 * check we are on an Intel Core PMU
Packit 577717
	 */
Packit 577717
	pfm_get_pmu_type(&type);
Packit 577717
	if (type != PFMLIB_INTEL_CORE_PMU && type != PFMLIB_INTEL_ATOM_PMU)
Packit 577717
		fatal_error("This program only works with an Intel Core processor\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
	memset(pd, 0, sizeof(pd));
Packit 577717
	memset(pc, 0, sizeof(pc));
Packit 577717
	memset(&inp, 0, sizeof(inp));
Packit 577717
	memset(&outp, 0, sizeof(outp));
Packit 577717
	memset(&mod_inp, 0, sizeof(mod_inp));
Packit 577717
Packit 577717
	memset(&ctx, 0, sizeof(ctx));
Packit 577717
	memset(&buf_arg, 0, sizeof(buf_arg));
Packit 577717
	memset(&load_args, 0, sizeof(load_args));
Packit 577717
Packit 577717
	/*
Packit 577717
	 * search for our sampling event
Packit 577717
	 */
Packit 577717
	if (pfm_find_full_event(SMPL_EVENT, &inp.pfp_events[0]) != PFMLIB_SUCCESS)
Packit 577717
		fatal_error("cannot find sampling event %s\n", SMPL_EVENT);
Packit 577717
Packit 577717
	inp.pfp_event_count = 1;
Packit 577717
	inp.pfp_dfl_plm = PFM_PLM3|PFM_PLM0;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * important: inform libpfm we do use PEBS
Packit 577717
	 */
Packit 577717
	mod_inp.pfp_core_pebs.pebs_used = 1;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * sampling buffer parameters
Packit 577717
	 */
Packit 577717
	buf_arg.buf_size = 4*getpagesize();
Packit 577717
Packit 577717
	/*
Packit 577717
	 * sampling period cannot use more bits than HW counter can supoprt
Packit 577717
	 */
Packit 577717
	buf_arg.cnt_reset = -SMPL_PERIOD;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * trigger notification (interrupt) when reached 90% of buffer
Packit 577717
	 */
Packit 577717
	buf_arg.intr_thres = (buf_arg.buf_size/sizeof(smpl_entry_t))*90/100;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * create context and sampling buffer
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, maybe you do not have the PEBS sampling format in the kernel.\nCheck /sys/kernel/perfmon/formats\n", strerror(errno));
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * map buffer into our address space
Packit 577717
	 */
Packit 577717
	buf_addr = mmap(NULL, (size_t)buf_arg.buf_size, PROT_READ, MAP_PRIVATE, fd, 0);
Packit 577717
	printf("context [%d] buffer mapped @%p\n", fd, buf_addr);
Packit 577717
	if (buf_addr == MAP_FAILED)
Packit 577717
		fatal_error("cannot mmap sampling buffer errno %d\n", errno);
Packit 577717
Packit 577717
	hdr = (smpl_hdr_t *)buf_addr;
Packit 577717
Packit 577717
	printf("pebs_base=0x%llx pebs_end=0x%llx index=0x%llx\n"
Packit 577717
	       "intr=0x%llx version=%u.%u\n"
Packit 577717
	       "entry_size=%zu ds_size=%zu\n",
Packit 577717
			(unsigned long long)hdr->ds.pebs_buf_base,
Packit 577717
			(unsigned long long)hdr->ds.pebs_abs_max,
Packit 577717
			(unsigned long long)hdr->ds.pebs_index,
Packit 577717
			(unsigned long long)hdr->ds.pebs_intr_thres,
Packit 577717
			PFM_VERSION_MAJOR(hdr->version),
Packit 577717
			PFM_VERSION_MINOR(hdr->version),
Packit 577717
			sizeof(smpl_entry_t),
Packit 577717
			sizeof(hdr->ds));
Packit 577717
Packit 577717
	if (PFM_VERSION_MAJOR(hdr->version) < 1)
Packit 577717
		fatal_error("invalid buffer format version\n");
Packit 577717
Packit 577717
	/*
Packit 577717
	 * get which PMC registers are available
Packit 577717
	 */
Packit 577717
	detect_unavail_pmcs(fd, &inp.pfp_unavail_pmcs);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * let libpfm figure out how to assign event onto PMU registers
Packit 577717
	 */
Packit 577717
	if (pfm_dispatch_events(&inp, &mod_inp, &outp, NULL) != PFMLIB_SUCCESS)
Packit 577717
		fatal_error("cannot assign event %s\n", SMPL_EVENT);
Packit 577717
Packit 577717
Packit 577717
	/*
Packit 577717
	 * propagate PMC setup from libpfm to perfmon
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
		 * must disable 64-bit emulation on the PMC0 counter
Packit 577717
		 * PMC0 is the only counter useable with PEBS. We must disable
Packit 577717
		 * 64-bit emulation to avoid getting interrupts for each
Packit 577717
		 * sampling period, PEBS takes care of this part.
Packit 577717
		 *
Packit 577717
		 * This is obsolete with 2.6.30
Packit 577717
		 */
Packit 577717
		if (pc[i].reg_num == 0)
Packit 577717
			pc[i].reg_flags = PFM_REGFL_NO_EMUL64;
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * propagate PMD set from libpfm to perfmon
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
	 * setup sampling period for first counter
Packit 577717
	 * we want notification on overflow, i.e., when buffer is full
Packit 577717
	 */
Packit 577717
	pd[0].reg_flags = PFM_REGFL_OVFL_NOTIFY;
Packit 577717
	pd[0].reg_value = -SMPL_PERIOD;
Packit 577717
	pd[0].reg_long_reset = -SMPL_PERIOD;
Packit 577717
	pd[0].reg_short_reset = -SMPL_PERIOD;
Packit 577717
	
Packit 577717
	/*
Packit 577717
	 * Now program the registers
Packit 577717
	 */
Packit 577717
	if (pfm_write_pmcs(fd, pc, outp.pfp_pmc_count) == -1)
Packit 577717
		fatal_error("pfm_write_pmcs error errno %d\n",errno);
Packit 577717
Packit 577717
	if (pfm_write_pmds(fd, pd, outp.pfp_pmd_count) == -1)
Packit 577717
		fatal_error("pfm_write_pmds error errno %d\n",errno);
Packit 577717
Packit 577717
	signal(SIGCHLD, SIG_IGN);
Packit 577717
	/*
Packit 577717
	 * Create the child task
Packit 577717
	 */
Packit 577717
	if ((pid=fork()) == -1) fatal_error("Cannot fork process\n");
Packit 577717
Packit 577717
	/*
Packit 577717
	 * In order to get the PFM_END_MSG message, it is important
Packit 577717
	 * to ensure that the child task does not inherit the file
Packit 577717
	 * descriptor of the context. By default, file descriptor
Packit 577717
	 * are inherited during exec(). We explicitely close it
Packit 577717
	 * here. We could have set it up through fcntl(FD_CLOEXEC)
Packit 577717
	 * to achieve the same thing.
Packit 577717
	 */
Packit 577717
	if (pid == 0) {
Packit 577717
		close(fd);
Packit 577717
		child(argv+1);
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", argv[1], pid, WEXITSTATUS(status));
Packit 577717
		goto terminate_session;
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 *  attach the context to child
Packit 577717
	 */
Packit 577717
	load_args.load_pid = pid;
Packit 577717
	if (pfm_load_context(fd, &load_args) == -1)
Packit 577717
		fatal_error("pfm_load_context error errno %d\n",errno);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * start monitoring
Packit 577717
	 */
Packit 577717
	if (pfm_start(fd, NULL) == -1)
Packit 577717
		fatal_error("pfm_start error errno %d\n",errno);
Packit 577717
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
	/*
Packit 577717
	 * core loop
Packit 577717
	 */
Packit 577717
	for(;;) {
Packit 577717
		/*
Packit 577717
		 * wait for overflow/end notification messages
Packit 577717
		 */
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
		switch(msg.type) {
Packit 577717
			case PFM_MSG_OVFL: /* the sampling buffer is full */
Packit 577717
				process_smpl_buf(hdr);
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) == -1) {
Packit 577717
					if (errno != EBUSY)
Packit 577717
						fatal_error("pfm_restart error errno %d\n",errno);
Packit 577717
					else
Packit 577717
						warning("pfm_restart: task has probably terminated \n");
Packit 577717
				}
Packit 577717
				break;
Packit 577717
			case PFM_MSG_END: /* monitored task terminated */
Packit 577717
				warning("task terminated\n");
Packit 577717
				goto terminate_session;
Packit 577717
			default: fatal_error("unknown message type %d\n", msg.type);
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
	printf("collected samples %"PRIu64"n", collected_samples);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * close context
Packit 577717
	 */
Packit 577717
	close(fd);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * unmap sampling buffer and actually free the perfmon context
Packit 577717
	 */
Packit 577717
	munmap(buf_addr, (size_t)buf_arg.buf_size);
Packit 577717
Packit 577717
	return 0;
Packit 577717
}