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

Packit 577717
/*
Packit 577717
 * task_smpl.c - example of a task sampling another one using a randomized sampling period
Packit 577717
 *
Packit 577717
 * Copyright (c) 2003-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 <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 <getopt.h>
Packit 577717
#include <time.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
#include <sys/resource.h>
Packit 577717
Packit 577717
#include <perfmon/pfmlib.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
Packit 577717
typedef struct {
Packit 577717
	int opt_no_show;
Packit 577717
	int opt_block;
Packit 577717
} options_t;
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 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
	{ 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
void
Packit 577717
show_task_rusage(const struct timeval *start, const struct timeval *end, const struct rusage *ru)
Packit 577717
{
Packit 577717
	long secs, suseconds, end_usec;
Packit 577717
Packit 577717
	 secs     =  end->tv_sec - start->tv_sec;
Packit 577717
	 end_usec = end->tv_usec;
Packit 577717
Packit 577717
	if (end_usec < start->tv_usec) {
Packit 577717
      		end_usec += 1000000;
Packit 577717
      		secs--;
Packit 577717
    	}
Packit 577717
Packit 577717
  	suseconds = end_usec - start->tv_usec;
Packit 577717
Packit 577717
	printf ("real %ldh%02ldm%02ld.%03lds user %ldh%02ldm%02ld.%03lds sys %ldh%02ldm%02ld.%03lds\n", 
Packit 577717
		secs / 3600, 
Packit 577717
		(secs % 3600) / 60, 
Packit 577717
		secs % 60,
Packit 577717
		suseconds / 1000,
Packit 577717
Packit 577717
		ru->ru_utime.tv_sec / 3600, 
Packit 577717
		(ru->ru_utime.tv_sec % 3600) / 60, 
Packit 577717
		ru->ru_utime.tv_sec% 60,
Packit 577717
		(long)(ru->ru_utime.tv_usec / 1000),
Packit 577717
Packit 577717
		ru->ru_stime.tv_sec / 3600, 
Packit 577717
		(ru->ru_stime.tv_sec % 3600) / 60, 
Packit 577717
		ru->ru_stime.tv_sec% 60,
Packit 577717
		(long)(ru->ru_stime.tv_usec / 1000)
Packit 577717
		);
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
	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
				printf("PMD%-3d:0x%016"PRIx64"\n", j, *reg);
Packit 577717
				reg++;
Packit 577717
				n--;
Packit 577717
			}
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_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
	struct timeval start_time, end_time;
Packit 577717
	struct rusage rusage;
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
	unsigned int i, num_counters;
Packit 577717
	unsigned int max_pmd = 0, num_smpl_pmds = 0;
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
Packit 577717
	pfm_get_num_counters(&num_counters);
Packit 577717
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 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
	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
	/*
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(-1, &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 use pfp_pmc_count to determine the number of PMC to intialize.
Packit 577717
	 * We use pfp_pmd_count to determine the number of PMD to initialize.
Packit 577717
	 * Some events/features may cause extra PMCs to be used, leading to:
Packit 577717
	 * 	- pfp_pmc_count may be >= pfp_event_count
Packit 577717
	 * 	- pfp_pmd_count may be >= pfp_event_count
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
		 * skip first counter (sampling period)
Packit 577717
		 * track highest PMD 
Packit 577717
		 */
Packit 577717
		if (i) {
Packit 577717
			pfm_bv_set(pd[0].reg_smpl_pmds, pd[i].reg_num);
Packit 577717
			if (pd[i].reg_num > max_pmd)
Packit 577717
				max_pmd = pd[i].reg_num;
Packit 577717
			num_smpl_pmds++;
Packit 577717
		}
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * we our sampling counter overflow, we want to be notified.
Packit 577717
	 * The notification will come ONLY when the sampling buffer
Packit 577717
	 * becomes full.
Packit 577717
	 *
Packit 577717
	 * We also activate randomization of the sampling period.
Packit 577717
	 */
Packit 577717
	pd[0].reg_flags	|= PFM_REGFL_OVFL_NOTIFY | PFM_REGFL_RANDOM;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * we also want to reset the other PMDs on
Packit 577717
	 * every overflow. If we do not set
Packit 577717
	 * this, the non-overflowed counters
Packit 577717
	 * will be untouched.
Packit 577717
	 */
Packit 577717
	pfm_bv_copy(pd[0].reg_reset_pmds, pd[0].reg_smpl_pmds, max_pmd);
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
	 * setup randomization parameters, we allow a range of up to +256 here.
Packit 577717
	 */
Packit 577717
	pd[0].reg_random_seed = 5;
Packit 577717
	pd[0].reg_random_mask = 0xff;
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
	printf("programming %u PMCS and %u PMDS\n", outp.pfp_pmc_count, inp.pfp_event_count);
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()+512;
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, outp.pfp_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, outp.pfp_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_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
	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, &rusage);
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
	/*
Packit 577717
	 * close file descritor. Because of mmap() the number of reference to the
Packit 577717
	 * "file" is 2, thus the context is only freed when the last reference is closed
Packit 577717
	 * either by closed or munmap() depending on the order in which those calls are
Packit 577717
	 * made:
Packit 577717
	 * 	- close() -> munmap(): context and buffer destroyed after munmap().
Packit 577717
	 * 			       buffer remains accessible after close().
Packit 577717
	 * 	- munmap() -> close(): buffer unaccessible after munmap(), context and
Packit 577717
	 * 			       buffer destroyed after close().
Packit 577717
	 *
Packit 577717
	 * It is important to free the resources cleanly, especially because the sampling
Packit 577717
	 * buffer reserves locked memory.
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
	show_task_rusage(&start_time, &end_time, &rusage);
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] 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 (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+optind);
Packit 577717
}