Blame src/libpfm4/perf_examples/self_basic.c

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