Skip to content

Commit

Permalink
perf evsel: Introduce perf_evlist
Browse files Browse the repository at this point in the history
Killing two more perf wide global variables: nr_counters and evsel_list
as a list_head.

There are more operations that will need more fields in perf_evlist,
like the pollfd for polling all the fds in a list of evsel instances.

Use option->value to pass the evsel_list to parse_{events,filters}.

LKML-Reference: <new-submission>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Tom Zanussi <tzanussi@gmail.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
  • Loading branch information
Arnaldo Carvalho de Melo committed Jan 22, 2011
1 parent 00e99a4 commit 361c99a
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 118 deletions.
2 changes: 2 additions & 0 deletions tools/perf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ LIB_H += util/debug.h
LIB_H += util/debugfs.h
LIB_H += util/event.h
LIB_H += util/evsel.h
LIB_H += util/evlist.h
LIB_H += util/exec_cmd.h
LIB_H += util/types.h
LIB_H += util/levenshtein.h
Expand Down Expand Up @@ -440,6 +441,7 @@ LIB_OBJS += $(OUTPUT)util/ctype.o
LIB_OBJS += $(OUTPUT)util/debugfs.o
LIB_OBJS += $(OUTPUT)util/environment.o
LIB_OBJS += $(OUTPUT)util/event.o
LIB_OBJS += $(OUTPUT)util/evlist.o
LIB_OBJS += $(OUTPUT)util/evsel.o
LIB_OBJS += $(OUTPUT)util/exec_cmd.o
LIB_OBJS += $(OUTPUT)util/help.o
Expand Down
47 changes: 28 additions & 19 deletions tools/perf/builtin-record.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "util/header.h"
#include "util/event.h"
#include "util/evlist.h"
#include "util/evsel.h"
#include "util/debug.h"
#include "util/session.h"
Expand Down Expand Up @@ -66,6 +67,7 @@ static bool sample_address = false;
static bool sample_time = false;
static bool no_buildid = false;
static bool no_buildid_cache = false;
static struct perf_evlist *evsel_list;

static long samples = 0;
static u64 bytes_written = 0;
Expand Down Expand Up @@ -229,7 +231,8 @@ static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int n
return h_attr;
}

static void create_counter(struct perf_evsel *evsel, int cpu)
static void create_counter(struct perf_evlist *evlist,
struct perf_evsel *evsel, int cpu)
{
char *filter = evsel->filter;
struct perf_event_attr *attr = &evsel->attr;
Expand Down Expand Up @@ -263,7 +266,7 @@ static void create_counter(struct perf_evsel *evsel, int cpu)

attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;

if (nr_counters > 1)
if (evlist->nr_entries > 1)
attr->sample_type |= PERF_SAMPLE_ID;

/*
Expand Down Expand Up @@ -410,7 +413,7 @@ static void create_counter(struct perf_evsel *evsel, int cpu)

if (evsel->idx || thread_index) {
struct perf_evsel *first;
first = list_entry(evsel_list.next, struct perf_evsel, node);
first = list_entry(evlist->entries.next, struct perf_evsel, node);
ret = ioctl(FD(evsel, nr_cpu, thread_index),
PERF_EVENT_IOC_SET_OUTPUT,
FD(first, nr_cpu, 0));
Expand Down Expand Up @@ -449,14 +452,14 @@ static void create_counter(struct perf_evsel *evsel, int cpu)
sample_type = attr->sample_type;
}

static void open_counters(int cpu)
static void open_counters(struct perf_evlist *evlist, int cpu)
{
struct perf_evsel *pos;

group_fd = -1;

list_for_each_entry(pos, &evsel_list, node)
create_counter(pos, cpu);
list_for_each_entry(pos, &evlist->entries, node)
create_counter(evlist, pos, cpu);

nr_cpu++;
}
Expand All @@ -481,9 +484,9 @@ static void atexit_header(void)

if (!no_buildid)
process_buildids();
perf_header__write(&session->header, output, true);
perf_header__write(&session->header, evsel_list, output, true);
perf_session__delete(session);
perf_evsel_list__delete();
perf_evlist__delete(evsel_list);
symbol__exit();
}
}
Expand Down Expand Up @@ -611,7 +614,7 @@ static int __cmd_record(int argc, const char **argv)
goto out_delete_session;
}

if (have_tracepoints(&evsel_list))
if (have_tracepoints(&evsel_list->entries))
perf_header__set_feat(&session->header, HEADER_TRACE_INFO);

/*
Expand Down Expand Up @@ -674,10 +677,10 @@ static int __cmd_record(int argc, const char **argv)
}

if (!system_wide && no_inherit && !cpu_list) {
open_counters(-1);
open_counters(evsel_list, -1);
} else {
for (i = 0; i < cpus->nr; i++)
open_counters(cpus->map[i]);
open_counters(evsel_list, cpus->map[i]);
}

perf_session__set_sample_type(session, sample_type);
Expand All @@ -687,7 +690,8 @@ static int __cmd_record(int argc, const char **argv)
if (err < 0)
return err;
} else if (file_new) {
err = perf_header__write(&session->header, output, false);
err = perf_header__write(&session->header, evsel_list,
output, false);
if (err < 0)
return err;
}
Expand All @@ -712,7 +716,7 @@ static int __cmd_record(int argc, const char **argv)
return err;
}

if (have_tracepoints(&evsel_list)) {
if (have_tracepoints(&evsel_list->entries)) {
/*
* FIXME err <= 0 here actually means that
* there were no tracepoints so its not really
Expand All @@ -721,7 +725,7 @@ static int __cmd_record(int argc, const char **argv)
* return this more properly and also
* propagate errors that now are calling die()
*/
err = event__synthesize_tracing_data(output, &evsel_list,
err = event__synthesize_tracing_data(output, evsel_list,
process_synthesized_event,
session);
if (err <= 0) {
Expand Down Expand Up @@ -797,7 +801,7 @@ static int __cmd_record(int argc, const char **argv)
for (i = 0; i < nr_cpu; i++) {
struct perf_evsel *pos;

list_for_each_entry(pos, &evsel_list, node) {
list_for_each_entry(pos, &evsel_list->entries, node) {
for (thread = 0;
thread < threads->nr;
thread++)
Expand Down Expand Up @@ -838,10 +842,10 @@ static const char * const record_usage[] = {
static bool force, append_file;

const struct option record_options[] = {
OPT_CALLBACK('e', "event", NULL, "event",
OPT_CALLBACK('e', "event", &evsel_list, "event",
"event selector. use 'perf list' to list available events",
parse_events),
OPT_CALLBACK(0, "filter", NULL, "filter",
OPT_CALLBACK(0, "filter", &evsel_list, "filter",
"event filter", parse_filter),
OPT_INTEGER('p', "pid", &target_pid,
"record events on existing process id"),
Expand Down Expand Up @@ -892,6 +896,10 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
int err = -ENOMEM;
struct perf_evsel *pos;

evsel_list = perf_evlist__new();
if (evsel_list == NULL)
return -ENOMEM;

argc = parse_options(argc, argv, record_options, record_usage,
PARSE_OPT_STOP_AT_NON_OPTION);
if (!argc && target_pid == -1 && target_tid == -1 &&
Expand All @@ -913,7 +921,8 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
if (no_buildid_cache || no_buildid)
disable_buildid_cache();

if (list_empty(&evsel_list) && perf_evsel_list__create_default() < 0) {
if (evsel_list->nr_entries == 0 &&
perf_evlist__add_default(evsel_list) < 0) {
pr_err("Not enough memory for event selector list\n");
goto out_symbol_exit;
}
Expand All @@ -933,7 +942,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
return -1;
}

list_for_each_entry(pos, &evsel_list, node) {
list_for_each_entry(pos, &evsel_list->entries, node) {
if (perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0)
goto out_free_fd;
if (perf_header__push_event(pos->attr.config, event_name(pos)))
Expand Down
34 changes: 19 additions & 15 deletions tools/perf/builtin-stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "util/parse-options.h"
#include "util/parse-events.h"
#include "util/event.h"
#include "util/evlist.h"
#include "util/evsel.h"
#include "util/debug.h"
#include "util/header.h"
Expand Down Expand Up @@ -71,6 +72,8 @@ static struct perf_event_attr default_attrs[] = {

};

struct perf_evlist *evsel_list;

static bool system_wide = false;
static struct cpu_map *cpus;
static int run_idx = 0;
Expand Down Expand Up @@ -309,7 +312,7 @@ static int run_perf_stat(int argc __used, const char **argv)
close(child_ready_pipe[0]);
}

list_for_each_entry(counter, &evsel_list, node) {
list_for_each_entry(counter, &evsel_list->entries, node) {
if (create_perf_stat_counter(counter) < 0) {
if (errno == -EPERM || errno == -EACCES) {
error("You may not have permission to collect %sstats.\n"
Expand Down Expand Up @@ -347,12 +350,12 @@ static int run_perf_stat(int argc __used, const char **argv)
update_stats(&walltime_nsecs_stats, t1 - t0);

if (no_aggr) {
list_for_each_entry(counter, &evsel_list, node) {
list_for_each_entry(counter, &evsel_list->entries, node) {
read_counter(counter);
perf_evsel__close_fd(counter, cpus->nr, 1);
}
} else {
list_for_each_entry(counter, &evsel_list, node) {
list_for_each_entry(counter, &evsel_list->entries, node) {
read_counter_aggr(counter);
perf_evsel__close_fd(counter, cpus->nr, threads->nr);
}
Expand Down Expand Up @@ -555,10 +558,10 @@ static void print_stat(int argc, const char **argv)
}

if (no_aggr) {
list_for_each_entry(counter, &evsel_list, node)
list_for_each_entry(counter, &evsel_list->entries, node)
print_counter(counter);
} else {
list_for_each_entry(counter, &evsel_list, node)
list_for_each_entry(counter, &evsel_list->entries, node)
print_counter_aggr(counter);
}

Expand Down Expand Up @@ -610,7 +613,7 @@ static int stat__set_big_num(const struct option *opt __used,
}

static const struct option options[] = {
OPT_CALLBACK('e', "event", NULL, "event",
OPT_CALLBACK('e', "event", &evsel_list, "event",
"event selector. use 'perf list' to list available events",
parse_events),
OPT_BOOLEAN('i', "no-inherit", &no_inherit,
Expand Down Expand Up @@ -648,6 +651,10 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)

setlocale(LC_ALL, "");

evsel_list = perf_evlist__new();
if (evsel_list == NULL)
return -ENOMEM;

argc = parse_options(argc, argv, options, stat_usage,
PARSE_OPT_STOP_AT_NON_OPTION);

Expand Down Expand Up @@ -679,17 +686,14 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)
usage_with_options(stat_usage, options);

/* Set attrs and nr_counters if no event is selected and !null_run */
if (!null_run && !nr_counters) {
if (!null_run && !evsel_list->nr_entries) {
size_t c;

nr_counters = ARRAY_SIZE(default_attrs);

for (c = 0; c < ARRAY_SIZE(default_attrs); ++c) {
pos = perf_evsel__new(&default_attrs[c],
nr_counters);
pos = perf_evsel__new(&default_attrs[c], c);
if (pos == NULL)
goto out;
list_add(&pos->node, &evsel_list);
perf_evlist__add(evsel_list, pos);
}
}

Expand All @@ -713,7 +717,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)
return -1;
}

list_for_each_entry(pos, &evsel_list, node) {
list_for_each_entry(pos, &evsel_list->entries, node) {
if (perf_evsel__alloc_stat_priv(pos) < 0 ||
perf_evsel__alloc_counts(pos, cpus->nr) < 0 ||
perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0)
Expand Down Expand Up @@ -741,9 +745,9 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)
if (status != -1)
print_stat(argc, argv);
out_free_fd:
list_for_each_entry(pos, &evsel_list, node)
list_for_each_entry(pos, &evsel_list->entries, node)
perf_evsel__free_stat_priv(pos);
perf_evsel_list__delete();
perf_evlist__delete(evsel_list);
out:
thread_map__delete(threads);
threads = NULL;
Expand Down
Loading

0 comments on commit 361c99a

Please sign in to comment.