Skip to content

Commit 429a5f9

Browse files
Jin Yaoacmel
authored andcommitted
perf report: Allow specifying event to be used as sort key in --group output
When performing "perf report --group", it shows the event group information together. By default, the output is sorted by the first event in group. It would be nice for user to select any event for sorting. This patch introduces a new option "--group-sort-idx" to sort the output by the event at the index n in event group. For example, Before: # perf report --group --stdio # To display the perf.data header info, please use --header/--header-only options. # # # Total Lost Samples: 0 # # Samples: 12K of events 'cpu/instructions,period=2000003/, cpu/cpu-cycles,period=200003/, BR_MISP_RETIRED.ALL_BRANCHES:pp, cpu/event=0xc0,umask=1,cmask=1, # Event count (approx.): 6451235635 # # Overhead Command Shared Object Symbol # ................................ ......... ....................... ................................... # 92.19% 98.68% 0.00% 93.30% mgen mgen [.] LOOP1 3.12% 0.29% 0.00% 0.16% gsd-color libglib-2.0.so.0.5600.4 [.] 0x0000000000049515 1.56% 0.03% 0.00% 0.04% gsd-color libglib-2.0.so.0.5600.4 [.] 0x00000000000494b7 1.56% 0.01% 0.00% 0.00% gsd-color libglib-2.0.so.0.5600.4 [.] 0x00000000000494ce 1.56% 0.00% 0.00% 0.00% mgen [kernel.kallsyms] [k] task_tick_fair 0.00% 0.15% 0.00% 0.04% perf [kernel.kallsyms] [k] smp_call_function_single 0.00% 0.13% 0.00% 6.08% swapper [kernel.kallsyms] [k] intel_idle 0.00% 0.03% 0.00% 0.00% gsd-color libglib-2.0.so.0.5600.4 [.] g_main_context_check 0.00% 0.03% 0.00% 0.00% swapper [kernel.kallsyms] [k] apic_timer_interrupt ... After: # perf report --group --stdio --group-sort-idx 3 # To display the perf.data header info, please use --header/--header-only options. # # # Total Lost Samples: 0 # # Samples: 12K of events 'cpu/instructions,period=2000003/, cpu/cpu-cycles,period=200003/, BR_MISP_RETIRED.ALL_BRANCHES:pp, cpu/event=0xc0,umask=1,cmask=1, # Event count (approx.): 6451235635 # # Overhead Command Shared Object Symbol # ................................ ......... ....................... ................................... # 92.19% 98.68% 0.00% 93.30% mgen mgen [.] LOOP1 0.00% 0.13% 0.00% 6.08% swapper [kernel.kallsyms] [k] intel_idle 3.12% 0.29% 0.00% 0.16% gsd-color libglib-2.0.so.0.5600.4 [.] 0x0000000000049515 0.00% 0.00% 0.00% 0.06% swapper [kernel.kallsyms] [k] hrtimer_start_range_ns 1.56% 0.03% 0.00% 0.04% gsd-color libglib-2.0.so.0.5600.4 [.] 0x00000000000494b7 0.00% 0.15% 0.00% 0.04% perf [kernel.kallsyms] [k] smp_call_function_single 0.00% 0.00% 0.00% 0.02% mgen [kernel.kallsyms] [k] update_curr 0.00% 0.00% 0.00% 0.02% mgen [kernel.kallsyms] [k] apic_timer_interrupt 0.00% 0.00% 0.00% 0.02% mgen [kernel.kallsyms] [k] native_apic_msr_eoi_write 0.00% 0.00% 0.00% 0.02% mgen [kernel.kallsyms] [k] __update_load_avg_se 0.00% 0.00% 0.00% 0.02% mgen [kernel.kallsyms] [k] scheduler_tick Now the output is sorted by the fourth event in group. v7: --- Rebase to latest perf/core, no other change. v4: --- 1. Update Documentation/perf-report.txt to mention '--group-sort-idx' support multiple groups with different amount of events and it should be used on grouped events. 2. Update __hpp__group_sort_idx(), just return when the idx is out of limit. 3. Return failure on symbol_conf.group_sort_idx && !session->evlist->nr_groups. So now we don't need to use together with --group. v3: --- Refine the code in __hpp__group_sort_idx(). Before: for (i = 1; i < nr_members; i++) { if (i == idx) { ret = field_cmp(fields_a[i], fields_b[i]); if (ret) goto out; } } After: if (idx >= 1 && idx < nr_members) { ret = field_cmp(fields_a[idx], fields_b[idx]); if (ret) goto out; } Signed-off-by: Jin Yao <yao.jin@linux.intel.com> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Acked-by: Jiri Olsa <jolsa@kernel.org> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lore.kernel.org/lkml/20200220013616.19916-2-yao.jin@linux.intel.com [ Renamed pair_fields_alloc() to hist_entry__new_pair() and combined decl + assignment of vars ] Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent ec0479a commit 429a5f9

4 files changed

Lines changed: 94 additions & 15 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,11 @@ OPTIONS
377377
Show event group information together. It forces group output also
378378
if there are no groups defined in data file.
379379

380+
--group-sort-idx::
381+
Sort the output by the event at the index n in group. If n is invalid,
382+
sort by the first event. It can support multiple groups with different
383+
amount of events. WARNING: This should be used on grouped events.
384+
380385
--demangle::
381386
Demangle symbol names to human readable form. It's enabled by default,
382387
disable with --no-demangle.

‎tools/perf/builtin-report.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,10 @@ int cmd_report(int argc, const char **argv)
12271227
"Show a column with the sum of periods"),
12281228
OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group, &report.group_set,
12291229
"Show event group information together"),
1230+
OPT_INTEGER(0, "group-sort-idx", &symbol_conf.group_sort_idx,
1231+
"Sort the output by the event at the index n in group. "
1232+
"If n is invalid, sort by the first event. "
1233+
"WARNING: should be used on grouped events."),
12301234
OPT_CALLBACK_NOOPT('b', "branch-stack", &branch_mode, "",
12311235
"use branch records for per branch histogram filling",
12321236
parse_branch_mode),
@@ -1369,6 +1373,12 @@ int cmd_report(int argc, const char **argv)
13691373

13701374
setup_forced_leader(&report, session->evlist);
13711375

1376+
if (symbol_conf.group_sort_idx && !session->evlist->nr_groups) {
1377+
parse_options_usage(NULL, options, "group-sort-idx", 0);
1378+
ret = -EINVAL;
1379+
goto error;
1380+
}
1381+
13721382
if (itrace_synth_opts.last_branch)
13731383
has_br_stack = true;
13741384

‎tools/perf/ui/hist.c‎

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,90 @@ static int field_cmp(u64 field_a, u64 field_b)
151151
return 0;
152152
}
153153

154+
static int hist_entry__new_pair(struct hist_entry *a, struct hist_entry *b,
155+
hpp_field_fn get_field, int nr_members,
156+
u64 **fields_a, u64 **fields_b)
157+
{
158+
u64 *fa = calloc(nr_members, sizeof(*fa)),
159+
*fb = calloc(nr_members, sizeof(*fb));
160+
struct hist_entry *pair;
161+
162+
if (!fa || !fb)
163+
goto out_free;
164+
165+
list_for_each_entry(pair, &a->pairs.head, pairs.node) {
166+
struct evsel *evsel = hists_to_evsel(pair->hists);
167+
fa[perf_evsel__group_idx(evsel)] = get_field(pair);
168+
}
169+
170+
list_for_each_entry(pair, &b->pairs.head, pairs.node) {
171+
struct evsel *evsel = hists_to_evsel(pair->hists);
172+
fb[perf_evsel__group_idx(evsel)] = get_field(pair);
173+
}
174+
175+
*fields_a = fa;
176+
*fields_b = fb;
177+
return 0;
178+
out_free:
179+
free(fa);
180+
free(fb);
181+
*fields_a = *fields_b = NULL;
182+
return -1;
183+
}
184+
185+
static int __hpp__group_sort_idx(struct hist_entry *a, struct hist_entry *b,
186+
hpp_field_fn get_field, int idx)
187+
{
188+
struct evsel *evsel = hists_to_evsel(a->hists);
189+
u64 *fields_a, *fields_b;
190+
int cmp, nr_members, ret, i;
191+
192+
cmp = field_cmp(get_field(a), get_field(b));
193+
if (!perf_evsel__is_group_event(evsel))
194+
return cmp;
195+
196+
nr_members = evsel->core.nr_members;
197+
if (idx < 1 || idx >= nr_members)
198+
return cmp;
199+
200+
ret = hist_entry__new_pair(a, b, get_field, nr_members, &fields_a, &fields_b);
201+
if (ret) {
202+
ret = cmp;
203+
goto out;
204+
}
205+
206+
ret = field_cmp(fields_a[idx], fields_b[idx]);
207+
if (ret)
208+
goto out;
209+
210+
for (i = 1; i < nr_members; i++) {
211+
if (i != idx) {
212+
ret = field_cmp(fields_a[i], fields_b[i]);
213+
if (ret)
214+
goto out;
215+
}
216+
}
217+
218+
out:
219+
free(fields_a);
220+
free(fields_b);
221+
222+
return ret;
223+
}
224+
154225
static int __hpp__sort(struct hist_entry *a, struct hist_entry *b,
155226
hpp_field_fn get_field)
156227
{
157228
s64 ret;
158229
int i, nr_members;
159230
struct evsel *evsel;
160-
struct hist_entry *pair;
161231
u64 *fields_a, *fields_b;
162232

233+
if (symbol_conf.group_sort_idx && symbol_conf.event_group) {
234+
return __hpp__group_sort_idx(a, b, get_field,
235+
symbol_conf.group_sort_idx);
236+
}
237+
163238
ret = field_cmp(get_field(a), get_field(b));
164239
if (ret || !symbol_conf.event_group)
165240
return ret;
@@ -169,22 +244,10 @@ static int __hpp__sort(struct hist_entry *a, struct hist_entry *b,
169244
return ret;
170245

171246
nr_members = evsel->core.nr_members;
172-
fields_a = calloc(nr_members, sizeof(*fields_a));
173-
fields_b = calloc(nr_members, sizeof(*fields_b));
174-
175-
if (!fields_a || !fields_b)
247+
i = hist_entry__new_pair(a, b, get_field, nr_members, &fields_a, &fields_b);
248+
if (i)
176249
goto out;
177250

178-
list_for_each_entry(pair, &a->pairs.head, pairs.node) {
179-
evsel = hists_to_evsel(pair->hists);
180-
fields_a[perf_evsel__group_idx(evsel)] = get_field(pair);
181-
}
182-
183-
list_for_each_entry(pair, &b->pairs.head, pairs.node) {
184-
evsel = hists_to_evsel(pair->hists);
185-
fields_b[perf_evsel__group_idx(evsel)] = get_field(pair);
186-
}
187-
188251
for (i = 1; i < nr_members; i++) {
189252
ret = field_cmp(fields_a[i], fields_b[i]);
190253
if (ret)

‎tools/perf/util/symbol_conf.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ struct symbol_conf {
7373
const char *symfs;
7474
int res_sample;
7575
int pad_output_len_dso;
76+
int group_sort_idx;
7677
};
7778

7879
extern struct symbol_conf symbol_conf;

0 commit comments

Comments
 (0)