Blame src/libpfm4/perf_examples/self_pipe.c

Packit 577717
/*
Packit 577717
 * self_pipe.c - dual process ping-pong example to stress PMU context switch of one process
Packit 577717
 *
Packit 577717
 * Copyright (c) 2008 Google, Inc
Packit 577717
 * Contributed by Stephane Eranian <eranian@gmail.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.
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 <signal.h>
Packit 577717
#include <sched.h>
Packit 577717
#include <err.h>
Packit 577717
#include <sys/ioctl.h>
Packit 577717
#include <sys/prctl.h>
Packit 577717
Packit 577717
#include <perfmon/pfmlib_perf_event.h>
Packit 577717
Packit 577717
#include "perf_util.h"
Packit 577717
Packit 577717
static struct {
Packit 577717
	const char *events;
Packit 577717
	int cpu;
Packit 577717
	int delay;
Packit 577717
} options;
Packit 577717
Packit 577717
int
Packit 577717
pin_cpu(pid_t pid, unsigned int cpu)
Packit 577717
{
Packit 577717
	cpu_set_t mask;
Packit 577717
	CPU_ZERO(&mask);
Packit 577717
	CPU_SET(cpu, &mask);
Packit 577717
Packit 577717
	return sched_setaffinity(pid, sizeof(mask), &mask);
Packit 577717
}
Packit 577717
Packit 577717
Packit 577717
static volatile int quit;
Packit 577717
void sig_handler(int n)
Packit 577717
{
Packit 577717
	quit = 1;
Packit 577717
}
Packit 577717
Packit 577717
static void
Packit 577717
do_child(int fr, int fw)
Packit 577717
{
Packit 577717
	char c;
Packit 577717
	ssize_t ret;
Packit 577717
Packit 577717
	for(;;) {
Packit 577717
		ret = read(fr, &c, 1);	
Packit 577717
		if (ret < 0)
Packit 577717
			break;
Packit 577717
		ret = write(fw, "c", 1);
Packit 577717
		if (ret < 0)
Packit 577717
			break;
Packit 577717
		
Packit 577717
	}
Packit 577717
	printf("child exited\n");
Packit 577717
	exit(0);
Packit 577717
}
Packit 577717
Packit 577717
static void
Packit 577717
measure(void)
Packit 577717
{
Packit 577717
	perf_event_desc_t *fds = NULL;
Packit 577717
	int num_fds = 0;
Packit 577717
	uint64_t values[3];
Packit 577717
	ssize_t n;
Packit 577717
	int i, ret;
Packit 577717
	int pr[2], pw[2];
Packit 577717
	pid_t pid;
Packit 577717
	char cc = '0';
Packit 577717
Packit 577717
	ret = pfm_initialize();
Packit 577717
	if (ret != PFM_SUCCESS)
Packit 577717
		err(1, "cannot initialize libpfm");
Packit 577717
Packit 577717
	if (options.cpu == -1) {
Packit 577717
		srandom(getpid());
Packit 577717
		options.cpu = random() % sysconf(_SC_NPROCESSORS_ONLN);
Packit 577717
	}
Packit 577717
Packit 577717
	ret = pipe(pr);
Packit 577717
	if (ret)
Packit 577717
		err(1, "cannot create read pipe");
Packit 577717
Packit 577717
	ret = pipe(pw);
Packit 577717
	if (ret)
Packit 577717
		err(1, "cannot create write pipe");
Packit 577717
Packit 577717
	ret = perf_setup_list_events(options.events, &fds, &num_fds);
Packit 577717
	if (ret || !num_fds)
Packit 577717
		exit(1);
Packit 577717
Packit 577717
	for(i=0; i < num_fds; i++) {
Packit 577717
		fds[i].hw.disabled = 1;
Packit 577717
		fds[i].hw.read_format = PERF_FORMAT_SCALE;
Packit 577717
Packit 577717
		fds[i].fd = perf_event_open(&fds[i].hw, 0, -1, -1, 0);
Packit 577717
		if (fds[i].fd == -1)
Packit 577717
			err(1, "cannot open event %d", i);
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
 	 * Pin to CPU0, inherited by child process. That will enforce
Packit 577717
 	 * the ping-pionging and thus stress the PMU context switch 
Packit 577717
 	 * which is what we want
Packit 577717
 	 */
Packit 577717
	ret = pin_cpu(getpid(), options.cpu);
Packit 577717
	if (ret)
Packit 577717
		err(1, "cannot pin to CPU%d", options.cpu);
Packit 577717
Packit 577717
	printf("Both processes pinned to CPU%d, running for %d seconds\n", options.cpu, options.delay);
Packit 577717
Packit 577717
	/*
Packit 577717
 	 * create second process which is not monitoring at the moment
Packit 577717
 	 */
Packit 577717
	switch(pid=fork()) {
Packit 577717
		case -1:
Packit 577717
			err(1, "cannot create child\n");
Packit 577717
			exit(1); /* not reached */
Packit 577717
		case 0:
Packit 577717
			/* do not inherit session fd */
Packit 577717
			for(i=0; i < num_fds; i++)
Packit 577717
				close(fds[i].fd);
Packit 577717
			/* pr[]: write master, read child */
Packit 577717
			/* pw[]: read master, write child */
Packit 577717
			close(pr[1]); close(pw[0]);
Packit 577717
			do_child(pr[0], pw[1]);
Packit 577717
			exit(1);
Packit 577717
	}
Packit 577717
Packit 577717
	close(pr[0]);
Packit 577717
	close(pw[1]);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * Let's roll now
Packit 577717
	 */
Packit 577717
	prctl(PR_TASK_PERF_EVENTS_ENABLE);
Packit 577717
	signal(SIGALRM, sig_handler);
Packit 577717
	alarm(options.delay);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * ping pong loop
Packit 577717
	 */
Packit 577717
	while(!quit) {
Packit 577717
		n = write(pr[1], "c", 1);
Packit 577717
		if (n < 1)
Packit 577717
			err(1, "write failed");
Packit 577717
		n = read(pw[0], &cc, 1);
Packit 577717
		if (n < 1)
Packit 577717
			err(1, "read failed");
Packit 577717
	}
Packit 577717
Packit 577717
	prctl(PR_TASK_PERF_EVENTS_DISABLE);
Packit 577717
Packit 577717
	for(i=0; i < num_fds; i++) {
Packit 577717
		uint64_t val;
Packit 577717
		double ratio;
Packit 577717
Packit 577717
		ret = read(fds[i].fd, values, sizeof(values));
Packit 577717
		if (ret == -1)
Packit 577717
			err(1,"pfm_read error");
Packit 577717
		if (ret != sizeof(values))
Packit 577717
			errx(1, "did not read correct amount %d", ret);
Packit 577717
Packit 577717
		val = perf_scale(values);
Packit 577717
		ratio = perf_scale_ratio(values);
Packit 577717
Packit 577717
		if (ratio == 1.0)
Packit 577717
			printf("%20"PRIu64" %s\n", val, fds[i].name);
Packit 577717
		else
Packit 577717
			if (ratio == 0.0)
Packit 577717
				printf("%20"PRIu64" %s (did not run: competing session)\n", val, fds[i].name);
Packit 577717
			else
Packit 577717
				printf("%20"PRIu64" %s (scaled from %.2f%% of time)\n", val, fds[i].name, ratio*100.0);
Packit 577717
	}
Packit 577717
	/*
Packit 577717
	 * kill child process
Packit 577717
	 */
Packit 577717
	kill(SIGKILL, pid);
Packit 577717
Packit 577717
	/*
Packit 577717
 	 * close pipes
Packit 577717
 	 */
Packit 577717
	close(pr[1]);
Packit 577717
	close(pw[0]);
Packit 577717
	/*
Packit 577717
	 * and destroy our session
Packit 577717
	 */
Packit 577717
	for(i=0; i < num_fds; i++)
Packit 577717
		close(fds[i].fd);
Packit 577717
Packit 577717
	perf_free_fds(fds, num_fds);
Packit 577717
Packit 577717
	/* free libpfm resources cleanly */
Packit 577717
	pfm_terminate();
Packit 577717
}
Packit 577717
Packit 577717
static void
Packit 577717
usage(void)
Packit 577717
{
Packit 577717
	printf("usage: self_pipe [-h] [-c cpu] [-d delay] [-e event1,event2,...]\n");
Packit 577717
}
Packit 577717
Packit 577717
int
Packit 577717
main(int argc, char **argv)
Packit 577717
{
Packit 577717
	int c;
Packit 577717
Packit 577717
	options.cpu = -1;
Packit 577717
	options.delay = -1;
Packit 577717
Packit 577717
	while ((c=getopt(argc, argv,"he:c:d:")) != -1) {
Packit 577717
		switch(c) {
Packit 577717
			case 'e':
Packit 577717
				options.events = optarg;
Packit 577717
				break;
Packit 577717
			case 'c':
Packit 577717
				options.cpu = atoi(optarg);
Packit 577717
				break;
Packit 577717
			case 'd':
Packit 577717
				options.delay = atoi(optarg);
Packit 577717
				break;
Packit 577717
			case 'h':
Packit 577717
				usage();
Packit 577717
				exit(0);
Packit 577717
			default:
Packit 577717
				errx(1, "unknown error");
Packit 577717
		}
Packit 577717
	}
Packit 577717
	if (!options.events)
Packit 577717
		options.events = "cycles,instructions";
Packit 577717
Packit 577717
	if (options.delay == -1)
Packit 577717
		options.delay = 10;
Packit 577717
Packit 577717
	measure();
Packit 577717
Packit 577717
	return 0;
Packit 577717
}