Blame src/libpfm-3.y/examples_ia64_v2.0/task.c

Packit Service a1973e
/*
Packit Service a1973e
 * task.c - example of a task monitoring another one
Packit Service a1973e
 *
Packit Service a1973e
 * Copyright (c) 2002-2006 Hewlett-Packard Development Company, L.P.
Packit Service a1973e
 * Contributed by Stephane Eranian <eranian@hpl.hp.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/ia64.
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 <stdarg.h>
Packit Service a1973e
#include <sys/wait.h>
Packit Service a1973e
#include <sys/ptrace.h>
Packit Service a1973e
Packit Service a1973e
#include <perfmon/pfmlib.h>
Packit Service a1973e
#include <perfmon/perfmon.h>
Packit Service a1973e
Packit Service a1973e
#define NUM_PMCS PFMLIB_MAX_PMCS
Packit Service a1973e
#define NUM_PMDS PFMLIB_MAX_PMDS
Packit Service a1973e
Packit Service a1973e
#define MAX_EVT_NAME_LEN	128
Packit Service a1973e
Packit Service a1973e
static void fatal_error(char *fmt,...) __attribute__((noreturn));
Packit Service a1973e
Packit Service a1973e
static void
Packit Service a1973e
fatal_error(char *fmt, ...)
Packit Service a1973e
{
Packit Service a1973e
	va_list ap;
Packit Service a1973e
Packit Service a1973e
	va_start(ap, fmt);
Packit Service a1973e
	vfprintf(stderr, fmt, ap);
Packit Service a1973e
	va_end(ap);
Packit Service a1973e
Packit Service a1973e
	exit(1);
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
int
Packit Service a1973e
child(char **arg)
Packit Service a1973e
{
Packit Service a1973e
	/*
Packit Service a1973e
	 * will cause the program to stop before executing the first
Packit Service a1973e
	 * user level instruction. We can only attach (load) a context
Packit Service a1973e
	 * if the task is in the STOPPED state.
Packit Service a1973e
	 */
Packit Service a1973e
	ptrace(PTRACE_TRACEME, 0, NULL, NULL);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * execute the requested command
Packit Service a1973e
	 */
Packit Service a1973e
	execvp(arg[0], arg);
Packit Service a1973e
Packit Service a1973e
	fatal_error("cannot exec: %s\n", arg[0]);
Packit Service a1973e
	/* not reached */
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
int
Packit Service a1973e
parent(char **arg)
Packit Service a1973e
{
Packit Service a1973e
	pfmlib_input_param_t inp;
Packit Service a1973e
	pfmlib_output_param_t outp;
Packit Service a1973e
	pfarg_context_t ctx[1];
Packit Service a1973e
	pfarg_reg_t pc[NUM_PMCS];
Packit Service a1973e
	pfarg_reg_t pd[NUM_PMDS];
Packit Service a1973e
	pfarg_load_t load_args;
Packit Service a1973e
	unsigned int i, num_counters;
Packit Service a1973e
	int status, ret;
Packit Service a1973e
	int ctx_fd;
Packit Service a1973e
	pid_t pid;
Packit Service a1973e
	char name[MAX_EVT_NAME_LEN];
Packit Service a1973e
Packit Service a1973e
	memset(pc, 0, sizeof(ctx));
Packit Service a1973e
	memset(pd, 0, sizeof(ctx));
Packit Service a1973e
	memset(ctx, 0, sizeof(ctx));
Packit Service a1973e
	memset(&inp,0, sizeof(inp));
Packit Service a1973e
	memset(&outp,0, sizeof(outp));
Packit Service a1973e
	memset(&load_args,0, sizeof(load_args));
Packit Service a1973e
Packit Service a1973e
	pfm_get_num_counters(&num_counters);
Packit Service a1973e
Packit Service a1973e
	if (pfm_get_cycle_event(&inp.pfp_events[0]) != PFMLIB_SUCCESS)
Packit Service a1973e
		fatal_error("cannot find cycle event\n");
Packit Service a1973e
Packit Service a1973e
	if (pfm_get_inst_retired_event(&inp.pfp_events[1]) != PFMLIB_SUCCESS)
Packit Service a1973e
		fatal_error("cannot find inst retired event\n");
Packit Service a1973e
	i = 2;
Packit Service a1973e
Packit Service a1973e
	if (num_counters < i) {
Packit Service a1973e
		i = num_counters;
Packit Service a1973e
		printf("too many events provided (max=%d events), using first %d event(s)\n", num_counters, i);
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * set the privilege mode:
Packit Service a1973e
	 * 	PFM_PLM3 : user level
Packit Service a1973e
	 * 	PFM_PLM0 : kernel level
Packit Service a1973e
	 */
Packit Service a1973e
	inp.pfp_dfl_plm   = PFM_PLM3;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * how many counters we use
Packit Service a1973e
	 */
Packit Service a1973e
	inp.pfp_event_count = i;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * let the library figure out the values for the PMCS
Packit Service a1973e
	 */
Packit Service a1973e
	if ((ret=pfm_dispatch_events(&inp, NULL, &outp, NULL)) != PFMLIB_SUCCESS) {
Packit Service a1973e
		fatal_error("cannot configure events: %s\n", pfm_strerror(ret));
Packit Service a1973e
	}
Packit Service a1973e
	/*
Packit Service a1973e
	 * now create a context. we will later attach it to the task we are creating.
Packit Service a1973e
	 */
Packit Service a1973e
	if (perfmonctl(0, PFM_CREATE_CONTEXT, ctx, 1) == -1) {
Packit Service a1973e
		if (errno == ENOSYS) {
Packit Service a1973e
			fatal_error("Your kernel does not have performance monitoring support!\n");
Packit Service a1973e
		}
Packit Service a1973e
		fatal_error("Can't create PFM context %s\n", strerror(errno));
Packit Service a1973e
	}
Packit Service a1973e
	/*
Packit Service a1973e
	 * extract the identifier for our context
Packit Service a1973e
	 */
Packit Service a1973e
	ctx_fd = ctx[0].ctx_fd;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * Now prepare the argument to initialize the PMDs and PMCS.
Packit Service a1973e
	 * We must pfp_pmc_count to determine the number of PMC to intialize.
Packit Service a1973e
	 * We must use pfp_event_count to determine the number of PMD to initialize.
Packit Service a1973e
	 * Some events causes extra PMCs to be used, so  pfp_pmc_count may be >= pfp_event_count.
Packit Service a1973e
	 *
Packit Service a1973e
	 * This step is new compared to libpfm-2.x. It is necessary because the library no
Packit Service a1973e
	 * longer knows about the kernel data structures.
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
	/*
Packit Service a1973e
	 * the PMC controlling the event ALWAYS come first, that's why this loop
Packit Service a1973e
	 * is safe even when extra PMC are needed to support a particular event.
Packit Service a1973e
	 */
Packit Service a1973e
	for (i=0; i < inp.pfp_event_count; i++) {
Packit Service a1973e
		pd[i].reg_num   = pc[i].reg_num;
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * Now program the registers
Packit Service a1973e
	 *
Packit Service a1973e
	 * We don't use the save variable to indicate the number of elements passed to
Packit Service a1973e
	 * the kernel because, as we said earlier, pc may contain more elements than
Packit Service a1973e
	 * the number of events we specified, i.e., contains more thann counting monitors.
Packit Service a1973e
	 */
Packit Service a1973e
Packit Service a1973e
	if (perfmonctl(ctx_fd, PFM_WRITE_PMCS, pc, outp.pfp_pmc_count) == -1) {
Packit Service a1973e
		fatal_error("perfmonctl error PFM_WRITE_PMCS errno %d\n",errno);
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	if (perfmonctl(ctx_fd, PFM_WRITE_PMDS, pd, inp.pfp_event_count) == -1) {
Packit Service a1973e
		fatal_error("perfmonctl error PFM_WRITE_PMDS errno %d\n",errno);
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * Create the child task
Packit Service a1973e
	 */
Packit Service a1973e
	if ((pid=fork()) == -1) fatal_error("Cannot fork process\n");
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * and launch the child code
Packit Service a1973e
	 */
Packit Service a1973e
	if (pid == 0) exit(child(arg));
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
	 * check if process exited early
Packit Service a1973e
	 */
Packit Service a1973e
	if (WIFEXITED(status)) {
Packit Service a1973e
		fatal_error("command %s exited too early with status %d\n", arg[0], WEXITSTATUS(status));
Packit Service a1973e
	}
Packit Service a1973e
	/*
Packit Service a1973e
	 * the task is stopped at this point
Packit Service a1973e
	 */
Packit Service a1973e
	
Packit Service a1973e
	
Packit Service a1973e
	/*
Packit Service a1973e
	 * now we load (i.e., attach) the context to ourself
Packit Service a1973e
	 */
Packit Service a1973e
	load_args.load_pid = pid;
Packit Service a1973e
Packit Service a1973e
	if (perfmonctl(ctx_fd, PFM_LOAD_CONTEXT, &load_args, 1) == -1) {
Packit Service a1973e
		fatal_error("perfmonctl error PFM_LOAD_CONTEXT errno %d\n",errno);
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * activate monitoring. The task is still STOPPED at this point. Monitoring
Packit Service a1973e
	 * will not take effect until the execution of the task is resumed.
Packit Service a1973e
	 */
Packit Service a1973e
	if (perfmonctl(ctx_fd, PFM_START, NULL, 0) == -1) {
Packit Service a1973e
		fatal_error("perfmonctl error PFM_START errno %d\n",errno);
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * now resume execution of the task, effectively activating
Packit Service a1973e
	 * monitoring.
Packit Service a1973e
	 */
Packit Service a1973e
	ptrace(PTRACE_DETACH, pid, NULL, 0);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * now the task is running
Packit Service a1973e
	 */
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * simply wait for completion
Packit Service a1973e
	 */
Packit Service a1973e
	waitpid(pid, &status, 0);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * the task has disappeared at this point but our context is still
Packit Service a1973e
	 * present and contains all the latest counts.
Packit Service a1973e
	 */
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * now simply read the results.
Packit Service a1973e
	 */
Packit Service a1973e
	if (perfmonctl(ctx_fd, PFM_READ_PMDS, pd, inp.pfp_event_count) == -1) {
Packit Service a1973e
		fatal_error("perfmonctl error READ_PMDS errno %d\n",errno);
Packit Service a1973e
		return -1;
Packit Service a1973e
	}
Packit Service a1973e
	/*
Packit Service a1973e
	 * print the results
Packit Service a1973e
	 *
Packit Service a1973e
	 * It is important to realize, that the first event we specified may not
Packit Service a1973e
	 * be in PMD4. Not all events can be measured by any monitor. That's why
Packit Service a1973e
	 * we need to use the pc[] array to figure out where event i was allocated.
Packit Service a1973e
	 *
Packit Service a1973e
	 */
Packit Service a1973e
	for (i=0; i < inp.pfp_event_count; i++) {
Packit Service a1973e
		pfm_get_full_event_name(&inp.pfp_events[i], name, MAX_EVT_NAME_LEN);
Packit Service a1973e
		printf("PMD%u %20"PRIu64" %s\n",
Packit Service a1973e
			pd[i].reg_num,
Packit Service a1973e
			pd[i].reg_value,
Packit Service a1973e
			name);
Packit Service a1973e
	}
Packit Service a1973e
	/*
Packit Service a1973e
	 * free the context
Packit Service a1973e
	 */
Packit Service a1973e
	close(ctx_fd);
Packit Service a1973e
Packit Service a1973e
	return 0;
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_options_t pfmlib_options;
Packit Service a1973e
Packit Service a1973e
	if (argc < 2) {
Packit Service a1973e
		fatal_error("You must specify a command to execute\n");
Packit Service a1973e
	}
Packit Service a1973e
	/*
Packit Service a1973e
	 * Initialize pfm library (required before we can use it)
Packit Service a1973e
	 */
Packit Service a1973e
	if (pfm_initialize() != PFMLIB_SUCCESS) {
Packit Service a1973e
		printf("Can't initialize library\n");
Packit Service a1973e
		exit(1);
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
	pfm_set_options(&pfmlib_options);
Packit Service a1973e
Packit Service a1973e
	return parent(argv+1);
Packit Service a1973e
}