Newer
Older
u64 ip = event->ip.ip;
struct sym_entry *syme;
struct addr_location al;
struct machine *machine;
u8 origin = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
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, event->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",
event->ip.pid);
return;
}
if (event->header.misc & PERF_RECORD_MISC_EXACT_IP)
exact_samples++;
if (perf_event__preprocess_sample(event, 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);
struct perf_evsel *evsel;
syme->origin = origin;
evsel = perf_evlist__id2evsel(evsel_list, sample->id);
assert(evsel != NULL);
syme->count[evsel->idx]++;
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);
}
static void perf_session__mmap_read_cpu(struct perf_session *self, int cpu)
struct perf_sample sample;
union perf_event *event;
while ((event = perf_evlist__read_on_cpu(evsel_list, cpu)) != NULL) {
perf_session__parse_sample(self, event, &sample);
if (event->header.type == PERF_RECORD_SAMPLE)
perf_event__process_sample(event, &sample, self);
perf_event__process(event, &sample, self);

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

Frederic Weisbecker
committed
{
int i;
for (i = 0; i < evsel_list->cpus->nr; i++)
perf_session__mmap_read_cpu(self, i);

Frederic Weisbecker
committed
}
static void start_counters(struct perf_evlist *evlist)
{
struct perf_evsel *counter;
list_for_each_entry(counter, &evlist->entries, node) {
struct perf_event_attr *attr = &counter->attr;

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;
}

Zhang, Yanmin
committed
if (evlist->nr_entries > 1) {
attr->sample_type |= PERF_SAMPLE_ID;
attr->read_format |= PERF_FORMAT_ID;
}
attr->mmap = 1;
try_again:
if (perf_evsel__open(counter, evsel_list->cpus,
evsel_list->threads, group, inherit) < 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) {

Zhang, Yanmin
committed
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",
err, strerror(err));

Zhang, Yanmin
committed
die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
exit(-1);
}

Ingo Molnar
committed
}
if (perf_evlist__mmap(evlist, mmap_pages, false) < 0)
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 *first;

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)
perf_event__synthesize_thread(target_tid, perf_event__process,
session);
perf_event__synthesize_threads(perf_event__process, session);
first = list_entry(evsel_list->entries.next, struct perf_evsel, node);
perf_session__set_sample_type(session, first->attr.sample_type);

Frederic Weisbecker
committed
/* Wait for a minimal set of events before starting the snapshot */
poll(evsel_list->pollfd, evsel_list->nr_fds, 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(evsel_list->pollfd, evsel_list->nr_fds, 100);
}
return 0;
}
static const char * const top_usage[] = {
"perf top [<options>]",
NULL
};
static const struct option options[] = {
OPT_CALLBACK('e', "event", &evsel_list, "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;
evsel_list = perf_evlist__new(NULL, NULL);
if (evsel_list == NULL)
return -ENOMEM;
page_size = sysconf(_SC_PAGE_SIZE);
argc = parse_options(argc, argv, options, top_usage, 0);
if (argc)
usage_with_options(top_usage, options);
/* 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 (target_pid != -1)
target_tid = target_pid;
if (perf_evlist__create_maps(evsel_list, target_pid,
target_tid, cpu_list) < 0)
usage_with_options(top_usage, options);
if (!evsel_list->nr_entries &&
perf_evlist__add_default(evsel_list) < 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);
}
list_for_each_entry(pos, &evsel_list->entries, node) {
if (perf_evsel__alloc_fd(pos, evsel_list->cpus->nr,
evsel_list->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;
}
if (perf_evlist__alloc_pollfd(evsel_list) < 0 ||
perf_evlist__alloc_mmap(evsel_list) < 0)
sym_evsel = list_entry(evsel_list->entries.next, struct perf_evsel, node);
symbol_conf.priv_size = (sizeof(struct sym_entry) +
(evsel_list->nr_entries + 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:
perf_evlist__delete(evsel_list);