[PATCH v10 03/16] perf annotate: Print the stats after the annotations
From: Arnaldo Carvalho de Melo
Date: Fri Nov 20 2026 - 22:21:20 EST
From: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
With --code-with-type the instruction stats are printed from
hists__find_annotations() before that event is annotated: the first
event reports zeros and later events can report stale counts. The data
type total is only counted in --data-type mode, so --type-stat without
it is incomplete too.
Print the stats once after all events have been annotated, warning if
--type-stat is used without --data-type. Leave the TUI's alternate
screen before printing, so the stats remain on the terminal.
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 | 19 ++++++++++++++-----
2 files changed, 15 insertions(+), 6 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..e768f0fd8d4813de 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;
@@ -663,6 +658,17 @@ static int __cmd_annotate(struct perf_annotate *ann)
hists__find_annotations(hists, pos, ann);
}
+ if (use_browser == 1 && (ann->insn_stat || (ann->data_type && ann->type_stat))) {
+ /* Leave the alternate screen before printing to the terminal. */
+ ui__exit(false);
+ use_browser = 0;
+ }
+
+ 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");
+
if (use_browser == 2) {
void (*show_annotations)(void);
@@ -868,6 +874,9 @@ int cmd_annotate(int argc, const char **argv)
if (quiet)
perf_quiet_option();
+ if (annotate.type_stat && !annotate.data_type)
+ pr_warning("Warning: --type-stat needs --data-type\n");
+
data.path = input_name;
perf_tool__init(&annotate.tool, /*ordered_events=*/true);
--
2.56.0