Blame src/libpfm4/perf_examples/notify_self.c

Packit 577717
/*
Packit 577717
 * notify_self.c - example of how you can use overflow notifications
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
#include <sys/types.h>
Packit 577717
#include <inttypes.h>
Packit 577717
#include <stdio.h>
Packit 577717
#include <stdlib.h>
Packit 577717
#include <errno.h>
Packit 577717
#include <unistd.h>
Packit 577717
#include <string.h>
Packit 577717
#include <signal.h>
Packit 577717
#include <fcntl.h>
Packit 577717
#include <err.h>
Packit 577717
#include <locale.h>
Packit 577717
#include <sys/ioctl.h>
Packit 577717
#include <sys/mman.h>
Packit 577717
Packit 577717
#include "perf_util.h"
Packit 577717
Packit 577717
#define SMPL_PERIOD	2400000000ULL
Packit 577717
Packit 577717
static volatile unsigned long notification_received;
Packit 577717
Packit 577717
static perf_event_desc_t *fds = NULL;
Packit 577717
static int num_fds = 0;
Packit 577717
Packit 577717
static int buffer_pages = 1; /* size of buffer payload (must be power of 2)*/
Packit 577717
Packit 577717
static void
Packit 577717
sigio_handler(int n, siginfo_t *info, void *uc)
Packit 577717
{
Packit 577717
	struct perf_event_header ehdr;
Packit 577717
	int ret, id;
Packit 577717
	
Packit 577717
	/*
Packit 577717
	 * positive si_code indicate kernel generated signal
Packit 577717
	 * which is normal for SIGIO
Packit 577717
	 */
Packit 577717
	if (info->si_code < 0)
Packit 577717
		errx(1, "signal not generated by kernel");
Packit 577717
Packit 577717
	/*
Packit 577717
	 * SIGPOLL = SIGIO
Packit 577717
	 * expect POLL_HUP instead of POLL_IN because we are
Packit 577717
	 * in one-shot mode (IOC_REFRESH)
Packit 577717
	 */
Packit 577717
	if (info->si_code != POLL_HUP)
Packit 577717
		errx(1, "signal not generated by SIGIO");
Packit 577717
Packit 577717
	id = perf_fd2event(fds, num_fds, info->si_fd);
Packit 577717
	if (id == -1)
Packit 577717
		errx(1, "no event associated with fd=%d", info->si_fd);
Packit 577717
Packit 577717
	ret = perf_read_buffer(fds+id, &ehdr, sizeof(ehdr));
Packit 577717
	if (ret)
Packit 577717
		errx(1, "cannot read event header");
Packit 577717
Packit 577717
	if (ehdr.type != PERF_RECORD_SAMPLE) {
Packit 577717
		warnx("unexpected sample type=%d, skipping\n", ehdr.type);
Packit 577717
		perf_skip_buffer(fds+id, ehdr.size);
Packit 577717
		goto skip;
Packit 577717
	}
Packit 577717
	printf("Notification:%lu ", notification_received);
Packit 577717
	ret = perf_display_sample(fds, num_fds, 0, &ehdr, stdout);
Packit 577717
	/*
Packit 577717
	 * increment our notification counter
Packit 577717
	 */
Packit 577717
	notification_received++;
Packit 577717
skip:
Packit 577717
	/*
Packit 577717
	 * rearm the counter for one more shot
Packit 577717
	 */
Packit 577717
	ret = ioctl(info->si_fd, PERF_EVENT_IOC_REFRESH, 1);
Packit 577717
	if (ret == -1)
Packit 577717
		err(1, "cannot refresh");
Packit 577717
Packit 577717
}
Packit 577717
Packit 577717
/*
Packit 577717
 * infinite loop waiting for notification to get out
Packit 577717
 */
Packit 577717
void
Packit 577717
busyloop(void)
Packit 577717
{
Packit 577717
	/*
Packit 577717
	 * busy loop to burn CPU cycles
Packit 577717
	 */
Packit 577717
	for(;notification_received < 20;) ;
Packit 577717
}
Packit 577717
Packit 577717
int
Packit 577717
main(int argc, char **argv)
Packit 577717
{
Packit 577717
	struct sigaction act;
Packit 577717
	sigset_t new, old;
Packit 577717
	uint64_t *val;
Packit 577717
	size_t sz, pgsz;
Packit 577717
	int ret, i;
Packit 577717
Packit 577717
	setlocale(LC_ALL, "");
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
	pgsz = sysconf(_SC_PAGESIZE);
Packit 577717
Packit 577717
	/*
Packit 577717
	 * Install the signal handler (SIGIO)
Packit 577717
	 * need SA_SIGINFO because we need the fd
Packit 577717
	 * in the signal handler
Packit 577717
	 */
Packit 577717
	memset(&act, 0, sizeof(act));
Packit 577717
	act.sa_sigaction = sigio_handler;
Packit 577717
	act.sa_flags = SA_SIGINFO;
Packit 577717
	sigaction (SIGIO, &act, 0);
Packit 577717
Packit 577717
	sigemptyset(&old;;
Packit 577717
	sigemptyset(&new;;
Packit 577717
	sigaddset(&new, SIGIO);
Packit 577717
Packit 577717
	ret = sigprocmask(SIG_SETMASK, NULL, &old;;
Packit 577717
	if (ret)
Packit 577717
		err(1, "sigprocmask failed");
Packit 577717
Packit 577717
	if (sigismember(&old, SIGIO)) {
Packit 577717
		warnx("program started with SIGIO masked, unmasking it now\n");
Packit 577717
		ret = sigprocmask(SIG_UNBLOCK, &new, NULL);
Packit 577717
		if (ret)
Packit 577717
			err(1, "sigprocmask failed");
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
 	 * allocates fd for us
Packit 577717
 	 */
Packit 577717
	ret = perf_setup_list_events("cycles,"
Packit 577717
				       "instructions",
Packit 577717
					&fds, &num_fds);
Packit 577717
	if (ret || (num_fds == 0))
Packit 577717
		exit(1);
Packit 577717
Packit 577717
	fds[0].fd = -1;
Packit 577717
	for(i=0; i < num_fds; i++) {
Packit 577717
Packit 577717
		/* want a notification for every each added to the buffer */
Packit 577717
		fds[i].hw.disabled = !i;
Packit 577717
		if (!i) {
Packit 577717
			fds[i].hw.wakeup_events = 1;
Packit 577717
			fds[i].hw.sample_type = PERF_SAMPLE_IP|PERF_SAMPLE_READ|PERF_SAMPLE_PERIOD;
Packit 577717
			fds[i].hw.sample_period = SMPL_PERIOD;
Packit 577717
Packit 577717
			/* read() returns event identification for signal handler */
Packit 577717
			fds[i].hw.read_format = PERF_FORMAT_GROUP|PERF_FORMAT_ID|PERF_FORMAT_SCALE;
Packit 577717
		}
Packit 577717
Packit 577717
		fds[i].fd = perf_event_open(&fds[i].hw, 0, -1, fds[0].fd, 0);
Packit 577717
		if (fds[i].fd == -1)
Packit 577717
			err(1, "cannot attach event %s", fds[i].name);
Packit 577717
	}
Packit 577717
	
Packit 577717
	sz = (3+2*num_fds)*sizeof(uint64_t);
Packit 577717
	val = malloc(sz);
Packit 577717
	if (!val)
Packit 577717
		err(1, "cannot allocated memory");
Packit 577717
	/*
Packit 577717
	 * On overflow, the non lead events are stored in the sample.
Packit 577717
	 * However we need some key to figure the order in which they
Packit 577717
	 * were laid out in the buffer. The file descriptor does not
Packit 577717
	 * work for this. Instead, we extract a unique ID for each event.
Packit 577717
	 * That id will be part of the sample for each event value.
Packit 577717
	 * Therefore we will be able to match value to events
Packit 577717
	 *
Packit 577717
	 * PERF_FORMAT_ID: returns unique 64-bit identifier in addition
Packit 577717
	 * to event value.
Packit 577717
	 */
Packit 577717
	if (fds[0].fd == -1)
Packit 577717
		errx(1, "cannot create event 0");
Packit 577717
Packit 577717
	ret = read(fds[0].fd, val, sz);
Packit 577717
	if (ret == -1)
Packit 577717
		err(1, "cannot read id %zu", sizeof(val));
Packit 577717
Packit 577717
	/*
Packit 577717
	 * we are using PERF_FORMAT_GROUP, therefore the structure
Packit 577717
	 * of val is as follows:
Packit 577717
	 *
Packit 577717
	 *      { u64           nr;
Packit 577717
	 *        { u64         time_enabled; } && PERF_FORMAT_ENABLED
Packit 577717
	 *        { u64         time_running; } && PERF_FORMAT_RUNNING
Packit 577717
	 *        { u64         value;                  
Packit 577717
	 *          { u64       id;           } && PERF_FORMAT_ID
Packit 577717
	 *        }             cntr[nr];               
Packit 577717
	 * We are skipping the first 3 values (nr, time_enabled, time_running)
Packit 577717
	 * and then for each event we get a pair of values.
Packit 577717
	 */ 
Packit 577717
	for(i=0; i < num_fds; i++) {
Packit 577717
		fds[i].id = val[2*i+1+3];
Packit 577717
		printf("%"PRIu64"  %s\n", fds[i].id, fds[i].name);
Packit 577717
	}
Packit 577717
	 
Packit 577717
	fds[0].buf = mmap(NULL, (buffer_pages+1)*pgsz, PROT_READ|PROT_WRITE, MAP_SHARED, fds[0].fd, 0);
Packit 577717
	if (fds[0].buf == MAP_FAILED)
Packit 577717
		err(1, "cannot mmap buffer");
Packit 577717
	
Packit 577717
	fds[0].pgmsk = (buffer_pages * pgsz) - 1;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * setup asynchronous notification on the file descriptor
Packit 577717
	 */
Packit 577717
	ret = fcntl(fds[0].fd, F_SETFL, fcntl(fds[0].fd, F_GETFL, 0) | O_ASYNC);
Packit 577717
	if (ret == -1)
Packit 577717
		err(1, "cannot set ASYNC");
Packit 577717
Packit 577717
	/*
Packit 577717
 	 * necessary if we want to get the file descriptor for
Packit 577717
 	 * which the SIGIO is sent in siginfo->si_fd.
Packit 577717
 	 * SA_SIGINFO in itself is not enough
Packit 577717
 	 */
Packit 577717
	ret = fcntl(fds[0].fd, F_SETSIG, SIGIO);
Packit 577717
	if (ret == -1)
Packit 577717
		err(1, "cannot setsig");
Packit 577717
Packit 577717
	/*
Packit 577717
	 * get ownership of the descriptor
Packit 577717
	 */
Packit 577717
	ret = fcntl(fds[0].fd, F_SETOWN, getpid());
Packit 577717
	if (ret == -1)
Packit 577717
		err(1, "cannot setown");
Packit 577717
Packit 577717
	/*
Packit 577717
	 * enable the group for one period
Packit 577717
	 */
Packit 577717
	ret = ioctl(fds[0].fd, PERF_EVENT_IOC_REFRESH , 1);
Packit 577717
	if (ret == -1)
Packit 577717
		err(1, "cannot refresh");
Packit 577717
Packit 577717
	busyloop();
Packit 577717
Packit 577717
	ret = ioctl(fds[0].fd, PERF_EVENT_IOC_DISABLE, 1);
Packit 577717
	if (ret == -1)
Packit 577717
		err(1, "cannot disable");
Packit 577717
Packit 577717
	/*
Packit 577717
	 * 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
	free(val);
Packit 577717
Packit 577717
	/* free libpfm resources cleanly */
Packit 577717
	pfm_terminate();
Packit 577717
Packit 577717
	return 0;
Packit 577717
}