|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* branch_smpl.c - example of a branch sampling on another task
|
|
Packit |
577717 |
*
|
|
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 <linux/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/wait.h>
|
|
Packit |
577717 |
#include <sys/poll.h>
|
|
Packit |
577717 |
#include <sys/mman.h>
|
|
Packit |
577717 |
#include <locale.h>
|
|
Packit |
577717 |
#include <sys/ioctl.h>
|
|
Packit |
577717 |
#include <err.h>
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#include "perf_util.h"
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#define DFL_BR_EVENT "branches:freq=100"
|
|
Packit |
577717 |
|
|
Packit |
577717 |
typedef struct {
|
|
Packit |
577717 |
int opt_no_show;
|
|
Packit |
577717 |
int opt_inherit;
|
|
Packit |
577717 |
uint64_t branch_filt;
|
|
Packit |
577717 |
int cpu;
|
|
Packit |
577717 |
int mmap_pages;
|
|
Packit |
577717 |
char *events;
|
|
Packit |
577717 |
FILE *output_file;
|
|
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 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 |
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 |
if (options.opt_no_show) {
|
|
Packit |
577717 |
perf_skip_buffer(hw, ehdr.size - sizeof(ehdr));
|
|
Packit |
577717 |
continue;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
switch(ehdr.type) {
|
|
Packit |
577717 |
case PERF_RECORD_SAMPLE:
|
|
Packit |
577717 |
collected_samples++;
|
|
Packit |
577717 |
ret = perf_display_sample(fds, num_fds, hw - fds, &ehdr, options.output_file);
|
|
Packit |
577717 |
if (ret)
|
|
Packit |
577717 |
errx(1, "cannot parse sample");
|
|
Packit |
577717 |
break;
|
|
Packit |
577717 |
case PERF_RECORD_EXIT:
|
|
Packit |
577717 |
display_exit(hw, options.output_file);
|
|
Packit |
577717 |
break;
|
|
Packit |
577717 |
case PERF_RECORD_LOST:
|
|
Packit |
577717 |
lost_samples += display_lost(hw, fds, num_fds, options.output_file);
|
|
Packit |
577717 |
break;
|
|
Packit |
577717 |
case PERF_RECORD_THROTTLE:
|
|
Packit |
577717 |
display_freq(1, hw, options.output_file);
|
|
Packit |
577717 |
break;
|
|
Packit |
577717 |
case PERF_RECORD_UNTHROTTLE:
|
|
Packit |
577717 |
display_freq(0, hw, options.output_file);
|
|
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 |
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 |
sigset_t bmask;
|
|
Packit |
577717 |
int go[2], ready[2];
|
|
Packit |
577717 |
size_t pgsz;
|
|
Packit |
577717 |
size_t map_size = 0;
|
|
Packit |
577717 |
pid_t pid;
|
|
Packit |
577717 |
int status, ret;
|
|
Packit |
577717 |
int i;
|
|
Packit |
577717 |
char buf;
|
|
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 |
/*
|
|
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 |
memset(pollfds, 0, sizeof(pollfds));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
ret = pipe(ready);
|
|
Packit |
577717 |
if (ret)
|
|
Packit |
577717 |
err(1, "cannot create pipe ready");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
ret = pipe(go);
|
|
Packit |
577717 |
if (ret)
|
|
Packit |
577717 |
err(1, "cannot create pipe go");
|
|
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 |
close(ready[0]);
|
|
Packit |
577717 |
close(go[1]);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* let the parent know we exist
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
close(ready[1]);
|
|
Packit |
577717 |
if (read(go[0], &buf, 1) == -1)
|
|
Packit |
577717 |
err(1, "unable to read go_pipe");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
exit(child(arg));
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
close(ready[1]);
|
|
Packit |
577717 |
close(go[0]);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (read(ready[0], &buf, 1) == -1)
|
|
Packit |
577717 |
err(1, "unable to read child_ready_pipe");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
close(ready[0]);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
fds[0].fd = -1;
|
|
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 |
for(i=0; i < num_fds; i++) {
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (i == 0) {
|
|
Packit |
577717 |
fds[i].hw.disabled = 1;
|
|
Packit |
577717 |
fds[i].hw.enable_on_exec = 1; /* start immediately */
|
|
Packit |
577717 |
} else
|
|
Packit |
577717 |
fds[i].hw.disabled = 0;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (options.opt_inherit)
|
|
Packit |
577717 |
fds[i].hw.inherit = 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 |
fds[i].hw.wakeup_watermark = (options.mmap_pages*pgsz) / 2;
|
|
Packit |
577717 |
fds[i].hw.watermark = 1;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
fds[i].hw.sample_type = PERF_SAMPLE_IP|PERF_SAMPLE_TID|PERF_SAMPLE_READ|PERF_SAMPLE_TIME|PERF_SAMPLE_PERIOD;
|
|
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 |
fprintf(options.output_file,"%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 |
fds[i].hw.sample_type = PERF_SAMPLE_BRANCH_STACK;
|
|
Packit |
577717 |
fds[i].hw.branch_sample_type = options.branch_filt;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* we are grouping the events, so there may be a limit
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
fds[i].fd = perf_event_open(&fds[i].hw, pid, options.cpu, fds[0].fd, 0);
|
|
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 |
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 |
fprintf(options.output_file,"ID %"PRIu64" %s\n", fds[i].id, fds[i].name);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
pollfds[0].fd = fds[0].fd;
|
|
Packit |
577717 |
pollfds[0].events = POLLIN;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
for(i=0; i < num_fds; i++) {
|
|
Packit |
577717 |
ret = ioctl(fds[i].fd, PERF_EVENT_IOC_ENABLE, 0);
|
|
Packit |
577717 |
if (ret)
|
|
Packit |
577717 |
err(1, "cannot enable event %s\n", fds[i].name);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
signal(SIGCHLD, cld_handler);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
close(go[1]);
|
|
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 |
/*
|
|
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 |
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 |
fprintf(options.output_file,
|
|
Packit |
577717 |
"%"PRIu64" samples collected in %"PRIu64" poll events, %"PRIu64" lost samples\n",
|
|
Packit |
577717 |
collected_samples,
|
|
Packit |
577717 |
ovfl_count, lost_samples);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/* free libpfm resources cleanly */
|
|
Packit |
577717 |
pfm_terminate();
|
|
Packit |
577717 |
|
|
Packit |
577717 |
fclose(options.output_file);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
return 0;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
typedef struct {
|
|
Packit |
577717 |
const char *filt;
|
|
Packit |
577717 |
const int flag;
|
|
Packit |
577717 |
} branch_filt_t;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#define FILT(a, b) { .filt = a, .flag = b }
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static const branch_filt_t br_filters[] = {
|
|
Packit |
577717 |
/* priv level filters */
|
|
Packit |
577717 |
FILT("u", PERF_SAMPLE_BRANCH_USER),
|
|
Packit |
577717 |
FILT("k", PERF_SAMPLE_BRANCH_KERNEL),
|
|
Packit |
577717 |
FILT("hv", PERF_SAMPLE_BRANCH_HV),
|
|
Packit |
577717 |
|
|
Packit |
577717 |
FILT("any", PERF_SAMPLE_BRANCH_ANY),
|
|
Packit |
577717 |
FILT("call", PERF_SAMPLE_BRANCH_ANY_CALL),
|
|
Packit |
577717 |
FILT("return", PERF_SAMPLE_BRANCH_ANY_RETURN),
|
|
Packit |
577717 |
FILT("indirect", PERF_SAMPLE_BRANCH_IND_CALL),
|
|
Packit |
577717 |
FILT("conditional", PERF_SAMPLE_BRANCH_COND),
|
|
Packit |
577717 |
FILT("indirect_jump", PERF_SAMPLE_BRANCH_IND_JUMP),
|
|
Packit |
577717 |
FILT(NULL, 0),
|
|
Packit |
577717 |
};
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void parse_branch_arg(const char *arg)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
const branch_filt_t *br;
|
|
Packit |
577717 |
char *q, *p, *str;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (!arg) {
|
|
Packit |
577717 |
options.branch_filt = PERF_SAMPLE_BRANCH_ANY;
|
|
Packit |
577717 |
return;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
str = q = strdup(arg);
|
|
Packit |
577717 |
if (!str)
|
|
Packit |
577717 |
err(1, "cannot allocate memory to dup string");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
while (*q) {
|
|
Packit |
577717 |
p = strchr(q, ',');
|
|
Packit |
577717 |
if (p)
|
|
Packit |
577717 |
*p = '\0';
|
|
Packit |
577717 |
|
|
Packit |
577717 |
for (br = br_filters; br->filt; br++) {
|
|
Packit |
577717 |
if (!strcasecmp(q, br->filt))
|
|
Packit |
577717 |
options.branch_filt |= br->flag;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
if (!br->filt)
|
|
Packit |
577717 |
errx(1, "unknown branch filter %s", q);
|
|
Packit |
577717 |
if (!p)
|
|
Packit |
577717 |
break;
|
|
Packit |
577717 |
str = p + 1;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
free(str);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#define BR_PLM (PERF_SAMPLE_BRANCH_USER|PERF_SAMPLE_BRANCH_KERNEL|PERF_SAMPLE_BRANCH_HV)
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (!(options.branch_filt & ~BR_PLM))
|
|
Packit |
577717 |
errx(1, "no branch mode specified, privilege level does not define a branch type, use the any filter");
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void
|
|
Packit |
577717 |
usage(void)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
printf("usage: branch_smpl [-h] [--help] [-i] [-c cpu] [-m mmap_pages] [-b] [-j br-filt] [-o output_file] [-e event1] cmd\n"
|
|
Packit |
577717 |
"\t-j br-filt\t : comma separated list of branch filters among: u, k, any, call, returrn, indirect, conditional, indirect_jmp\n"
|
|
Packit |
577717 |
"\t-b\t\t : sample any branch (equivalent to -j any), default mode\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.output_file = stdout;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
while ((c=getopt(argc, argv,"he:m:ic:o:j:b")) != -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 '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 'b':
|
|
Packit |
577717 |
if (options.branch_filt)
|
|
Packit |
577717 |
errx(1, "cannot use multiple branch filter options");
|
|
Packit |
577717 |
options.branch_filt = PERF_SAMPLE_BRANCH_ANY;
|
|
Packit |
577717 |
break;
|
|
Packit |
577717 |
case 'j':
|
|
Packit |
577717 |
if (options.branch_filt)
|
|
Packit |
577717 |
errx(1, "cannot set multiple branch options");
|
|
Packit |
577717 |
parse_branch_arg(optarg);
|
|
Packit |
577717 |
break;
|
|
Packit |
577717 |
case 'c':
|
|
Packit |
577717 |
options.cpu = atoi(optarg);
|
|
Packit |
577717 |
break;
|
|
Packit |
577717 |
case 'o':
|
|
Packit |
577717 |
options.output_file = fopen(optarg,"w");
|
|
Packit |
577717 |
if (!options.output_file)
|
|
Packit |
577717 |
err(1, "cannot create file %s\n", 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.branch_filt)
|
|
Packit |
577717 |
options.branch_filt = PERF_SAMPLE_BRANCH_ANY;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* use low frequency rate to avoid flooding output
|
|
Packit |
577717 |
* use generic branches event to make this test more portable
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (!options.events)
|
|
Packit |
577717 |
options.events = strdup(DFL_BR_EVENT);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (!options.mmap_pages)
|
|
Packit |
577717 |
options.mmap_pages = 1;
|
|
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 greater than 1\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("branch_filt=0x%"PRIx64"\n", options.branch_filt);
|
|
Packit |
577717 |
printf("event=%s\n", options.events);
|
|
Packit |
577717 |
return mainloop(argv+optind);
|
|
Packit |
577717 |
}
|