Skip to contentSkip to conversation

Updated patchkit to show weights in annotation

PATCH Superseded Andi Kleen 3 participants 12 messages Aug 13 – Aug 15 perf events

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

Sashiko actually found some real bugs in its v1 review, this version has them all fixed.

author
#1 lore raw

PATCH v2 1/4 perf tools record: Modernize -W man page

Modernize the -W / --weight description in the manpage to cover more cases that are supported now.

---
 tools/perf/Documentation/perf-record.txt | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/perf/Documentation/perf-record.txt +5 -3
index 178f483140ed..2c6510c5a87b 100644
@@ -480,9 +480,11 @@ Note that this feature may not be available on all processors.
 
 -W::
 --weight::
-Enable weightened sampling. An additional weight is recorded per sample and can be
-displayed with the weight and local_weight sort keys.  This currently works for TSX
-abort events and some memory events in precise mode on modern Intel CPUs.
+Enable weighted sampling. One or more additional weights can be recorded per sample
+and can be displayed with the weight, weight2, weight3 and local_weight sort keys.
+Possible uses for weights are memory or instruction retirement latencies.
+Support depends on the PMU and the event, as well as its configuration. Typically
+it requires use of 'p'.
 
 --namespaces::
 Record events of type PERF_RECORD_NAMESPACES.  This enables 'cgroup_id' sort key.
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH v2 1/4] perf tools record: Modernize -W man page

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines

Modernize the -W / --weight description in the manpage to cover more cases that are supported now.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#3 lore raw

PATCH v2 2/4 perf tools top: Add --weight option

In reply to Andi Kleen

Add a -W/--weight option to perf top to collect weights too. Useful with follow on patches.

Assisted-by omp:GPT-5.6-Luna
---
 tools/perf/Documentation/perf-top.txt | 6 ++++++
 tools/perf/builtin-top.c              | 2 ++
 2 files changed, 8 insertions(+)
tools/perf/Documentation/perf-top.txt +6 -0
index af3e4230c72f..2da2a16bbf26 100644
@@ -25,6 +25,12 @@ OPTIONS
 --count=<count>::
 	Event period to sample.
 
+-W::
+--weight::
+Enable weighted sampling. One or more additional weights can be recorded per sample
+and can be displayed with the weight, weight2, weight3 and local_weight sort keys.
+Possible uses for weights are memory or instruction retirement latencies.
+
 -C <cpu-list>::
 --cpu=<cpu>::
 Monitor only on the list of CPUs provided. Multiple CPUs can be provided as a
tools/perf/builtin-top.c +2 -0
index 570410599f1b..2888ac3b7b92 100644
@@ -1493,6 +1493,8 @@ int cmd_top(int argc, const char **argv)
 	OPT_CALLBACK(0, "filter", &top.evlist, "filter",
 		     "event filter", parse_filter),
 	OPT_U64('c', "count", &opts->user_interval, "event period to sample"),
+	OPT_BOOLEAN('W', "weight", &opts->sample_weight,
+		    "sample by weight (on special events only)"),
 	OPT_STRING('p', "pid", &target->pid, "pid",
 		    "profile events on existing process id"),
 	OPT_STRING('t', "tid", &target->tid, "tid",
Signature · 1 line
2.55.0
#4 lore raw

Re: [PATCH v2 2/4] perf tools top: Add --weight option

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines

Add a -W/--weight option to perf top to collect weights too. Useful with follow on patches.

Assisted-by omp:GPT-5.6-Luna

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author
#5 lore raw

PATCH v2 3/4 perf tools: Add support for displaying weights in annotate

In reply to Andi Kleen

Add support for showing all the three possible per IP weights in annotate. The weights are shown by defaults if any are non zero. This is useful, especially with the new insn lat statistics, but also for all the existing weights.

Add a hotkey to the interactive browser to turn them off (w), as well as a perf annotate command line option.

The weights are stored unconditionally in the sym_hist_entry, which will increase memory consumption somewhat.

Assisted-by omp:GPT-5.6-Luna
---
 tools/perf/Documentation/perf-annotate.txt |   3 +
 tools/perf/builtin-annotate.c              |   3 +
 tools/perf/builtin-report.c                |   1 +
 tools/perf/ui/browsers/annotate.c          |  17 ++-
 tools/perf/util/annotate.c                 | 117 ++++++++++++++++++---
 tools/perf/util/annotate.h                 |  32 +++++-
 tools/perf/util/symbol.c                   |   1 +
 tools/perf/util/symbol_conf.h              |  17 ++-
 8 files changed, 170 insertions(+), 21 deletions(-)
tools/perf/Documentation/perf-annotate.txt +3 -0
index a688738809c4..50c6ea680d0d 100644
@@ -79,6 +79,9 @@ include::itrace.txt[]
 
 --stdio:: Use the stdio interface.
 
+--no-weights:: Hide weight columns in annotation output. By default, weight columns
+	are shown when samples contain non-zero weights.
+
 --stdio2:: Use the stdio2 interface, non-interactive, uses the TUI formatting.
 
 --stdio-color=<mode>::
tools/perf/builtin-annotate.c +3 -0
index 69cb72b2082a..c6c481145e45 100644
@@ -259,6 +259,7 @@ static int add_sample(struct perf_sample *sample,
 		return 0;
 	}
 
+
 	/*
 	 * XXX filtered samples can still have branch entries pointing into our
 	 * symbol and are missed.
@@ -719,6 +720,8 @@ int cmd_annotate(int argc, const char **argv)
 	OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
 #endif
 	OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
+	OPT_BOOLEAN(0, "no-weights", &symbol_conf.hide_weight,
+		    "Hide weight columns in annotation"),
 	OPT_BOOLEAN(0, "stdio2", &annotate.use_stdio2, "Use the stdio interface"),
 	OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
                     "don't load vmlinux even if found"),
tools/perf/builtin-report.c +1 -0
index 60d1f166629e..921aa03093f3 100644
@@ -329,6 +329,7 @@ static int process_sample_event(const struct perf_tool *tool,
 				     &rep->total_cycles);
 	}
 
+
 	rep->total_samples++;
 	if (al.parallelism == 1)
 		rep->singlethreaded_samples++;
tools/perf/ui/browsers/annotate.c +14 -3
index e47a46775089..d6126a7d51e4 100644
@@ -189,7 +189,7 @@ static void annotate_browser__draw_current_jump(struct ui_browser *browser)
 	struct map_symbol *ms = ab->b.priv;
 	struct symbol *sym = ms->sym;
 	struct annotation *notes = symbol__annotation(sym);
-	u8 pcnt_width = annotation__pcnt_width(notes);
+	u8 pcnt_width = annotation__pcnt_width(notes, ab->evsel);
 	u8 cntr_width = annotation__br_cntr_width();
 	int width;
 	int diff = 0;
@@ -255,7 +255,8 @@ static unsigned int annotate_browser__refresh(struct ui_browser *browser)
 {
 	struct annotation *notes = browser__annotation(browser);
 	int ret = ui_browser__list_head_refresh(browser);
-	int pcnt_width = annotation__pcnt_width(notes);
+	int pcnt_width = annotation__pcnt_width(notes,
+		container_of(browser, struct annotate_browser, b)->evsel);
 
 	if (annotate_opts.jump_arrows)
 		annotate_browser__draw_current_jump(browser);
@@ -972,6 +973,7 @@ static int annotate_browser__run(struct annotate_browser *browser,
 		"O             Bump offset level (jump targets -> +call -> all -> cycle thru)\n"
 		"s             Toggle source code view\n"
 		"t             Circulate percent, total period, samples view\n"
+		"w             Toggle weight columns\n"
 		"c             Show min/max cycle\n"
 		"/             Search string\n"
 		"k             Toggle line numbers\n"
@@ -1091,6 +1093,15 @@ static int annotate_browser__run(struct annotate_browser *browser,
 				symbol_conf.show_total_period = true;
 			annotation__update_column_widths(notes);
 			continue;
+		case 'w':
+			symbol_conf.hide_weight = !symbol_conf.hide_weight;
+			symbol_conf.weight_mask_user = true;
+			symbol_conf.weight_mask = symbol_conf.hide_weight ?
+				0 : sym_hist__weight_mask(annotation__histogram(notes, evsel));
+			browser->b.width = notes->src->widths.max_line_len +
+					   annotation__pcnt_width(notes, evsel);
+			ui_browser__refresh_dimensions(&browser->b);
+			continue;
 		case 'c':
 			if (annotate_opts.show_minmax_cycle)
 				annotate_opts.show_minmax_cycle = false;
@@ -1228,7 +1239,7 @@ int __hist_entry__tui_annotate(struct hist_entry *he, struct map_symbol *ms,
 	browser.b.width = notes->src->widths.max_line_len;
 	browser.b.nr_entries = notes->src->nr_entries;
 	browser.b.entries = &notes->src->source;
-	browser.b.width += 18; /* Percentage */
+	browser.b.width += annotation__pcnt_width(notes, evsel);
 
 	if (annotate_opts.hide_src_code)
 		ui_browser__init_asm_mode(&browser.b);
tools/perf/util/annotate.c +102 -15

230 lines not on this page · raw message

tools/perf/util/annotate.h +30 -2
index fa08d09b80f7..63bce7a920f3 100644
@@ -6,6 +6,7 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <linux/types.h>
+#include <linux/bitops.h>
 #include <linux/list.h>
 #include <linux/rbtree.h>
 #include <asm/bug.h>
@@ -86,6 +87,8 @@ struct annotation;
 struct sym_hist_entry {
 	u64		nr_samples;
 	u64		period;
+	u64		weight_sum[WEIGHT_WEIGHT3 + 1];
+	u64		weight_num[WEIGHT_WEIGHT3 + 1];
 };
 
 enum {
@@ -231,8 +234,20 @@ void symbol__calc_percent(struct symbol *sym, struct evsel *evsel);
 struct sym_hist {
 	u64		      nr_samples;
 	u64		      period;
+	u8		      weight_mask;
 };
 
+/* Can be set asynchronously by top. */
+static inline u8 sym_hist__weight_mask(const struct sym_hist *hist)
+{
+	return __atomic_load_n(&hist->weight_mask, __ATOMIC_RELAXED);
+}
+
+static inline void sym_hist__set_weight_mask(struct sym_hist *hist, u8 mask)
+{
+	__atomic_fetch_or(&hist->weight_mask, mask, __ATOMIC_RELAXED);
+}
+
 /**
  * struct cyc_hist - (CPU) cycle histogram for a basic block
  *
@@ -376,9 +391,22 @@ static inline int annotation__cycles_width(struct annotation *notes)
 	return notes->branch ? ANNOTATION__IPC_WIDTH + ANNOTATION__CYCLES_WIDTH : 0;
 }
 
-static inline int annotation__pcnt_width(struct annotation *notes)
+static inline u8 annotation__weight_mask(struct annotation *notes,
+					 const struct evsel *evsel)
+{
+	if (symbol_conf.weight_mask_user)
+		return symbol_conf.weight_mask;
+	if (symbol_conf.hide_weight)
+		return 0;
+	return sym_hist__weight_mask(&notes->src->histograms[evsel->core.idx]);
+}
+
+static inline int annotation__pcnt_width(struct annotation *notes,
+					 const struct evsel *evsel)
 {
-	return (symbol_conf.show_total_period ? 12 : 8) * notes->src->nr_events;
+	int extra = hweight8(annotation__weight_mask(notes, evsel)) * 8;
+	return ((symbol_conf.show_total_period ? 12 : 8) + extra) *
+	       notes->src->nr_events;
 }
 
 static inline bool annotation_line__filter(struct annotation_line *al)
tools/perf/util/symbol.c +1 -0
index 94f9c8faedda..92dcce617de2 100644
@@ -2763,3 +2763,4 @@ char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
 
 	return java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET);
 }
+
tools/perf/util/symbol_conf.h +16 -1
index 0dee5aa6a534..8b074121b680 100644
@@ -27,6 +27,16 @@ enum a2l_style {
 };
 #define MAX_A2L_STYLE (A2L_STYLE_CMD + 1)
 
+enum symbol__weight_mode {
+	WEIGHT_NONE = 0,
+	WEIGHT_WEIGHT,
+	WEIGHT_INSNLAT,
+	WEIGHT_WEIGHT3,
+};
+
+#define for_each_weight(_weight) \
+	for ((_weight) = WEIGHT_WEIGHT; (_weight) <= WEIGHT_WEIGHT3; (_weight)++)
+
 struct symbol_conf {
 	bool		nanosecs;
 	unsigned short	priv_size;
@@ -69,7 +79,10 @@ struct symbol_conf {
 			annotate_data_sample,
 			skip_empty,
 			enable_latency,
-			prefer_latency;
+			prefer_latency,
+			hide_weight,
+			weight_mask_user;
+	u8		weight_mask;
 	const char	*vmlinux_name,
 			*kallsyms_name,
 			*source_prefix,
@@ -114,4 +127,6 @@ struct symbol_conf {
 
 extern struct symbol_conf symbol_conf;
 
+struct perf_sample;
+
 #endif // __PERF_SYMBOL_CONF
Signature · 1 line
2.55.0
#6 lore raw

Re: [PATCH v2 3/4] perf tools: Add support for displaying weights in annotate

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Medium] Toggling the 'w' hotkey caches the current event's weight mask globally, corrupting weight column display for all subsequent symbols and events.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but there is an out-of-bounds heap memory write in symbol__annotate_zero_histogram() when zeroing an event's histogram. --

--- Patch [3]: [PATCH v2 3/4] perf tools: Add support for displaying weights in annotate --- commit 89fdbcaac6998b08ac6d1c7549a509183b024849
Author: Andi Kleen <[email protected]>

perf tools: Add support for displaying weights in annotate

Add support for showing all the three possible per IP weights in annotate. The weights are shown by defaults if any are non zero. This is useful, especially with the new insn lat statistics, but also for all the existing weights.

Quoted 4 lines
tools/perf/ui/browsers/annotate.c +0 -0
index e47a467750890..d6126a7d51e4a 100644

[ ... ]

Quoted 13 lines
tools/perf/ui/browsers/annotate.c +9 -0
@@ -1091,6 +1093,15 @@ static int annotate_browser__run(struct annotate_browser *browser,
 				symbol_conf.show_total_period = true;
 			annotation__update_column_widths(notes);
 			continue;
+		case 'w':
+			symbol_conf.hide_weight = !symbol_conf.hide_weight;
+			symbol_conf.weight_mask_user = true;
+			symbol_conf.weight_mask = symbol_conf.hide_weight ?
+				0 : sym_hist__weight_mask(annotation__histogram(notes, evsel));
+			browser->b.width = notes->src->widths.max_line_len +
+					   annotation__pcnt_width(notes, evsel);
+			ui_browser__refresh_dimensions(&browser->b);
+			continue;

[ ... ]

Quoted 4 lines
tools/perf/util/annotate.h +0 -0
index fa08d09b80f76..63bce7a920f32 100644

[ ... ]

tools/perf/util/annotate.h static inline int annotation__cycles_width(struct annotation *notes) · line 398

Quoted 10 lines
tools/perf/util/annotate.h +5 -1
@@ -376,9 +391,22 @@ static inline int annotation__cycles_width(struct annotation *notes)
 	return notes->branch ? ANNOTATION__IPC_WIDTH + ANNOTATION__CYCLES_WIDTH : 0;
 }
 
-static inline int annotation__pcnt_width(struct annotation *notes)
+static inline u8 annotation__weight_mask(struct annotation *notes,
+					 const struct evsel *evsel)
+{
+	if (symbol_conf.weight_mask_user)
+		return symbol_conf.weight_mask;

[Severity: Medium]
Will this globally cache the current event's dynamically generated weight mask?

In annotate_browser__run(), pressing 'w' sets symbol_conf.weight_mask_user to true and saves the mask. Returning that cached mask here seems to force all subsequently viewed symbols and events to use this exact column layout.

If a different event has different weight types, or if in perf top new weight types arrive after 'w' is pressed, would the UI incorrectly hide valid weight columns or display empty zero-filled columns for missing weights?

Quoted 4 lines
tools/perf/util/annotate.h +4 -0
+	if (symbol_conf.hide_weight)
+		return 0;
+	return sym_hist__weight_mask(&notes->src->histograms[evsel->core.idx]);
+}

[ ... ]

Quoted 4 lines
tools/perf/util/annotate.c +0 -0
index df70e95a84704..b636f54675a1e 100644

[ ... ]

tools/perf/util/annotate.c int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel, · line 1530

Quoted 10 lines
tools/perf/util/annotate.c +2 -1
@@ -1448,7 +1523,8 @@ int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel,
 	return err;
 }
 
-void symbol__annotate_zero_histogram(struct symbol *sym, struct evsel *evsel)
+void symbol__annotate_zero_histogram(struct symbol *sym,
+				     struct evsel *evsel)
 {
 	struct annotation *notes = symbol__annotation(sym);
 	struct sym_hist *h = annotation__histogram(notes, evsel);

[Severity: High]
This is a pre-existing issue, but does the memset in this function cause an out-of-bounds heap memory write?

When profiling multiple events (e.g. perf top -e cycles,instructions) and zeroing the histogram (e.g. pressing 'z'), this gets called for top->sym_evsel:

void symbol__annotate_zero_histogram(...)

{
	...
	struct sym_hist *h = annotation__histogram(notes, evsel);

	memset(h, 0, sizeof(*notes->src->histograms) * notes->src->nr_histograms);
}

The pointer h is offset to the specific event's index, but the memset zeroes an area equal to the size of the entire histograms array. Could this write past the end of the heap allocation?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer
#7 lore raw

Re: [PATCH v2 3/4] perf tools: Add support for displaying weights in annotate

In reply to Andi Kleen

Hi Andi,

Quoted 51 lines · Andi Kleen

Add support for showing all the three possible per IP weights in annotate. The weights are shown by defaults if any are non zero. This is useful, especially with the new insn lat statistics, but also for all the existing weights.

Add a hotkey to the interactive browser to turn them off (w), as well as a perf annotate command line option.

The weights are stored unconditionally in the sym_hist_entry, which will increase memory consumption somewhat.

Assisted-by omp:GPT-5.6-Luna
---
 tools/perf/Documentation/perf-annotate.txt |   3 +
 tools/perf/builtin-annotate.c              |   3 +
 tools/perf/builtin-report.c                |   1 +
 tools/perf/ui/browsers/annotate.c          |  17 ++-
 tools/perf/util/annotate.c                 | 117 ++++++++++++++++++---
 tools/perf/util/annotate.h                 |  32 +++++-
 tools/perf/util/symbol.c                   |   1 +
 tools/perf/util/symbol_conf.h              |  17 ++-
 8 files changed, 170 insertions(+), 21 deletions(-)
tools/perf/Documentation/perf-annotate.txt +3 -0
index a688738809c4..50c6ea680d0d 100644
@@ -79,6 +79,9 @@ include::itrace.txt[]
 
 --stdio:: Use the stdio interface.
 
+--no-weights:: Hide weight columns in annotation output. By default, weight columns
+	are shown when samples contain non-zero weights.
+
 --stdio2:: Use the stdio2 interface, non-interactive, uses the TUI formatting.
 
 --stdio-color=<mode>::
tools/perf/builtin-annotate.c +1 -0
index 69cb72b2082a..c6c481145e45 100644
@@ -259,6 +259,7 @@ static int add_sample(struct perf_sample *sample,
 		return 0;
 	}
 
+
 	/*
 	 * XXX filtered samples can still have branch entries pointing into our
 	 * symbol and are missed.

tools/perf/builtin-annotate.c int cmd_annotate(int argc, const char **argv) · line 724

Quoted 5 lines
tools/perf/builtin-annotate.c +2 -0
@@ -719,6 +720,8 @@ int cmd_annotate(int argc, const char **argv)
 	OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
 #endif
 	OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
+	OPT_BOOLEAN(0, "no-weights", &symbol_conf.hide_weight,
+		    "Hide weight columns in annotation"),

Can it be just "weights"? Then we would get --no-weights for free. :) Of course, the help message needs to say differently.

Thanks,
Namhyung

Quoted 3 lines
tools/perf/builtin-annotate.c +0 -0
 	OPT_BOOLEAN(0, "stdio2", &annotate.use_stdio2, "Use the stdio interface"),
 	OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
                     "don't load vmlinux even if found"),
author
#8 lore raw

Re: [PATCH v2 3/4] perf tools: Add support for displaying weights in annotate

Quoted 10 lines · Namhyung Kim

tools/perf/builtin-annotate.c int cmd_annotate(int argc, const char **argv) · line 724

Quoted 7 lines
tools/perf/builtin-annotate.c +2 -0
 	 * symbol and are missed.
@@ -719,6 +720,8 @@ int cmd_annotate(int argc, const char **argv)
 	OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
 #endif
 	OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
+	OPT_BOOLEAN(0, "no-weights", &symbol_conf.hide_weight,
+		    "Hide weight columns in annotation"),

Can it be just "weights"? Then we would get --no-weights for free. :) Of course, the help message needs to say differently.

Sure I can rename it.

BTW this was the v2 with the broken white space hunks, please ignore or look at v3.

-Andi

maintainer
#9 lore raw

Re: [PATCH v2 3/4] perf tools: Add support for displaying weights in annotate

Quoted 12 lines · Andi Kleen
Quoted 10 lines · Namhyung Kim

tools/perf/builtin-annotate.c int cmd_annotate(int argc, const char **argv) · line 724

Quoted 7 lines
tools/perf/builtin-annotate.c +2 -0
 	 * symbol and are missed.
@@ -719,6 +720,8 @@ int cmd_annotate(int argc, const char **argv)
 	OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
 #endif
 	OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
+	OPT_BOOLEAN(0, "no-weights", &symbol_conf.hide_weight,
+		    "Hide weight columns in annotation"),

Can it be just "weights"? Then we would get --no-weights for free. :) Of course, the help message needs to say differently.

Quoted 3 lines

Sure I can rename it.

BTW this was the v2 with the broken white space hunks, please ignore or look at v3.

Ok, will do.

Thanks,
Namhyung

author
#10 lore raw

PATCH v2 4/4 perf tools: Add test for weight annotation

In reply to Andi Kleen

Add a test case to check if --stdio weight annotation works

Assisted-by omp:GPT-5.6-Luna
---
 tools/perf/tests/shell/annotate_weight.sh | 63 +++++++++++++++++++++++
 1 file changed, 63 insertions(+)
 create mode 100755 tools/perf/tests/shell/annotate_weight.sh
tools/perf/tests/shell/annotate_weight.sh new file +63 -0
new file mode 100755
index 000000000000..6b8c105c048b
@@ -0,0 +1,63 @@
+#!/bin/bash
+# perf annotate weight regression test
+# SPDX-License-Identifier: GPL-2.0
+
+set -e
+
+shelldir=$(dirname "$0")
+# shellcheck source=tools/perf/tests/shell/lib/perf_has_symbol.sh
+. "${shelldir}"/lib/perf_has_symbol.sh
+
+testsym="test_loop"
+skip_test_missing_symbol "${testsym}"
+
+perfdata=$(mktemp /tmp/__perf_test.annotate_weight.XXXXX)
+record_log=$(mktemp /tmp/__perf_test.annotate_weight.XXXXX.log)
+report_out=$(mktemp /tmp/__perf_test.annotate_weight.XXXXX.report)
+annotate_out=$(mktemp /tmp/__perf_test.annotate_weight.XXXXX.annotate)
+
+cleanup() {
+	rm -f "${perfdata}" "${record_log}" "${report_out}" "${annotate_out}"
+	trap - EXIT TERM INT
+}
+
+trap 'cleanup; exit 1' TERM INT
+trap cleanup EXIT
+
+# mem-loads:pu requests a precise user PEBS event whose sample weight should
+# be populated by -W. Unsupported PEBS/weight PMUs are skipped below.
+if ! perf record -W -e mem-loads:pu -o "${perfdata}" -- perf test -w thloop \
+	> /dev/null 2> "${record_log}"; then
+	echo "[SKIP] precise PEBS weight sampling is unavailable"
+	exit 2
+fi
+
+# Confirm the PMU actually produced nonzero weights. A successful record alone
+# is insufficient: some PMUs accept the event but provide no weight payload.
+if ! perf report --stdio -i "${perfdata}" --fields=weight1,weight2,weight3,symbol --percent-limit 0 \
+	> "${report_out}" 2> "${record_log}"; then
+	echo "[SKIP] weighted samples cannot be decoded"
+	exit 2
+fi
+
+if ! awk '$1 ~ /^[0-9]/ && ($1 + 0) > 0 { found = 1 } END { exit !found }' \
+	"${report_out}"; then
+	echo "[SKIP] PEBS weight sampling produced no nonzero weights"
+	exit 2
+fi
+
+perf annotate --stdio -i "${perfdata}" --symbol "${testsym}" \
+	> "${annotate_out}" 2> "${record_log}"
+
+grep -q 'Percent Weight' "${annotate_out}"
+
+# The second numeric column is the rendered average weight. This assertion
+# fails on the original regression because calc_percent() left it at zero.
+if ! awk '$1 ~ /^[0-9]/ && $2 ~ /^[0-9]/ && ($2 + 0) > 0 { found = 1 } END { exit !found }' \
+	"${annotate_out}"; then
+	echo "Annotation output contained no nonzero weight"
+	cat "${annotate_out}"
+	exit 1
+fi
+
+echo "PEBS annotation weights: PASS"
Signature · 1 line
2.55.0
#11 lore raw

Re: [PATCH v2 4/4] perf tools: Add test for weight annotation

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The pre-condition check for non-zero weights in `perf report` output is not restricted to the…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The pre-condition check for non-zero weights in perf report output is not restricted to the target symbol test_loop, causing flaky test failures instead of graceful skips on CPUs with fast L1 caches.
- [Medium] The grep -q 'Percent Weight' command silently aborts the script on failure due to set -e, suppressing intended diagnostic output.
--

--- Patch [4]: [PATCH v2 4/4] perf tools: Add test for weight annotation --- commit 4fef6c496fea22a20514f5de2e321ad5a4c0bb3e
Author: Andi Kleen <[email protected]>

perf tools: Add test for weight annotation

This commit adds a test case to check if the --stdio weight annotation works correctly in perf tools.

Quoted 3 lines
tools/perf/tests/shell/annotate_weight.sh new file +0 -0

[ ... ]

tools/perf/tests/shell/annotate_weight.sh line 47

Quoted 5 lines
tools/perf/tests/shell/annotate_weight.sh new file +5 -0
+if ! awk '$1 ~ /^[0-9]/ && ($1 + 0) > 0 { found = 1 } END { exit !found }' \
+	"${report_out}"; then
+	echo "[SKIP] PEBS weight sampling produced no nonzero weights"
+	exit 2
+fi

[Severity: Medium]
Will this global check cause flaky test failures on CPUs with fast L1 caches?

The awk check validates that any symbol produced non-zero weights. Since the thloop workload spins on a volatile variable, it heavily hits the L1 cache. On fast CPUs, these L1 hits might not trigger the PEBS mem-loads event, meaning test_loop might produce zero samples.

However, thread initialization code can still cause cache misses and produce samples with non-zero weights, which would pass this check. The script then runs perf annotate for test_loop, finds zero weight, and fails the test (exit 1) instead of properly skipping it (exit 2).

Should this condition be restricted to the target symbol test_loop before proceeding to annotate it?

tools/perf/tests/shell/annotate_weight.sh line 52

Quoted 5 lines
tools/perf/tests/shell/annotate_weight.sh new file +5 -0
+
+perf annotate --stdio -i "${perfdata}" --symbol "${testsym}" \
+	> "${annotate_out}" 2> "${record_log}"
+
+grep -q 'Percent Weight' "${annotate_out}"

[Severity: Medium]
Because set -e is enabled at the top of the script, will this grep command silently abort the script if the expected header is missing?

If grep returns an exit code of 1 (e.g., if the symbol had no samples or annotation failed), the script will immediately exit and run the cleanup trap, deleting the temporary files.

This bypasses the subsequent awk check which is explicitly designed to output the diagnostic message "Annotation output contained no nonzero weight" and print the file for debugging.

Could this be handled in a way that preserves the diagnostic output when it fails?

Quoted 6 lines
tools/perf/tests/shell/annotate_weight.sh new file +6 -0
+
+# The second numeric column is the rendered average weight. This assertion
+# fails on the original regression because calc_percent() left it at zero.
+if ! awk '$1 ~ /^[0-9]/ && $2 ~ /^[0-9]/ && ($2 + 0) > 0 { found = 1 } END { exit !found }' \
+	"${annotate_out}"; then
+	echo "Annotation output contained no nonzero weight"
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4