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

Packit Service a1973e
/*
Packit Service a1973e
 * smpl_pebs.c - Unified Intel PEBS sampling example
Packit Service a1973e
 *
Packit Service a1973e
 * Copyright (c) 2009 Google, Inc
Packit Service a1973e
 * Contributed by Stephane Eranian <eranian@gmail.com>
Packit Service a1973e
 *
Packit Service a1973e
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit Service a1973e
 * of this software and associated documentation files (the "Software"), to deal
Packit Service a1973e
 * in the Software without restriction, including without limitation the rights
Packit Service a1973e
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
Packit Service a1973e
 * of the Software, and to permit persons to whom the Software is furnished to do so,
Packit Service a1973e
 * subject to the following conditions:
Packit Service a1973e
 *
Packit Service a1973e
 * The above copyright notice and this permission notice shall be included in all
Packit Service a1973e
 * copies or substantial portions of the Software.
Packit Service a1973e
 *
Packit Service a1973e
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
Packit Service a1973e
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
Packit Service a1973e
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
Packit Service a1973e
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Packit Service a1973e
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
Packit Service a1973e
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit Service a1973e
 *
Packit Service a1973e
 * This file is part of libpfm, a performance monitoring support library for
Packit Service a1973e
 * applications on Linux.
Packit Service a1973e
 */
Packit Service a1973e
#include <sys/types.h>
Packit Service a1973e
#include <inttypes.h>
Packit Service a1973e
#include <stdio.h>
Packit Service a1973e
#include <stdlib.h>
Packit Service a1973e
#include <stdarg.h>
Packit Service a1973e
#include <errno.h>
Packit Service a1973e
#include <unistd.h>
Packit Service a1973e
#include <string.h>
Packit Service a1973e
#include <signal.h>
Packit Service a1973e
#include <syscall.h>
Packit Service a1973e
#include <unistd.h>
Packit Service a1973e
#include <sys/wait.h>
Packit Service a1973e
#include <sys/ptrace.h>
Packit Service a1973e
#include <sys/mman.h>
Packit Service a1973e
#include <err.h>
Packit Service a1973e
#include <perfmon/perfmon.h>
Packit Service a1973e
#include <perfmon/perfmon_pebs_smpl.h>
Packit Service a1973e
Packit Service a1973e
#include <perfmon/pfmlib.h>
Packit Service a1973e
#include <perfmon/pfmlib_core.h>
Packit Service a1973e
#include <perfmon/pfmlib_intel_nhm.h>
Packit Service a1973e
Packit Service a1973e
#include "../detect_pmcs.h"
Packit Service a1973e
Packit Service a1973e
#define SMPL_EVENT	"INST_RETIRED:ANY_P" /* PEBS event on all processors */
Packit Service a1973e
Packit Service a1973e
#define NUM_PMCS	16
Packit Service a1973e
#define NUM_PMDS	16
Packit Service a1973e
Packit Service a1973e
#define SMPL_PERIOD	240000ULL /* must not use more bits than actual HW counter width */
Packit Service a1973e
Packit Service a1973e
typedef pfm_pebs_smpl_hdr_t	smpl_hdr_t;
Packit Service a1973e
typedef pfm_pebs_smpl_arg_t	smpl_arg_t;
Packit Service a1973e
#define FMT_NAME		PFM_PEBS_SMPL_NAME
Packit Service a1973e
Packit Service a1973e
static uint64_t collected_samples;
Packit Service a1973e
static uint64_t last_overflow = ~0; /* initialize to biggest value possible */
Packit Service a1973e
Packit Service a1973e
static void (*print_entry)(uint64_t entry, void *addr);
Packit Service a1973e
static int maxpebs = 1; /* 1=Atom/Core, up to 4 on Nehalem */
Packit Service a1973e
Packit Service a1973e
int
Packit Service a1973e
child(char **arg)
Packit Service a1973e
{
Packit Service a1973e
	/*
Packit Service a1973e
	 * force the task to stop before executing the first
Packit Service a1973e
	 * user level instruction
Packit Service a1973e
	 */
Packit Service a1973e
	ptrace(PTRACE_TRACEME, 0, NULL, NULL);
Packit Service a1973e
Packit Service a1973e
	execvp(arg[0], arg);
Packit Service a1973e
	/* not reached */
Packit Service a1973e
	exit(1);
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
static void
Packit Service a1973e
print_p4_entry(uint64_t entry, void *addr)
Packit Service a1973e
{
Packit Service a1973e
	pfm_pebs_p4_smpl_entry_t *ent = addr;
Packit Service a1973e
Packit Service a1973e
	printf("entry %06"PRIu64" eflags:0x%08llx EAX:0x%08llx ESP:0x%08llx IP:0x%08llx\n",
Packit Service a1973e
		entry,
Packit Service a1973e
		(unsigned long long)ent->eflags,
Packit Service a1973e
		(unsigned long long)ent->eax,
Packit Service a1973e
		(unsigned long long)ent->esp,
Packit Service a1973e
		(unsigned long long)ent->ip);
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
Packit Service a1973e
static void
Packit Service a1973e
print_core_entry(uint64_t entry, void *addr)
Packit Service a1973e
{
Packit Service a1973e
	pfm_pebs_core_smpl_entry_t *ent = addr;
Packit Service a1973e
Packit Service a1973e
	printf("entry %06"PRIu64" eflags:0x%08llx EAX:0x%08llx ESP:0x%08llx IP:0x%08llx\n",
Packit Service a1973e
		entry,
Packit Service a1973e
		(unsigned long long)ent->eflags,
Packit Service a1973e
		(unsigned long long)ent->eax,
Packit Service a1973e
		(unsigned long long)ent->esp,
Packit Service a1973e
		(unsigned long long)ent->ip);
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
static void
Packit Service a1973e
print_nhm_entry(uint64_t entry, void *addr)
Packit Service a1973e
{
Packit Service a1973e
	pfm_pebs_nhm_smpl_entry_t *ent = addr;
Packit Service a1973e
Packit Service a1973e
	printf("entry %06"PRIu64" eflags:0x%08llx EAX:0x%08llx ESP:0x%08llx IP:0x%08llx OVFL:0x%08llx\n",
Packit Service a1973e
		entry,
Packit Service a1973e
		(unsigned long long)ent->eflags,
Packit Service a1973e
		(unsigned long long)ent->eax,
Packit Service a1973e
		(unsigned long long)ent->esp,
Packit Service a1973e
		(unsigned long long)ent->ip,
Packit Service a1973e
		(unsigned long long)ent->ia32_perf_global_status);
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
static void
Packit Service a1973e
process_smpl_buf(smpl_hdr_t *hdr)
Packit Service a1973e
{
Packit Service a1973e
	static uint64_t last_count;
Packit Service a1973e
	void *ent;
Packit Service a1973e
	uint64_t entry;
Packit Service a1973e
	unsigned long count;
Packit Service a1973e
Packit Service a1973e
	count = hdr->count;
Packit Service a1973e
Packit Service a1973e
	if (hdr->overflows == last_overflow && last_count == count) {
Packit Service a1973e
		warnx("skipping identical set of samples %"PRIu64" = %"PRIu64"\n",
Packit Service a1973e
			hdr->overflows, last_overflow);
Packit Service a1973e
		return;	
Packit Service a1973e
	}
Packit Service a1973e
	last_count = count;
Packit Service a1973e
	last_overflow = hdr->overflows;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * the beginning of the buffer does not necessarily follow the header
Packit Service a1973e
	 * due to alignement.
Packit Service a1973e
	 */
Packit Service a1973e
	ent   = (hdr+1);
Packit Service a1973e
	entry = collected_samples;
Packit Service a1973e
Packit Service a1973e
	while(count--) {
Packit Service a1973e
		(*print_entry)(entry, ent);
Packit Service a1973e
		ent += hdr->entry_size;
Packit Service a1973e
		entry++;
Packit Service a1973e
	}
Packit Service a1973e
	collected_samples = entry;
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
Packit Service a1973e
int
Packit Service a1973e
main(int argc, char **argv)
Packit Service a1973e
{
Packit Service a1973e
	pfmlib_input_param_t inp;
Packit Service a1973e
	pfmlib_output_param_t outp;
Packit Service a1973e
	pfmlib_core_input_param_t core_inp;
Packit Service a1973e
	pfmlib_nhm_input_param_t nhm_inp;
Packit Service a1973e
	void *mod_inp = NULL;
Packit Service a1973e
	pfmlib_options_t pfmlib_options;
Packit Service a1973e
	pfarg_pmd_t pd[NUM_PMDS];
Packit Service a1973e
	pfarg_pmc_t pc[NUM_PMCS];
Packit Service a1973e
	pfarg_ctx_t ctx;
Packit Service a1973e
	smpl_arg_t buf_arg;
Packit Service a1973e
	pfarg_load_t load_args;
Packit Service a1973e
	pfarg_msg_t msg;
Packit Service a1973e
	smpl_hdr_t *hdr;
Packit Service a1973e
	void *buf_addr;
Packit Service a1973e
	pid_t pid;
Packit Service a1973e
	int ret, fd, status, type;
Packit Service a1973e
	unsigned int i;
Packit Service a1973e
Packit Service a1973e
	if (argc < 2)
Packit Service a1973e
		errx(1, "you need to pass a program to sample");
Packit Service a1973e
Packit Service a1973e
	if (pfm_initialize() != PFMLIB_SUCCESS)
Packit Service a1973e
		errx(1, "libpfm intialization failed");
Packit Service a1973e
Packit Service a1973e
	memset(&core_inp, 0, sizeof(core_inp));
Packit Service a1973e
	memset(&nhm_inp, 0, sizeof(nhm_inp));
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * check we are on an Intel Core PMU
Packit Service a1973e
	 */
Packit Service a1973e
	pfm_get_pmu_type(&type);
Packit Service a1973e
	switch(type) {
Packit Service a1973e
		case PFMLIB_INTEL_CORE_PMU:
Packit Service a1973e
		case PFMLIB_INTEL_ATOM_PMU:
Packit Service a1973e
			print_entry = print_core_entry;
Packit Service a1973e
			core_inp.pfp_core_pebs.pebs_used = 1;
Packit Service a1973e
			mod_inp = &core_inp;
Packit Service a1973e
			break;
Packit Service a1973e
		case PFMLIB_INTEL_NHM_PMU:
Packit Service a1973e
			print_entry = print_nhm_entry;
Packit Service a1973e
			nhm_inp.pfp_nhm_pebs.pebs_used = 1;
Packit Service a1973e
			mod_inp = &nhm_inp;
Packit Service a1973e
			break;
Packit Service a1973e
		case PFMLIB_PENTIUM4_PMU:
Packit Service a1973e
			print_entry = print_p4_entry;
Packit Service a1973e
			break;
Packit Service a1973e
		default:
Packit Service a1973e
			errx(1, "PMU model does not have PEBS support");
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * pass options to library (optional)
Packit Service a1973e
	 */
Packit Service a1973e
	memset(&pfmlib_options, 0, sizeof(pfmlib_options));
Packit Service a1973e
	pfmlib_options.pfm_debug   = 0; /* set to 1 for debug */
Packit Service a1973e
	pfmlib_options.pfm_verbose = 1; /* set to 1 for verbose */
Packit Service a1973e
	pfm_set_options(&pfmlib_options);
Packit Service a1973e
Packit Service a1973e
	memset(pd, 0, sizeof(pd));
Packit Service a1973e
	memset(pc, 0, sizeof(pc));
Packit Service a1973e
	memset(&inp, 0, sizeof(inp));
Packit Service a1973e
	memset(&outp, 0, sizeof(outp));
Packit Service a1973e
Packit Service a1973e
	memset(&ctx, 0, sizeof(ctx));
Packit Service a1973e
	memset(&buf_arg, 0, sizeof(buf_arg));
Packit Service a1973e
	memset(&load_args, 0, sizeof(load_args));
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * search for our sampling event
Packit Service a1973e
	 */
Packit Service a1973e
	if (pfm_find_full_event(SMPL_EVENT, &inp.pfp_events[0]) != PFMLIB_SUCCESS)
Packit Service a1973e
		errx(1, "cannot find sampling event %s", SMPL_EVENT);
Packit Service a1973e
Packit Service a1973e
	for(i=1; i < maxpebs; i++)
Packit Service a1973e
		inp.pfp_events[i] = inp.pfp_events[0];
Packit Service a1973e
Packit Service a1973e
	inp.pfp_event_count = i;
Packit Service a1973e
	inp.pfp_dfl_plm = PFM_PLM3|PFM_PLM0;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * sampling buffer parameters
Packit Service a1973e
	 */
Packit Service a1973e
	buf_arg.buf_size = 2 * getpagesize();
Packit Service a1973e
Packit Service a1973e
	for(i=0; i < maxpebs; i++)
Packit Service a1973e
		buf_arg.cnt_reset[i] = -SMPL_PERIOD;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * create context and sampling buffer
Packit Service a1973e
	 */
Packit Service a1973e
	fd = pfm_create_context(&ctx, FMT_NAME, &buf_arg, sizeof(buf_arg));
Packit Service a1973e
	if (fd == -1) {
Packit Service a1973e
		if (errno == ENOSYS) {
Packit Service a1973e
			errx(1, "Your kernel does not have performance monitoring support!\n");
Packit Service a1973e
		}
Packit Service a1973e
		err(1, "cannot create session, maybe you do not have the PEBS"
Packit Service a1973e
		       " sampling format in the kernel. You need perfmon_pebs_smpl."
Packit Service a1973e
		       "\nCheck /sys/kernel/perfmon/formats");
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * map buffer into our address space
Packit Service a1973e
	 */
Packit Service a1973e
	buf_addr = mmap(NULL, (size_t)buf_arg.buf_size, PROT_READ, MAP_PRIVATE, fd, 0);
Packit Service a1973e
	if (buf_addr == MAP_FAILED)
Packit Service a1973e
		err(1, "cannot mmap sampling buffer");
Packit Service a1973e
Packit Service a1973e
	printf("context [%d] buffer mapped @%p\n", fd, buf_addr);
Packit Service a1973e
Packit Service a1973e
	hdr = (smpl_hdr_t *)buf_addr;
Packit Service a1973e
Packit Service a1973e
	printf("pebs_start=%p pebs_end=%p version=%u.%u.%u entry_size=%u\n",
Packit Service a1973e
		hdr+1,
Packit Service a1973e
		hdr+1,
Packit Service a1973e
		(hdr->version >> 16) & 0xff,
Packit Service a1973e
		(hdr->version >> 8) & 0xff, 
Packit Service a1973e
		hdr->version & 0xff, 
Packit Service a1973e
		hdr->entry_size);
Packit Service a1973e
Packit Service a1973e
	printf("max PEBS entries: %zu\n", (size_t)hdr->pebs_size / hdr->entry_size);
Packit Service a1973e
Packit Service a1973e
	if (((hdr->version >> 16) & 0xff) < 1)
Packit Service a1973e
		errx(1, "invalid buffer format version");
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * get which PMC registers are available
Packit Service a1973e
	 */
Packit Service a1973e
	detect_unavail_pmcs(fd, &inp.pfp_unavail_pmcs);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * let libpfm figure out how to assign event onto PMU registers
Packit Service a1973e
	 */
Packit Service a1973e
	if (pfm_dispatch_events(&inp, mod_inp, &outp, NULL) != PFMLIB_SUCCESS)
Packit Service a1973e
		errx(1, "cannot assign event %s\n", SMPL_EVENT);
Packit Service a1973e
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * propagate PMC setup from libpfm to perfmon
Packit Service a1973e
	 */
Packit Service a1973e
	for (i=0; i < outp.pfp_pmc_count; i++) {
Packit Service a1973e
		pc[i].reg_num   = outp.pfp_pmcs[i].reg_num;
Packit Service a1973e
		pc[i].reg_value = outp.pfp_pmcs[i].reg_value;
Packit Service a1973e
Packit Service a1973e
		/*
Packit Service a1973e
		 * must disable 64-bit emulation on the PMC0 counter
Packit Service a1973e
		 * PMC0 is the only counter useable with PEBS. We must disable
Packit Service a1973e
		 * 64-bit emulation to avoid getting interrupts for each
Packit Service a1973e
		 * sampling period, PEBS takes care of this part.
Packit Service a1973e
		 */
Packit Service a1973e
		if (pc[i].reg_num == 0)
Packit Service a1973e
			pc[i].reg_flags = PFM_REGFL_NO_EMUL64;
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * propagate PMD set from libpfm to perfmon
Packit Service a1973e
	 */
Packit Service a1973e
	for (i=0; i < outp.pfp_pmd_count; i++)
Packit Service a1973e
		pd[i].reg_num = outp.pfp_pmds[i].reg_num;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * setup sampling period for first counter
Packit Service a1973e
	 * we want notification on overflow, i.e., when buffer is full
Packit Service a1973e
	 */
Packit Service a1973e
	for(i=0; i < maxpebs; i++) {
Packit Service a1973e
		pd[i].reg_flags = PFM_REGFL_OVFL_NOTIFY;
Packit Service a1973e
		pd[i].reg_value = -SMPL_PERIOD;
Packit Service a1973e
		pd[i].reg_long_reset = -SMPL_PERIOD;
Packit Service a1973e
		pd[i].reg_short_reset = -SMPL_PERIOD;
Packit Service a1973e
	}
Packit Service a1973e
	
Packit Service a1973e
	/*
Packit Service a1973e
	 * Now program the registers
Packit Service a1973e
	 */
Packit Service a1973e
	if (pfm_write_pmcs(fd, pc, outp.pfp_pmc_count) == -1)
Packit Service a1973e
		err(1, "pfm_write_pmcs error");
Packit Service a1973e
Packit Service a1973e
	if (pfm_write_pmds(fd, pd, outp.pfp_pmd_count) == -1)
Packit Service a1973e
		err(1, "pfm_write_pmds error");
Packit Service a1973e
Packit Service a1973e
	signal(SIGCHLD, SIG_IGN);
Packit Service a1973e
	/*
Packit Service a1973e
	 * Create the child task
Packit Service a1973e
	 */
Packit Service a1973e
	if ((pid=fork()) == -1)
Packit Service a1973e
		err(1, "cannot fork process");
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * In order to get the PFM_END_MSG message, it is important
Packit Service a1973e
	 * to ensure that the child task does not inherit the file
Packit Service a1973e
	 * descriptor of the context. By default, file descriptor
Packit Service a1973e
	 * are inherited during exec(). We explicitely close it
Packit Service a1973e
	 * here. We could have set it up through fcntl(FD_CLOEXEC)
Packit Service a1973e
	 * to achieve the same thing.
Packit Service a1973e
	 */
Packit Service a1973e
	if (pid == 0) {
Packit Service a1973e
		close(fd);
Packit Service a1973e
		child(argv+1);
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * wait for the child to exec
Packit Service a1973e
	 */
Packit Service a1973e
	waitpid(pid, &status, WUNTRACED);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * process is stopped at this point
Packit Service a1973e
	 */
Packit Service a1973e
	if (WIFEXITED(status)) {
Packit Service a1973e
		warnx("task %s [%d] exited already status %d\n", argv[1], pid, WEXITSTATUS(status));
Packit Service a1973e
		goto terminate_session;
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 *  attach the context to child
Packit Service a1973e
	 */
Packit Service a1973e
	load_args.load_pid = pid;
Packit Service a1973e
	if (pfm_load_context(fd, &load_args) == -1)
Packit Service a1973e
		err(1, "pfm_load_context error");
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * start monitoring
Packit Service a1973e
	 */
Packit Service a1973e
	if (pfm_start(fd, NULL) == -1)
Packit Service a1973e
		err(1, "pfm_start error");
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * detach child. Side effect includes
Packit Service a1973e
	 * activation of monitoring.
Packit Service a1973e
	 */
Packit Service a1973e
	ptrace(PTRACE_DETACH, pid, NULL, 0);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * core loop
Packit Service a1973e
	 */
Packit Service a1973e
	for(;;) {
Packit Service a1973e
		/*
Packit Service a1973e
		 * wait for overflow/end notification messages
Packit Service a1973e
		 */
Packit Service a1973e
		ret = read(fd, &msg, sizeof(msg));
Packit Service a1973e
		if (ret == -1) {
Packit Service a1973e
			if(ret == -1 && errno == EINTR) {
Packit Service a1973e
				warnx("read interrupted, retrying");
Packit Service a1973e
				continue;
Packit Service a1973e
			}
Packit Service a1973e
			err(1, "cannot read perfmon msg");
Packit Service a1973e
		}
Packit Service a1973e
		switch(msg.type) {
Packit Service a1973e
			case PFM_MSG_OVFL: /* the sampling buffer is full */
Packit Service a1973e
				process_smpl_buf(hdr);
Packit Service a1973e
				/*
Packit Service a1973e
				 * reactivate monitoring once we are done with the samples
Packit Service a1973e
				 *
Packit Service a1973e
				 * Note that this call can fail with EBUSY in non-blocking mode
Packit Service a1973e
				 * as the task may have disappeared while we were processing
Packit Service a1973e
				 * the samples.
Packit Service a1973e
				 */
Packit Service a1973e
				if (pfm_restart(fd) == -1) {
Packit Service a1973e
					if (errno != EBUSY)
Packit Service a1973e
						err(1, "pfm_restart error");
Packit Service a1973e
					else
Packit Service a1973e
						warnx("pfm_restart: task has probably terminated \n");
Packit Service a1973e
				}
Packit Service a1973e
				break;
Packit Service a1973e
			case PFM_MSG_END: /* monitored task terminated */
Packit Service a1973e
				warnx("TASK terminated");
Packit Service a1973e
				goto terminate_session;
Packit Service a1973e
			default:
Packit Service a1973e
				errx(1, "unknown message type %d", msg.type);
Packit Service a1973e
		}
Packit Service a1973e
	}
Packit Service a1973e
terminate_session:
Packit Service a1973e
	/*
Packit Service a1973e
	 * cleanup child
Packit Service a1973e
	 */
Packit Service a1973e
	wait4(pid, &status, 0, NULL);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * check for any leftover samples
Packit Service a1973e
	 */
Packit Service a1973e
	process_smpl_buf(hdr);
Packit Service a1973e
Packit Service a1973e
	printf("collected samples %"PRIu64", %"PRIu64" overflows\n",
Packit Service a1973e
		collected_samples, last_overflow);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * close context
Packit Service a1973e
	 */
Packit Service a1973e
	close(fd);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * unmap sampling buffer and actually free the perfmon context
Packit Service a1973e
	 */
Packit Service a1973e
	munmap(buf_addr, (size_t)buf_arg.buf_size);
Packit Service a1973e
Packit Service a1973e
	return 0;
Packit Service a1973e
}