Skip to content

Commit 453fa03

Browse files
olsajiriacmel
authored andcommitted
libperf: Add perf_evlist__set_maps() function
Move the evlist__set_maps() function from tools/perf to libperf. Committer notes: Fix up reject due to earlier inversion in calling perf_evlist__init(). Signed-off-by: Jiri Olsa <jolsa@kernel.org> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Alexey Budankov <alexey.budankov@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Michael Petlan <mpetlan@redhat.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/20190721112506.12306-57-jolsa@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 03617c2 commit 453fa03

14 files changed

Lines changed: 83 additions & 67 deletions

File tree

‎tools/perf/arch/x86/tests/perf-time-to-tsc.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <linux/types.h>
77
#include <sys/prctl.h>
88
#include <perf/cpumap.h>
9+
#include <perf/evlist.h>
910

1011
#include "parse-events.h"
1112
#include "evlist.h"
@@ -72,7 +73,7 @@ int test__perf_time_to_tsc(struct test *test __maybe_unused, int subtest __maybe
7273
evlist = evlist__new();
7374
CHECK_NOT_NULL__(evlist);
7475

75-
perf_evlist__set_maps(evlist, cpus, threads);
76+
perf_evlist__set_maps(&evlist->core, cpus, threads);
7677

7778
CHECK__(parse_events(evlist, "cycles:u", NULL));
7879

‎tools/perf/builtin-script.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <fcntl.h>
4949
#include <unistd.h>
5050
#include <subcmd/pager.h>
51+
#include <perf/evlist.h>
5152

5253
#include <linux/ctype.h>
5354

@@ -3264,7 +3265,7 @@ static int set_maps(struct perf_script *script)
32643265
if (WARN_ONCE(script->allocated, "stats double allocation\n"))
32653266
return -EINVAL;
32663267

3267-
perf_evlist__set_maps(evlist, script->cpus, script->threads);
3268+
perf_evlist__set_maps(&evlist->core, script->cpus, script->threads);
32683269

32693270
if (perf_evlist__alloc_stats(evlist, true))
32703271
return -ENOMEM;

‎tools/perf/builtin-stat.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
#include <sys/resource.h>
8484

8585
#include <linux/ctype.h>
86+
#include <perf/evlist.h>
8687

8788
#define DEFAULT_SEPARATOR " "
8889
#define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi"
@@ -1517,7 +1518,7 @@ static int set_maps(struct perf_stat *st)
15171518
if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
15181519
return -EINVAL;
15191520

1520-
perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
1521+
perf_evlist__set_maps(&evsel_list->core, st->cpus, st->threads);
15211522

15221523
if (perf_evlist__alloc_stats(evsel_list, true))
15231524
return -ENOMEM;

‎tools/perf/lib/evlist.c‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,48 @@
55
#include <internal/evsel.h>
66
#include <linux/zalloc.h>
77
#include <stdlib.h>
8+
#include <perf/cpumap.h>
9+
#include <perf/threadmap.h>
810

911
void perf_evlist__init(struct perf_evlist *evlist)
1012
{
1113
INIT_LIST_HEAD(&evlist->entries);
1214
evlist->nr_entries = 0;
1315
}
1416

17+
static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
18+
struct perf_evsel *evsel)
19+
{
20+
/*
21+
* We already have cpus for evsel (via PMU sysfs) so
22+
* keep it, if there's no target cpu list defined.
23+
*/
24+
if (!evsel->own_cpus || evlist->has_user_cpus) {
25+
perf_cpu_map__put(evsel->cpus);
26+
evsel->cpus = perf_cpu_map__get(evlist->cpus);
27+
} else if (evsel->cpus != evsel->own_cpus) {
28+
perf_cpu_map__put(evsel->cpus);
29+
evsel->cpus = perf_cpu_map__get(evsel->own_cpus);
30+
}
31+
32+
perf_thread_map__put(evsel->threads);
33+
evsel->threads = perf_thread_map__get(evlist->threads);
34+
}
35+
36+
static void perf_evlist__propagate_maps(struct perf_evlist *evlist)
37+
{
38+
struct perf_evsel *evsel;
39+
40+
perf_evlist__for_each_evsel(evlist, evsel)
41+
__perf_evlist__propagate_maps(evlist, evsel);
42+
}
43+
1544
void perf_evlist__add(struct perf_evlist *evlist,
1645
struct perf_evsel *evsel)
1746
{
1847
list_add_tail(&evsel->node, &evlist->entries);
1948
evlist->nr_entries += 1;
49+
__perf_evlist__propagate_maps(evlist, evsel);
2050
}
2151

2252
void perf_evlist__remove(struct perf_evlist *evlist,
@@ -60,3 +90,27 @@ void perf_evlist__delete(struct perf_evlist *evlist)
6090
{
6191
free(evlist);
6292
}
93+
94+
void perf_evlist__set_maps(struct perf_evlist *evlist,
95+
struct perf_cpu_map *cpus,
96+
struct perf_thread_map *threads)
97+
{
98+
/*
99+
* Allow for the possibility that one or another of the maps isn't being
100+
* changed i.e. don't put it. Note we are assuming the maps that are
101+
* being applied are brand new and evlist is taking ownership of the
102+
* original reference count of 1. If that is not the case it is up to
103+
* the caller to increase the reference count.
104+
*/
105+
if (cpus != evlist->cpus) {
106+
perf_cpu_map__put(evlist->cpus);
107+
evlist->cpus = perf_cpu_map__get(cpus);
108+
}
109+
110+
if (threads != evlist->threads) {
111+
perf_thread_map__put(evlist->threads);
112+
evlist->threads = perf_thread_map__get(threads);
113+
}
114+
115+
perf_evlist__propagate_maps(evlist);
116+
}

‎tools/perf/lib/include/perf/evlist.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
struct perf_evlist;
88
struct perf_evsel;
9+
struct perf_cpu_map;
10+
struct perf_thread_map;
911

1012
LIBPERF_API void perf_evlist__init(struct perf_evlist *evlist);
1113
LIBPERF_API void perf_evlist__add(struct perf_evlist *evlist,
@@ -22,4 +24,8 @@ LIBPERF_API struct perf_evsel* perf_evlist__next(struct perf_evlist *evlist,
2224
(pos) != NULL; \
2325
(pos) = perf_evlist__next((evlist), (pos)))
2426

27+
LIBPERF_API void perf_evlist__set_maps(struct perf_evlist *evlist,
28+
struct perf_cpu_map *cpus,
29+
struct perf_thread_map *threads);
30+
2531
#endif /* __LIBPERF_EVLIST_H */

‎tools/perf/lib/libperf.map‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ LIBPERF_0.0.1 {
2020
perf_evlist__add;
2121
perf_evlist__remove;
2222
perf_evlist__next;
23+
perf_evlist__set_maps;
2324
local:
2425
*;
2526
};

‎tools/perf/tests/code-reading.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <string.h>
1010
#include <sys/param.h>
1111
#include <perf/cpumap.h>
12+
#include <perf/evlist.h>
1213

1314
#include "parse-events.h"
1415
#include "evlist.h"
@@ -629,7 +630,7 @@ static int do_test_code_reading(bool try_kcore)
629630
goto out_put;
630631
}
631632

632-
perf_evlist__set_maps(evlist, cpus, threads);
633+
perf_evlist__set_maps(&evlist->core, cpus, threads);
633634

634635
str = do_determine_event(excl_kernel);
635636
pr_debug("Parsing event '%s'\n", str);
@@ -658,7 +659,7 @@ static int do_test_code_reading(bool try_kcore)
658659
*/
659660
perf_cpu_map__get(cpus);
660661
perf_thread_map__get(threads);
661-
perf_evlist__set_maps(evlist, NULL, NULL);
662+
perf_evlist__set_maps(&evlist->core, NULL, NULL);
662663
evlist__delete(evlist);
663664
evlist = NULL;
664665
continue;

‎tools/perf/tests/keep-tracking.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <unistd.h>
44
#include <sys/prctl.h>
55
#include <perf/cpumap.h>
6+
#include <perf/evlist.h>
67

78
#include "parse-events.h"
89
#include "evlist.h"
@@ -82,7 +83,7 @@ int test__keep_tracking(struct test *test __maybe_unused, int subtest __maybe_un
8283
evlist = evlist__new();
8384
CHECK_NOT_NULL__(evlist);
8485

85-
perf_evlist__set_maps(evlist, cpus, threads);
86+
perf_evlist__set_maps(&evlist->core, cpus, threads);
8687

8788
CHECK__(parse_events(evlist, "dummy:u", NULL));
8889
CHECK__(parse_events(evlist, "cycles:u", NULL));

‎tools/perf/tests/mmap-basic.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "tests.h"
1313
#include <linux/err.h>
1414
#include <linux/kernel.h>
15+
#include <perf/evlist.h>
1516

1617
/*
1718
* This test will generate random numbers of calls to some getpid syscalls,
@@ -68,7 +69,7 @@ int test__basic_mmap(struct test *test __maybe_unused, int subtest __maybe_unuse
6869
goto out_free_cpus;
6970
}
7071

71-
perf_evlist__set_maps(evlist, cpus, threads);
72+
perf_evlist__set_maps(&evlist->core, cpus, threads);
7273

7374
for (i = 0; i < nsyscalls; ++i) {
7475
char name[64];

‎tools/perf/tests/sw-clock.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "util/evlist.h"
1212
#include "util/cpumap.h"
1313
#include "util/thread_map.h"
14+
#include <perf/evlist.h>
1415

1516
#define NR_LOOPS 10000000
1617

@@ -64,7 +65,7 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
6465
goto out_free_maps;
6566
}
6667

67-
perf_evlist__set_maps(evlist, cpus, threads);
68+
perf_evlist__set_maps(&evlist->core, cpus, threads);
6869

6970
cpus = NULL;
7071
threads = NULL;

0 commit comments

Comments
 (0)