Blame src/libpfm4/perf_examples/self_count.c

Packit 577717
/*
Packit 577717
 * self_count.c - example of a simple self monitoring using mmapped page
Packit 577717
 *
Packit 577717
 * Copyright (c) 2009 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
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 <sys/mman.h>
Packit 577717
#include <sys/ioctl.h>
Packit 577717
#include <err.h>
Packit 577717
Packit 577717
#include "perf_util.h"
Packit 577717
Packit 577717
static const char *gen_events[]={
Packit 577717
	"cycles",
Packit 577717
	NULL
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
#if defined(__x86_64__) || defined(__i386__)
Packit 577717
Packit 577717
#ifdef __x86_64__
Packit 577717
#define DECLARE_ARGS(val, low, high)	unsigned low, high
Packit 577717
#define EAX_EDX_VAL(val, low, high)	((low) | ((uint64_t )(high) << 32))
Packit 577717
#define EAX_EDX_ARGS(val, low, high)	"a" (low), "d" (high)
Packit 577717
#define EAX_EDX_RET(val, low, high)	"=a" (low), "=d" (high)
Packit 577717
#else
Packit 577717
#define DECLARE_ARGS(val, low, high)	unsigned long long val
Packit 577717
#define EAX_EDX_VAL(val, low, high)	(val)
Packit 577717
#define EAX_EDX_ARGS(val, low, high)	"A" (val)
Packit 577717
#define EAX_EDX_RET(val, low, high)	"=A" (val)
Packit 577717
#endif
Packit 577717
Packit 577717
#define barrier() __asm__ __volatile__("": : :"memory")
Packit 577717
Packit 577717
static inline int rdpmc(struct perf_event_mmap_page *hdr, uint64_t *value)
Packit 577717
{
Packit 577717
	int counter = hdr->index - 1;
Packit 577717
	DECLARE_ARGS(val, low, high);
Packit 577717
Packit 577717
	if (counter < 0)
Packit 577717
		return -1;
Packit 577717
Packit 577717
	asm volatile("rdpmc" : EAX_EDX_RET(val, low, high) : "c" (counter));
Packit 577717
	*value = EAX_EDX_VAL(val, low, high);
Packit 577717
	return 0;
Packit 577717
}
Packit 577717
#else
Packit 577717
/*
Packit 577717
 *  Default barrier macro.
Packit 577717
 *  Given this is architecture specific, it must be defined when
Packit 577717
 *  libpfm is ported to new architecture. The default macro below
Packit 577717
 *  simply does nothing.
Packit 577717
 */
Packit 577717
#define barrier() {}
Packit 577717
Packit 577717
/*
Packit 577717
 *  Default function to read counter directly from user level mode.
Packit 577717
 *  Given this is architecture specific, it must be defined when
Packit 577717
 *  libpfm is ported to new architecture. The default routine below
Packit 577717
 *  simply fails and the caller falls backs to syscall.
Packit 577717
 */
Packit 577717
static inline int rdpmc(struct perf_event_mmap_page *hdr, uint64_t *value)
Packit 577717
{
Packit 577717
	int counter = hdr->index - 1;
Packit 577717
Packit 577717
	if (counter < 0)
Packit 577717
		return -1;
Packit 577717
Packit 577717
	printf("your architecture does not have a way to read counters from user mode\n");
Packit 577717
	return -1;
Packit 577717
}
Packit 577717
#endif
Packit 577717
Packit 577717
/*
Packit 577717
 * our test code (function cannot be made static otherwise it is optimized away)
Packit 577717
 */
Packit 577717
unsigned long 
Packit 577717
fib(unsigned long n)
Packit 577717
{
Packit 577717
	if (n == 0)
Packit 577717
		return 0;
Packit 577717
	if (n == 1)
Packit 577717
		return 2;
Packit 577717
	return fib(n-1)+fib(n-2);
Packit 577717
}
Packit 577717
Packit 577717
uint64_t
Packit 577717
read_count(perf_event_desc_t *fds)
Packit 577717
{
Packit 577717
	struct perf_event_mmap_page *hdr;
Packit 577717
	uint64_t values[3];
Packit 577717
	uint64_t count = 0;
Packit 577717
	uint32_t width;
Packit 577717
	unsigned int seq;
Packit 577717
	ssize_t ret;
Packit 577717
	int idx = -1;
Packit 577717
Packit 577717
	hdr = fds->buf;
Packit 577717
	width = hdr->pmc_width;
Packit 577717
	do {
Packit 577717
		seq = hdr->lock;
Packit 577717
		barrier();
Packit 577717
Packit 577717
		/* try reading directly from user mode */
Packit 577717
		if (!rdpmc(hdr, &values[0])) {
Packit 577717
			values[1] = hdr->time_enabled;
Packit 577717
			values[2] = hdr->time_running;
Packit 577717
			ret = 0;
Packit 577717
		} else {
Packit 577717
			idx = -1;
Packit 577717
			ret = read(fds->fd, values, sizeof(values));
Packit 577717
			if (ret < (ssize_t)sizeof(values))
Packit 577717
				errx(1, "cannot read values");
Packit 577717
			printf("using read\n");
Packit 577717
			break;
Packit 577717
		}
Packit 577717
		barrier();
Packit 577717
	} while (hdr->lock != seq);
Packit 577717
Packit 577717
	printf("raw=0x%"PRIx64 " width=%d ena=%"PRIu64 " run=%"PRIu64" idx=%d\n",
Packit 577717
		values[0],
Packit 577717
		width,
Packit 577717
		values[1],
Packit 577717
		values[2],
Packit 577717
		idx);
Packit 577717
Packit 577717
	count = values[0];
Packit 577717
	count <<= 64 - width;
Packit 577717
	count >>= 64 - width;
Packit 577717
	values[0] = count;
Packit 577717
	return perf_scale(values);
Packit 577717
}
Packit 577717
Packit 577717
int
Packit 577717
main(int argc, char **argv)
Packit 577717
{
Packit 577717
	perf_event_desc_t *fds = NULL;
Packit 577717
	long lret;
Packit 577717
	size_t pgsz;
Packit 577717
	uint64_t val, prev_val;
Packit 577717
	int i, ret, num_fds = 0;
Packit 577717
Packit 577717
	lret = sysconf(_SC_PAGESIZE);
Packit 577717
	if (lret < 0)
Packit 577717
		err(1, "cannot get page size");
Packit 577717
Packit 577717
	pgsz = (size_t)lret;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * Initialize pfm library (required before we can use it)
Packit 577717
	 */
Packit 577717
	ret = pfm_initialize();
Packit 577717
	if (ret != PFM_SUCCESS)
Packit 577717
		errx(1, "Cannot initialize library: %s", pfm_strerror(ret));
Packit 577717
Packit 577717
	ret = perf_setup_argv_events(argc > 1 ? (const char **)argv+1 : gen_events, &fds, &num_fds);
Packit 577717
	if (ret || !num_fds)
Packit 577717
		errx(1, "cannot setup events");
Packit 577717
Packit 577717
	fds[0].fd = -1;
Packit 577717
	for(i=0; i < num_fds; i++) {
Packit 577717
		/* request timing information necesaary for scaling */
Packit 577717
		fds[i].hw.read_format = PERF_FORMAT_SCALE;
Packit 577717
		fds[i].hw.disabled = 0;
Packit 577717
		//fds[i].fd = perf_event_open(&fds[i].hw, 0, -1, fds[0].fd, 0);
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
		fds[i].buf = mmap(NULL, pgsz, PROT_READ, MAP_SHARED, fds[i].fd, 0);
Packit 577717
		if (fds[i].buf == MAP_FAILED)
Packit 577717
			err(1, "cannot mmap page");
Packit 577717
Packit 577717
	}
Packit 577717
	signal(SIGALRM, sig_handler);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * enable all counters attached to this thread
Packit 577717
	 */
Packit 577717
	ioctl(fds[0].fd, PERF_EVENT_IOC_ENABLE, 0);
Packit 577717
Packit 577717
	alarm(10);
Packit 577717
	prev_val = 0;
Packit 577717
	for(;quit == 0;) {
Packit 577717
		for (i = 0; i < num_fds; i++) {
Packit 577717
			val = read_count(&fds[i]);
Packit 577717
			/* print evnet deltas */
Packit 577717
			printf("%20"PRIu64" %s\n", val - prev_val, fds[i].name);
Packit 577717
			prev_val = val;
Packit 577717
		}
Packit 577717
		fib(35);
Packit 577717
	}
Packit 577717
	/*
Packit 577717
	 * disable all counters attached to this thread
Packit 577717
	 */
Packit 577717
	ioctl(fds[0].fd, PERF_EVENT_IOC_DISABLE, 0);
Packit 577717
Packit 577717
	for (i=0; i < num_fds; i++) {
Packit 577717
		munmap(fds[i].buf, pgsz);
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
	return 0;
Packit 577717
}