perf tools record: Modernize -W man page
PATCH 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
@@ -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 weightened 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
PATCH 2/4 perf tools top: Add --weight option
Add a -W/--weight option to perf top to collect weights too. Useful with follow on patches.
--- 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
@@ -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
@@ -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
[email protected]
AI reviewRe: [PATCH v1 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-LunaSigned-off-by Andi Kleen <[email protected]>
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
PATCH 3/4 perf tools: Add support for displaying weights in annotate
In reply to Andi KleenAdd 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.
--- tools/perf/Documentation/perf-annotate.txt | 3 + tools/perf/builtin-annotate.c | 4 + tools/perf/builtin-report.c | 2 + tools/perf/builtin-top.c | 1 + tools/perf/ui/browsers/annotate.c | 13 ++- tools/perf/util/annotate.c | 94 +++++++++++++++++++--- tools/perf/util/annotate.h | 6 +- tools/perf/util/symbol.c | 11 +++ tools/perf/util/symbol_conf.h | 18 ++++- 9 files changed, 140 insertions(+), 12 deletions(-)
tools/perf/Documentation/perf-annotate.txt +3 -0
@@ -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 +4 -0
@@ -259,6 +259,8 @@ static int add_sample(struct perf_sample *sample,return 0 ; } +symbol_conf__set_weight_mode (&symbol_conf, sample); +/* * XXX filtered samples can still have branch entries pointing into our * symbol and are missed.
@@ -719,6 +721,8 @@ int cmd_annotate(int argc, const char **argv)OPT_BOOLEAN (0 ,"tui" , &annotate.use_tui,"Use the TUI interface" ), #endifOPT_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 +2 -0
@@ -329,6 +329,8 @@ static int process_sample_event(const struct perf_tool *tool, &rep->total_cycles); } +symbol_conf__set_weight_mode (&symbol_conf, sample); + rep->total_samples++;if (al.parallelism ==1 ) rep->singlethreaded_samples++;
tools/perf/builtin-top.c +1 -0
@@ -827,6 +827,7 @@ static void perf_event__process_sample(const struct perf_tool *tool, top->vmlinux_warned = true; } } +symbol_conf__set_weight_mode (&symbol_conf, sample);if (al.sym ==NULL || !symbol__is_idle (al.sym, al.map ?map__dso (al.map) :NULL , machine->env)) {
tools/perf/ui/browsers/annotate.c +12 -1
@@ -972,6 +972,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 +1092,14 @@ 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 = symbol_conf.hide_weight ?0 : +annotation__histogram (notes, evsel)->weight_mask; + browser->b.width = notes->src->widths.max_line_len + +annotation__pcnt_width (notes); +ui_browser__refresh_dimensions (&browser->b); +continue ;case 'c' :if (annotate_opts.show_minmax_cycle) annotate_opts.show_minmax_cycle = false;
@@ -1225,10 +1234,12 @@ int __hist_entry__tui_annotate(struct hist_entry *he, struct map_symbol *ms, browser.type_hash =hashmap__new (type_hash, type_equal,/*ctx=*/ NULL ); } + symbol_conf.weight_mask = symbol_conf.hide_weight ?0 : +annotation__histogram (notes, evsel)->weight_mask; browser.b.width = notes->src->widths.max_line_len; browser.b.nr_entries = notes->src->nr_entries; browser.b.entries = ¬es->src->source; - browser.b.width +=18 ;/* Percentage */ + browser.b.width +=annotation__pcnt_width (notes);if (annotate_opts.hide_src_code)ui_browser__init_asm_mode (&browser.b);
tools/perf/util/annotate.c +85 -9
171 lines not on this page · raw message
tools/perf/util/annotate.h +5 -1
@@ -86,6 +86,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,6 +233,7 @@ void symbol__calc_percent(struct symbol *sym, struct evsel *evsel);struct sym_hist {u64 nr_samples;u64 period; +u8 weight_mask; };/**
@@ -378,7 +381,8 @@ static inline int annotation__cycles_width(struct annotation *notes)static inline int annotation__pcnt_width (struct annotation *notes) { -return (symbol_conf.show_total_period ?12 :8 ) * notes->src->nr_events; +int extra =__builtin_popcount (symbol_conf.weight_mask) *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 +11 -0
@@ -2763,3 +2763,14 @@ char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name)return java_demangle_sym (elf_name,JAVA_DEMANGLE_NORET ); } + +void symbol_conf__set_weight_mode (struct symbol_conf *c, +struct perf_sample *sample) +{ +if (sample->weight) + c->weight_mask |=BIT (WEIGHT_WEIGHT ); +if (sample->ins_lat) + c->weight_mask |=BIT (WEIGHT_INSNLAT ); +if (sample->weight3) + c->weight_mask |=BIT (WEIGHT_WEIGHT3 ); +}
tools/perf/util/symbol_conf.h +17 -1
@@ -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,9 @@ struct symbol_conf { annotate_data_sample, skip_empty, enable_latency, - prefer_latency; + prefer_latency, + hide_weight; +u8 weight_mask;const char *vmlinux_name, *kallsyms_name, *source_prefix,
@@ -114,4 +126,8 @@ struct symbol_conf {extern struct symbol_conf symbol_conf; +struct perf_sample ; +void symbol_conf__set_weight_mode (struct symbol_conf *c, +struct perf_sample *sample); +#endif // __PERF_SYMBOL_CONF
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v1 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: - [Medium] Variable shadowing by `for_each_weight` macro in `annotation_line__print` causes either…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Variable shadowing by for_each_weight macro in annotation_line__print causes either premature loop termination or an infinite loop.
- [Medium] Asynchronous global modification of symbol_conf.weight_mask by the background thread in perf top causes data races and defeats the user's hotkey toggle. --
--- Patch [3]: [PATCH v1 3/4] perf tools: Add support for displaying weights in annotate --- commit 7947c9d694355b6721ccd7944dc41891a9968db4
Author: Andi Kleen <[email protected]> Date: Thu Aug 13 13:29:06 2026 +0000
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 12 lines
tools/perf/builtin-top.c +1
-0@@ -827,6 +827,7 @@ static void perf_event__process_sample(const struct perf_tool *tool, top->vmlinux_warned = true; } } +symbol_conf__set_weight_mode (&symbol_conf, sample);if (al.sym ==NULL || !symbol__is_idle (al.sym, al.map ?map__dso (al.map) :NULL , machine->env)) {
[ ... ]
Quoted 4 lines
tools/perf/ui/browsers/annotate.c +0
-0
[ ... ]
Quoted 12 lines
tools/perf/ui/browsers/annotate.c +8
-0@@ -1091,6 +1092,14 @@ 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 = symbol_conf.hide_weight ?0 : +annotation__histogram (notes, evsel)->weight_mask; + browser->b.width = notes->src->widths.max_line_len + +annotation__pcnt_width (notes); +ui_browser__refresh_dimensions (&browser->b); +continue ;
[ ... ]
Quoted 4 lines
tools/perf/util/symbol.c +0
-0
[ ... ]
Quoted 5 lines
tools/perf/util/symbol.c +1
-0@@ -2763,3 +2763,14 @@ 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.c char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name) · line 2776
Quoted 10 lines
tools/perf/util/symbol.c +10
-0+void symbol_conf__set_weight_mode (struct symbol_conf *c, +struct perf_sample *sample) +{ +if (sample->weight) + c->weight_mask |=BIT (WEIGHT_WEIGHT ); +if (sample->ins_lat) + c->weight_mask |=BIT (WEIGHT_INSNLAT ); +if (sample->weight3) + c->weight_mask |=BIT (WEIGHT_WEIGHT3 ); +}
[Severity: Medium]
In perf top, event processing runs in a background thread while the UI runs in the foreground. When a user presses the 'w' hotkey, it sets symbol_conf.weight_mask to 0.
However, the background thread constantly calls symbol_conf__set_weight_mode() for new samples, which unconditionally modifies symbol_conf.weight_mask without checking if symbol_conf.hide_weight is set.
Could this data race cause the TUI to tear or miscalculate column widths, effectively defeating the user's attempt to hide the weight columns?
[ ... ]
Quoted 4 lines
tools/perf/util/annotate.c +0
-0
[ ... ]
tools/perf/util/annotate.c annotation_line__print(struct annotation_line *al, struct annotation_print_data · line 901
Quoted 11 lines
tools/perf/util/annotate.c +6
-0@@ -845,6 +892,12 @@ annotation_line__print(struct annotation_line *al, struct annotation_print_data data->he.nr_samples); else color_fprintf(stdout, color, " %7.2f", percent); +for_each_weight (i) { +if (symbol_conf.weight_mask &BIT (i)) +color_fprintf (stdout, color," %7" PRIu64, + data->he.weight_num[i] ? + data->he.weight_sum[i] / data->he.weight_num[i] :0 ); + } }
[Severity: Medium]
Will this for_each_weight(i) macro silently shadow and corrupt the outer loop's iterator variable 'i'?
The outer loop in annotation_line__print() uses 'i' to iterate up to nr_percent:
for (i = 0; i < nr_percent; i++) {
struct annotation_data *data = &al->data[i];
...
for_each_weight(i) {
...
}
}Since for_each_weight() expands to a for loop that reuses and resets 'i' from WEIGHT_WEIGHT to WEIGHT_WEIGHT3, won't this cause either premature loop termination (if nr_percent <= 5) or an infinite loop (if nr_percent > 5) when processing event groups?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH 4/4 perf tools: Add test for weight annotation
In reply to Andi KleenAdd a test case to check if --stdio weight annotation works
--- 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
@@ -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
[email protected]
AI reviewRe: [PATCH v1 4/4] perf tools: Add test for weight annotation
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Background thread in perf top continually overwrites the TUI user preference for displaying…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Background thread in perf top continually overwrites the TUI user preference for displaying weight columns due to an unprotected data race. --
--- Patch [4]: [PATCH v1 4/4] perf tools: Add test for weight annotation --- commit 7947c9d694355b6721ccd7944dc41891a9968db4
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.
tools/perf/builtin-top.c static void perf_event__process_sample(const struct perf_tool *tool, · line 830
Quoted 9 lines
tools/perf/builtin-top.c +1
-0@@ -827,6 +827,7 @@ static void perf_event__process_sample(const struct perf_tool *tool, top->vmlinux_warned = true; } } +symbol_conf__set_weight_mode (&symbol_conf, sample);
[Severity: Medium]
Will this background sample processing thread concurrently overwrite the TUI user preference for displaying weight columns?
The TUI thread modifies the global symbol_conf structure when the user attempts to hide the weight columns. If the background thread is continually calling symbol_conf__set_weight_mode() on every sample without locking, it appears the user's preference could get immediately overridden, causing the TUI to flicker or misalign columns.
[ ... ]
Quoted 4 lines
tools/perf/ui/browsers/annotate.c +0
-0
[ ... ]
tools/perf/ui/browsers/annotate.c static int annotate_browser__run(struct annotate_browser *browser, · line 1098
Quoted 8 lines
tools/perf/ui/browsers/annotate.c +4
-0@@ -1091,6 +1092,14 @@ 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 = symbol_conf.hide_weight ?0 : +annotation__histogram (notes, evsel)->weight_mask;
[Severity: Medium]
Could this unprotected update to the global symbol_conf.weight_mask race with the background thread's read-modify-write operations?
[ ... ]
Quoted 4 lines
tools/perf/util/symbol.c +0
-0
[ ... ]
tools/perf/util/symbol.c
Quoted 5 lines
tools/perf/util/symbol.c +5
-0+void symbol_conf__set_weight_mode (struct symbol_conf *c, +struct perf_sample *sample) +{ +if (sample->weight) + c->weight_mask |=BIT (WEIGHT_WEIGHT );
[Severity: Medium]
Does this unprotected read-modify-write on the global symbol_conf structure corrupt the state when called concurrently with the TUI thread?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The patch introduces a double space in the updated text for the `--weight` option ('weight3 and…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The patch introduces a double space in the updated text for the --weight option ('weight3 and local_weight'). Additionally, it propagates the pre-existing typo 'weightened' (which should likely be 'weighted') in the same modified block. --
--- Patch [1]: [PATCH v1 1/4] perf tools record: Modernize -W man page --- commit 3b23df9a5d2c30f9e6db8d77004cb5c3a03f441d
Author: Andi Kleen <[email protected]>
perf tools record: Modernize -W man page
This commit updates the -W / --weight description in the perf-record man page to cover newly supported cases and configurations.
tools/perf/Documentation/perf-record.txt Note that this feature may not be available on all processors. · line 484
Quoted 12 lines
tools/perf/Documentation/perf-record.txt +2
-3@@ -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 weightened 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.
[Severity: Low]
This isn't a bug, but it looks like a double space was introduced between weight3 and the word and.
Additionally, this is a pre-existing issue, but the phrase Enable weightened sampling should likely be Enable weighted sampling. Since this block of text is being modified, would it make sense to correct the typo here?
Quoted 3 lines
tools/perf/Documentation/perf-record.txt +3
-0+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'.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1