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

Packit 577717
/*
Packit 577717
 * task_attach_timeout.c - attach to another task for monitoring for a short while
Packit 577717
 *
Packit 577717
 * Copyright (c) 2002-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/ia64.
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 <stdarg.h>
Packit 577717
#include <sys/wait.h>
Packit 577717
#include <sys/ptrace.h>
Packit 577717
#include <sys/poll.h>
Packit 577717
Packit 577717
#include <perfmon/pfmlib.h>
Packit 577717
#include <perfmon/perfmon.h>
Packit 577717
Packit 577717
Packit 577717
#define NUM_PMCS PFMLIB_MAX_PMCS
Packit 577717
#define NUM_PMDS PFMLIB_MAX_PMDS
Packit 577717
Packit 577717
#define MAX_EVT_NAME_LEN	128
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
int
Packit 577717
parent(pid_t pid, unsigned long delay)
Packit 577717
{
Packit 577717
	pfmlib_input_param_t inp;
Packit 577717
	pfmlib_output_param_t outp;
Packit 577717
	pfarg_context_t ctx[1];
Packit 577717
	pfarg_reg_t pc[NUM_PMCS];
Packit 577717
	pfarg_reg_t pd[NUM_PMDS];
Packit 577717
	pfarg_load_t load_args;
Packit 577717
	struct pollfd pollfd;
Packit 577717
	pfm_msg_t msg;
Packit 577717
	unsigned int i, num_counters;
Packit 577717
	int status, ret;
Packit 577717
	int ctx_fd;
Packit 577717
	char name[MAX_EVT_NAME_LEN];
Packit 577717
Packit 577717
Packit 577717
	memset(pc, 0, sizeof(ctx));
Packit 577717
	memset(pd, 0, sizeof(ctx));
Packit 577717
	memset(ctx, 0, sizeof(ctx));
Packit 577717
	memset(&inp,0, sizeof(inp));
Packit 577717
	memset(&outp,0, sizeof(outp));
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
	i = 2;
Packit 577717
Packit 577717
	if (num_counters < i) {
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
	 * 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
	/*
Packit 577717
	 * how many counters we use
Packit 577717
	 */
Packit 577717
	inp.pfp_event_count = i;
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 create a context. we will later attach it to the task we are creating.
Packit 577717
	 */
Packit 577717
	if (perfmonctl(0, PFM_CREATE_CONTEXT, ctx, 1) == -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
	 * extract the identifier for our context
Packit 577717
	 */
Packit 577717
	ctx_fd = ctx[0].ctx_fd;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * use our file descriptor for the poll.
Packit 577717
	 * we are interested in read events only.
Packit 577717
	 */
Packit 577717
	pollfd.fd     = ctx_fd;
Packit 577717
	pollfd.events = POLLIN;
Packit 577717
Packit 577717
Packit 577717
	/*
Packit 577717
	 * Now prepare the argument to initialize the PMDs and PMCS.
Packit 577717
	 * We must pfp_pmc_count to determine the number of PMC to intialize.
Packit 577717
	 * We must use pfp_event_count to determine the number of PMD to initialize.
Packit 577717
	 * Some events causes extra PMCs to be used, so  pfp_pmc_count may be >= pfp_event_count.
Packit 577717
	 *
Packit 577717
	 * This step is new compared to libpfm-2.x. It is necessary because the library no
Packit 577717
	 * longer knows about the kernel data structures.
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
	/*
Packit 577717
	 * the PMC controlling the event ALWAYS come first, that's why this loop
Packit 577717
	 * is safe even when extra PMC are needed to support a particular event.
Packit 577717
	 */
Packit 577717
	for (i=0; i < inp.pfp_event_count; i++) {
Packit 577717
		pd[i].reg_num   = pc[i].reg_num;
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * Now program the registers
Packit 577717
	 *
Packit 577717
	 * We don't use the save variable to indicate the number of elements passed to
Packit 577717
	 * the kernel because, as we said earlier, pc may contain more elements than
Packit 577717
	 * the number of events we specified, i.e., contains more thann counting monitors.
Packit 577717
	 */
Packit 577717
Packit 577717
	if (perfmonctl(ctx_fd, PFM_WRITE_PMCS, pc, outp.pfp_pmc_count) == -1) {
Packit 577717
		fatal_error("perfmonctl error PFM_WRITE_PMCS errno %d\n",errno);
Packit 577717
	}
Packit 577717
Packit 577717
	if (perfmonctl(ctx_fd, PFM_WRITE_PMDS, pd, inp.pfp_event_count) == -1) {
Packit 577717
		fatal_error("perfmonctl error PFM_WRITE_PMDS errno %d\n",errno);
Packit 577717
	}
Packit 577717
Packit 577717
	ret = ptrace(PTRACE_ATTACH, pid, NULL, 0);
Packit 577717
	if (ret == -1) {
Packit 577717
		fatal_error("cannot attach to %d: %s\n", pid, strerror(errno));
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * wait for the child to be actually stopped
Packit 577717
	 */
Packit 577717
	waitpid(pid, &status, WUNTRACED);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * check if process exited early
Packit 577717
	 */
Packit 577717
	if (WIFEXITED(status)) {
Packit 577717
		fatal_error("command process %d exited too early with status %d\n", pid, WEXITSTATUS(status));
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * the task is stopped at this point
Packit 577717
	 */
Packit 577717
	
Packit 577717
	/*
Packit 577717
	 * now we load (i.e., attach) the context to ourself
Packit 577717
	 */
Packit 577717
	load_args.load_pid = pid;
Packit 577717
Packit 577717
	if (perfmonctl(ctx_fd, PFM_LOAD_CONTEXT, &load_args, 1) == -1) {
Packit 577717
		fatal_error("perfmonctl error PFM_LOAD_CONTEXT errno %d\n",errno);
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * activate monitoring. The task is still STOPPED at this point. Monitoring
Packit 577717
	 * will not take effect until the execution of the task is resumed.
Packit 577717
	 */
Packit 577717
	if (perfmonctl(ctx_fd, PFM_START, NULL, 0) == -1) {
Packit 577717
		fatal_error("perfmonctl error PFM_START errno %d\n",errno);
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * now resume execution of the task, effectively activating
Packit 577717
	 * monitoring.
Packit 577717
	 */
Packit 577717
	ptrace(PTRACE_DETACH, pid, NULL, 0);
Packit 577717
	printf("attached to [%d], timeout set to %lu seconds\n", pid, delay);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * now the task is running
Packit 577717
	 */
Packit 577717
Packit 577717
	/*
Packit 577717
	 * We cannot simply do a waitpid() because we may be attaching to a process
Packit 577717
	 * totally unrelated to our program. Instead we use a perfmon facility that
Packit 577717
	 * notifies us when the monitoring task is exiting.
Packit 577717
	 *
Packit 577717
	 * When a task with a monitoring context attached to it exits, a PFM_MSG_END
Packit 577717
	 * is generated. It can be retrieve with a simple read() on the context's descriptor.
Packit 577717
	 *
Packit 577717
	 * Another reason why you might return from the read is if there was a counter
Packit 577717
	 * overflow, unlikely in this example.
Packit 577717
	 *
Packit 577717
	 * To measure only for short period of time, use select or poll with a timeout,
Packit 577717
	 * see task_attach_timeout.c
Packit 577717
	 *
Packit 577717
	 */
Packit 577717
	ret = poll(&pollfd, 1, delay*1000);
Packit 577717
	switch( ret ) {
Packit 577717
		case -1:
Packit 577717
			fatal_error("cannot read from descriptor: %s\n", strerror(errno));
Packit 577717
			/* no return */
Packit 577717
		case  1:
Packit 577717
			/*
Packit 577717
	 		 * there is a message, i.e., the program exited before our timeout
Packit 577717
	 		 */
Packit 577717
			if (ret == 1) {
Packit 577717
				/*
Packit 577717
		 		* extract message
Packit 577717
		 		*/
Packit 577717
				ret = read(ctx_fd, &msg, sizeof(msg));
Packit 577717
Packit 577717
				if (msg.type != PFM_MSG_END) {
Packit 577717
					fatal_error("unexpected msg type : %d\n", msg.type);
Packit 577717
				}
Packit 577717
			}
Packit 577717
			break;
Packit 577717
		case   0:
Packit 577717
			/*
Packit 577717
			 * we timed out, we need to stop the task to unload
Packit 577717
			 */
Packit 577717
			ret = ptrace(PTRACE_ATTACH, pid, NULL, 0);
Packit 577717
			if (ret == -1) {
Packit 577717
				fatal_error("cannot attach to %d: %s\n", pid, strerror(errno));
Packit 577717
			}
Packit 577717
			/*
Packit 577717
			 * wait for task to be actually stopped
Packit 577717
			 */
Packit 577717
			waitpid(pid, &status, WUNTRACED);
Packit 577717
Packit 577717
			/*
Packit 577717
	 		 * check if process exited, then no need to unload
Packit 577717
	 		 */
Packit 577717
			if (WIFEXITED(status)) goto read_results;
Packit 577717
Packit 577717
			if (perfmonctl(ctx_fd, PFM_UNLOAD_CONTEXT, NULL, 0) == -1) {
Packit 577717
				fatal_error("perfmonctl error PFM_UNLOAD_CONTEXT errno %d\n",errno);
Packit 577717
			}
Packit 577717
Packit 577717
			/*
Packit 577717
			 * let it run free again
Packit 577717
			 */
Packit 577717
			ptrace(PTRACE_DETACH, pid, NULL, 0);
Packit 577717
			break;
Packit 577717
		default:
Packit 577717
			fatal_error("unexpected return from poll: %d\n", ret);
Packit 577717
	}
Packit 577717
Packit 577717
read_results:
Packit 577717
	/*
Packit 577717
	 * now simply read the results.
Packit 577717
	 */
Packit 577717
	if (perfmonctl(ctx_fd, PFM_READ_PMDS, pd, inp.pfp_event_count) == -1) {
Packit 577717
		fatal_error("perfmonctl error READ_PMDS errno %d\n",errno);
Packit 577717
		return -1;
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * print the results
Packit 577717
	 *
Packit 577717
	 * It is important to realize, that the first event we specified may not
Packit 577717
	 * be in PMD4. Not all events can be measured by any monitor. That's why
Packit 577717
	 * we need to use the pc[] array to figure out where event i was allocated.
Packit 577717
	 *
Packit 577717
	 */
Packit 577717
	for (i=0; i < inp.pfp_event_count; i++) {
Packit 577717
		pfm_get_full_event_name(&inp.pfp_events[i], name, MAX_EVT_NAME_LEN);
Packit 577717
		printf("PMD%u %20"PRIu64" %s\n",
Packit 577717
			pd[i].reg_num,
Packit 577717
			pd[i].reg_value,
Packit 577717
			name);
Packit 577717
	}
Packit 577717
	/*
Packit 577717
	 * free the context
Packit 577717
	 */
Packit 577717
	close(ctx_fd);
Packit 577717
Packit 577717
	return 0;
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
	unsigned long delay;
Packit 577717
	pid_t pid;
Packit 577717
Packit 577717
	if (argc < 2) {
Packit 577717
		fatal_error("usage: %s pid [timeout]\n", argv[0]);
Packit 577717
	}
Packit 577717
Packit 577717
	pid   = atoi(argv[1]);
Packit 577717
	delay = argc > 2 ? strtoul(argv[2], NULL, 10) : 10;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * Initialize pfm library (required before we can use it)
Packit 577717
	 */
Packit 577717
	if (pfm_initialize() != PFMLIB_SUCCESS) {
Packit 577717
		printf("Can't initialize library\n");
Packit 577717
		exit(1);
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
	pfm_set_options(&pfmlib_options);
Packit 577717
Packit 577717
	return parent(pid, delay);
Packit 577717
}