Skip to content

Commit bf8e8f4

Browse files
ahunter6acmel
authored andcommitted
perf evlist: Add 'system_wide' option
Add an option to cause a selected event to be opened always without a pid when configured by perf_evsel__config(). This is needed when using the sched_switch tracepoint to follow object code execution. sched_switch occurs before the task switch and so it cannot record it in a context limited to that task. Note that also means that sched_switch is useless when capturing data per-thread, as is the 'context-switches' software event for the same reason. Signed-off-by: Adrian Hunter <adrian.hunter@intel.com> Cc: David Ahern <dsahern@gmail.com> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Namhyung Kim <namhyung@gmail.com> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stephane Eranian <eranian@google.com> Link: http://lkml.kernel.org/r/1406786474-9306-9-git-send-email-adrian.hunter@intel.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent f247fb8 commit bf8e8f4

3 files changed

Lines changed: 64 additions & 13 deletions

File tree

‎tools/perf/util/evlist.c‎

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,27 @@ int perf_evlist__add_newtp(struct perf_evlist *evlist,
265265
return 0;
266266
}
267267

268+
static int perf_evlist__nr_threads(struct perf_evlist *evlist,
269+
struct perf_evsel *evsel)
270+
{
271+
if (evsel->system_wide)
272+
return 1;
273+
else
274+
return thread_map__nr(evlist->threads);
275+
}
276+
268277
void perf_evlist__disable(struct perf_evlist *evlist)
269278
{
270279
int cpu, thread;
271280
struct perf_evsel *pos;
272281
int nr_cpus = cpu_map__nr(evlist->cpus);
273-
int nr_threads = thread_map__nr(evlist->threads);
282+
int nr_threads;
274283

275284
for (cpu = 0; cpu < nr_cpus; cpu++) {
276285
evlist__for_each(evlist, pos) {
277286
if (!perf_evsel__is_group_leader(pos) || !pos->fd)
278287
continue;
288+
nr_threads = perf_evlist__nr_threads(evlist, pos);
279289
for (thread = 0; thread < nr_threads; thread++)
280290
ioctl(FD(pos, cpu, thread),
281291
PERF_EVENT_IOC_DISABLE, 0);
@@ -288,12 +298,13 @@ void perf_evlist__enable(struct perf_evlist *evlist)
288298
int cpu, thread;
289299
struct perf_evsel *pos;
290300
int nr_cpus = cpu_map__nr(evlist->cpus);
291-
int nr_threads = thread_map__nr(evlist->threads);
301+
int nr_threads;
292302

293303
for (cpu = 0; cpu < nr_cpus; cpu++) {
294304
evlist__for_each(evlist, pos) {
295305
if (!perf_evsel__is_group_leader(pos) || !pos->fd)
296306
continue;
307+
nr_threads = perf_evlist__nr_threads(evlist, pos);
297308
for (thread = 0; thread < nr_threads; thread++)
298309
ioctl(FD(pos, cpu, thread),
299310
PERF_EVENT_IOC_ENABLE, 0);
@@ -305,12 +316,14 @@ int perf_evlist__disable_event(struct perf_evlist *evlist,
305316
struct perf_evsel *evsel)
306317
{
307318
int cpu, thread, err;
319+
int nr_cpus = cpu_map__nr(evlist->cpus);
320+
int nr_threads = perf_evlist__nr_threads(evlist, evsel);
308321

309322
if (!evsel->fd)
310323
return 0;
311324

312-
for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
313-
for (thread = 0; thread < evlist->threads->nr; thread++) {
325+
for (cpu = 0; cpu < nr_cpus; cpu++) {
326+
for (thread = 0; thread < nr_threads; thread++) {
314327
err = ioctl(FD(evsel, cpu, thread),
315328
PERF_EVENT_IOC_DISABLE, 0);
316329
if (err)
@@ -324,12 +337,14 @@ int perf_evlist__enable_event(struct perf_evlist *evlist,
324337
struct perf_evsel *evsel)
325338
{
326339
int cpu, thread, err;
340+
int nr_cpus = cpu_map__nr(evlist->cpus);
341+
int nr_threads = perf_evlist__nr_threads(evlist, evsel);
327342

328343
if (!evsel->fd)
329344
return -EINVAL;
330345

331-
for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
332-
for (thread = 0; thread < evlist->threads->nr; thread++) {
346+
for (cpu = 0; cpu < nr_cpus; cpu++) {
347+
for (thread = 0; thread < nr_threads; thread++) {
333348
err = ioctl(FD(evsel, cpu, thread),
334349
PERF_EVENT_IOC_ENABLE, 0);
335350
if (err)
@@ -343,7 +358,16 @@ static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
343358
{
344359
int nr_cpus = cpu_map__nr(evlist->cpus);
345360
int nr_threads = thread_map__nr(evlist->threads);
346-
int nfds = nr_cpus * nr_threads * evlist->nr_entries;
361+
int nfds = 0;
362+
struct perf_evsel *evsel;
363+
364+
list_for_each_entry(evsel, &evlist->entries, node) {
365+
if (evsel->system_wide)
366+
nfds += nr_cpus;
367+
else
368+
nfds += nr_cpus * nr_threads;
369+
}
370+
347371
evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
348372
return evlist->pollfd != NULL ? 0 : -ENOMEM;
349373
}
@@ -636,7 +660,12 @@ static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
636660
struct perf_evsel *evsel;
637661

638662
evlist__for_each(evlist, evsel) {
639-
int fd = FD(evsel, cpu, thread);
663+
int fd;
664+
665+
if (evsel->system_wide && thread)
666+
continue;
667+
668+
fd = FD(evsel, cpu, thread);
640669

641670
if (*output == -1) {
642671
*output = fd;

‎tools/perf/util/evsel.c‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,10 @@ void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
695695
int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
696696
{
697697
int cpu, thread;
698+
699+
if (evsel->system_wide)
700+
nthreads = 1;
701+
698702
evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
699703

700704
if (evsel->fd) {
@@ -713,6 +717,9 @@ static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthrea
713717
{
714718
int cpu, thread;
715719

720+
if (evsel->system_wide)
721+
nthreads = 1;
722+
716723
for (cpu = 0; cpu < ncpus; cpu++) {
717724
for (thread = 0; thread < nthreads; thread++) {
718725
int fd = FD(evsel, cpu, thread),
@@ -743,6 +750,9 @@ int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
743750

744751
int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
745752
{
753+
if (evsel->system_wide)
754+
nthreads = 1;
755+
746756
evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
747757
if (evsel->sample_id == NULL)
748758
return -ENOMEM;
@@ -787,6 +797,9 @@ void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
787797
{
788798
int cpu, thread;
789799

800+
if (evsel->system_wide)
801+
nthreads = 1;
802+
790803
for (cpu = 0; cpu < ncpus; cpu++)
791804
for (thread = 0; thread < nthreads; ++thread) {
792805
close(FD(evsel, cpu, thread));
@@ -875,6 +888,9 @@ int __perf_evsel__read(struct perf_evsel *evsel,
875888
int cpu, thread;
876889
struct perf_counts_values *aggr = &evsel->counts->aggr, count;
877890

891+
if (evsel->system_wide)
892+
nthreads = 1;
893+
878894
aggr->val = aggr->ena = aggr->run = 0;
879895

880896
for (cpu = 0; cpu < ncpus; cpu++) {
@@ -997,13 +1013,18 @@ static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
9971013
static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
9981014
struct thread_map *threads)
9991015
{
1000-
int cpu, thread;
1016+
int cpu, thread, nthreads;
10011017
unsigned long flags = PERF_FLAG_FD_CLOEXEC;
10021018
int pid = -1, err;
10031019
enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
10041020

1021+
if (evsel->system_wide)
1022+
nthreads = 1;
1023+
else
1024+
nthreads = threads->nr;
1025+
10051026
if (evsel->fd == NULL &&
1006-
perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
1027+
perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
10071028
return -ENOMEM;
10081029

10091030
if (evsel->cgrp) {
@@ -1027,10 +1048,10 @@ static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
10271048

10281049
for (cpu = 0; cpu < cpus->nr; cpu++) {
10291050

1030-
for (thread = 0; thread < threads->nr; thread++) {
1051+
for (thread = 0; thread < nthreads; thread++) {
10311052
int group_fd;
10321053

1033-
if (!evsel->cgrp)
1054+
if (!evsel->cgrp && !evsel->system_wide)
10341055
pid = threads->map[thread];
10351056

10361057
group_fd = get_group_fd(evsel, cpu, thread);
@@ -1103,7 +1124,7 @@ static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
11031124
close(FD(evsel, cpu, thread));
11041125
FD(evsel, cpu, thread) = -1;
11051126
}
1106-
thread = threads->nr;
1127+
thread = nthreads;
11071128
} while (--cpu >= 0);
11081129
return err;
11091130
}

‎tools/perf/util/evsel.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ struct perf_evsel {
8585
bool needs_swap;
8686
bool no_aux_samples;
8787
bool immediate;
88+
bool system_wide;
8889
/* parse modifier helper */
8990
int exclude_GH;
9091
int nr_members;

0 commit comments

Comments
 (0)