|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* bts_smpl.c - example of Intel Branch Trace Stack sampling
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* Copyright (c) 2009 Google, Inc
|
|
Packit |
577717 |
* Contributed by Stephane Eranian <eranian@gmail.com>
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* Based on:
|
|
Packit |
577717 |
* Copyright (c) 2003-2006 Hewlett-Packard Development Company, L.P.
|
|
Packit |
577717 |
* Contributed by Stephane Eranian <eranian@hpl.hp.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 <errno.h>
|
|
Packit |
577717 |
#include <unistd.h>
|
|
Packit |
577717 |
#include <string.h>
|
|
Packit |
577717 |
#include <signal.h>
|
|
Packit |
577717 |
#include <stdarg.h>
|
|
Packit |
577717 |
#include <getopt.h>
|
|
Packit |
577717 |
#include <setjmp.h>
|
|
Packit |
577717 |
#include <limits.h>
|
|
Packit |
577717 |
#include <time.h>
|
|
Packit |
577717 |
#include <sys/time.h>
|
|
Packit |
577717 |
#include <sys/ptrace.h>
|
|
Packit |
577717 |
#include <sys/wait.h>
|
|
Packit |
577717 |
#include <sys/poll.h>
|
|
Packit |
577717 |
#include <sys/mman.h>
|
|
Packit |
577717 |
#include <sys/resource.h>
|
|
Packit |
577717 |
#include <err.h>
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#include "perf_util.h"
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#define SMPL_PERIOD 24000000ULL
|
|
Packit |
577717 |
|
|
Packit |
577717 |
typedef struct {
|
|
Packit |
577717 |
int opt_no_show;
|
|
Packit |
577717 |
int opt_inherit;
|
|
Packit |
577717 |
int mmap_pages;
|
|
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 |
|
|
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 void
|
|
Packit |
577717 |
cld_handler(int n)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
longjmp(jbuf, 1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
int
|
|
Packit |
577717 |
child(char **arg)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* force the task to stop before executing the first
|
|
Packit |
577717 |
* user level instruction
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
execvp(arg[0], arg);
|
|
Packit |
577717 |
/* not reached */
|
|
Packit |
577717 |
return -1;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
struct timeval last_read, this_read;
|
|
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 |
perf_display_sample(fds, num_fds, hw - fds, &ehdr, stdout);
|
|
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 |
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 sz=%d\n", ehdr.type, ehdr.size);
|
|
Packit |
577717 |
perf_skip_buffer(hw, ehdr.size - sizeof(ehdr));
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
int
|
|
Packit |
577717 |
mainloop(char **arg)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
static uint64_t ovfl_count; /* static to avoid setjmp issue */
|
|
Packit |
577717 |
struct pollfd pollfds[1];
|
|
Packit |
577717 |
size_t map_size = 0;
|
|
Packit |
577717 |
sigset_t bmask;
|
|
Packit |
577717 |
pid_t pid;
|
|
Packit |
577717 |
uint64_t val[2];
|
|
Packit |
577717 |
int status, ret;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (pfm_initialize() != PFM_SUCCESS)
|
|
Packit |
577717 |
errx(1, "libpfm initialization failed\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
map_size = (options.mmap_pages+1)*getpagesize();
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* does allocate fds
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ret = perf_setup_list_events("branches:u", &fds, &num_fds);
|
|
Packit |
577717 |
if (ret || !num_fds)
|
|
Packit |
577717 |
errx(1, "cannot setup event");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
memset(pollfds, 0, sizeof(pollfds));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Create the child task
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if ((pid=fork()) == -1)
|
|
Packit |
577717 |
err(1, "cannot fork process\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (pid == 0)
|
|
Packit |
577717 |
exit(child(arg));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* wait for the child to exec
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ret = waitpid(pid, &status, WUNTRACED);
|
|
Packit |
577717 |
if (ret == -1)
|
|
Packit |
577717 |
err(1, "waitpid failed");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (WIFEXITED(status))
|
|
Packit |
577717 |
errx(1, "task %s [%d] exited already status %d\n", arg[0], pid, WEXITSTATUS(status));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
fds[0].fd = -1;
|
|
Packit |
577717 |
fds[0].hw.disabled = 0; /* start immediately */
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (options.opt_inherit)
|
|
Packit |
577717 |
fds[0].hw.inherit = 1;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
fds[0].hw.sample_type = PERF_SAMPLE_IP|PERF_SAMPLE_ADDR;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* BTS only supported at user level
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (fds[0].hw.exclude_user ||fds[0].hw.exclude_kernel == 0)
|
|
Packit |
577717 |
errx(1, "BTS currently supported only at the user level\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* period MUST be one to trigger BTS: tracing not sampling anymore
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
fds[0].hw.sample_period = 1;
|
|
Packit |
577717 |
fds[0].hw.exclude_kernel = 1;
|
|
Packit |
577717 |
fds[0].hw.exclude_hv = 1;
|
|
Packit |
577717 |
fds[0].hw.read_format |= PERF_FORMAT_ID;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
fds[0].fd = perf_event_open(&fds[0].hw, pid, -1, -1, 0);
|
|
Packit |
577717 |
if (fds[0].fd == -1)
|
|
Packit |
577717 |
err(1, "cannot attach event %s", fds[0].name);
|
|
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*getpagesize())-1;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
ret = read(fds[0].fd, val, sizeof(val));
|
|
Packit |
577717 |
if (ret == -1)
|
|
Packit |
577717 |
err(1, "cannot read id %zu", sizeof(val));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
fds[0].id = val[1];
|
|
Packit |
577717 |
printf("%"PRIu64" %s\n", fds[0].id, fds[0].name);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* effectively activate monitoring
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ptrace(PTRACE_DETACH, pid, NULL, 0);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
signal(SIGCHLD, cld_handler);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
pollfds[0].fd = fds[0].fd;
|
|
Packit |
577717 |
pollfds[0].events = POLLIN;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (setjmp(jbuf) == 1)
|
|
Packit |
577717 |
goto terminate_session;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
sigemptyset(&bmask);
|
|
Packit |
577717 |
sigaddset(&bmask, SIGCHLD);
|
|
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 |
ret = sigprocmask(SIG_SETMASK, &bmask, NULL);
|
|
Packit |
577717 |
if (ret)
|
|
Packit |
577717 |
err(1, "setmask");
|
|
Packit |
577717 |
process_smpl_buf(&fds[0]);
|
|
Packit |
577717 |
ret = sigprocmask(SIG_UNBLOCK, &bmask, NULL);
|
|
Packit |
577717 |
if (ret)
|
|
Packit |
577717 |
err(1, "unblock");
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
terminate_session:
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* cleanup child
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
wait4(pid, &status, 0, NULL);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
close(fds[0].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 |
free(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: bts_smpl [-h] [--help] [-i] [-m mmap_pages] cmd\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 |
while ((c=getopt_long(argc, argv,"+hm:p:if", the_options, 0)) != -1) {
|
|
Packit |
577717 |
switch(c) {
|
|
Packit |
577717 |
case 0: continue;
|
|
Packit |
577717 |
case 'i':
|
|
Packit |
577717 |
options.opt_inherit = 1;
|
|
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 'h':
|
|
Packit |
577717 |
usage();
|
|
Packit |
577717 |
exit(0);
|
|
Packit |
577717 |
default:
|
|
Packit |
577717 |
errx(1, "unknown option");
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (argv[optind] == NULL)
|
|
Packit |
577717 |
errx(1, "you must specify a command to execute\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (!options.mmap_pages)
|
|
Packit |
577717 |
options.mmap_pages = 4;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
return mainloop(argv+optind);
|
|
Packit |
577717 |
}
|