Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf annotate: Add --skip-empty option

Like in 'perf report', we want to hide empty events in the 'perf annotate'
output. This is consistent when the option is set in perf report.

For example, the following command would use 3 events including dummy.

$ perf mem record -a -- perf test -w noploop

$ perf evlist
cpu/mem-loads,ldlat=30/P
cpu/mem-stores/P
dummy:u

Just using perf annotate with --group will show the all 3 events.

$ perf annotate --group --stdio | head
Percent | Source code & Disassembly of ...
--------------------------------------------------------------
: 0 0xe060 <_dl_relocate_object>:
0.00 0.00 0.00 : e060: pushq %rbp
0.00 0.00 0.00 : e061: movq %rsp, %rbp
0.00 0.00 0.00 : e064: pushq %r15
0.00 0.00 0.00 : e066: movq %rdi, %r15
0.00 0.00 0.00 : e069: pushq %r14
0.00 0.00 0.00 : e06b: pushq %r13
0.00 0.00 0.00 : e06d: movl %edx, %r13d

Now with --skip-empty, it'll hide the last dummy event.

$ perf annotate --group --stdio --skip-empty | head
Percent | Source code & Disassembly of ...
------------------------------------------------------
: 0 0xe060 <_dl_relocate_object>:
0.00 0.00 : e060: pushq %rbp
0.00 0.00 : e061: movq %rsp, %rbp
0.00 0.00 : e064: pushq %r15
0.00 0.00 : e066: movq %rdi, %r15
0.00 0.00 : e069: pushq %r14
0.00 0.00 : e06b: pushq %r13
0.00 0.00 : e06d: movl %edx, %r13d

Committer testing:

root@x1:~# perf evlist
cpu_atom/mem-loads,ldlat=30/P
cpu_atom/mem-stores/P
dummy:u
root@x1:~#

Before:

root@x1:~# perf annotate --group --stdio2 do_lookup_x | head -25
Samples: 20 of events 'cpu_atom/mem-loads,ldlat=30/P, cpu_atom/mem-stores/P, dummy:u', 4000 Hz, Event count (approx.): 769079, [percent: local period]
do_lookup_x() /usr/lib64/ld-linux-x86-64.so.2
Percent 0x9900 <do_lookup_x>:
pushq %rbp
movq %rsp,%rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88,%rsp
movq %rdi,-0x50(%rbp)
movl 8(%r9),%edi
movq 0x10(%rbp),%r12
movq 0x28(%rbp),%r10
movq %rdx,-0x70(%rbp)
movq %rcx,-0x58(%rbp)
movq %rdi,%r11
0.00 5.73 0.00 movq %r8,-0x68(%rbp)
movq (%r9),%r8
movl %esi,%eax
8.30 0.00 0.00 movl 0x30(%rbp),%r9d
movl %esi,%r15d
shrl $6, %eax
movq %r8,%r13
root@x1:~#

After:

root@x1:~# perf annotate --group --skip-empty --stdio2 do_lookup_x | head -25
Samples: 20 of events 'cpu_atom/mem-loads,ldlat=30/P, cpu_atom/mem-stores/P', 4000 Hz, Event count (approx.): 769079, [percent: local period]
do_lookup_x() /usr/lib64/ld-linux-x86-64.so.2
Percent 0x9900 <do_lookup_x>:
pushq %rbp
movq %rsp,%rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88,%rsp
movq %rdi,-0x50(%rbp)
movl 8(%r9),%edi
movq 0x10(%rbp),%r12
movq 0x28(%rbp),%r10
movq %rdx,-0x70(%rbp)
movq %rcx,-0x58(%rbp)
movq %rdi,%r11
0.00 5.73 movq %r8,-0x68(%rbp)
movq (%r9),%r8
movl %esi,%eax
8.30 0.00 movl 0x30(%rbp),%r9d
movl %esi,%r15d
shrl $6, %eax
movq %r8,%r13
root@x1:~#

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240803211332.1107222-6-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Namhyung Kim and committed by
Arnaldo Carvalho de Melo
ce533c9b bb588e38

+22 -5
+3
tools/perf/Documentation/perf-annotate.txt
··· 165 165 --type-stat:: 166 166 Show stats for the data type annotation. 167 167 168 + --skip-empty:: 169 + Do not display empty (or dummy) events. 170 + 168 171 169 172 SEE ALSO 170 173 --------
+2
tools/perf/builtin-annotate.c
··· 795 795 "Show stats for the data type annotation"), 796 796 OPT_BOOLEAN(0, "insn-stat", &annotate.insn_stat, 797 797 "Show instruction stats for the data type annotation"), 798 + OPT_BOOLEAN(0, "skip-empty", &symbol_conf.skip_empty, 799 + "Do not display empty (or dummy) events in the output"), 798 800 OPT_END() 799 801 }; 800 802 int ret;
+17 -5
tools/perf/util/annotate.c
··· 848 848 849 849 BUG_ON(i >= al->data_nr); 850 850 851 + if (symbol_conf.skip_empty && 852 + evsel__hists(evsel)->stats.nr_samples == 0) 853 + continue; 854 + 851 855 data = &al->data[i++]; 852 856 853 857 calc_percent(notes, evsel, data, al->offset, end); ··· 905 901 .options = &annotate_opts, 906 902 }; 907 903 struct arch *arch = NULL; 908 - int err; 904 + int err, nr; 909 905 910 906 err = evsel__get_arch(evsel, &arch); 911 907 if (err < 0) ··· 926 922 return -1; 927 923 } 928 924 929 - if (evsel__is_group_event(evsel)) 930 - notes->src->nr_events = evsel->core.nr_members; 931 - else 932 - notes->src->nr_events = 1; 925 + nr = 0; 926 + if (evsel__is_group_event(evsel)) { 927 + struct evsel *pos; 928 + 929 + for_each_group_evsel(pos, evsel) { 930 + if (symbol_conf.skip_empty && 931 + evsel__hists(pos)->stats.nr_samples == 0) 932 + continue; 933 + nr++; 934 + } 935 + } 936 + notes->src->nr_events = nr ? nr : 1; 933 937 934 938 if (annotate_opts.full_addr) 935 939 notes->src->start = map__objdump_2mem(ms->map, ms->sym->start);