Blame src/libpfm4/perf_examples/syst_smpl.c

Packit 577717
/*
Packit 577717
 * syst_smpl.c - example of a system-wide sampling
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
#include <sys/types.h>
Packit 577717
#include <stdio.h>
Packit 577717
#include <stdlib.h>
Packit 577717
#include <stdarg.h>
Packit 577717
#include <unistd.h>
Packit 577717
#include <errno.h>
Packit 577717
#include <string.h>
Packit 577717
#include <signal.h>
Packit 577717
#include <getopt.h>
Packit 577717
#include <setjmp.h>
Packit 577717
#include <sys/ptrace.h>
Packit 577717
#include <sys/wait.h>
Packit 577717
#include <sys/poll.h>
Packit 577717
#include <sys/ioctl.h>
Packit 577717
#include <sys/mman.h>
Packit 577717
#include <fcntl.h>
Packit 577717
#include <err.h>
Packit 577717
#include <locale.h>
Packit 577717
Packit 577717
#include "perf_util.h"
Packit 577717
Packit 577717
#define SMPL_PERIOD	240000000ULL
Packit 577717
Packit 577717
#define MAX_PATH	1024
Packit 577717
#ifndef STR
Packit 577717
# define _STR(x) #x
Packit 577717
# define STR(x) _STR(x)
Packit 577717
#endif
Packit 577717
Packit 577717
typedef struct {
Packit 577717
	int opt_no_show;
Packit 577717
	int mmap_pages;
Packit 577717
	int cpu;
Packit 577717
	int pin;
Packit 577717
	int delay;
Packit 577717
	char *events;
Packit 577717
	char *cgroup;
Packit 577717
} options_t;
Packit 577717
Packit 577717
static jmp_buf jbuf;
Packit 577717
static uint64_t collected_samples, lost_samples;
Packit 577717
static perf_event_desc_t *fds;
Packit 577717
static int num_fds;
Packit 577717
static options_t options;
Packit 577717
static size_t pgsz;
Packit 577717
static size_t map_size;
Packit 577717
Packit 577717
static struct option the_options[]={
Packit 577717
	{ "help", 0, 0,  1},
Packit 577717
	{ "no-show", 0, &options.opt_no_show, 1},
Packit 577717
	{ 0, 0, 0, 0}
Packit 577717
};
Packit 577717
Packit 577717
static const char *gen_events = "cycles,instructions";
Packit 577717
Packit 577717
static void
Packit 577717
process_smpl_buf(perf_event_desc_t *hw)
Packit 577717
{
Packit 577717
	struct perf_event_header ehdr;
Packit 577717
	int ret;
Packit 577717
Packit 577717
	for(;;) {
Packit 577717
		ret = perf_read_buffer(hw, &ehdr, sizeof(ehdr));
Packit 577717
		if (ret)
Packit 577717
			return; /* nothing to read */
Packit 577717
Packit 577717
		switch(ehdr.type) {
Packit 577717
			case PERF_RECORD_SAMPLE:
Packit 577717
				ret = perf_display_sample(fds, num_fds, hw - fds, &ehdr, stdout);
Packit 577717
				if (ret)
Packit 577717
					errx(1, "cannot parse sample");
Packit 577717
				collected_samples++;
Packit 577717
				break;
Packit 577717
			case PERF_RECORD_EXIT:
Packit 577717
				display_exit(hw, stdout);
Packit 577717
				break;
Packit 577717
			case PERF_RECORD_LOST:
Packit 577717
				lost_samples += display_lost(hw, fds, num_fds, stdout);
Packit 577717
				break;
Packit 577717
			case PERF_RECORD_THROTTLE:
Packit 577717
				display_freq(1, hw, stdout);
Packit 577717
				break;
Packit 577717
			case PERF_RECORD_UNTHROTTLE:
Packit 577717
				display_freq(0, hw, stdout);
Packit 577717
				break;
Packit 577717
			default:
Packit 577717
				printf("unknown sample type %d\n", ehdr.type);
Packit 577717
				perf_skip_buffer(hw, ehdr.size - sizeof(ehdr));
Packit 577717
		}
Packit 577717
	}
Packit 577717
}
Packit 577717
Packit 577717
int
Packit 577717
setup_cpu(int cpu, int fd)
Packit 577717
{
Packit 577717
	int ret, flags;
Packit 577717
	int i, pid;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * does allocate fds
Packit 577717
	 */
Packit 577717
	ret = perf_setup_list_events(options.events, &fds, &num_fds);
Packit 577717
	if (ret || !num_fds)
Packit 577717
		errx(1, "cannot setup event list");
Packit 577717
Packit 577717
	if (!fds[0].hw.sample_period)
Packit 577717
		errx(1, "need to set sampling period or freq on first event, use :period= or :freq=");
Packit 577717
Packit 577717
	fds[0].fd = -1;
Packit 577717
	for(i=0; i < num_fds; i++) {
Packit 577717
Packit 577717
		fds[i].hw.disabled = !i; /* start immediately */
Packit 577717
Packit 577717
		if (options.cgroup) {
Packit 577717
			flags = PERF_FLAG_PID_CGROUP;
Packit 577717
			pid = fd;
Packit 577717
		} else {
Packit 577717
			flags = 0;
Packit 577717
			pid = -1;
Packit 577717
		}
Packit 577717
Packit 577717
		if (options.pin)
Packit 577717
			fds[i].hw.pinned = 1;
Packit 577717
Packit 577717
		if (fds[i].hw.sample_period) {
Packit 577717
			/*
Packit 577717
			 * set notification threshold to be halfway through the buffer
Packit 577717
			 */
Packit 577717
			if (fds[i].hw.sample_period) {
Packit 577717
				fds[i].hw.wakeup_watermark = (options.mmap_pages*pgsz) / 2;
Packit 577717
				fds[i].hw.watermark = 1;
Packit 577717
			}
Packit 577717
Packit 577717
			fds[i].hw.sample_type = PERF_SAMPLE_IP|PERF_SAMPLE_TID|PERF_SAMPLE_READ|PERF_SAMPLE_TIME|PERF_SAMPLE_PERIOD|PERF_SAMPLE_STREAM_ID|PERF_SAMPLE_CPU;
Packit 577717
			/*
Packit 577717
			 * if we have more than one event, then record event identifier to help with parsing
Packit 577717
			 */
Packit 577717
			if (num_fds > 1)
Packit 577717
				fds[i].hw.sample_type |= PERF_SAMPLE_IDENTIFIER;
Packit 577717
Packit 577717
			printf("%s period=%"PRIu64" freq=%d\n", fds[i].name, fds[i].hw.sample_period, fds[i].hw.freq);
Packit 577717
Packit 577717
			fds[i].hw.read_format = PERF_FORMAT_SCALE;
Packit 577717
Packit 577717
			if (fds[i].hw.freq)
Packit 577717
				fds[i].hw.sample_type |= PERF_SAMPLE_PERIOD;
Packit 577717
		}
Packit 577717
Packit 577717
		fds[i].fd = perf_event_open(&fds[i].hw, pid, cpu, fds[0].fd, flags);
Packit 577717
		if (fds[i].fd == -1) {
Packit 577717
			if (fds[i].hw.precise_ip)
Packit 577717
				err(1, "cannot attach event %s: precise mode may not be supported", fds[i].name);
Packit 577717
			err(1, "cannot attach event %s", fds[i].name);
Packit 577717
		}
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * kernel adds the header page to the size of the mmapped region
Packit 577717
	 */
Packit 577717
	fds[0].buf = mmap(NULL, map_size, 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
	/* does not include header page */
Packit 577717
	fds[0].pgmsk = (options.mmap_pages*pgsz)-1;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * send samples for all events to first event's buffer
Packit 577717
	 */
Packit 577717
	for (i = 1; i < num_fds; i++) {
Packit 577717
		if (!fds[i].hw.sample_period)
Packit 577717
			continue;
Packit 577717
		ret = ioctl(fds[i].fd, PERF_EVENT_IOC_SET_OUTPUT, fds[0].fd);
Packit 577717
		if (ret)
Packit 577717
			err(1, "cannot redirect sampling output");
Packit 577717
	}
Packit 577717
Packit 577717
	/*
Packit 577717
	 * collect event ids
Packit 577717
	 */
Packit 577717
	if (num_fds > 1 && fds[0].fd > -1) {
Packit 577717
		for(i = 0; i < num_fds; i++) {
Packit 577717
			/*
Packit 577717
			 * read the event identifier using ioctl
Packit 577717
			 * new method replaced the trick with PERF_FORMAT_GROUP + PERF_FORMAT_ID + read()
Packit 577717
			 */
Packit 577717
			ret = ioctl(fds[i].fd, PERF_EVENT_IOC_ID, &fds[i].id);
Packit 577717
			if (ret == -1)
Packit 577717
				err(1, "cannot read ID");
Packit 577717
			printf("ID %"PRIu64"  %s\n", fds[i].id, fds[i].name);
Packit 577717
		}
Packit 577717
	}
Packit 577717
	return 0;
Packit 577717
}
Packit 577717
Packit 577717
static void
Packit 577717
start_cpu(void)
Packit 577717
{
Packit 577717
	int ret;
Packit 577717
Packit 577717
	ret = ioctl(fds[0].fd, PERF_EVENT_IOC_ENABLE, 0);
Packit 577717
	if (ret)
Packit 577717
		err(1, "cannot start counter");
Packit 577717
}
Packit 577717
Packit 577717
static int
Packit 577717
cgroupfs_find_mountpoint(char *buf, size_t maxlen)
Packit 577717
{
Packit 577717
	FILE *fp;
Packit 577717
	char mountpoint[MAX_PATH+1], tokens[MAX_PATH+1], type[MAX_PATH+1];
Packit 577717
	char *token, *saved_ptr = NULL;
Packit 577717
	int found = 0;
Packit 577717
Packit 577717
	fp = fopen("/proc/mounts", "r");
Packit 577717
	if (!fp)
Packit 577717
		return -1;
Packit 577717
Packit 577717
	/*
Packit 577717
	 * in order to handle split hierarchy, we need to scan /proc/mounts
Packit 577717
	 * and inspect every cgroupfs mount point to find one that has
Packit 577717
	 * perf_event subsystem
Packit 577717
	 */
Packit 577717
	while (fscanf(fp, "%*s %"STR(MAX_PATH)"s %"STR(MAX_PATH)"s %"
Packit 577717
				STR(MAX_PATH)"s %*d %*d\n",
Packit 577717
				mountpoint, type, tokens) == 3) {
Packit 577717
Packit 577717
		if (!strcmp(type, "cgroup")) {
Packit 577717
Packit 577717
			token = strtok_r(tokens, ",", &saved_ptr);
Packit 577717
Packit 577717
			while (token != NULL) {
Packit 577717
				if (!strcmp(token, "perf_event")) {
Packit 577717
					found = 1;
Packit 577717
					break;
Packit 577717
				}
Packit 577717
				token = strtok_r(NULL, ",", &saved_ptr);
Packit 577717
			}
Packit 577717
		}
Packit 577717
		if (found)
Packit 577717
			break;
Packit 577717
	}
Packit 577717
	fclose(fp);
Packit 577717
	if (!found)
Packit 577717
		return -1;
Packit 577717
Packit 577717
	if (strlen(mountpoint) < maxlen) {
Packit 577717
		strcpy(buf, mountpoint);
Packit 577717
		return 0;
Packit 577717
	}
Packit 577717
	return -1;
Packit 577717
}
Packit 577717
Packit 577717
int
Packit 577717
open_cgroup(char *name)
Packit 577717
{
Packit 577717
        char path[MAX_PATH+1];
Packit 577717
        char mnt[MAX_PATH+1];
Packit 577717
        int cfd;
Packit 577717
Packit 577717
        if (cgroupfs_find_mountpoint(mnt, MAX_PATH+1))
Packit 577717
                errx(1, "cannot find cgroup fs mount point");
Packit 577717
Packit 577717
        snprintf(path, MAX_PATH, "%s/%s", mnt, name);
Packit 577717
Packit 577717
        cfd = open(path, O_RDONLY);
Packit 577717
        if (cfd == -1)
Packit 577717
                warn("no access to cgroup %s\n", name);
Packit 577717
Packit 577717
        return cfd;
Packit 577717
}
Packit 577717
Packit 577717
static void handler(int n)
Packit 577717
{
Packit 577717
	longjmp(jbuf, 1);
Packit 577717
}
Packit 577717
Packit 577717
int
Packit 577717
mainloop(char **arg)
Packit 577717
{
Packit 577717
	static uint64_t ovfl_count = 0; /* static to avoid setjmp issue */
Packit 577717
	struct pollfd pollfds[1];
Packit 577717
	int ret;
Packit 577717
	int fd = -1;
Packit 577717
	int i;
Packit 577717
Packit 577717
	if (pfm_initialize() != PFM_SUCCESS)
Packit 577717
		errx(1, "libpfm initialization failed\n");
Packit 577717
Packit 577717
	pgsz = sysconf(_SC_PAGESIZE);
Packit 577717
	map_size = (options.mmap_pages+1)*pgsz;
Packit 577717
Packit 577717
	if (options.cgroup) {
Packit 577717
		fd = open_cgroup(options.cgroup);
Packit 577717
		if (fd == -1)
Packit 577717
			err(1, "cannot open cgroup file %s\n", options.cgroup);
Packit 577717
	}
Packit 577717
Packit 577717
	setup_cpu(options.cpu, fd);
Packit 577717
Packit 577717
	/* done with cgroup */
Packit 577717
	if (fd != -1)
Packit 577717
		close(fd);
Packit 577717
Packit 577717
	signal(SIGALRM, handler);
Packit 577717
	signal(SIGINT, handler);
Packit 577717
Packit 577717
	pollfds[0].fd = fds[0].fd;
Packit 577717
	pollfds[0].events = POLLIN;
Packit 577717
Packit 577717
	printf("monitoring on CPU%d, session ending in %ds\n", options.cpu, options.delay);
Packit 577717
Packit 577717
	if (setjmp(jbuf) == 1)
Packit 577717
		goto terminate_session;
Packit 577717
Packit 577717
	start_cpu();
Packit 577717
Packit 577717
	alarm(options.delay);
Packit 577717
	/*
Packit 577717
	 * core loop
Packit 577717
	 */
Packit 577717
	for(;;) {
Packit 577717
		ret = poll(pollfds, 1, -1);
Packit 577717
		if (ret < 0 && errno == EINTR)
Packit 577717
			break;
Packit 577717
		ovfl_count++;
Packit 577717
		process_smpl_buf(&fds[0]);
Packit 577717
	}
Packit 577717
terminate_session:
Packit 577717
	for(i=0; i < num_fds; i++)
Packit 577717
		close(fds[i].fd);
Packit 577717
Packit 577717
	/* check for partial event buffer */
Packit 577717
	process_smpl_buf(&fds[0]);
Packit 577717
	munmap(fds[0].buf, map_size);
Packit 577717
Packit 577717
	perf_free_fds(fds, num_fds);
Packit 577717
Packit 577717
	printf("%"PRIu64" samples collected in %"PRIu64" poll events, %"PRIu64" lost samples\n",
Packit 577717
		collected_samples,
Packit 577717
		ovfl_count, lost_samples);
Packit 577717
	return 0;
Packit 577717
}
Packit 577717
Packit 577717
static void
Packit 577717
usage(void)
Packit 577717
{
Packit 577717
	printf("usage: syst_smpl [-h] [-P] [--help] [-m mmap_pages] [-f] [-e event1,...,eventn] [-c cpu] [-d seconds]\n");
Packit 577717
}
Packit 577717
Packit 577717
int
Packit 577717
main(int argc, char **argv)
Packit 577717
{
Packit 577717
	int c;
Packit 577717
Packit 577717
	setlocale(LC_ALL, "");
Packit 577717
Packit 577717
	options.cpu = -1;
Packit 577717
	options.delay = -1;
Packit 577717
Packit 577717
	while ((c=getopt_long(argc, argv,"hPe:m:c:d:G:", the_options, 0)) != -1) {
Packit 577717
		switch(c) {
Packit 577717
			case 0: continue;
Packit 577717
			case 'e':
Packit 577717
				if (options.events)
Packit 577717
					errx(1, "events specified twice\n");
Packit 577717
				options.events = optarg;
Packit 577717
				break;
Packit 577717
			case 'm':
Packit 577717
				if (options.mmap_pages)
Packit 577717
					errx(1, "mmap pages already set\n");
Packit 577717
				options.mmap_pages = atoi(optarg);
Packit 577717
				break;
Packit 577717
			case 'P':
Packit 577717
				options.pin = 1;
Packit 577717
				break;
Packit 577717
			case 'd':
Packit 577717
				options.delay = atoi(optarg);
Packit 577717
				break;
Packit 577717
			case 'G':
Packit 577717
				options.cgroup = optarg;
Packit 577717
				break;
Packit 577717
			case 'c':
Packit 577717
				options.cpu = atoi(optarg);
Packit 577717
				break;
Packit 577717
			case 'h':
Packit 577717
				usage();
Packit 577717
				exit(0);
Packit 577717
			default:
Packit 577717
				errx(1, "unknown option");
Packit 577717
		}
Packit 577717
	}
Packit 577717
	if (!options.events)
Packit 577717
		options.events = strdup(gen_events);
Packit 577717
Packit 577717
	if (!options.mmap_pages)
Packit 577717
		options.mmap_pages = 1;
Packit 577717
	
Packit 577717
	if (options.cpu == -1)
Packit 577717
		options.cpu = random() % sysconf(_SC_NPROCESSORS_ONLN);
Packit 577717
Packit 577717
	if (options.delay == -1)
Packit 577717
		options.delay = 10;
Packit 577717
Packit 577717
	if (options.mmap_pages > 1 && ((options.mmap_pages) & 0x1))
Packit 577717
		errx(1, "number of pages must be power of 2\n");
Packit 577717
Packit 577717
	return mainloop(argv+optind);
Packit 577717
}