perf script: Refactor branch flags for Arm SPE
Subsystems (2)
v2 cover perf script: Refactor branch flags for Arm SPE
This patch series refactors branch flags for support Arm SPE. The patch set is divided into two parts, the first part is for refactoring common code and the second part is for enabling Arm SPE.
For refactoring branch flags, the sample flaghs are classified as branch types and events. A program branch type can be conditional branch, function call, return or expection taken. A branch event happens when taking a branch. This series combines branch types and the associated events to present a sample flag.
The second part is to enable Arm SPE's sample flags for expressing branch types and events, and support branch stack.
Patches 01 - 03 are to refactor branch types and branch events. Patches 04, 05 extend to support not-taken event.
Patches 06 - 09 enables branch flags in Arm SPE. This allows to print out sample flags for samples.
Patch 10 supports branch stack for Arm SPE. Patch 11 is an enhancement for PBT feature.
Changes from v1:
- For patch 05, changed to append the 'not_taken' bit in the branch_flags. This can avoid altering bit layout and cause breakage.
- Added Ian's review tags.
Leo Yan (11):
perf script: Make printing flags reliable
perf script: Refactor sample_flags_to_name() function perf script: Separate events from branch types perf script: Add not taken event for branches perf script: Add not taken event for branch stack perf arm-spe: Extend branch operations
perf arm-spe: Decode transactional event
perf arm-spe: Fill branch operations and events to record perf arm-spe: Set sample flags with supplement info perf arm-spe: Add branch stack
perf arm-spe: Support previous branch target (PBT) address
tools/perf/builtin-script.c | 30 ++-- .../util/arm-spe-decoder/arm-spe-decoder.c | 23 ++- .../util/arm-spe-decoder/arm-spe-decoder.h | 11 +- .../arm-spe-decoder/arm-spe-pkt-decoder.c | 14 +- .../arm-spe-decoder/arm-spe-pkt-decoder.h | 12 +- tools/perf/util/arm-spe.c | 135 ++++++++++++++++++ tools/perf/util/branch.h | 3 +- tools/perf/util/event.h | 12 +- tools/perf/util/trace-event-scripting.c | 116 +++++++++++---- tools/perf/util/trace-event.h | 2 + 10 files changed, 307 insertions(+), 51 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/11 perf script: Make printing flags reliable
Add a check for the generated string of flags. Print out the raw number if the string generation fails.
In another case, if the string length is longer than the aligned size, allow the completed string to be printed.
--- tools/perf/builtin-script.c | 10 ++++++++-- tools/perf/util/trace-event.h | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-)
tools/perf/builtin-script.c +8 -2
@@ -1709,9 +1709,15 @@ static int perf_sample__fprintf_bts(struct perf_sample *sample,static int perf_sample__fprintf_flags (u32 flags,FILE *fp) {char str[SAMPLE_FLAGS_BUF_SIZE ]; +int ret; + + ret =perf_sample__sprintf_flags (flags, str,sizeof (str)); +if (ret <0 ) +return fprintf (fp," raw flags:0x%-*x " , +SAMPLE_FLAGS_STR_ALIGNED_SIZE -12 , flags); -perf_sample__sprintf_flags (flags, str,sizeof (str)); -return fprintf (fp," %-21s " , str); + ret =max (ret,SAMPLE_FLAGS_STR_ALIGNED_SIZE ); +return fprintf (fp," %-*s " , ret, str); }struct printer_data {
tools/perf/util/trace-event.h +2 -0
@@ -145,6 +145,8 @@ int common_flags(struct scripting_context *context);int common_lock_depth (struct scripting_context *context);#define SAMPLE_FLAGS_BUF_SIZE 64 +#define SAMPLE_FLAGS_STR_ALIGNED_SIZE 21 +int perf_sample__sprintf_flags (u32 flags,char *str,size_t sz);#if defined(LIBTRACEEVENT_VERSION ) &&LIBTRACEEVENT_VERSION >=MAKE_LIBTRACEEVENT_VERSION (1 ,5 ,0 )
Signature · 1 line
2.34.1
PATCH v2 2/11 perf script: Refactor sample_flags_to_name() function
In reply to Leo YanWhen generating a string for sample flags, the sample_flags_to_name() function lacks the ability to parse the trace start bit or trace end bit. Therefore, the function is invoked multiple times after clearing its unsupported bits.
This commit improves the sample_flags_to_name() function to parse sample flags in one go for three kinds of information:
- The prefix info for trace start, trace end, etc.
- Branch types.
- Extra info for transaction and interrupt related info.
As a result, the code is simplified to call the sample_flags_to_name() only once. No expectation for any changes in the perf script output.
--- tools/perf/util/event.h | 5 ++ tools/perf/util/trace-event-scripting.c | 85 ++++++++++++++++--------- 2 files changed, 59 insertions(+), 31 deletions(-)
tools/perf/util/event.h +5 -0
@@ -71,6 +71,11 @@ enum {#define PERF_IP_FLAG_CHARS "bcrosyiABExghDt" +#define PERF_ADDITIONAL_STATE_MASK \ + (PERF_IP_FLAG_IN_TX | \ + PERF_IP_FLAG_INTR_DISABLE | \ + PERF_IP_FLAG_INTR_TOGGLE) +#define PERF_BRANCH_MASK (\ PERF_IP_FLAG_BRANCH |\ PERF_IP_FLAG_CALL |\
tools/perf/util/trace-event-scripting.c +54 -31
103 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 3/11 perf script: Separate events from branch types
In reply to Leo YanBranch types and events are two different things. A branch type can be a conditional branch, an indirect branch, a procedure call, a return, or an exception taken, etc. The extra event information is provided for what happens during a branch, e.g. if a branch is mispredicted or not taken (specific to conditional branches).
To deliver information about branches, this commit separates events from branch types. It parses branch types first, then appends event strings embraced by the '/' character. If multiple events occur, the events is separated with a comma (,).
Also add a minor improvement by adding char 'm' in char array for branch mispredict event.
Below are extracted sample flags.
Before:
branch: br miss
instructions: br miss
After:
branch: jmp/miss/
instructions: jmp/miss/
--- tools/perf/util/event.h | 5 +++- tools/perf/util/trace-event-scripting.c | 36 ++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-)
tools/perf/util/event.h +4 -1
@@ -69,7 +69,7 @@ enum { PERF_IP_FLAG_BRANCH_MISS = 1ULL << 15, }; -#define PERF_IP_FLAG_CHARS "bcrosyiABExghDt" +#define PERF_IP_FLAG_CHARS "bcrosyiABExghDtm"#define PERF_ADDITIONAL_STATE_MASK \ (PERF_IP_FLAG_IN_TX | \
@@ -90,6 +90,9 @@ enum { PERF_IP_FLAG_VMENTRY |\ PERF_IP_FLAG_VMEXIT) +#define PERF_IP_FLAG_BRACH_EVENT_MASK \ + PERF_IP_FLAG_BRANCH_MISS +#define PERF_MEM_DATA_SRC_NONE \ (PERF_MEM_S(OP, NA) |\ PERF_MEM_S(LVL, NA) |\
tools/perf/util/trace-event-scripting.c +33 -3
@@ -309,7 +309,14 @@ static const struct { {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"}, {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMENTRY, "vmentry"}, {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMEXIT, "vmexit"}, - {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_BRANCH_MISS, "br miss"}, + {0, NULL} +}; + +static const struct { +u32 flags; +const char *name; +} branch_events[] = { + {PERF_IP_FLAG_BRANCH_MISS ,"miss" }, {0 ,NULL } };
@@ -317,8 +324,9 @@ static int sample_flags_to_name(u32 flags, char *str, size_t size) {int i;const char *prefix; -int pos =0 , ret; +int pos =0 , ret, ev_idx =0 ;u32 xf = flags &PERF_ADDITIONAL_STATE_MASK ; +u32 types, events;char xs[16 ] = {0 };/* Clear additional state bits */
@@ -338,8 +346,9 @@ static int sample_flags_to_name(u32 flags, char *str, size_t size) flags &= ~(PERF_IP_FLAG_TRACE_BEGIN |PERF_IP_FLAG_TRACE_END ); + types = flags & ~PERF_IP_FLAG_BRACH_EVENT_MASK ;for (i =0 ; sample_flags[i].name; i++) { -if (sample_flags[i].flags != flags) +if (sample_flags[i].flags != types)continue ; ret =snprintf (str + pos, size - pos,"%s" , sample_flags[i].name);
@@ -349,6 +358,27 @@ static int sample_flags_to_name(u32 flags, char *str, size_t size)break ; } + events = flags &PERF_IP_FLAG_BRACH_EVENT_MASK ; +for (i =0 ; branch_events[i].name; i++) { +if (!(branch_events[i].flags & events)) +continue ; + + ret =snprintf (str + pos, size - pos, !ev_idx ?"/%s" :",%s" , + branch_events[i].name); +if (ret <0 ) +return ret; + pos += ret; + ev_idx++; + } + +/* Add an end character '/' for events */ +if (ev_idx) { + ret =snprintf (str + pos, size - pos,"/" ); +if (ret <0 ) +return ret; + pos += ret; + } +if (!xf)return pos;
Signature · 1 line
2.34.1
PATCH v2 4/11 perf script: Add not taken event for branches
In reply to Leo YanSome hardware (e.g., Arm SPE) can trace the not taken event for branches. Add a flag for this event and support printing it.
--- tools/perf/util/event.h | 6 ++++-- tools/perf/util/trace-event-scripting.c | 1 + 2 files changed, 5 insertions(+), 2 deletions(-)
tools/perf/util/event.h +4 -2
@@ -67,9 +67,10 @@ enum {PERF_IP_FLAG_INTR_DISABLE =1ULL <<13 ,PERF_IP_FLAG_INTR_TOGGLE =1ULL <<14 ,PERF_IP_FLAG_BRANCH_MISS =1ULL <<15 , + PERF_IP_FLAG_NOT_TAKEN = 1ULL << 16, }; -#define PERF_IP_FLAG_CHARS "bcrosyiABExghDtm" +#define PERF_IP_FLAG_CHARS "bcrosyiABExghDtmn"#define PERF_ADDITIONAL_STATE_MASK \ (PERF_IP_FLAG_IN_TX | \
@@ -91,7 +92,8 @@ enum { PERF_IP_FLAG_VMEXIT)#define PERF_IP_FLAG_BRACH_EVENT_MASK \ - PERF_IP_FLAG_BRANCH_MISS + (PERF_IP_FLAG_BRANCH_MISS | \ + PERF_IP_FLAG_NOT_TAKEN)#define PERF_MEM_DATA_SRC_NONE \ (PERF_MEM_S(OP, NA) |\
tools/perf/util/trace-event-scripting.c +1 -0
@@ -317,6 +317,7 @@ static const struct {const char *name; } branch_events[] = { {PERF_IP_FLAG_BRANCH_MISS, "miss"}, + {PERF_IP_FLAG_NOT_TAKEN, "not_taken"}, {0, NULL} };
Signature · 1 line
2.34.1
PATCH v2 5/11 perf script: Add not taken event for branch stack
In reply to Leo YanThe branch stack has an existed field for printing mispredict, extend the field for printing events and add support not-taken event.
--- tools/perf/builtin-script.c | 20 +++++++++++++------- tools/perf/util/branch.h | 3 ++- 2 files changed, 15 insertions(+), 8 deletions(-)
tools/perf/builtin-script.c +13 -7
@@ -935,19 +935,25 @@ static int perf_sample__fprintf_start(struct perf_script *script,return printed; } -static inline char -mispred_str (struct branch_entry *br) +static inline size_t +bstack_event_str (struct branch_entry *br,char *buf,size_t sz) { -if (!(br->flags.mispred || br->flags.predicted)) -return '-' ; +if (!(br->flags.mispred || br->flags.predicted || br->flags.not_taken)) +return snprintf (buf, sz,"-" ); -return br->flags.predicted ?'P' :'M' ; +return snprintf (buf, sz,"%s%s" , + br->flags.predicted ?"P" :"M" , + br->flags.not_taken ?"N" :"" ); }static int print_bstack_flags (FILE *fp,struct branch_entry *br) { - return fprintf(fp, "/%c/%c/%c/%d/%s/%s ", - mispred_str(br), +char events[16 ] = {0 }; +size_t pos; + + pos =bstack_event_str (br, events,sizeof (events)); + return fprintf(fp, "/%s/%c/%c/%d/%s/%s ", + pos < 0 ? "-" : events, br->flags.in_tx ? 'X' : '-', br->flags.abort ? 'A' : '-', br->flags.cycles,
tools/perf/util/branch.h +2 -1
@@ -25,7 +25,8 @@ struct branch_flags { u64 spec:2; u64 new_type:4; u64 priv:3; - u64 reserved:31; + u64 not_taken:1; + u64 reserved:30; }; }; };
Signature · 1 line
2.34.1
PATCH v2 6/11 perf arm-spe: Extend branch operations
In reply to Leo YanIn Arm ARM (ARM DDI 0487, L.a), the section "D18.2.7 Operation Type packet", the branch subclass is extended for Call Return (CR), Guarded control stack data access (GCS).
This commit adds support CR and GCS operations. The IND (indirect) operation is defined only in bit [1], its macro is updated accordingly.
Move the COND (Conditional) macro into the same group with other operations for better maintenance.
--- .../perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c | 12 +++++++++--- .../perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h | 11 ++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-)
tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c +9 -3
@@ -397,10 +397,16 @@ static int arm_spe_pkt_desc_op_type(const struct arm_spe_pkt *packet,if (payload &SPE_OP_PKT_COND )arm_spe_pkt_out_string (&err, &buf, &buf_len," COND" ); - -if (SPE_OP_PKT_IS_INDIRECT_BRANCH (payload)) +if (payload &SPE_OP_PKT_INDIRECT_BRANCH )arm_spe_pkt_out_string (&err, &buf, &buf_len," IND" ); - +if (payload &SPE_OP_PKT_GCS ) +arm_spe_pkt_out_string (&err, &buf, &buf_len," GCS" ); +if (SPE_OP_PKT_CR_BL (payload)) +arm_spe_pkt_out_string (&err, &buf, &buf_len," CR-BL" ); +if (SPE_OP_PKT_CR_RET (payload)) +arm_spe_pkt_out_string (&err, &buf, &buf_len," CR-RET" ); +if (SPE_OP_PKT_CR_NON_BL_RET (payload)) +arm_spe_pkt_out_string (&err, &buf, &buf_len," CR-NON-BL-RET" );break ;default :/* Unknown index */
tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h +8 -3
@@ -7,6 +7,7 @@#ifndef INCLUDE__ARM_SPE_PKT_DECODER_H__ #define INCLUDE__ARM_SPE_PKT_DECODER_H__ +#include <linux/bitfield.h> #include <stddef.h> #include <stdint.h>
@@ -116,8 +117,6 @@ enum arm_spe_events {#define SPE_OP_PKT_IS_OTHER_SVE_OP (v) (((v) & (BIT(7) | BIT(3) | BIT(0))) == 0x8) -#define SPE_OP_PKT_COND BIT(0) -#define SPE_OP_PKT_LDST_SUBCLASS_GET (v) ((v) & GENMASK_ULL(7, 1))#define SPE_OP_PKT_LDST_SUBCLASS_GP_REG 0x0#define SPE_OP_PKT_LDST_SUBCLASS_SIMD_FP 0x4
@@ -148,7 +147,13 @@ enum arm_spe_events {#define SPE_OP_PKT_SVE_PRED BIT(2)#define SPE_OP_PKT_SVE_FP BIT(1) -#define SPE_OP_PKT_IS_INDIRECT_BRANCH (v) (((v) & GENMASK_ULL(7, 1)) == 0x2) +#define SPE_OP_PKT_CR_MASK GENMASK_ULL(4, 3) +#define SPE_OP_PKT_CR_BL (v) (FIELD_GET(SPE_OP_PKT_CR_MASK, (v)) == 1) +#define SPE_OP_PKT_CR_RET (v) (FIELD_GET(SPE_OP_PKT_CR_MASK, (v)) == 2) +#define SPE_OP_PKT_CR_NON_BL_RET (v) (FIELD_GET(SPE_OP_PKT_CR_MASK, (v)) == 3) +#define SPE_OP_PKT_GCS BIT(2) +#define SPE_OP_PKT_INDIRECT_BRANCH BIT(1) +#define SPE_OP_PKT_COND BIT(0)const char *arm_spe_pkt_name (enum arm_spe_pkt_type );
Signature · 1 line
2.34.1
PATCH v2 7/11 perf arm-spe: Decode transactional event
In reply to Leo YanThe bit[16] in an event payload indicates an operation is in transactional state. Decode the bit.
--- tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c | 2 ++ tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h | 1 + 2 files changed, 3 insertions(+)
tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c +2 -0
@@ -308,6 +308,8 @@ static int arm_spe_pkt_desc_event(const struct arm_spe_pkt *packet,arm_spe_pkt_out_string (&err, &buf, &buf_len," REMOTE-ACCESS" );if (payload &BIT (EV_ALIGNMENT ))arm_spe_pkt_out_string (&err, &buf, &buf_len," ALIGNMENT" ); +if (payload &BIT (EV_TRANSACTIONAL )) +arm_spe_pkt_out_string (&err, &buf, &buf_len," TXN" );if (payload &BIT (EV_PARTIAL_PREDICATE ))arm_spe_pkt_out_string (&err, &buf, &buf_len," SVE-PARTIAL-PRED" );if (payload &BIT (EV_EMPTY_PREDICATE ))
tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h +1 -0
@@ -105,6 +105,7 @@ enum arm_spe_events {EV_LLC_MISS =9 ,EV_REMOTE_ACCESS =10 ,EV_ALIGNMENT =11 , +EV_TRANSACTIONAL =16 ,EV_PARTIAL_PREDICATE =17 , EV_EMPTY_PREDICATE = 18, };
Signature · 1 line
2.34.1
PATCH v2 8/11 perf arm-spe: Fill branch operations and events to record
In reply to Leo YanThe new added branch operations and events are filled into record, the information will be consumed when synthesizing samples.
--- .../util/arm-spe-decoder/arm-spe-decoder.c | 18 ++++++++++++++++++ .../util/arm-spe-decoder/arm-spe-decoder.h | 10 ++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-)
tools/perf/util/arm-spe-decoder/arm-spe-decoder.c +18 -0
@@ -207,6 +207,18 @@ static int arm_spe_read_record(struct arm_spe_decoder *decoder)break ;case SPE_OP_PKT_HDR_CLASS_BR_ERET : decoder->record.op |=ARM_SPE_OP_BRANCH_ERET ; +if (payload &SPE_OP_PKT_COND ) + decoder->record.op |=ARM_SPE_OP_BR_COND ; +if (payload &SPE_OP_PKT_INDIRECT_BRANCH ) + decoder->record.op |=ARM_SPE_OP_BR_INDIRECT ; +if (payload &SPE_OP_PKT_GCS ) + decoder->record.op |=ARM_SPE_OP_BR_GCS ; +if (SPE_OP_PKT_CR_BL (payload)) + decoder->record.op |=ARM_SPE_OP_BR_CR_BL ; +if (SPE_OP_PKT_CR_RET (payload)) + decoder->record.op |=ARM_SPE_OP_BR_CR_RET ; +if (SPE_OP_PKT_CR_NON_BL_RET (payload)) + decoder->record.op |=ARM_SPE_OP_BR_CR_NON_BL_RET ;break ;default :pr_err ("Get packet error!\n" );
@@ -238,6 +250,12 @@ static int arm_spe_read_record(struct arm_spe_decoder *decoder)if (payload &BIT (EV_MISPRED )) decoder->record.type |=ARM_SPE_BRANCH_MISS ; +if (payload &BIT (EV_NOT_TAKEN )) + decoder->record.type |=ARM_SPE_BRANCH_NOT_TAKEN ; + +if (payload &BIT (EV_TRANSACTIONAL )) + decoder->record.type |=ARM_SPE_IN_TXN ; +if (payload &BIT (EV_PARTIAL_PREDICATE )) decoder->record.type |=ARM_SPE_SVE_PARTIAL_PRED ;
tools/perf/util/arm-spe-decoder/arm-spe-decoder.h +8 -2
@@ -24,6 +24,8 @@ enum arm_spe_sample_type {ARM_SPE_REMOTE_ACCESS =1 <<7 ,ARM_SPE_SVE_PARTIAL_PRED =1 <<8 ,ARM_SPE_SVE_EMPTY_PRED =1 <<9 , +ARM_SPE_BRANCH_NOT_TAKEN =1 <<10 , + ARM_SPE_IN_TXN = 1 << 11, }; enum arm_spe_op_type {
@@ -52,8 +54,12 @@ enum arm_spe_op_type {ARM_SPE_OP_SVE_SG =1 <<27 ,/* Second level operation type for BRANCH_ERET */ -ARM_SPE_OP_BR_COND =1 <<16 , - ARM_SPE_OP_BR_INDIRECT = 1 << 17, +ARM_SPE_OP_BR_COND =1 <<16 , +ARM_SPE_OP_BR_INDIRECT =1 <<17 , +ARM_SPE_OP_BR_GCS =1 <<18 , +ARM_SPE_OP_BR_CR_BL =1 <<19 , +ARM_SPE_OP_BR_CR_RET =1 <<20 , + ARM_SPE_OP_BR_CR_NON_BL_RET = 1 << 21, }; enum arm_spe_common_data_source {
Signature · 1 line
2.34.1
James Clark
reviewerRe: [PATCH v2 08/11] perf arm-spe: Fill branch operations and events to record
Quoted 15 lines · Leo Yan
The new added branch operations and events are filled into record, the information will be consumed when synthesizing samples.
Reviewed-by Ian Rogers <[email protected]>Signed-off-by Leo Yan <[email protected]>--- .../util/arm-spe-decoder/arm-spe-decoder.c | 18 ++++++++++++++++++ .../util/arm-spe-decoder/arm-spe-decoder.h | 10 ++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-)tools/perf/util/arm-spe-decoder/arm-spe-decoder.c +0
-0
tools/perf/util/arm-spe-decoder/arm-spe-decoder.c static int arm_spe_read_record(struct arm_spe_decoder *decoder) · line 211
Quoted 5 lines
tools/perf/util/arm-spe-decoder/arm-spe-decoder.c +2
-0@@ -207,6 +207,18 @@ static int arm_spe_read_record(struct arm_spe_decoder *decoder)break ;case SPE_OP_PKT_HDR_CLASS_BR_ERET : decoder->record.op |=ARM_SPE_OP_BRANCH_ERET ; +if (payload &SPE_OP_PKT_COND ) + decoder->record.op |=ARM_SPE_OP_BR_COND ;
I think this results in memory events being synthesised for these samples because of a bug in arm_spe__synth_data_source(). ARM_SPE_OP_BR_COND overlaps with bits from other packet types like ARM_SPE_OP_LD:
if (record->op & ARM_SPE_OP_LD) data_src.mem_op = PERF_MEM_OP_LOAD;
arm_spe__synth_data_source() needs to only interpret that as a data source packet if ARM_SPE_OP_LDST is set. This was reported by Mike Williams but you have the privilege of hitting it for real first.
Quoted 54 lines
tools/perf/util/arm-spe-decoder/arm-spe-decoder.c +16
-0+if (payload &SPE_OP_PKT_INDIRECT_BRANCH ) + decoder->record.op |=ARM_SPE_OP_BR_INDIRECT ; +if (payload &SPE_OP_PKT_GCS ) + decoder->record.op |=ARM_SPE_OP_BR_GCS ; +if (SPE_OP_PKT_CR_BL (payload)) + decoder->record.op |=ARM_SPE_OP_BR_CR_BL ; +if (SPE_OP_PKT_CR_RET (payload)) + decoder->record.op |=ARM_SPE_OP_BR_CR_RET ; +if (SPE_OP_PKT_CR_NON_BL_RET (payload)) + decoder->record.op |=ARM_SPE_OP_BR_CR_NON_BL_RET ;break ;default :pr_err ("Get packet error!\n" );@@ -238,6 +250,12 @@ static int arm_spe_read_record(struct arm_spe_decoder *decoder)if (payload &BIT (EV_MISPRED )) decoder->record.type |=ARM_SPE_BRANCH_MISS ; +if (payload &BIT (EV_NOT_TAKEN )) + decoder->record.type |=ARM_SPE_BRANCH_NOT_TAKEN ; + +if (payload &BIT (EV_TRANSACTIONAL )) + decoder->record.type |=ARM_SPE_IN_TXN ; +if (payload &BIT (EV_PARTIAL_PREDICATE )) decoder->record.type |=ARM_SPE_SVE_PARTIAL_PRED ;tools/perf/util/arm-spe-decoder/arm-spe-decoder.h +8
-2@@ -24,6 +24,8 @@ enum arm_spe_sample_type {ARM_SPE_REMOTE_ACCESS =1 <<7 ,ARM_SPE_SVE_PARTIAL_PRED =1 <<8 ,ARM_SPE_SVE_EMPTY_PRED =1 <<9 , +ARM_SPE_BRANCH_NOT_TAKEN =1 <<10 , + ARM_SPE_IN_TXN = 1 << 11, }; enum arm_spe_op_type {@@ -52,8 +54,12 @@ enum arm_spe_op_type {ARM_SPE_OP_SVE_SG =1 <<27 ,/* Second level operation type for BRANCH_ERET */ -ARM_SPE_OP_BR_COND =1 <<16 , - ARM_SPE_OP_BR_INDIRECT = 1 << 17, +ARM_SPE_OP_BR_COND =1 <<16 , +ARM_SPE_OP_BR_INDIRECT =1 <<17 , +ARM_SPE_OP_BR_GCS =1 <<18 , +ARM_SPE_OP_BR_CR_BL =1 <<19 , +ARM_SPE_OP_BR_CR_RET =1 <<20 , + ARM_SPE_OP_BR_CR_NON_BL_RET = 1 << 21, }; enum arm_spe_common_data_source {
Re: [PATCH v2 08/11] perf arm-spe: Fill branch operations and events to record
On Mon, Feb 17, 2025 at 04:20:57PM +0000, James Clark wrote:
[...]
Quoted 16 lines
tools/perf/util/arm-spe-decoder/arm-spe-decoder.c static int arm_spe_read_record(struct arm_spe_decoder *decoder) · line 211
Quoted 10 lines
tools/perf/util/arm-spe-decoder/arm-spe-decoder.c +2
-0@@ -207,6 +207,18 @@ static int arm_spe_read_record(struct arm_spe_decoder *decoder)break ;case SPE_OP_PKT_HDR_CLASS_BR_ERET : decoder->record.op |=ARM_SPE_OP_BRANCH_ERET ; +if (payload &SPE_OP_PKT_COND ) + decoder->record.op |=ARM_SPE_OP_BR_COND ;I think this results in memory events being synthesised for these samples because of a bug in
arm_spe__synth_data_source().ARM_SPE_OP_BR_CONDoverlaps with bits from other packet types likeARM_SPE_OP_LD:
Quoted 6 lines
if (record->op & ARM_SPE_OP_LD) data_src.mem_op = PERF_MEM_OP_LOAD;
arm_spe__synth_data_source()needs to only interpret that as a data source packet ifARM_SPE_OP_LDSTis set. This was reported by Mike Williams but you have the privilege of hitting it for real first.
Good catching. I have added a new patch 06 to fix the bug in the v3 series. Thanks for hooking up the info.
Leo
PATCH v2 9/11 perf arm-spe: Set sample flags with supplement info
In reply to Leo YanBased on the supplement information in the record, this commit sets the sample flags for conditional branch, function call, return. It also sets events in flags, such as mispredict, not taken, and in transaction.
--- tools/perf/util/arm-spe.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
tools/perf/util/arm-spe.c +20 -0
@@ -482,6 +482,26 @@ static void arm_spe__sample_flags(struct arm_spe_queue *speq)if (record->type &ARM_SPE_BRANCH_MISS ) speq->flags |=PERF_IP_FLAG_BRANCH_MISS ; + +if (record->type &ARM_SPE_BRANCH_NOT_TAKEN ) + speq->flags |=PERF_IP_FLAG_NOT_TAKEN ; + +if (record->type &ARM_SPE_IN_TXN ) + speq->flags |=PERF_IP_FLAG_IN_TX ; + +if (record->op &ARM_SPE_OP_BR_COND ) + speq->flags |=PERF_IP_FLAG_CONDITIONAL ; + +if (record->op &ARM_SPE_OP_BR_CR_BL ) + speq->flags |=PERF_IP_FLAG_CALL ; +else if (record->op &ARM_SPE_OP_BR_CR_RET ) + speq->flags |=PERF_IP_FLAG_RETURN ; +/* +* Indirect branch instruction without link (e.g. BR), +* take it as a function return. +*/ +else if (record->op &ARM_SPE_OP_BR_INDIRECT ) + speq->flags |=PERF_IP_FLAG_RETURN ; } }
Signature · 1 line
2.34.1
PATCH v2 10/11 perf arm-spe: Add branch stack
In reply to Leo YanAlthough Arm SPE cannot generate continuous branch records, this commit creates a branch stack with only one branch entry. A single branch info can be used for performance optimization.
A branch stack structure is dynamically allocated in the decode queue. The branch stack and stack flags are synthesized based on branch types and associated events.
After:
# perf script --itrace=bl1 -F flags,addr,brstack
jcc ffffc0fad9c6b214 0xffffc0fad9c6b234/0xffffc0fad9c6b214/P/-/-/7/COND/- jcc/miss,not_taken/ ffffc0fadaaebb30 0xffffc0fadaaebb2c/0xffffc0fadaaebb30/MN/-/-/7/COND/- jmp ffffc0fadaaea358 0xffffc0fadaaea5ec/0xffffc0fadaaea358/P/-/-/5//- jcc/not_taken/ ffffc0fadaae6494 0xffffc0fadaae6490/0xffffc0fadaae6494/PN/-/-/11/COND/- jcc/not_taken/ ffff7f83ab54 0xffff7f83ab50/0xffff7f83ab54/PN/-/-/13/COND/- jcc/not_taken/ ffff7f83ab08 0xffff7f83ab04/0xffff7f83ab08/PN/-/-/8/COND/- jcc ffff7f83aa80 0xffff7f83aa58/0xffff7f83aa80/P/-/-/10/COND/- jcc ffff7f9a45d0 0xffff7f9a43f0/0xffff7f9a45d0/P/-/-/29/COND/- jcc/not_taken/ ffffc0fad9ba6db4 0xffffc0fad9ba6db0/0xffffc0fad9ba6db4/PN/-/-/44/COND/- jcc ffffc0fadaac2964 0xffffc0fadaac2970/0xffffc0fadaac2964/P/-/-/6/COND/- jcc ffffc0fad99ddc10 0xffffc0fad99ddc04/0xffffc0fad99ddc10/P/-/-/72/COND/- jcc/not_taken/ ffffc0fad9b3f21c 0xffffc0fad9b3f218/0xffffc0fad9b3f21c/PN/-/-/64/COND/- jcc ffffc0fad9c3b604 0xffffc0fad9c3b5f8/0xffffc0fad9c3b604/P/-/-/13/COND/- jcc ffffc0fadaad6048 0xffffc0fadaad5f8c/0xffffc0fadaad6048/P/-/-/5/COND/-
return/miss/ ffff7f84e614 0xffffc0fad98a2274/0xffff7f84e614/M/-/-/13/RET/-
jcc/not_taken/ ffffc0fadaac4eb4 0xffffc0fadaac4eb0/0xffffc0fadaac4eb4/PN/-/-/5/COND/-
jmp ffff7f8e3130 0xffff7f87555c/0xffff7f8e3130/P/-/-/5//-
jcc/not_taken/ ffffc0fad9b3d9b0 0xffffc0fad9b3d9ac/0xffffc0fad9b3d9b0/PN/-/-/14/COND/-
return ffffc0fad9b91950 0xffffc0fad98c3e28/0xffffc0fad9b91950/P/-/-/12/RET/-
--- tools/perf/util/arm-spe.c | 99 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+)
tools/perf/util/arm-spe.c +99 -0
@@ -101,6 +101,7 @@ struct arm_spe_queue {struct thread *thread;u64 period_instructions;u32 flags; +struct branch_stack *last_branch; };struct data_source_handle {
@@ -231,6 +232,16 @@ static struct arm_spe_queue *arm_spe__alloc_queue(struct arm_spe *spe, params.get_trace = arm_spe_get_trace; params.data = speq; +if (spe->synth_opts.last_branch) { +size_t sz =sizeof (struct branch_stack ); + +/* Allocate one entry for TGT */ + sz +=sizeof (struct branch_entry ); + speq->last_branch =zalloc (sz); +if (!speq->last_branch) + goto out_free; + } +/* create new decoder */ speq->decoder =arm_spe_decoder_new (¶ms);if (!speq->decoder)
@@ -240,6 +251,7 @@ static struct arm_spe_queue *arm_spe__alloc_queue(struct arm_spe *spe, out_free:zfree (&speq->event_buf); +zfree (&speq->last_branch);free (speq);return NULL ;
@@ -346,6 +358,73 @@ static void arm_spe_prep_sample(struct arm_spe *spe, event->sample.header.size =sizeof (struct perf_event_header ); } +static void arm_spe__prep_branch_stack (struct arm_spe_queue *speq) +{ +struct arm_spe_record *record = &speq->decoder->record; +struct branch_stack *bstack = speq->last_branch; +struct branch_flags *bs_flags; +size_t sz =sizeof (struct branch_stack ) + +sizeof (struct branch_entry )/* TGT */ ; + +/* Clean up branch stack */ +memset (bstack,0x0 , sz); + +if (!(speq->flags &PERF_IP_FLAG_BRANCH )) +return ; + + bstack->entries[0 ].from = record->from_ip; + bstack->entries[0 ].to = record->to_ip; + + bs_flags = &bstack->entries[0 ].flags; + bs_flags->value =0 ; + +if (record->op &ARM_SPE_OP_BR_CR_BL ) { +if (record->op &ARM_SPE_OP_BR_COND ) + bs_flags->type |=PERF_BR_COND_CALL ; +else + bs_flags->type |=PERF_BR_CALL ; +/* +* Indirect branch instruction without link (e.g. BR), +* take this case as function return. +*/ + }else if (record->op &ARM_SPE_OP_BR_CR_RET || + record->op &ARM_SPE_OP_BR_INDIRECT ) { +if (record->op &ARM_SPE_OP_BR_COND ) + bs_flags->type |=PERF_BR_COND_RET ; +else + bs_flags->type |=PERF_BR_RET ; + }else if (record->op &ARM_SPE_OP_BR_CR_NON_BL_RET ) { +if (record->op &ARM_SPE_OP_BR_COND ) + bs_flags->type |=PERF_BR_COND ; +else + bs_flags->type |=PERF_BR_UNCOND ; + }else { +if (record->op &ARM_SPE_OP_BR_COND ) + bs_flags->type |=PERF_BR_COND ; +else + bs_flags->type |=PERF_BR_UNKNOWN ; + } + +if (record->type &ARM_SPE_BRANCH_MISS ) { + bs_flags->mispred =1 ; + bs_flags->predicted =0 ; + }else { + bs_flags->mispred =0 ; + bs_flags->predicted =1 ; + } + +if (record->type &ARM_SPE_BRANCH_NOT_TAKEN ) + bs_flags->not_taken =1 ; + +if (record->type &ARM_SPE_IN_TXN ) + bs_flags->in_tx =1 ; + + bs_flags->cycles =min (record->latency,0xFFFFU ); + + bstack->nr =1 ; + bstack->hw_idx =-1ULL ; +} +static int arm_spe__inject_event (union perf_event *event,struct perf_sample *sample,u64 type) { event->header.size =perf_event__sample_event_size (sample, type,0 );
@@ -414,6 +493,7 @@ static int arm_spe__synth_branch_sample(struct arm_spe_queue *speq, sample.addr = record->to_ip; sample.weight = record->latency; sample.flags = speq->flags; + sample.branch_stack = speq->last_branch; ret =arm_spe_deliver_synth_event (spe, speq, event, &sample);perf_sample__exit (&sample);
@@ -448,6 +528,7 @@ static int arm_spe__synth_instruction_sample(struct arm_spe_queue *speq, sample.period = spe->instructions_sample_period; sample.weight = record->latency; sample.flags = speq->flags; + sample.branch_stack = speq->last_branch; ret =arm_spe_deliver_synth_event (spe, speq, event, &sample);perf_sample__exit (&sample);
@@ -781,6 +862,10 @@ static int arm_spe_sample(struct arm_spe_queue *speq) } } +if (spe->synth_opts.last_branch && + (spe->sample_branch || spe->sample_instructions)) +arm_spe__prep_branch_stack (speq); +if (spe->sample_branch && (record->op &ARM_SPE_OP_BRANCH_ERET )) { err =arm_spe__synth_branch_sample (speq, spe->branch_id);if (err)
@@ -1272,6 +1357,7 @@ static void arm_spe_free_queue(void *priv)thread__zput (speq->thread);arm_spe_decoder_free (speq->decoder);zfree (&speq->event_buf); +zfree (&speq->last_branch);free (speq); }
@@ -1491,6 +1577,19 @@ arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session) id +=1 ; } +if (spe->synth_opts.last_branch) { +if (spe->synth_opts.last_branch_sz >1 ) +pr_debug ("Arm SPE supports only one bstack entry (TGT).\n" ); + + attr.sample_type |=PERF_SAMPLE_BRANCH_STACK ; +/* +* We don't use the hardware index, but the sample generation +* code uses the new format branch_stack with this field, +* so the event attributes must indicate that it's present. +*/ + attr.branch_sample_type |=PERF_SAMPLE_BRANCH_HW_INDEX ; + } +if (spe->synth_opts.branches) { spe->sample_branch = true;
Signature · 1 line
2.34.1
PATCH v2 11/11 perf arm-spe: Support previous branch target (PBT) address
In reply to Leo YanWhen FEAT_SPE_PBT is implemented, the previous branch target address (named as PBT) before the sampled operation, will be recorded.
This commit first introduces a 'prev_br_tgt' field in the record for saving the PBT address in the decoder.
If the current operation is a branch instruction, by combining with PBT, it can create a chain with two consecutive branches. As the branch stack stores branches in descending order, meaning a newer branch is stored in a lower entry in the stack. Arm SPE stores the latest branch in the first entry of branch stack, and the previous branch coming from PBT is stored into the second entry.
Otherwise, if current operation is not a branch, the last branch will be saved for PBT only. PBT lacks associated information such as branch source address, branch type, and events. The branch entry fills zeros for the corresponding fields and only set its target address.
After:
perf script -f --itrace=bl -F flags,addr,brstack jcc/not_taken/ ff00000000000000 0xffff80008141e920/0xff00000000000000/PN/-/-/1/COND/- 0x0/0xffff8000803607d8/-/-/-/0//- jcc ffff800080187914 0xffff8000801878fc/0xffff800080187914/P/-/-/1/COND/- 0x0/0xffff8000801878f8/-/-/-/0//- jcc ffff8000802d12d8 0xffff8000802d12f8/0xffff8000802d12d8/P/-/-/1/COND/- 0x0/0xffff8000802d12ec/-/-/-/0//- jcc ffff8000813fe200 0xffff8000813fe20c/0xffff8000813fe200/P/-/-/1/COND/- 0x0/0xffff8000813fe200/-/-/-/0//- jcc ffff8000813fe200 0xffff8000813fe20c/0xffff8000813fe200/P/-/-/1/COND/- 0x0/0xffff8000813fe200/-/-/-/0//- jmp ffff800081410980 0xffff800081419108/0xffff800081410980/P/-/-/1//- 0x0/0xffff800081419104/-/-/-/0//- return ffff80008036e064 0xffff80008141ba84/0xffff80008036e064/P/-/-/1/RET/- 0x0/0xffff80008141ba60/-/-/-/0//- jcc ffff8000803d54f0 0xffff8000803d54e8/0xffff8000803d54f0/P/-/-/1/COND/- 0x0/0xffff8000803d54e0/-/-/-/0//- jmp ffff80008015e468 0xffff8000803d46dc/0xffff80008015e468/P/-/-/1//- 0x0/0xffff8000803d46c8/-/-/-/0//- jmp ffff8000806e2d50 0xffff80008040f710/0xffff8000806e2d50/P/-/-/1//- 0x0/0xffff80008040f6e8/-/-/-/0//- jcc ffff800080721704 0xffff8000807216b4/0xffff800080721704/P/-/-/1/COND/- 0x0/0xffff8000807216ac/-/-/-/0//-
--- .../util/arm-spe-decoder/arm-spe-decoder.c | 5 +- .../util/arm-spe-decoder/arm-spe-decoder.h | 1 + tools/perf/util/arm-spe.c | 114 ++++++++++-------- 3 files changed, 70 insertions(+), 50 deletions(-)
tools/perf/util/arm-spe-decoder/arm-spe-decoder.c +4 -1
@@ -28,7 +28,8 @@ static u64 arm_spe_calc_ip(int index, u64 payload)/* Instruction virtual address or Branch target address */ if (index ==SPE_ADDR_PKT_HDR_INDEX_INS || - index ==SPE_ADDR_PKT_HDR_INDEX_BRANCH ) { + index ==SPE_ADDR_PKT_HDR_INDEX_BRANCH || + index ==SPE_ADDR_PKT_HDR_INDEX_PREV_BRANCH ) { ns =SPE_ADDR_PKT_GET_NS (payload); el =SPE_ADDR_PKT_GET_EL (payload);
@@ -181,6 +182,8 @@ static int arm_spe_read_record(struct arm_spe_decoder *decoder) decoder->record.virt_addr = ip; else if (idx == SPE_ADDR_PKT_HDR_INDEX_DATA_PHYS) decoder->record.phys_addr = ip; + else if (idx == SPE_ADDR_PKT_HDR_INDEX_PREV_BRANCH) + decoder->record.prev_br_tgt = ip;break ;case ARM_SPE_COUNTER :if (idx ==SPE_CNT_PKT_HDR_INDEX_TOTAL_LAT )
tools/perf/util/arm-spe-decoder/arm-spe-decoder.h +1 -0
@@ -89,6 +89,7 @@ struct arm_spe_record {u32 latency;u64 from_ip;u64 to_ip; +u64 prev_br_tgt;u64 timestamp;u64 virt_addr;u64 phys_addr;
tools/perf/util/arm-spe.c +65 -49
149 lines not on this page · raw message
Signature · 1 line
2.34.1
Quoted 41 lines · Leo Yan
This patch series refactors branch flags for support Arm SPE. The patch set is divided into two parts, the first part is for refactoring common code and the second part is for enabling Arm SPE.
For refactoring branch flags, the sample flaghs are classified as branch types and events. A program branch type can be conditional branch, function call, return or expection taken. A branch event happens when taking a branch. This series combines branch types and the associated events to present a sample flag.
The second part is to enable Arm SPE's sample flags for expressing branch types and events, and support branch stack.
Patches 01 - 03 are to refactor branch types and branch events. Patches 04, 05 extend to support not-taken event.
Patches 06 - 09 enables branch flags in Arm SPE. This allows to print out sample flags for samples.
Patch 10 supports branch stack for Arm SPE. Patch 11 is an enhancement for PBT feature.
Changes from v1:
- For patch 05, changed to append the 'not_taken' bit in thebranch_flags. This can avoid altering bit layout and cause breakage.
- Added Ian's review tags.Leo Yan (11):
perf script: Make printing flags reliable
perf script: Refactorsample_flags_to_name()function perf script: Separate events from branch types perf script: Add not taken event for branches perf script: Add not taken event for branch stack perf arm-spe: Extend branch operations
perf arm-spe: Decode transactional event
perf arm-spe: Fill branch operations and events to record perf arm-spe: Set sample flags with supplement info perf arm-spe: Add branch stack
perf arm-spe: Support previous branch target (PBT) address
Quoted 12 lines
tools/perf/builtin-script.c | 30 ++-- .../util/arm-spe-decoder/arm-spe-decoder.c | 23 ++- .../util/arm-spe-decoder/arm-spe-decoder.h | 11 +- .../arm-spe-decoder/arm-spe-pkt-decoder.c | 14 +- .../arm-spe-decoder/arm-spe-pkt-decoder.h | 12 +- tools/perf/util/arm-spe.c | 135 ++++++++++++++++++ tools/perf/util/branch.h | 3 +- tools/perf/util/event.h | 12 +- tools/perf/util/trace-event-scripting.c | 116 +++++++++++---- tools/perf/util/trace-event.h | 2 + 10 files changed, 307 insertions(+), 51 deletions(-)
Just one potential issue on patch 8, with that fixed: