Blame src/libpfm-3.y/examples_v3.x/x86/smpl_core_pebs_sys.c

Packit 577717
/*
Packit 577717
 * smpl_core_pebs_sys.c - Intel Core processor PEBS system-wide example 
Packit 577717
 *
Packit 577717
 * Copyright (c) 2008 Google, Inc
Packit 577717
 * Contributed by Stephane Eranian <eranian@gmail.com>
Packit 577717
 *
Packit 577717
 * Based on code:
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 <poll.h>
Packit 577717
#include <sys/wait.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	"INSTRUCTIONS_RETIRED" /* not all event 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
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
 * pin task to CPU
Packit 577717
 */
Packit 577717
#ifndef __NR_sched_setaffinity
Packit 577717
#error "you need to define __NR_sched_setaffinity"
Packit 577717
#endif
Packit 577717
Packit 577717
#define MAX_CPUS	2048
Packit 577717
#define NR_CPU_BITS	(MAX_CPUS>>3)
Packit 577717
int
Packit 577717
pin_cpu(pid_t pid, unsigned int cpu)
Packit 577717
{
Packit 577717
	uint64_t my_mask[NR_CPU_BITS];
Packit 577717
Packit 577717
	if (cpu >= MAX_CPUS)
Packit 577717
		fatal_error("this program supports only up to %d CPUs\n", MAX_CPUS);
Packit 577717
Packit 577717
	my_mask[cpu>>6] = 1ULL << (cpu&63);
Packit 577717
Packit 577717
	return syscall(__NR_sched_setaffinity, pid, sizeof(my_mask), &my_mask);
Packit 577717
}
Packit 577717
Packit 577717
static volatile int done;
Packit 577717
Packit 577717
static void handler(int n)
Packit 577717
{
Packit 577717
	done = 1;
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_pmr_t pc[NUM_PMCS];
Packit 577717
	pfarg_pmd_attr_t pd[NUM_PMDS];
Packit 577717
	pfarg_sinfo_t sif;
Packit 577717
	struct pollfd fds;
Packit 577717
	smpl_arg_t buf_arg;
Packit 577717
	pfarg_msg_t msg;
Packit 577717
	smpl_hdr_t *hdr;
Packit 577717
	void *buf_addr;
Packit 577717
	uint64_t pebs_size;
Packit 577717
	pid_t pid;
Packit 577717
	int ret, fd, type;
Packit 577717
	unsigned int i;
Packit 577717
	uint32_t ctx_flags;
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
	memset(&sif, 0, sizeof(sif));
Packit 577717
Packit 577717
	memset(&buf_arg, 0, sizeof(buf_arg));
Packit 577717
Packit 577717
	memset(&fds, 0, sizeof(fds));
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;
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
	pebs_size = 3 * getpagesize();
Packit 577717
	buf_arg.buf_size = pebs_size;
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
	 * We want a system-wide context for sampling
Packit 577717
	 */
Packit 577717
	ctx_flags = PFM_FL_SYSTEM_WIDE | PFM_FL_SMPL_FMT;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * trigger notification (interrupt) when reaching the very end of
Packit 577717
	 * the buffer
Packit 577717
	 */
Packit 577717
	buf_arg.intr_thres = (pebs_size/sizeof(smpl_entry_t))*90/100;
Packit 577717
Packit 577717
	/*
Packit 577717
 	 * we want to measure CPU0, thus we pin ourself to the CPU before invoking
Packit 577717
 	 * perfmon. This ensures that the sampling buffer will be allocated on the
Packit 577717
 	 * same NUMA node.
Packit 577717
 	 */
Packit 577717
	ret = pin_cpu(getpid(), 0);
Packit 577717
	if (ret)
Packit 577717
		fatal_error("cannot pin on CPU0");
Packit 577717
Packit 577717
	/*
Packit 577717
	 * create session and sampling buffer
Packit 577717
	 */
Packit 577717
	fd = pfm_create(ctx_flags, &sif, 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 session %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("session [%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_pmu_regs(&sif, &inp.pfp_unavail_pmcs, NULL);
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
		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
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(fd, 0, PFM_RW_PMC, pc, outp.pfp_pmc_count * sizeof(*pc)) == -1)
Packit 577717
		fatal_error("pfm_write error errno %d\n",errno);
Packit 577717
Packit 577717
	if (pfm_write(fd, 0, PFM_RW_PMD_ATTR, pd, outp.pfp_pmd_count * sizeof(*pd)) == -1)
Packit 577717
		fatal_error("pfm_write(PMD) error errno %d\n",errno);
Packit 577717
Packit 577717
	/*
Packit 577717
	 *  attach the session to CPU0
Packit 577717
	 */
Packit 577717
	if (pfm_attach(fd, 0, 0) == -1)
Packit 577717
		fatal_error("pfm_attach error errno %d\n",errno);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * Create the child task
Packit 577717
	 */
Packit 577717
	signal(SIGCHLD, handler);
Packit 577717
Packit 577717
	if ((pid=fork()) == -1)
Packit 577717
		fatal_error("Cannot fork process\n");
Packit 577717
Packit 577717
	if (pid == 0) {
Packit 577717
		/* child does not inherit context file descriptor */
Packit 577717
		close(fd);
Packit 577717
Packit 577717
		/* if child is too short-lived we may not measure it */
Packit 577717
		child(argv+1);
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * start monitoring
Packit 577717
	 */
Packit 577717
	if (pfm_set_state(fd, 0, PFM_ST_START) == -1)
Packit 577717
		fatal_error("pfm_set_state(start) error errno %d\n",errno);
Packit 577717
Packit 577717
	fds.fd = fd;
Packit 577717
	fds.events = POLLIN;
Packit 577717
	/*
Packit 577717
	 * core loop
Packit 577717
	 */
Packit 577717
	for(;done == 0;) {
Packit 577717
		/*
Packit 577717
		 * Must use a timeout to avoid a race condition
Packit 577717
		 * with the SIGCHLD signal
Packit 577717
		 */
Packit 577717
		ret = poll(&fds, 1, 500);
Packit 577717
Packit 577717
		/*
Packit 577717
		 * if timeout expired, then check done
Packit 577717
		 */
Packit 577717
		if (ret == 0)
Packit 577717
			continue;
Packit 577717
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("poll failed: %s\n", strerror(errno));
Packit 577717
		}
Packit 577717
Packit 577717
		ret = read(fd, &msg, sizeof(msg));
Packit 577717
		if (ret == -1)
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
				 * in syste-wide, interface guarantees monitoring is active
Packit 577717
				 * upon return from the pfm_restart() syscall
Packit 577717
				 */
Packit 577717
				if (pfm_set_state(fd, 0, PFM_ST_RESTART) == -1)
Packit 577717
					fatal_error("pfm_set_state(restart) error errno %d\n",errno);
Packit 577717
				break;
Packit 577717
			default: fatal_error("unknown message type %d\n", msg.type);
Packit 577717
		}
Packit 577717
	}
Packit 577717
	/*
Packit 577717
	 * cleanup child
Packit 577717
	 */
Packit 577717
	waitpid(pid, NULL, 0);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * stop monitoring, this is required in order to guarantee that the PEBS buffer
Packit 577717
	 * header is updated with the latest position, such that we see see the final
Packit 577717
	 * samples
Packit 577717
	 */
Packit 577717
	if (pfm_set_state(fd, 0, PFM_ST_STOP) == -1)
Packit 577717
		fatal_error("pfm_set_state(stop) error errno %d\n",errno);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * check for any leftover samples. Must have monitoring stopped
Packit 577717
	 * for this operation to have guarantee it is up to date
Packit 577717
	 */
Packit 577717
	process_smpl_buf(hdr);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * close session
Packit 577717
	 */
Packit 577717
	close(fd);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * unmap sampling buffer and actually free the perfmon session
Packit 577717
	 */
Packit 577717
	munmap(buf_addr, (size_t)buf_arg.buf_size);
Packit 577717
Packit 577717
	return 0;
Packit 577717
}