Blame src/libpfm4/perf_examples/self_basic.c

Packit Service a1973e
/*
Packit Service a1973e
 * self-basic.c - example of a simple self monitoring task no-helper
Packit Service a1973e
 *
Packit Service a1973e
 * Copyright (c) 2010 Google, Inc
Packit Service a1973e
 * Contributed by Stephane Eranian <eranian@gmail.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 <locale.h>
Packit Service a1973e
#include <sys/ioctl.h>
Packit Service a1973e
#include <err.h>
Packit Service a1973e
Packit Service a1973e
#include <perfmon/pfmlib_perf_event.h>
Packit Service a1973e
Packit Service a1973e
#define N 30
Packit Service a1973e
Packit Service a1973e
static unsigned long 
Packit Service a1973e
fib(unsigned long n)
Packit Service a1973e
{
Packit Service a1973e
	if (n == 0)
Packit Service a1973e
		return 0;
Packit Service a1973e
	if (n == 1)
Packit Service a1973e
		return 2;
Packit Service a1973e
	return fib(n-1)+fib(n-2);
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
int
Packit Service a1973e
main(int argc, char **argv)
Packit Service a1973e
{
Packit Service a1973e
	struct perf_event_attr attr;
Packit Service a1973e
	int fd, ret;
Packit Service a1973e
	uint64_t count = 0, values[3];
Packit Service a1973e
Packit Service a1973e
	setlocale(LC_ALL, "");
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * Initialize libpfm 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
	memset(&attr, 0, sizeof(attr));
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
 	 * 1st argument: event string
Packit Service a1973e
 	 * 2nd argument: default privilege level (used if not specified in the event string)
Packit Service a1973e
 	 * 3rd argument: the perf_event_attr to initialize
Packit Service a1973e
 	 */
Packit Service a1973e
	ret = pfm_get_perf_event_encoding("cycles", PFM_PLM0|PFM_PLM3, &attr, NULL, NULL);
Packit Service a1973e
	if (ret != PFM_SUCCESS)
Packit Service a1973e
		errx(1, "cannot find encoding: %s", pfm_strerror(ret));
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
	 * request timing information because event may be multiplexed
Packit Service a1973e
	 * and thus it may not count all the time. The scaling information
Packit Service a1973e
	 * will be used to scale the raw count as if the event had run all
Packit Service a1973e
	 * along
Packit Service a1973e
	 */
Packit Service a1973e
	attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED|PERF_FORMAT_TOTAL_TIME_RUNNING;
Packit Service a1973e
Packit Service a1973e
	/* do not start immediately after perf_event_open() */
Packit Service a1973e
	attr.disabled = 1;
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
 	 * create the event and attach to self
Packit Service a1973e
 	 * Note that it attaches only to the main thread, there is no inheritance
Packit Service a1973e
 	 * to threads that may be created subsequently.
Packit Service a1973e
 	 *
Packit Service a1973e
 	 * if mulithreaded, then getpid() must be replaced by gettid()
Packit Service a1973e
 	 */
Packit Service a1973e
	fd = perf_event_open(&attr, getpid(), -1, -1, 0);
Packit Service a1973e
	if (fd < 0) 
Packit Service a1973e
		err(1, "cannot create event");
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
 	 * start counting now
Packit Service a1973e
 	 */
Packit Service a1973e
	ret = ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
Packit Service a1973e
	if (ret)
Packit Service a1973e
		err(1, "ioctl(enable) failed");
Packit Service a1973e
Packit Service a1973e
	printf("Fibonacci(%d)=%lu\n", N, fib(N));
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
 	 * stop counting
Packit Service a1973e
 	 */
Packit Service a1973e
	ret = ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
Packit Service a1973e
	if (ret)
Packit Service a1973e
		err(1, "ioctl(disable) failed");
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
 	 * read the count + scaling values
Packit Service a1973e
 	 *
Packit Service a1973e
 	 * It is not necessary to stop an event to read its value
Packit Service a1973e
 	 */
Packit Service a1973e
	ret = read(fd, values, sizeof(values));
Packit Service a1973e
	if (ret != sizeof(values))
Packit Service a1973e
		err(1, "cannot read results: %s", strerror(errno));
Packit Service a1973e
Packit Service a1973e
	/*
Packit Service a1973e
 	 * scale count
Packit Service a1973e
	 *
Packit Service a1973e
	 * values[0] = raw count
Packit Service a1973e
	 * values[1] = TIME_ENABLED
Packit Service a1973e
	 * values[2] = TIME_RUNNING
Packit Service a1973e
 	 */
Packit Service a1973e
	if (values[2])
Packit Service a1973e
		count = (uint64_t)((double)values[0] * values[1]/values[2]);
Packit Service a1973e
Packit Service a1973e
	printf("count=%'"PRIu64"\n", count);
Packit Service a1973e
Packit Service a1973e
	close(fd);
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
}