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

perf script: Show branch speculation info

Show the branch speculation info if provided by the branch recording
hardware feature. This can be useful for optimizing code further.

The speculation info is appended to the end of the list of fields so any
existing tools that use "/" as a delimiter for access fields via an index
remain unaffected. Also show "-" instead of "N/A" when speculation info
is unavailable because "/" is used as the field separator.

E.g.

$ perf record -j any,u,save_type ./test_branch
$ perf script --fields brstacksym

Before:

[...]
check_match+0x60/strcmp+0x0/P/-/-/0/CALL
do_lookup_x+0x3c5/check_match+0x0/P/-/-/0/CALL
[...]

After:

[...]
check_match+0x60/strcmp+0x0/P/-/-/0/CALL/NON_SPEC_CORRECT_PATH
do_lookup_x+0x3c5/check_match+0x0/P/-/-/0/CALL/NON_SPEC_CORRECT_PATH
[...]

The bitfield swapping scheme used duing sample parsing has changed
because of the addition of new branch flags, namely "spec", "new_type"
and "priv". Earlier, these were all part of the "reserved" field but
now, each of these fields get swapped separately. Change the expected
flag values accordingly for the test to pass.

E.g.

$ perf test -v 27

Before:

27: Sample parsing :
--- start ---
test child forked, pid 61979
parsing failed for sample_type 0x800
test child finished with -1
---- end ----
Sample parsing: FAILED!

After:

27: Sample parsing :
--- start ---
test child forked, pid 63293
test child finished with 0
---- end ----
Sample parsing: Ok

Signed-off-by: Sandipan Das <sandipan.das@amd.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ananth Narayan <ananth.narayan@amd.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Santosh Shukla <santosh.shukla@amd.com>
Cc: Stephane Eranian <eranian@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: x86@kernel.org
Link: https://lore.kernel.org/r/56e272583552526e999ba0b536ac009ae3613966.1675333809.git.sandipan.das@amd.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Sandipan Das and committed by
Arnaldo Carvalho de Melo
6ade6c64 79b7ca78

+33 -6
+3 -2
tools/perf/builtin-script.c
··· 889 889 890 890 static int print_bstack_flags(FILE *fp, struct branch_entry *br) 891 891 { 892 - return fprintf(fp, "/%c/%c/%c/%d/%s ", 892 + return fprintf(fp, "/%c/%c/%c/%d/%s/%s ", 893 893 mispred_str(br), 894 894 br->flags.in_tx ? 'X' : '-', 895 895 br->flags.abort ? 'A' : '-', 896 896 br->flags.cycles, 897 - get_branch_type(br)); 897 + get_branch_type(br), 898 + br->flags.spec ? branch_spec_desc(br->flags.spec) : "-"); 898 899 } 899 900 900 901 static int perf_sample__fprintf_brstack(struct perf_sample *sample,
+1 -1
tools/perf/tests/sample-parsing.c
··· 37 37 * in branch_stack variable. 38 38 */ 39 39 #define BS_EXPECTED_BE 0xa000d00000000000 40 - #define BS_EXPECTED_LE 0xd5000000 40 + #define BS_EXPECTED_LE 0x1aa00000000 41 41 #define FLAG(s) s->branch_stack->entries[i].flags 42 42 43 43 static bool samples_same(const struct perf_sample *s1,
+15
tools/perf/util/branch.c
··· 212 212 213 213 return printed; 214 214 } 215 + 216 + const char *branch_spec_desc(int spec) 217 + { 218 + const char *branch_spec_outcomes[PERF_BR_SPEC_MAX] = { 219 + "N/A", 220 + "SPEC_WRONG_PATH", 221 + "NON_SPEC_CORRECT_PATH", 222 + "SPEC_CORRECT_PATH", 223 + }; 224 + 225 + if (spec >= 0 && spec < PERF_BR_SPEC_MAX) 226 + return branch_spec_outcomes[spec]; 227 + 228 + return NULL; 229 + }
+2
tools/perf/util/branch.h
··· 89 89 void branch_type_stat_display(FILE *fp, struct branch_type_stat *st); 90 90 int branch_type_str(struct branch_type_stat *st, char *bf, int bfsize); 91 91 92 + const char *branch_spec_desc(int spec); 93 + 92 94 #endif /* _PERF_BRANCH_H */
+12 -3
tools/perf/util/evsel.c
··· 2319 2319 * abort:1 //transaction abort 2320 2320 * cycles:16 //cycle count to last branch 2321 2321 * type:4 //branch type 2322 - * reserved:40 2322 + * spec:2 //branch speculation info 2323 + * new_type:4 //additional branch type 2324 + * priv:3 //privilege level 2325 + * reserved:31 2323 2326 * } 2324 2327 * } 2325 2328 * ··· 2338 2335 new_val |= bitfield_swap(value, 3, 1); 2339 2336 new_val |= bitfield_swap(value, 4, 16); 2340 2337 new_val |= bitfield_swap(value, 20, 4); 2341 - new_val |= bitfield_swap(value, 24, 40); 2338 + new_val |= bitfield_swap(value, 24, 2); 2339 + new_val |= bitfield_swap(value, 26, 4); 2340 + new_val |= bitfield_swap(value, 30, 3); 2341 + new_val |= bitfield_swap(value, 33, 31); 2342 2342 } else { 2343 2343 new_val = bitfield_swap(value, 63, 1); 2344 2344 new_val |= bitfield_swap(value, 62, 1); ··· 2349 2343 new_val |= bitfield_swap(value, 60, 1); 2350 2344 new_val |= bitfield_swap(value, 44, 16); 2351 2345 new_val |= bitfield_swap(value, 40, 4); 2352 - new_val |= bitfield_swap(value, 0, 40); 2346 + new_val |= bitfield_swap(value, 38, 2); 2347 + new_val |= bitfield_swap(value, 34, 4); 2348 + new_val |= bitfield_swap(value, 31, 3); 2349 + new_val |= bitfield_swap(value, 0, 31); 2353 2350 } 2354 2351 2355 2352 return new_val;