Skip to content

Commit 5d67be9

Browse files
antonblanchardIngo Molnar
authored andcommitted
perf report/annotate/script: Add option to specify a CPU range
Add an option to perf report/annotate/script to specify which CPUs to operate on. This enables us to take a single system wide profile and analyse each CPU (or group of CPUs) in isolation. This was useful when profiling a multiprocess workload where the bottleneck was on one CPU but this was hidden in the overall profile. Per process and per thread breakdowns didn't help because multiple processes were running on each CPU and no single process consumed an entire CPU. The patch converts the list of CPUs returned by cpu_map__new into a bitmap for fast lookup. I wanted to use -C to be consistent with perf top/record/stat, but unfortunately perf report already uses -C <comms>. v2: Incorporate suggestions from David Ahern: - Added -c to perf script - Check that SAMPLE_CPU is set when -c is used - Update documentation v3: Create perf_session__cpu_bitmap() Signed-off-by: Anton Blanchard <anton@samba.org> Acked-by: David Ahern <dsahern@gmail.com> Cc: Arnaldo Carvalho de Melo <acme@ghostprotocols.net> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Paul Mackerras <paulus@samba.org> Link: http://lkml.kernel.org/r/20110704215750.11647eb9@kryten Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent 9f8b6a6 commit 5d67be9

8 files changed

Lines changed: 102 additions & 0 deletions

File tree

‎tools/perf/Documentation/perf-annotate.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ OPTIONS
6666
used. This interfaces starts by centering on the line with more
6767
samples, TAB/UNTAB cycles through the lines with more samples.
6868

69+
-c::
70+
--cpu:: Only report samples for the list of CPUs provided. Multiple CPUs can
71+
be provided as a comma-separated list with no space: 0,1. Ranges of
72+
CPUs are specified with -: 0-2. Default is to report samples on all
73+
CPUs.
74+
6975
SEE ALSO
7076
--------
7177
linkperf:perf-record[1], linkperf:perf-report[1]

‎tools/perf/Documentation/perf-report.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ OPTIONS
128128
--symfs=<directory>::
129129
Look for files with symbols relative to this directory.
130130

131+
-c::
132+
--cpu:: Only report samples for the list of CPUs provided. Multiple CPUs can
133+
be provided as a comma-separated list with no space: 0,1. Ranges of
134+
CPUs are specified with -: 0-2. Default is to report samples on all
135+
CPUs.
136+
131137
SEE ALSO
132138
--------
133139
linkperf:perf-stat[1]

‎tools/perf/Documentation/perf-script.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ OPTIONS
182182
--hide-call-graph::
183183
When printing symbols do not display call chain.
184184

185+
-c::
186+
--cpu:: Only report samples for the list of CPUs provided. Multiple CPUs can
187+
be provided as a comma-separated list with no space: 0,1. Ranges of
188+
CPUs are specified with -: 0-2. Default is to report samples on all
189+
CPUs.
190+
185191
SEE ALSO
186192
--------
187193
linkperf:perf-record[1], linkperf:perf-script-perl[1],

‎tools/perf/builtin-annotate.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include "util/hist.h"
2929
#include "util/session.h"
3030

31+
#include <linux/bitmap.h>
32+
3133
static char const *input_name = "perf.data";
3234

3335
static bool force, use_tui, use_stdio;
@@ -38,6 +40,9 @@ static bool print_line;
3840

3941
static const char *sym_hist_filter;
4042

43+
static const char *cpu_list;
44+
static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
45+
4146
static int perf_evlist__add_sample(struct perf_evlist *evlist,
4247
struct perf_sample *sample,
4348
struct perf_evsel *evsel,
@@ -90,6 +95,9 @@ static int process_sample_event(union perf_event *event,
9095
return -1;
9196
}
9297

98+
if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
99+
return 0;
100+
93101
if (!al.filtered &&
94102
perf_evlist__add_sample(session->evlist, sample, evsel, &al)) {
95103
pr_warning("problem incrementing symbol count, "
@@ -177,6 +185,12 @@ static int __cmd_annotate(void)
177185
if (session == NULL)
178186
return -ENOMEM;
179187

188+
if (cpu_list) {
189+
ret = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
190+
if (ret)
191+
goto out_delete;
192+
}
193+
180194
ret = perf_session__process_events(session, &event_ops);
181195
if (ret)
182196
goto out_delete;
@@ -252,6 +266,7 @@ static const struct option options[] = {
252266
"print matching source lines (may be slow)"),
253267
OPT_BOOLEAN('P', "full-paths", &full_paths,
254268
"Don't shorten the displayed pathnames"),
269+
OPT_STRING('c', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
255270
OPT_END()
256271
};
257272

‎tools/perf/builtin-report.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include "util/sort.h"
3434
#include "util/hist.h"
3535

36+
#include <linux/bitmap.h>
37+
3638
static char const *input_name = "perf.data";
3739

3840
static bool force, use_tui, use_stdio;
@@ -49,6 +51,9 @@ static char callchain_default_opt[] = "fractal,0.5,callee";
4951
static bool inverted_callchain;
5052
static symbol_filter_t annotate_init;
5153

54+
static const char *cpu_list;
55+
static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
56+
5257
static int perf_session__add_hist_entry(struct perf_session *session,
5358
struct addr_location *al,
5459
struct perf_sample *sample,
@@ -117,6 +122,9 @@ static int process_sample_event(union perf_event *event,
117122
if (al.filtered || (hide_unresolved && al.sym == NULL))
118123
return 0;
119124

125+
if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
126+
return 0;
127+
120128
if (al.map != NULL)
121129
al.map->dso->hit = 1;
122130

@@ -263,6 +271,12 @@ static int __cmd_report(void)
263271
if (session == NULL)
264272
return -ENOMEM;
265273

274+
if (cpu_list) {
275+
ret = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
276+
if (ret)
277+
goto out_delete;
278+
}
279+
266280
if (show_threads)
267281
perf_read_values_init(&show_threads_values);
268282

@@ -473,6 +487,7 @@ static const struct option options[] = {
473487
"Only display entries resolved to a symbol"),
474488
OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
475489
"Look for files with symbols relative to this directory"),
490+
OPT_STRING('c', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
476491
OPT_END()
477492
};
478493

‎tools/perf/builtin-script.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "util/util.h"
1414
#include "util/evlist.h"
1515
#include "util/evsel.h"
16+
#include <linux/bitmap.h>
1617

1718
static char const *script_name;
1819
static char const *generate_script_lang;
@@ -21,6 +22,8 @@ static u64 last_timestamp;
2122
static u64 nr_unordered;
2223
extern const struct option record_options[];
2324
static bool no_callchain;
25+
static const char *cpu_list;
26+
static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
2427

2528
enum perf_output_field {
2629
PERF_OUTPUT_COMM = 1U << 0,
@@ -453,6 +456,10 @@ static int process_sample_event(union perf_event *event,
453456
last_timestamp = sample->time;
454457
return 0;
455458
}
459+
460+
if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
461+
return 0;
462+
456463
scripting_ops->process_event(event, sample, evsel, session, thread);
457464

458465
session->hists.stats.total_period += sample->period;
@@ -1075,6 +1082,7 @@ static const struct option options[] = {
10751082
OPT_CALLBACK('f', "fields", NULL, "str",
10761083
"comma separated output fields prepend with 'type:'. Valid types: hw,sw,trace,raw. Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,addr",
10771084
parse_output_fields),
1085+
OPT_STRING('c', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
10781086

10791087
OPT_END()
10801088
};
@@ -1255,6 +1263,11 @@ int cmd_script(int argc, const char **argv, const char *prefix __used)
12551263
if (session == NULL)
12561264
return -ENOMEM;
12571265

1266+
if (cpu_list) {
1267+
if (perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap))
1268+
return -1;
1269+
}
1270+
12581271
if (!no_callchain)
12591272
symbol_conf.use_callchain = true;
12601273
else

‎tools/perf/util/session.c‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "session.h"
1313
#include "sort.h"
1414
#include "util.h"
15+
#include "cpumap.h"
1516

1617
static int perf_session__open(struct perf_session *self, bool force)
1718
{
@@ -1282,3 +1283,40 @@ void perf_session__print_ip(union perf_event *event,
12821283
}
12831284
}
12841285
}
1286+
1287+
int perf_session__cpu_bitmap(struct perf_session *session,
1288+
const char *cpu_list, unsigned long *cpu_bitmap)
1289+
{
1290+
int i;
1291+
struct cpu_map *map;
1292+
1293+
for (i = 0; i < PERF_TYPE_MAX; ++i) {
1294+
struct perf_evsel *evsel;
1295+
1296+
evsel = perf_session__find_first_evtype(session, i);
1297+
if (!evsel)
1298+
continue;
1299+
1300+
if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1301+
pr_err("File does not contain CPU events. "
1302+
"Remove -c option to proceed.\n");
1303+
return -1;
1304+
}
1305+
}
1306+
1307+
map = cpu_map__new(cpu_list);
1308+
1309+
for (i = 0; i < map->nr; i++) {
1310+
int cpu = map->map[i];
1311+
1312+
if (cpu >= MAX_NR_CPUS) {
1313+
pr_err("Requested CPU %d too large. "
1314+
"Consider raising MAX_NR_CPUS\n", cpu);
1315+
return -1;
1316+
}
1317+
1318+
set_bit(cpu, cpu_bitmap);
1319+
}
1320+
1321+
return 0;
1322+
}

‎tools/perf/util/session.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,7 @@ void perf_session__print_ip(union perf_event *event,
172172
struct perf_session *session,
173173
int print_sym, int print_dso);
174174

175+
int perf_session__cpu_bitmap(struct perf_session *session,
176+
const char *cpu_list, unsigned long *cpu_bitmap);
177+
175178
#endif /* __PERF_SESSION_H */

0 commit comments

Comments
 (0)