Newer
Older
switch (origin) {
case PERF_RECORD_MISC_USER:
++us_samples;
if (hide_user_symbols)
return;
machine = perf_session__find_host_machine(session);
break;
case PERF_RECORD_MISC_KERNEL:
++kernel_samples;
if (hide_kernel_symbols)
return;
machine = perf_session__find_host_machine(session);
break;
case PERF_RECORD_MISC_GUEST_KERNEL:
++guest_kernel_samples;
machine = perf_session__find_machine(session, self->ip.pid);
case PERF_RECORD_MISC_GUEST_USER:
++guest_us_samples;
/*
* TODO: we don't process guest user from host side
* except simple counting.
*/
return;
default:
return;
}
if (!machine && perf_guest) {
pr_err("Can't find guest [%d]'s kernel information\n",
self->ip.pid);
return;
}
if (self->header.misc & PERF_RECORD_MISC_EXACT_IP)
exact_samples++;

Arnaldo Carvalho de Melo
committed
if (event__preprocess_sample(self, session, &al, sample,
symbol_filter) < 0 ||
al.filtered)
return;
if (al.sym == NULL) {
/*
* As we do lazy loading of symtabs we only will know if the
* specified vmlinux file is invalid when we actually have a
* hit in kernel space and then try to load it. So if we get
* here and there are _no_ symbols in the DSO backing the
* kernel map, bail out.
*
* We may never get here, for instance, if we use -K/
* --hide-kernel-symbols, even if the user specifies an
* invalid --vmlinux ;-)
*/
if (al.map == machine->vmlinux_maps[MAP__FUNCTION] &&
RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
pr_err("The %s file can't be used\n",
symbol_conf.vmlinux_name);
exit(1);
}
return;
}
/* let's see, whether we need to install initial sym_filter_entry */
if (sym_filter_entry_sched) {
sym_filter_entry = sym_filter_entry_sched;
sym_filter_entry_sched = NULL;
if (parse_source(sym_filter_entry) < 0) {
struct symbol *sym = sym_entry__symbol(sym_filter_entry);
pr_err("Can't annotate %s", sym->name);
if (sym_filter_entry->map->dso->origin == DSO__ORIG_KERNEL) {
pr_err(": No vmlinux file was found in the path:\n");
machine__fprintf_vmlinux_path(machine, stderr);
} else
pr_err(".\n");
exit(1);
}
syme = symbol__priv(al.sym);
syme->count[evsel->idx]++;
syme->origin = origin;
record_precise_ip(syme, evsel->idx, ip);
pthread_mutex_lock(&active_symbols_lock);
if (list_empty(&syme->node) || !syme->node.next)
__list_insert_active_sym(syme);
pthread_mutex_unlock(&active_symbols_lock);
}
}
struct mmap_data {
unsigned int prev;
static int perf_evsel__alloc_mmap_per_thread(struct perf_evsel *evsel,
int ncpus, int nthreads)
{
evsel->priv = xyarray__new(ncpus, nthreads, sizeof(struct mmap_data));
return evsel->priv != NULL ? 0 : -ENOMEM;
}
static void perf_evsel__free_mmap(struct perf_evsel *evsel)
{
xyarray__delete(evsel->priv);
evsel->priv = NULL;
}
static unsigned int mmap_read_head(struct mmap_data *md)
{
struct perf_event_mmap_page *pc = md->base;
int head;
head = pc->data_head;
rmb();
return head;
}

Arnaldo Carvalho de Melo
committed
static void perf_session__mmap_read_counter(struct perf_session *self,
struct perf_evsel *evsel,
int cpu, int thread_idx)
struct xyarray *mmap_array = evsel->priv;
struct mmap_data *md = xyarray__entry(mmap_array, cpu, thread_idx);
unsigned int head = mmap_read_head(md);
unsigned int old = md->prev;
unsigned char *data = md->base + page_size;

Arnaldo Carvalho de Melo
committed
struct sample_data sample;
int diff;
/*
* If we're further behind than half the buffer, there's a chance
* the writer will bite our tail and mess up the samples under us.
*
* If we somehow ended up ahead of the head, we got messed up.
*
* In either case, truncate and restart at head.
*/
diff = head - old;
if (diff > md->mask / 2 || diff < 0) {
fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
/*
* head points to a known good entry, start there.
*/
old = head;
}
for (; old != head;) {
event_t *event = (event_t *)&data[old & md->mask];
event_t event_copy;
size_t size = event->header.size;
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
/*
* Event straddles the mmap boundary -- header should always
* be inside due to u64 alignment of output.
*/
if ((old & md->mask) + size != ((old + size) & md->mask)) {
unsigned int offset = old;
unsigned int len = min(sizeof(*event), size), cpy;
void *dst = &event_copy;
do {
cpy = min(md->mask + 1 - (offset & md->mask), len);
memcpy(dst, &data[offset & md->mask], cpy);
offset += cpy;
dst += cpy;
len -= cpy;
} while (len);
event = &event_copy;
}

Arnaldo Carvalho de Melo
committed
event__parse_sample(event, self, &sample);
if (event->header.type == PERF_RECORD_SAMPLE)
event__process_sample(event, &sample, self, evsel);

Arnaldo Carvalho de Melo
committed
event__process(event, &sample, self);
old += size;
}
md->prev = old;
}

Zhang, Yanmin
committed
static struct pollfd *event_array;

Arnaldo Carvalho de Melo
committed
static void perf_session__mmap_read(struct perf_session *self)

Frederic Weisbecker
committed
{
struct perf_evsel *counter;
int i, thread_index;

Frederic Weisbecker
committed
for (i = 0; i < cpus->nr; i++) {
list_for_each_entry(counter, &evsel_list, node) {

Zhang, Yanmin
committed
for (thread_index = 0;
thread_index < threads->nr;

Zhang, Yanmin
committed
thread_index++) {
perf_session__mmap_read_counter(self,
counter, i, thread_index);

Zhang, Yanmin
committed
}

Frederic Weisbecker
committed
}
}

Ingo Molnar
committed
int nr_poll;
int group_fd;
static void start_counter(int i, struct perf_evsel *evsel)
struct xyarray *mmap_array = evsel->priv;
struct mmap_data *mm;
struct perf_event_attr *attr;
int cpu = -1;

Zhang, Yanmin
committed
int thread_index;

Ingo Molnar
committed
if (target_tid == -1)
cpu = cpus->map[i];

Ingo Molnar
committed

Ingo Molnar
committed
attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
if (freq) {
attr->sample_type |= PERF_SAMPLE_PERIOD;
attr->freq = 1;
attr->sample_freq = freq;
}
attr->inherit = (cpu < 0) && inherit;

Ingo Molnar
committed
for (thread_index = 0; thread_index < threads->nr; thread_index++) {

Ingo Molnar
committed
try_again:
FD(evsel, i, thread_index) = sys_perf_event_open(attr,
threads->map[thread_index], cpu, group_fd, 0);

Zhang, Yanmin
committed
if (FD(evsel, i, thread_index) < 0) {

Zhang, Yanmin
committed
int err = errno;
if (err == EPERM || err == EACCES)
die("Permission error - are you root?\n"
"\t Consider tweaking"
" /proc/sys/kernel/perf_event_paranoid.\n");

Zhang, Yanmin
committed
/*
* If it's cycles then fall back to hrtimer
* based cpu-clock-tick sw counter, which
* is always available even if no PMU support:
*/
if (attr->type == PERF_TYPE_HARDWARE
&& attr->config == PERF_COUNT_HW_CPU_CYCLES) {
if (verbose)
warning(" ... trying to fall back to cpu-clock-ticks\n");
attr->type = PERF_TYPE_SOFTWARE;
attr->config = PERF_COUNT_SW_CPU_CLOCK;
goto try_again;
}
printf("\n");
error("sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information.\n",
FD(evsel, i, thread_index), strerror(err));

Zhang, Yanmin
committed
die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
exit(-1);
}
assert(FD(evsel, i, thread_index) >= 0);
fcntl(FD(evsel, i, thread_index), F_SETFL, O_NONBLOCK);

Ingo Molnar
committed
/*

Zhang, Yanmin
committed
* First counter acts as the group leader:

Ingo Molnar
committed
*/

Zhang, Yanmin
committed
if (group && group_fd == -1)
group_fd = FD(evsel, i, thread_index);

Zhang, Yanmin
committed
event_array[nr_poll].fd = FD(evsel, i, thread_index);

Zhang, Yanmin
committed
event_array[nr_poll].events = POLLIN;
nr_poll++;
mm = xyarray__entry(mmap_array, i, thread_index);
mm->prev = 0;
mm->mask = mmap_pages*page_size - 1;
mm->base = mmap(NULL, (mmap_pages+1)*page_size,
PROT_READ, MAP_SHARED, FD(evsel, i, thread_index), 0);
if (mm->base == MAP_FAILED)

Zhang, Yanmin
committed
die("failed to mmap with %d (%s)\n", errno, strerror(errno));

Ingo Molnar
committed
}
}
static int __cmd_top(void)
{
pthread_t thread;
struct perf_evsel *counter;
int i, ret;

Arnaldo Carvalho de Melo
committed
/*
* FIXME: perf_session__new should allow passing a O_MMAP, so that all this
* mmap reading, etc is encapsulated in it. Use O_WRONLY for now.

Arnaldo Carvalho de Melo
committed
*/
struct perf_session *session = perf_session__new(NULL, O_WRONLY, false, false, NULL);
if (session == NULL)
return -ENOMEM;

Zhang, Yanmin
committed
if (target_tid != -1)
event__synthesize_thread(target_tid, event__process, session);

Arnaldo Carvalho de Melo
committed
event__synthesize_threads(event__process, session);
for (i = 0; i < cpus->nr; i++) {
list_for_each_entry(counter, &evsel_list, node)

Ingo Molnar
committed
start_counter(i, counter);

Frederic Weisbecker
committed
/* Wait for a minimal set of events before starting the snapshot */

Zhang, Yanmin
committed
poll(&event_array[0], nr_poll, 100);

Frederic Weisbecker
committed

Arnaldo Carvalho de Melo
committed
perf_session__mmap_read(session);

Frederic Weisbecker
committed
if (pthread_create(&thread, NULL, display_thread, session)) {
printf("Could not create display thread.\n");
exit(-1);
}
if (realtime_prio) {
struct sched_param param;
param.sched_priority = realtime_prio;
if (sched_setscheduler(0, SCHED_FIFO, ¶m)) {
printf("Could not set realtime priority.\n");
exit(-1);
}
}
while (1) {

Arnaldo Carvalho de Melo
committed
perf_session__mmap_read(session);
ret = poll(event_array, nr_poll, 100);
}
return 0;
}
static const char * const top_usage[] = {
"perf top [<options>]",
NULL
};
static const struct option options[] = {
OPT_CALLBACK('e', "event", NULL, "event",
"event selector. use 'perf list' to list available events",
parse_events),
OPT_INTEGER('c', "count", &default_interval,
"event period to sample"),
OPT_INTEGER('p', "pid", &target_pid,

Zhang, Yanmin
committed
"profile events on existing process id"),
OPT_INTEGER('t', "tid", &target_tid,
"profile events on existing thread id"),
OPT_BOOLEAN('a', "all-cpus", &system_wide,
"system-wide collection from all CPUs"),
OPT_STRING('C', "cpu", &cpu_list, "cpu",
"list of cpus to monitor"),
OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
"file", "vmlinux pathname"),
OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
"hide kernel symbols"),
OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"),
OPT_INTEGER('r', "realtime", &realtime_prio,
"collect data with this RT SCHED_FIFO priority"),
OPT_INTEGER('d', "delay", &delay_secs,
"number of seconds to delay between refreshes"),
OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
"dump the symbol table used for profiling"),
OPT_INTEGER('f', "count-filter", &count_filter,
"only display functions with more events than this"),
OPT_BOOLEAN('g', "group", &group,
"put the counters into a counter group"),
OPT_BOOLEAN('i', "inherit", &inherit,
"child tasks inherit counters"),

Mike Galbraith
committed
OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
OPT_INTEGER('F', "freq", &freq,
OPT_INTEGER('E', "entries", &print_entries,
"display this many functions"),
OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
"hide user symbols"),

Ian Munsie
committed
OPT_INCR('v', "verbose", &verbose,
"be more verbose (show counter open errors, etc)"),
int cmd_top(int argc, const char **argv, const char *prefix __used)
struct perf_evsel *pos;
int status = -ENOMEM;
page_size = sysconf(_SC_PAGE_SIZE);
argc = parse_options(argc, argv, options, top_usage, 0);
if (argc)
usage_with_options(top_usage, options);
if (target_pid != -1)

Zhang, Yanmin
committed
target_tid = target_pid;
threads = thread_map__new(target_pid, target_tid);
if (threads == NULL) {
pr_err("Problems finding threads of monitor\n");
usage_with_options(top_usage, options);

Zhang, Yanmin
committed
}
event_array = malloc((sizeof(struct pollfd) *
MAX_NR_CPUS * MAX_COUNTERS * threads->nr));

Zhang, Yanmin
committed
if (!event_array)
return -ENOMEM;
/* CPU and PID are mutually exclusive */
if (target_tid > 0 && cpu_list) {
printf("WARNING: PID switch overriding CPU\n");
sleep(1);
cpu_list = NULL;
if (!nr_counters && perf_evsel_list__create_default() < 0) {
pr_err("Not enough memory for event selector list\n");
return -ENOMEM;
}
if (delay_secs < 1)
delay_secs = 1;
/*
* User specified count overrides default frequency.
*/
if (default_interval)
freq = 0;
else if (freq) {
default_interval = freq;
} else {
fprintf(stderr, "frequency and count are zero, aborting\n");
exit(EXIT_FAILURE);
}
if (target_tid != -1)
cpus = cpu_map__dummy_new();
cpus = cpu_map__new(cpu_list);
if (cpus == NULL)
usage_with_options(top_usage, options);
list_for_each_entry(pos, &evsel_list, node) {
if (perf_evsel__alloc_mmap_per_thread(pos, cpus->nr, threads->nr) < 0 ||
perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0)
goto out_free_fd;
/*
* Fill in the ones not specifically initialized via -c:
*/
if (pos->attr.sample_period)
continue;
pos->attr.sample_period = default_interval;
}
sym_evsel = list_entry(evsel_list.next, struct perf_evsel, node);
symbol_conf.priv_size = (sizeof(struct sym_entry) +
(nr_counters + 1) * sizeof(unsigned long));
symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
if (symbol__init() < 0)
return -1;
get_term_dimensions(&winsize);
if (print_entries == 0) {
update_print_entries(&winsize);
signal(SIGWINCH, sig_winch_handler);
}
status = __cmd_top();
out_free_fd:
list_for_each_entry(pos, &evsel_list, node)
perf_evsel__free_mmap(pos);
perf_evsel_list__delete();