Blame src/libpfm4/perf_examples/self.c

Packit Service a1973e
/*
Packit Service a1973e
 * self.c - example of a simple self monitoring task
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
 * Based on:
Packit Service a1973e
 * Copyright (c) 2002-2007 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.
Packit Service a1973e
 */
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 <locale.h>
Packit Service a1973e
#include <sys/prctl.h>
Packit Service a1973e
#include <err.h>
Packit Service a1973e
Packit Service a1973e
#include <perfmon/pfmlib_perf_event.h>
Packit Service a1973e
#include "perf_util.h"
Packit Service a1973e
Packit Service a1973e
static const char *gen_events[]={
Packit Service a1973e
	"cycles",
Packit Service a1973e
	"instructions",
Packit Service a1973e
	NULL
Packit Service a1973e
};
Packit Service a1973e
Packit Service a1973e
static volatile int quit;
Packit Service a1973e
void sig_handler(int n)
Packit Service a1973e
{
Packit Service a1973e
	quit = 1;
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
void
Packit Service a1973e
noploop(void)
Packit Service a1973e
{
Packit Service a1973e
	for(;quit == 0;);
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
static void
Packit Service a1973e
print_counts(perf_event_desc_t *fds, int num_fds, const char *msg)
Packit Service a1973e
{
Packit Service a1973e
	uint64_t val;
Packit Service a1973e
	uint64_t values[3];
Packit Service a1973e
	double ratio;
Packit Service a1973e
	int i;
Packit Service a1973e
	ssize_t ret;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * now read the results. We use pfp_event_count because
Packit Service a1973e
	 * libpfm guarantees that counters for the events always
Packit Service a1973e
	 * come first.
Packit Service a1973e
	 */
Packit Service a1973e
	memset(values, 0, sizeof(values));
Packit Service a1973e
Packit Service a1973e
	for (i = 0; i < num_fds; i++) {
Packit Service a1973e
Packit Service a1973e
		ret = read(fds[i].fd, values, sizeof(values));
Packit Service a1973e
		if (ret < (ssize_t)sizeof(values)) {
Packit Service a1973e
			if (ret == -1)
Packit Service a1973e
				err(1, "cannot read results: %s", strerror(errno));
Packit Service a1973e
			else
Packit Service a1973e
				warnx("could not read event%d", i);
Packit Service a1973e
		}
Packit Service a1973e
		/*
Packit Service a1973e
		 * scaling is systematic because we may be sharing the PMU and
Packit Service a1973e
		 * thus may be multiplexed
Packit Service a1973e
		 */
Packit Service a1973e
		val = perf_scale(values);
Packit Service a1973e
		ratio = perf_scale_ratio(values);
Packit Service a1973e
Packit Service a1973e
		printf("%s %'20"PRIu64" %s (%.2f%% scaling, raw=%'"PRIu64", ena=%'"PRIu64", run=%'"PRIu64")\n",
Packit Service a1973e
			msg,
Packit Service a1973e
			val,
Packit Service a1973e
			fds[i].name,
Packit Service a1973e
			(1.0-ratio)*100.0,
Packit Service a1973e
		        values[0],
Packit Service a1973e
			values[1],
Packit Service a1973e
			values[2]);
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
	perf_event_desc_t *fds = NULL;
Packit Service a1973e
	int i, ret, num_fds = 0;
Packit Service a1973e
Packit Service a1973e
	setlocale(LC_ALL, "");
Packit Service a1973e
	/*
Packit Service a1973e
	 * Initialize pfm library (required before we can use it)
Packit Service a1973e
	 */
Packit Service a1973e
	ret = pfm_initialize();
Packit Service a1973e
	if (ret != PFM_SUCCESS)
Packit Service a1973e
		errx(1, "Cannot initialize library: %s", pfm_strerror(ret));
Packit Service a1973e
Packit Service a1973e
	ret = perf_setup_argv_events(argc > 1 ? (const char **)argv+1 : gen_events, &fds, &num_fds);
Packit Service a1973e
	if (ret || !num_fds)
Packit Service a1973e
		errx(1, "cannot setup events");
Packit Service a1973e
Packit Service a1973e
	fds[0].fd = -1;
Packit Service a1973e
	for(i=0; i < num_fds; i++) {
Packit Service a1973e
		/* request timing information necessary for scaling */
Packit Service a1973e
		fds[i].hw.read_format = PERF_FORMAT_SCALE;
Packit Service a1973e
Packit Service a1973e
		fds[i].hw.disabled = 1; /* do not start now */
Packit Service a1973e
Packit Service a1973e
		/* each event is in an independent group (multiplexing likely) */
Packit Service a1973e
		fds[i].fd = perf_event_open(&fds[i].hw, 0, -1, -1, 0);
Packit Service a1973e
		if (fds[i].fd == -1)
Packit Service a1973e
			err(1, "cannot open event %d", i);
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	signal(SIGALRM, sig_handler);
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * enable all counters attached to this thread and created by it
Packit Service a1973e
	 */
Packit Service a1973e
	ret = prctl(PR_TASK_PERF_EVENTS_ENABLE);
Packit Service a1973e
	if (ret)
Packit Service a1973e
		err(1, "prctl(enable) failed");
Packit Service a1973e
Packit Service a1973e
	print_counts(fds, num_fds, "INITIAL: ");
Packit Service a1973e
Packit Service a1973e
	alarm(10);
Packit Service a1973e
Packit Service a1973e
	noploop();
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * disable all counters attached to this thread
Packit Service a1973e
	 */
Packit Service a1973e
	ret = prctl(PR_TASK_PERF_EVENTS_DISABLE);
Packit Service a1973e
	if (ret)
Packit Service a1973e
		err(1, "prctl(disable) failed");
Packit Service a1973e
Packit Service a1973e
	printf("Final counts:\n");
Packit Service a1973e
	print_counts(fds, num_fds, "FINAL:  ");
Packit Service a1973e
Packit Service a1973e
	for (i = 0; i < num_fds; i++)
Packit Service a1973e
	  close(fds[i].fd);
Packit Service a1973e
Packit Service a1973e
	perf_free_fds(fds, num_fds);
Packit Service a1973e
Packit Service a1973e
	/* free libpfm resources cleanly */
Packit Service a1973e
	pfm_terminate();
Packit Service a1973e
Packit Service a1973e
	return 0;
Packit Service a1973e
}