[PATCH v13 03/19] perf annotate: Print the stats where the counters are final
From: Arnaldo Carvalho de Melo
Date: Wed Nov 25 2026 - 14:23:33 EST
From: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
The stats are printed from hists__find_annotations(), before that event
is annotated: with --code-with-type the first event reports zeros,
because those types are resolved while printing, and a later event can
report a total that is not its own. The data type total is only
counted in --data-type mode, so --type-stat without it is incomplete
too.
The data type pass resolves the samples while the histograms are being
resorted, so in stdio without --code-with-type the counters are final
before any annotation is printed. Print the stats once there, before
the annotations, so that the quality can be checked without scrolling,
and once after them when --code-with-type or a browser is used, where
the counters only reach their final values during the output. Leave
the TUI's alternate screen before printing, so the stats remain on the
terminal. Warn if --type-stat is used without --data-type.
Fixes: 58824fa0087e1cb7 ("perf annotate: Add --insn-stat option for debugging")
Fixes: 61a9741e9f78c64c ("perf annotate: Add --type-stat option for debugging")
Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
---
tools/perf/Documentation/perf-annotate.txt | 2 +-
tools/perf/builtin-annotate.c | 68 +++++++++++++++++-----
2 files changed, 53 insertions(+), 17 deletions(-)
diff --git a/tools/perf/Documentation/perf-annotate.txt b/tools/perf/Documentation/perf-annotate.txt
index 1a90b09a12d5abb1..d27eff28ba4f6af0 100644
--- a/tools/perf/Documentation/perf-annotate.txt
+++ b/tools/perf/Documentation/perf-annotate.txt
@@ -169,7 +169,7 @@ include::itrace.txt[]
all data types it finds.
--type-stat::
- Show stats for the data type annotation.
+ Show stats for the data type annotation (with --data-type).
--skip-empty::
Do not display empty (or dummy) events.
diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index 7cb7e03d9a21d81b..52fbb88a6b63bd83 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -414,11 +414,6 @@ static void hists__find_annotations(struct hists *hists,
struct rb_node *nd = rb_first_cached(&hists->entries), *next;
int key = K_RIGHT;
- if (ann->type_stat)
- print_annotate_data_stat(&ann_data_stat);
- if (ann->insn_stat)
- print_annotate_item_stat(&ann_insn_stat, "Instruction");
-
while (nd) {
struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
struct annotation *notes;
@@ -552,12 +547,25 @@ static void hists__find_annotations(struct hists *hists,
}
}
+static void print_annotation_stats(struct perf_annotate *ann)
+{
+ /* pr_warning reaches the terminal: the browser mode has exited. */
+ if (ann->type_stat && !ann->data_type)
+ pr_warning("Warning: --type-stat needs --data-type\n");
+
+ if (ann->data_type && ann->type_stat)
+ print_annotate_data_stat(&ann_data_stat);
+ if (ann->insn_stat)
+ print_annotate_item_stat(&ann_insn_stat, "Instruction");
+}
+
static int __cmd_annotate(struct perf_annotate *ann)
{
int ret;
struct perf_session *session = ann->session;
struct evsel *pos;
u64 total_nr_samples;
+ bool stats_first;
if (ann->cpu_list) {
ret = perf_session__cpu_bitmap(session, ann->cpu_list,
@@ -619,18 +627,14 @@ static int __cmd_annotate(struct perf_annotate *ann)
* An event group needs to display other events too.
* Let's delay printing until other events are processed.
*/
- if (symbol_conf.event_group) {
- if (!evsel__is_group_leader(pos)) {
- struct hists *leader_hists;
-
- leader_hists = evsel__hists(evsel__leader(pos));
- hists__match(leader_hists, hists);
- hists__link(leader_hists, hists);
- }
- continue;
- }
+ if (symbol_conf.event_group &&
+ !evsel__is_group_leader(pos)) {
+ struct hists *leader_hists;
- hists__find_annotations(hists, pos, ann);
+ leader_hists = evsel__hists(evsel__leader(pos));
+ hists__match(leader_hists, hists);
+ hists__link(leader_hists, hists);
+ }
}
}
@@ -639,6 +643,29 @@ static int __cmd_annotate(struct perf_annotate *ann)
goto out;
}
+ /*
+ * The data type pass resolves the samples while the histograms are
+ * being resorted, so with nothing left to resolve during the
+ * annotation print the stats are complete here: show them before
+ * the annotations, where judging quality needs no scrolling. With
+ * --code-with-type and in a browser the types are resolved while
+ * printing, there the stats go after the annotations, once the
+ * totals are final.
+ */
+ stats_first = use_browser == 0 && !annotate_opts.code_with_type;
+ if (stats_first)
+ print_annotation_stats(ann);
+
+ /* Standalone events, group leaders are printed together below */
+ evlist__for_each_entry(session->evlist, pos) {
+ struct hists *hists = evsel__hists(pos);
+
+ if (symbol_conf.event_group || hists->stats.nr_samples == 0)
+ continue;
+
+ hists__find_annotations(hists, pos, ann);
+ }
+
/* Display group events together */
evlist__for_each_entry(session->evlist, pos) {
struct hists *hists = evsel__hists(pos);
@@ -663,6 +690,15 @@ static int __cmd_annotate(struct perf_annotate *ann)
hists__find_annotations(hists, pos, ann);
}
+ if (!stats_first && (ann->insn_stat || ann->type_stat)) {
+ if (use_browser == 1) {
+ /* Leave the alternate screen before printing to the terminal. */
+ ui__exit(false);
+ use_browser = 0;
+ }
+ print_annotation_stats(ann);
+ }
+
if (use_browser == 2) {
void (*show_annotations)(void);
--
2.56.0