|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* notify_group.c - self-sampling multuiple events in one group
|
|
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 <sys/prctl.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 |
typedef struct {
|
|
Packit |
577717 |
uint64_t ip;
|
|
Packit |
577717 |
} sample_t;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static volatile unsigned long notification_received;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static perf_event_desc_t *fds;
|
|
Packit |
577717 |
static int num_fds;
|
|
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, struct sigcontext *sc)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
struct perf_event_header ehdr;
|
|
Packit |
577717 |
uint64_t ip;
|
|
Packit |
577717 |
int id, ret;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
id = perf_fd2event(fds, num_fds, info->si_fd);
|
|
Packit |
577717 |
if (id == -1)
|
|
Packit |
577717 |
errx(1, "cannot find event for descriptor %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("unknown event type %d, skipping", ehdr.type);
|
|
Packit |
577717 |
perf_skip_buffer(fds+id, ehdr.size - sizeof(ehdr));
|
|
Packit |
577717 |
goto skip;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
ret = perf_read_buffer(fds+id, &ip, sizeof(ip));
|
|
Packit |
577717 |
if (ret)
|
|
Packit |
577717 |
errx(1, "cannot read IP");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
notification_received++;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("Notification %lu: 0x%"PRIx64" fd=%d %s\n",
|
|
Packit |
577717 |
notification_received,
|
|
Packit |
577717 |
ip,
|
|
Packit |
577717 |
info->si_fd, fds[id].name);
|
|
Packit |
577717 |
|
|
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 < 1024;) ;
|
|
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 |
size_t pgsz;
|
|
Packit |
577717 |
int ret, i;
|
|
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 |
*/
|
|
Packit |
577717 |
memset(&act, 0, sizeof(act));
|
|
Packit |
577717 |
act.sa_sigaction = (void *)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 |
"cycles",
|
|
Packit |
577717 |
&fds, &num_fds);
|
|
Packit |
577717 |
if (ret || !num_fds)
|
|
Packit |
577717 |
exit(1);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
fds[0].fd = -1;
|
|
Packit |
577717 |
for(i=0; i < num_fds; i++) {
|
|
Packit |
577717 |
/* want a notification for each sample added to the buffer */
|
|
Packit |
577717 |
fds[i].hw.disabled = !!i;
|
|
Packit |
577717 |
printf("i=%d disabled=%d\n", i, fds[i].hw.disabled);
|
|
Packit |
577717 |
fds[i].hw.wakeup_events = 1;
|
|
Packit |
577717 |
fds[i].hw.sample_type = PERF_SAMPLE_IP;
|
|
Packit |
577717 |
fds[i].hw.sample_period = SMPL_PERIOD;
|
|
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 |
warn("cannot attach event %s", fds[i].name);
|
|
Packit |
577717 |
goto error;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
fds[i].buf = mmap(NULL, (buffer_pages + 1)*pgsz, PROT_READ|PROT_WRITE, MAP_SHARED, fds[i].fd, 0);
|
|
Packit |
577717 |
if (fds[i].buf == MAP_FAILED)
|
|
Packit |
577717 |
err(1, "cannot mmap buffer");
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* setup asynchronous notification on the file descriptor
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ret = fcntl(fds[i].fd, F_SETFL, fcntl(fds[i].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 for in siginfo->si_fd.
|
|
Packit |
577717 |
* SA_SIGINFO in itself is not enough
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ret = fcntl(fds[i].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[i].fd, F_SETOWN, getpid());
|
|
Packit |
577717 |
if (ret == -1)
|
|
Packit |
577717 |
err(1, "cannot setown");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
fds[i].pgmsk = (buffer_pages * pgsz) - 1;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
for(i=0; i < num_fds; i++) {
|
|
Packit |
577717 |
ret = ioctl(fds[i].fd, PERF_EVENT_IOC_REFRESH , 1);
|
|
Packit |
577717 |
if (ret == -1)
|
|
Packit |
577717 |
err(1, "cannot refresh");
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
busyloop();
|
|
Packit |
577717 |
|
|
Packit |
577717 |
prctl(PR_TASK_PERF_EVENTS_DISABLE);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
error:
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* destroy our session
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
for(i=0; i < num_fds; i++)
|
|
Packit |
577717 |
if (fds[i].fd > -1)
|
|
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 |
}
|