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

Packit 577717
/*
Packit 577717
 * smpl_amd64_ibs.c - AMD64 Family 10h IBS sampling
Packit 577717
 *
Packit 577717
 * Copyright (c) 2007 Hewlett-Packard Development Company, L.P.
Packit 577717
 * Contributed by Stephane Eranian <eranian@hpl.hp.com>
Packit 577717
 *
Packit 577717
 * Copyright (c) 2008 Advanced Mirco Devices Inc.
Packit 577717
 * Contributed by Robert Richter <robert.richter@amd.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
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 <getopt.h>
Packit 577717
#include <sys/ptrace.h>
Packit 577717
#include <sys/wait.h>
Packit 577717
#include <sys/mman.h>
Packit 577717
#include <sys/time.h>
Packit 577717
Packit 577717
#include <perfmon/pfmlib.h>
Packit 577717
#include <perfmon/pfmlib_amd64.h>
Packit 577717
Packit 577717
#include <perfmon/perfmon.h>
Packit 577717
#include <perfmon/perfmon_dfl_smpl.h>
Packit 577717
Packit 577717
typedef struct {
Packit 577717
	int opt_no_show;
Packit 577717
	int opt_block;
Packit 577717
	int opt_setup;
Packit 577717
} options_t;
Packit 577717
Packit 577717
enum {
Packit 577717
	OPT_IBSOP,		/* 0: default */
Packit 577717
	OPT_IBSFETCH,
Packit 577717
	OPT_IBSOP_NATIVE,
Packit 577717
};
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
#define PMD_IBSOP_NUM		7
Packit 577717
#define PMD_IBSFETCH_NUM	3
Packit 577717
Packit 577717
static uint64_t collected_samples, collected_partial;
Packit 577717
static options_t options;
Packit 577717
Packit 577717
static struct option the_options[]={
Packit 577717
	{ "help", 0, 0,  1},
Packit 577717
	{ "ovfl-block", 0, &options.opt_block, 1},
Packit 577717
	{ "no-show", 0, &options.opt_no_show, 1},
Packit 577717
	{ "ibsop", 0, &options.opt_setup, OPT_IBSOP},
Packit 577717
	{ "ibsfetch", 0, &options.opt_setup, OPT_IBSFETCH},
Packit 577717
	{ "ibsop-native", 0, &options.opt_setup, OPT_IBSOP_NATIVE},
Packit 577717
	{ 0, 0, 0, 0}
Packit 577717
};
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 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 inline int pfm_bv_isset(uint64_t *bv, uint16_t rnum)
Packit 577717
{
Packit 577717
	return bv[rnum>>LBPL] & (1UL <<(rnum&(BPL-1))) ? 1 : 0;
Packit 577717
}
Packit 577717
Packit 577717
static inline void pfm_bv_copy(uint64_t *d, uint64_t *j, uint16_t n)
Packit 577717
{
Packit 577717
	if (n <= BPL)
Packit 577717
		*d = *j;
Packit 577717
	else {
Packit 577717
		memcpy(d, j, (n>>LBPL)*sizeof(uint64_t));
Packit 577717
	}
Packit 577717
}
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, uint64_t *smpl_pmds, unsigned int num_smpl_pmds, size_t entry_size)
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;
Packit 577717
	ibsopdata_t *opdata;
Packit 577717
	ibsopdata2_t *opdata2;
Packit 577717
	ibsopdata3_t *opdata3;
Packit 577717
	uint64_t entry, *reg;
Packit 577717
	unsigned int j, n;
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
	count = hdr->hdr_count;
Packit 577717
Packit 577717
	if (options.opt_no_show) {
Packit 577717
		collected_samples += count;
Packit 577717
		return;
Packit 577717
	}
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" 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
			(unsigned long long)ent->ip);
Packit 577717
Packit 577717
		/*
Packit 577717
		 * print body: additional PMDs recorded
Packit 577717
		 * PMD are recorded in increasing index order
Packit 577717
		 */
Packit 577717
		reg = (uint64_t *)(ent+1);
Packit 577717
Packit 577717
		n = num_smpl_pmds;
Packit 577717
		for(j=0; n; j++) {	
Packit 577717
			if (pfm_bv_isset(smpl_pmds, j)) {
Packit 577717
				switch(j) {
Packit 577717
					case 7:
Packit 577717
						printf("PMD%-3d:0x%016"PRIx64"\n", j, *reg);
Packit 577717
						/* check valid "record" bit */
Packit 577717
						if ((*reg & (1ull<<18)) == 0) {
Packit 577717
							printf("no data captured\n");
Packit 577717
							goto skip;
Packit 577717
						}
Packit 577717
						break;
Packit 577717
					case 9: /*IBSOPSDATA */
Packit 577717
						opdata = (ibsopdata_t *)reg;
Packit 577717
						printf("PMD%-3d:0x%016"PRIx64" : comptoret=%u tagtoretctr=%u opbrnresync=%u opmispret=%u opret=%u brntk=%u brnmips=%u bnrret=%u\n",
Packit 577717
							j,
Packit 577717
							*reg,
Packit 577717
							opdata->reg.ibscomptoretctr,
Packit 577717
							opdata->reg.ibstagtoretctr,
Packit 577717
							opdata->reg.ibsopbrnresync,
Packit 577717
							opdata->reg.ibsopmispreturn,
Packit 577717
							opdata->reg.ibsopreturn,
Packit 577717
							opdata->reg.ibsopbrntaken,
Packit 577717
							opdata->reg.ibsopbrnmisp,
Packit 577717
							opdata->reg.ibsopbrnret);
Packit 577717
						break;
Packit 577717
					case 10:
Packit 577717
						opdata2 = (ibsopdata2_t *)reg;
Packit 577717
						printf("PMD%-3d:0x%016"PRIx64" : reqsrc=%u reqdstproc=%s reqcachehitst=%u\n",
Packit 577717
							j,
Packit 577717
							*reg,
Packit 577717
							opdata2->reg.nbibsreqsrc,
Packit 577717
							opdata2->reg.nbibsreqdstproc ? "local" : "remote",
Packit 577717
							opdata2->reg.nbibsreqcachehitst);
Packit 577717
						break;
Packit 577717
					case 11:
Packit 577717
						opdata3 = (ibsopdata3_t *)reg;
Packit 577717
						printf("PMD%-3d:0x%016"PRIx64" : ld=%u st=%u L1TLBmiss=%u L2TLBmiss=%u L1TLBhit2M=%u L1TLBhit1G=%u L2TLBhit2M=%u miss=%u misalign=%u ld_bankconf=%u  st_bankconf=%u st_to_ld_conf=%u st_to_ld_canc=%u UCaccess=%u WCaccess=%u lock=%u MAB=%u linevalid=%u physvalid=%u miss_lat=%u\n",
Packit 577717
							j,
Packit 577717
							*reg,
Packit 577717
							opdata3->reg.ibsldop,
Packit 577717
							opdata3->reg.ibsstop,
Packit 577717
							opdata3->reg.ibsdcl1tlbmiss,
Packit 577717
							opdata3->reg.ibsdcl2tlbmiss,
Packit 577717
							opdata3->reg.ibsdcl1tlbhit2m,
Packit 577717
							opdata3->reg.ibsdcl1tlbhit1g,
Packit 577717
							opdata3->reg.ibsdcl2tlbhit2m,
Packit 577717
							opdata3->reg.ibsdcmiss,
Packit 577717
							opdata3->reg.ibsdcmissacc,
Packit 577717
							opdata3->reg.ibsdcldbnkcon,
Packit 577717
							opdata3->reg.ibsdcstbnkcon,
Packit 577717
							opdata3->reg.ibsdcsttoldfwd,
Packit 577717
							opdata3->reg.ibsdcsttoldcan,
Packit 577717
							opdata3->reg.ibsdcucmemacc,
Packit 577717
							opdata3->reg.ibsdcwcmemacc,
Packit 577717
							opdata3->reg.ibsdclockedop,
Packit 577717
							opdata3->reg.ibsdcmabhit,
Packit 577717
							opdata3->reg.ibsdclinaddrvalid,
Packit 577717
							opdata3->reg.ibsdcphyaddrvalid,
Packit 577717
							opdata3->reg.ibsdcmisslat);
Packit 577717
						break;
Packit 577717
Packit 577717
					default:
Packit 577717
						printf("PMD%-3d:0x%016"PRIx64"\n", j, *reg);
Packit 577717
				}
Packit 577717
				reg++;
Packit 577717
				n--;
Packit 577717
			}
Packit 577717
		}
Packit 577717
skip:
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
static int
Packit 577717
setup_pmu_ibsop_native(pfarg_pmc_t *pc, pfarg_pmd_t *pd)
Packit 577717
{
Packit 577717
	uint64_t ibs_ops_smpl;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * OBSCTL sampling period (20 bits)
Packit 577717
	 * bits 3:0 must be zero
Packit 577717
	 */
Packit 577717
	ibs_ops_smpl = 0xffff0;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * IBSOPSCTL config
Packit 577717
	 *
Packit 577717
	 * bit    17: enable
Packit 577717
	 * bits 0-15: bit 19-4 of sampling period
Packit 577717
	 */
Packit 577717
	pc[0].reg_num = 5;
Packit 577717
	pc[0].reg_value = (1ULL <<17) | ((ibs_ops_smpl >> 4) & 0xffffULL);
Packit 577717
Packit 577717
	/* IBSOPSCTL data
Packit 577717
	 *
Packit 577717
	 * point to the same MSR register. It correspond to the associated
Packit 577717
	 * data register, i.e., the register to which the IBS interrupt will
Packit 577717
	 * be associated.
Packit 577717
	 *
Packit 577717
	 * Randomization on IBS control register (IBSOPSCTL, IBSFETCHCTL) is
Packit 577717
	 * ignored.
Packit 577717
	 * 
Packit 577717
	 * The value, short_reset, long_reset values are ignored. Use the
Packit 577717
	 * corresponding PMC registers to set sampling period.
Packit 577717
	 *
Packit 577717
	 * If the last_reset-value is important for your program, then you can
Packit 577717
	 * get it frmo the controlling PMC (4, 5). Alternatively, you can set
Packit 577717
	 * the reg_value field to the value of the corresponding PMC register.
Packit 577717
	 */
Packit 577717
	pd[0].reg_num = 7;
Packit 577717
	pd[0].reg_flags = PFM_REGFL_OVFL_NOTIFY;
Packit 577717
	pd[0].reg_value =  pc[0].reg_value;
Packit 577717
	pd[0].reg_long_reset  = pc[0].reg_value;
Packit 577717
	pd[0].reg_short_reset = pc[0].reg_value;
Packit 577717
Packit 577717
	pfm_bv_set(pd[0].reg_smpl_pmds, 7);
Packit 577717
	pfm_bv_set(pd[0].reg_smpl_pmds, 8);
Packit 577717
	pfm_bv_set(pd[0].reg_smpl_pmds, 9);
Packit 577717
	pfm_bv_set(pd[0].reg_smpl_pmds, 10);
Packit 577717
	pfm_bv_set(pd[0].reg_smpl_pmds, 11);
Packit 577717
	pfm_bv_set(pd[0].reg_smpl_pmds, 12);
Packit 577717
	pfm_bv_set(pd[0].reg_smpl_pmds, 13);
Packit 577717
Packit 577717
	return PFMLIB_SUCCESS;
Packit 577717
}
Packit 577717
Packit 577717
static int
Packit 577717
setup_pmu_ibsop(pfarg_pmc_t *pc, pfarg_pmd_t *pd)
Packit 577717
{
Packit 577717
	pfmlib_amd64_input_param_t inp_mod;
Packit 577717
	pfmlib_output_param_t outp;
Packit 577717
	pfmlib_amd64_output_param_t outp_mod;
Packit 577717
	int ret;
Packit 577717
	
Packit 577717
	memset(&inp_mod,0, sizeof(inp_mod));
Packit 577717
	memset(&outp,0, sizeof(outp));
Packit 577717
	memset(&outp_mod,0, sizeof(outp_mod));
Packit 577717
Packit 577717
	/* setup ibsopctl register */
Packit 577717
	inp_mod.ibsop.maxcnt = 0xFFFF0;
Packit 577717
	inp_mod.flags |= PFMLIB_AMD64_USE_IBSOP;
Packit 577717
Packit 577717
	/* setup Perfmon2 registers */
Packit 577717
	ret = pfm_dispatch_events(NULL, &inp_mod, &outp, &outp_mod);
Packit 577717
	if (ret != PFMLIB_SUCCESS) {
Packit 577717
		fprintf(stderr, "cannot dispatch events: %s\n", pfm_strerror(ret));
Packit 577717
		return ret;
Packit 577717
	}
Packit 577717
	if (outp.pfp_pmc_count != 1) {
Packit 577717
		fprintf(stderr, "Unexpected PMC register count: %d\n",
Packit 577717
			outp.pfp_pmc_count);
Packit 577717
		return PFMLIB_ERR_INVAL;
Packit 577717
	}
Packit 577717
	if (outp.pfp_pmd_count != 1) {
Packit 577717
		fprintf(stderr, "Unexpected PMD register count: %d\n",
Packit 577717
			outp.pfp_pmd_count);
Packit 577717
		return PFMLIB_ERR_INVAL;
Packit 577717
	}
Packit 577717
	if (outp_mod.ibsop_base != 0) {
Packit 577717
		fprintf(stderr, "Unexpected IBSOP base register: %d\n",
Packit 577717
			outp_mod.ibsop_base);
Packit 577717
		return PFMLIB_ERR_INVAL;
Packit 577717
	}
Packit 577717
Packit 577717
	/* PMC_IBSOPCTL */
Packit 577717
	pc[0].reg_num   = outp.pfp_pmcs[0].reg_num;
Packit 577717
	pc[0].reg_value = outp.pfp_pmcs[0].reg_value;
Packit 577717
	/* PMD_IBSOPCTL */
Packit 577717
	pd[0].reg_num   = outp.pfp_pmds[0].reg_num;
Packit 577717
	pd[0].reg_value = 0;
Packit 577717
Packit 577717
	/* setup all IBSOP registers for sampling */
Packit 577717
	pd[0].reg_flags = PFM_REGFL_OVFL_NOTIFY;
Packit 577717
	if (pd[0].reg_num > 64 - PMD_IBSOP_NUM) {
Packit 577717
		fprintf(stderr, "Unexpected IBSOP base: %d\n",
Packit 577717
			(int)pd[0].reg_num);
Packit 577717
		return PFMLIB_ERR_INVAL;
Packit 577717
	}
Packit 577717
	pd[0].reg_smpl_pmds[0] =
Packit 577717
		((1UL << PMD_IBSOP_NUM) - 1) << outp.pfp_pmds[0].reg_num;
Packit 577717
Packit 577717
	return PFMLIB_SUCCESS;
Packit 577717
}
Packit 577717
Packit 577717
static int
Packit 577717
setup_pmu_ibsfetch(pfarg_pmc_t *pc, pfarg_pmd_t *pd)
Packit 577717
{
Packit 577717
	pfmlib_amd64_input_param_t inp_mod;
Packit 577717
	pfmlib_output_param_t outp;
Packit 577717
	pfmlib_amd64_output_param_t outp_mod;
Packit 577717
	int ret;
Packit 577717
Packit 577717
	memset(&inp_mod,0, sizeof(inp_mod));
Packit 577717
	memset(&outp,0, sizeof(outp));
Packit 577717
	memset(&outp_mod,0, sizeof(outp_mod));
Packit 577717
Packit 577717
	/* setup ibsfetchctl register */
Packit 577717
	inp_mod.ibsfetch.maxcnt = 0xFFFF0;
Packit 577717
	inp_mod.flags |= PFMLIB_AMD64_USE_IBSFETCH;
Packit 577717
Packit 577717
	/* setup Perfmon2 registers */
Packit 577717
	ret = pfm_dispatch_events(NULL, &inp_mod, &outp, &outp_mod);
Packit 577717
	if (ret != PFMLIB_SUCCESS) {
Packit 577717
		fprintf(stderr, "cannot dispatch events: %s\n", pfm_strerror(ret));
Packit 577717
		return ret;
Packit 577717
	}
Packit 577717
	if (outp.pfp_pmc_count != 1) {
Packit 577717
		fprintf(stderr, "Unexpected PMC register count: %d\n",
Packit 577717
			outp.pfp_pmc_count);
Packit 577717
		return PFMLIB_ERR_INVAL;
Packit 577717
	}
Packit 577717
	if (outp.pfp_pmd_count != 1) {
Packit 577717
		fprintf(stderr, "Unexpected PMD register count: %d\n",
Packit 577717
			outp.pfp_pmd_count);
Packit 577717
		return PFMLIB_ERR_INVAL;
Packit 577717
	}
Packit 577717
	if (outp_mod.ibsfetch_base != 0) {
Packit 577717
		fprintf(stderr, "Unexpected IBSFETCH base register: %d\n",
Packit 577717
			outp_mod.ibsfetch_base);
Packit 577717
		return PFMLIB_ERR_INVAL;
Packit 577717
	}
Packit 577717
Packit 577717
	/* PMC_IBSFETCHCTL */
Packit 577717
	pc[0].reg_num   = outp.pfp_pmcs[0].reg_num;
Packit 577717
	pc[0].reg_value = outp.pfp_pmcs[0].reg_value;
Packit 577717
	/* PMD_IBSFETCHCTL */
Packit 577717
	pd[0].reg_num   = outp.pfp_pmds[0].reg_num;
Packit 577717
	pd[0].reg_value = 0;
Packit 577717
Packit 577717
	/* setup all IBSFETCH registers for sampling */
Packit 577717
	pd[0].reg_flags = PFM_REGFL_OVFL_NOTIFY;
Packit 577717
	if (pd[0].reg_num > 64 - PMD_IBSFETCH_NUM) {
Packit 577717
		fprintf(stderr, "Unexpected IBSFETCH base: %d\n",
Packit 577717
			(int)pd[0].reg_num);
Packit 577717
		return PFMLIB_ERR_INVAL;
Packit 577717
	}
Packit 577717
	pd[0].reg_smpl_pmds[0] =
Packit 577717
		((1UL << PMD_IBSFETCH_NUM) - 1) << outp.pfp_pmds[0].reg_num;
Packit 577717
Packit 577717
	return PFMLIB_SUCCESS;
Packit 577717
}
Packit 577717
Packit 577717
int
Packit 577717
mainloop(char **arg)
Packit 577717
{
Packit 577717
	pfarg_pmc_t pc[1];
Packit 577717
	pfarg_pmd_t pd[1];
Packit 577717
Packit 577717
	smpl_hdr_t *hdr;
Packit 577717
	pfarg_ctx_t ctx;
Packit 577717
	smpl_arg_t buf_arg;
Packit 577717
	pfarg_load_t load_arg;
Packit 577717
	struct timeval start_time, end_time;
Packit 577717
	pfarg_msg_t msg;
Packit 577717
	uint64_t ovfl_count = 0;
Packit 577717
	size_t entry_size;
Packit 577717
	void *buf_addr;
Packit 577717
	pid_t pid;
Packit 577717
	int status, ret, fd;
Packit 577717
	int pmc_count, pmd_count;
Packit 577717
	unsigned int num_smpl_pmds = 0;
Packit 577717
Packit 577717
	memset(&ctx,0, sizeof(ctx));
Packit 577717
	memset(pd, 0, sizeof(pd));
Packit 577717
	memset(pc, 0, sizeof(pc));
Packit 577717
	memset(&load_arg, 0, sizeof(load_arg));
Packit 577717
Packit 577717
	/* defaults */
Packit 577717
	num_smpl_pmds = 7;
Packit 577717
	pmc_count = pmd_count = 1;
Packit 577717
Packit 577717
	switch (options.opt_setup) {
Packit 577717
	case OPT_IBSOP:
Packit 577717
		ret = setup_pmu_ibsop(pc, pd);
Packit 577717
		break;
Packit 577717
	case OPT_IBSOP_NATIVE:
Packit 577717
		ret = setup_pmu_ibsop_native(pc, pd);
Packit 577717
		break;
Packit 577717
	case OPT_IBSFETCH:
Packit 577717
		num_smpl_pmds = 3;
Packit 577717
		ret = setup_pmu_ibsfetch(pc, pd);
Packit 577717
		break;
Packit 577717
	default:
Packit 577717
		ret = PFMLIB_ERR_NOTSUPP;
Packit 577717
		break;
Packit 577717
	}
Packit 577717
Packit 577717
	if (ret != PFMLIB_SUCCESS) {
Packit 577717
		fatal_error("Can't setup #%d\n", options.opt_setup);
Packit 577717
		exit(1);
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * in this example program, we use fixed-size entries, therefore we
Packit 577717
	 * can compute the entry size in advance. Perfmon-2 supports variable
Packit 577717
	 * size entries.
Packit 577717
	 */
Packit 577717
	entry_size = sizeof(smpl_entry_t)+(num_smpl_pmds<<3);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * prepare context structure.
Packit 577717
	 */
Packit 577717
Packit 577717
	/*
Packit 577717
	 * We initialize the format specific information.
Packit 577717
	 * The format is identified by its UUID which must be copied
Packit 577717
	 * into the ctx_buf_fmt_id field.
Packit 577717
	 */
Packit 577717
	ctx.ctx_flags = options.opt_block ? PFM_FL_NOTIFY_BLOCK : 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 = 3*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("Can't create PFM context %s\n", strerror(errno));
Packit 577717
	}
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
	 * Now program the registers
Packit 577717
	 */
Packit 577717
	if (pfm_write_pmcs(fd, pc, pmc_count))
Packit 577717
		fatal_error("pfm_write_pmcs error errno %d\n",errno);
Packit 577717
	/*
Packit 577717
	 * initialize the PMDs
Packit 577717
	 * To be read, each PMD must be either written or declared
Packit 577717
	 * as being part of a sample (reg_smpl_pmds, reg_reset_pmds)
Packit 577717
	 */
Packit 577717
	if (pfm_write_pmds(fd, pd, pmd_count))
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
	 * 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(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_arg.load_pid = pid;
Packit 577717
	if (pfm_load_context (fd, &load_arg))
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
	gettimeofday(&start_time, NULL);
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
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, pd[0].reg_smpl_pmds, num_smpl_pmds, entry_size);
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
					else
Packit 577717
						warning("pfm_restart: task probably terminated \n");
Packit 577717
				}
Packit 577717
				break;
Packit 577717
			case PFM_MSG_END: /* monitored task terminated */
Packit 577717
				printf("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
	gettimeofday(&end_time, NULL);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * check for any leftover samples
Packit 577717
	 */
Packit 577717
	process_smpl_buf(hdr, pd[0].reg_smpl_pmds, num_smpl_pmds, entry_size);
Packit 577717
Packit 577717
	close(fd);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * unmap buffer, actually free the buffer and context because placed after
Packit 577717
	 * the close(), i.e. is the last reference. See comments about close() above.
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
static void
Packit 577717
usage(void)
Packit 577717
{
Packit 577717
	printf("usage: task_smpl [-h] [--help] [--no-show] [--ovfl-block] "
Packit 577717
	       "[--ibsop] [--ibsfetch] [--ibsop-native] cmd\n");
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 c, ret;
Packit 577717
Packit 577717
	while ((c=getopt_long(argc, argv,"h", the_options, 0)) != -1) {
Packit 577717
		switch(c) {
Packit 577717
			case 0: continue;
Packit 577717
Packit 577717
			case 1:
Packit 577717
			case 'h':
Packit 577717
				usage();
Packit 577717
				exit(0);
Packit 577717
			default:
Packit 577717
				fatal_error("");
Packit 577717
		}
Packit 577717
	}
Packit 577717
Packit 577717
	if (argv[optind] == NULL) {
Packit 577717
		fatal_error("You must specify a command to execute\n");
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
	/*
Packit 577717
	 * Initialize pfm library
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_pmu_type(&c);
Packit 577717
	if (c != PFMLIB_AMD64_PMU) {
Packit 577717
		fatal_error("not running on an AMD64 processor\n");
Packit 577717
	}
Packit 577717
	/*
Packit 577717
	 * XXX: would need to check for family 10h
Packit 577717
	 */
Packit 577717
Packit 577717
	return mainloop(argv+optind);
Packit 577717
}