Skip to contentSkip to conversation

perf script: Refactor branch flags for Arm SPE

PATCH Superseded Leo Yan 4 participants 25 messages Feb 17, 2025 – Mar 5, 2025

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Leo Yan

author
#0 lore raw

v3 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.

Patch 06 is to fix a bug for parsing ldst operations. Without this fix, the seqential changes will cause breaking for memory samples.

Patches 07 - 10 enables branch flags in Arm SPE. This allows to print out sample flags for samples.

Patch 11 supports branch stack for Arm SPE. Patch 12 is an enhancement for PBT feature.

Changes from v2:
- Added a new patch 06 for fixing the ldst operation bug (James).
- Added James' review tags (except the new added patch 06).
- Removed a wrong output result in the last patch's commit log.

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 (12):
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: Fix load-store operation checking 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                     | 143 +++++++++++++++++-
 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, 314 insertions(+), 52 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 1/12 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
index d797cec4f054..2c4b1fb7dc72 100644
@@ -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
index ac9fde2f980c..71e680bc3d4b 100644
@@ -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
reviewer
#2 lore raw

Re: [PATCH v3 01/12] perf script: Make printing flags reliable

Quoted 2 lines · Leo Yan

Add a check for the generated string of flags. Print out the raw number if the string generation fails.

How does it fail?

Quoted 17 lines

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 +0 -0
index d797cec4f054..2c4b1fb7dc72 100644

tools/perf/builtin-script.c static int perf_sample__fprintf_bts(struct perf_sample *sample, · line 1715

Quoted 7 lines
tools/perf/builtin-script.c +4 -0
@@ -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)

AFAICT ret is always >= 0

tools/perf/builtin-script.c static int perf_sample__fprintf_bts(struct perf_sample *sample, · line 1720

Quoted 7 lines
tools/perf/builtin-script.c +4 -2
+		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);

-21 means the field width is 21 and left-justified. It should not truncate the string.

Quoted 16 lines
tools/perf/builtin-script.c +0 -0
 }
 
 struct printer_data {
tools/perf/util/trace-event.h +2 -0
index ac9fde2f980c..71e680bc3d4b 100644
@@ -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)
author
#3 lore raw

Re: [PATCH v3 01/12] perf script: Make printing flags reliable

Hi Adrian,

Quoted 5 lines · Adrian Hunter
Quoted 2 lines · Leo Yan

Add a check for the generated string of flags. Print out the raw number if the string generation fails.

How does it fail?

In practice, I agreed perf_sample__sprintf_flags() will not fail. This bases on a careful calculation for every invoking snprintf().

Please see comment below.

Quoted 11 lines
Quoted 11 lines

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(-)
Quoted 14 lines

tools/perf/builtin-script.c static int perf_sample__fprintf_bts(struct perf_sample *sample, · line 1715

Quoted 12 lines
tools/perf/builtin-script.c +4 -0
index d797cec4f054..2c4b1fb7dc72 100644
@@ -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)

AFAICT ret is always >= 0

Since I refactored perf_sample__sprintf_flags() in the sequential patches, for easier capturing and debugging, here checks the return value to detect any potential issues.

Later when we review a perf log, a printed raw number for error cases can remind there must be something wrong for printing flags.

Quoted 10 lines

tools/perf/builtin-script.c static int perf_sample__fprintf_bts(struct perf_sample *sample, · line 1720

Quoted 7 lines
tools/perf/builtin-script.c +4 -2
+		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);

-21 means the field width is 21 and left-justified. It should not truncate the string.

No, it does not truncate the string.

It calculates a maximum value between the returned length and 21 ( defined in SAMPLE_FLAGS_STR_ALIGNED_SIZE). It keeps left-justified and can printing a complete string if the string length is bigger than 21.

Thanks,
Leo

Quoted 18 lines
Quoted 17 lines
tools/perf/builtin-script.c +0 -0
 }
 
 struct printer_data {
tools/perf/util/trace-event.h +2 -0
index ac9fde2f980c..71e680bc3d4b 100644
@@ -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)
reviewer
#4 lore raw

Re: [PATCH v3 01/12] perf script: Make printing flags reliable

Quoted 58 lines · Leo Yan

Hi Adrian,

Quoted 5 lines · Adrian Hunter
Quoted 2 lines · Leo Yan

Add a check for the generated string of flags. Print out the raw number if the string generation fails.

How does it fail?

In practice, I agreed perf_sample__sprintf_flags() will not fail. This bases on a careful calculation for every invoking snprintf().

Please see comment below.

Quoted 11 lines
Quoted 11 lines

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(-)
Quoted 14 lines

tools/perf/builtin-script.c static int perf_sample__fprintf_bts(struct perf_sample *sample, · line 1715

Quoted 12 lines
tools/perf/builtin-script.c +4 -0
index d797cec4f054..2c4b1fb7dc72 100644
@@ -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)

AFAICT ret is always >= 0

Since I refactored perf_sample__sprintf_flags() in the sequential patches, for easier capturing and debugging, here checks the return value to detect any potential issues.

Later when we review a perf log, a printed raw number for error cases can remind there must be something wrong for printing flags.

Quoted 10 lines

tools/perf/builtin-script.c static int perf_sample__fprintf_bts(struct perf_sample *sample, · line 1720

Quoted 7 lines
tools/perf/builtin-script.c +4 -2
+		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);

-21 means the field width is 21 and left-justified. It should not truncate the string.

Quoted 5 lines

No, it does not truncate the string.

It calculates a maximum value between the returned length and 21 ( defined in SAMPLE_FLAGS_STR_ALIGNED_SIZE). It keeps left-justified and can printing a complete string if the string length is bigger than 21.

Maybe I am missing something, but that isn't that what

	return fprintf(fp, "  %-21s ", str);

does anyway? Why change it to something more complicated.

Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
tools/perf/builtin-script.c +0 -0
 }
 
 struct printer_data {
tools/perf/util/trace-event.h +2 -0
index ac9fde2f980c..71e680bc3d4b 100644
@@ -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)
author
#5 lore raw

Re: [PATCH v3 01/12] perf script: Make printing flags reliable

On Mon, Mar 03, 2025 at 05:05:13PM +0200, Adrian Hunter wrote:

[...]

Quoted 12 lines
Quoted 11 lines
Quoted 5 lines

tools/perf/builtin-script.c static int perf_sample__fprintf_bts(struct perf_sample *sample, · line 1720

Quoted 2 lines
tools/perf/builtin-script.c +2 -0
+	ret = max(ret, SAMPLE_FLAGS_STR_ALIGNED_SIZE);
+	return fprintf(fp, "  %-*s ", ret, str);

-21 means the field width is 21 and left-justified. It should not truncate the string.

No, it does not truncate the string.

It calculates a maximum value between the returned length and 21 ( defined in SAMPLE_FLAGS_STR_ALIGNED_SIZE). It keeps left-justified and can printing a complete string if the string length is bigger than 21.

Quoted 5 lines

Maybe I am missing something, but that isn't that what

	return fprintf(fp, "  %-21s ", str);

does anyway? Why change it to something more complicated.

You are right. I should have done an experiment for this.

I will remove the max() sentence and update the last line:

        return fprintf(fp, "  %-*s ", SAMPLE_FLAGS_STR_ALIGNED_SIZE, str);

Another option is to drop this patch. But I prefer to keep it, the reason is except the benefit for the debugging log, an extra reason is the SAMPLE_FLAGS_STR_ALIGNED_SIZE macro is used to replace the opened value '21'. The macro also will be used by a later patch for right-alignment printing. How about you think?

Thanks,
Leo

reviewer
#6 lore raw

Re: [PATCH v3 01/12] perf script: Make printing flags reliable

Quoted 26 lines · Leo Yan

On Mon, Mar 03, 2025 at 05:05:13PM +0200, Adrian Hunter wrote:

[...]

Quoted 12 lines
Quoted 11 lines
Quoted 5 lines

tools/perf/builtin-script.c static int perf_sample__fprintf_bts(struct perf_sample *sample, · line 1720

Quoted 2 lines
tools/perf/builtin-script.c +2 -0
+	ret = max(ret, SAMPLE_FLAGS_STR_ALIGNED_SIZE);
+	return fprintf(fp, "  %-*s ", ret, str);

-21 means the field width is 21 and left-justified. It should not truncate the string.

No, it does not truncate the string.

It calculates a maximum value between the returned length and 21 ( defined in SAMPLE_FLAGS_STR_ALIGNED_SIZE). It keeps left-justified and can printing a complete string if the string length is bigger than 21.

Quoted 5 lines

Maybe I am missing something, but that isn't that what

	return fprintf(fp, "  %-21s ", str);

does anyway? Why change it to something more complicated.

You are right. I should have done an experiment for this.

I will remove the max() sentence and update the last line:

Quoted 7 lines
        return fprintf(fp, "  %-*s ", SAMPLE_FLAGS_STR_ALIGNED_SIZE, str);

Another option is to drop this patch. But I prefer to keep it, the reason is except the benefit for the debugging log, an extra reason is the SAMPLE_FLAGS_STR_ALIGNED_SIZE macro is used to replace the opened value '21'. The macro also will be used by a later patch for right-alignment printing. How about you think?

Sure

author
#7 lore raw

Re: [PATCH v3 01/12] perf script: Make printing flags reliable

Quoted 29 lines · Adrian Hunter
Quoted 28 lines · Leo Yan

On Mon, Mar 03, 2025 at 05:05:13PM +0200, Adrian Hunter wrote:

[...]

Quoted 12 lines
Quoted 11 lines
Quoted 5 lines

tools/perf/builtin-script.c static int perf_sample__fprintf_bts(struct perf_sample *sample, · line 1720

Quoted 2 lines
tools/perf/builtin-script.c +2 -0
+	ret = max(ret, SAMPLE_FLAGS_STR_ALIGNED_SIZE);
+	return fprintf(fp, "  %-*s ", ret, str);

-21 means the field width is 21 and left-justified. It should not truncate the string.

No, it does not truncate the string.

It calculates a maximum value between the returned length and 21 ( defined in SAMPLE_FLAGS_STR_ALIGNED_SIZE). It keeps left-justified and can printing a complete string if the string length is bigger than 21.

Quoted 5 lines

Maybe I am missing something, but that isn't that what

	return fprintf(fp, "  %-21s ", str);

does anyway? Why change it to something more complicated.

You are right. I should have done an experiment for this.

I will remove the max() sentence and update the last line:

        return fprintf(fp, "  %-*s ", SAMPLE_FLAGS_STR_ALIGNED_SIZE, str);
Quoted 7 lines
Quoted 5 lines

Another option is to drop this patch. But I prefer to keep it, the reason is except the benefit for the debugging log, an extra reason is the SAMPLE_FLAGS_STR_ALIGNED_SIZE macro is used to replace the opened value '21'. The macro also will be used by a later patch for right-alignment printing. How about you think?

Sure

Thanks a lot for confirmation and review!

I will send new version in my tomorrow.

Leo

author
#8 lore raw

PATCH v3 2/12 perf script: Refactor sample_flags_to_name() function

In reply to Leo Yan

When 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
index 2744c54f404e..cd75efc09834 100644
@@ -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
author
#9 lore raw

PATCH v3 3/12 perf script: Separate events from branch types

In reply to Leo Yan

Branch 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
index cd75efc09834..962fbc1714cf 100644
@@ -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
index 712ba3a51bbe..55d7e4e612d5 100644
@@ -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
author
#10 lore raw

PATCH v3 4/12 perf script: Add not taken event for branches

In reply to Leo Yan

Some 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
index 962fbc1714cf..c7f4b4b841ca 100644
@@ -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
index 55d7e4e612d5..29cc467be14a 100644
@@ -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
author
#11 lore raw

PATCH v3 5/12 perf script: Add not taken event for branch stack

In reply to Leo Yan

The 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
index 2c4b1fb7dc72..43bf327bb72b 100644
@@ -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
index b80c12c74bbb..7429530fa774 100644
@@ -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
author
#12 lore raw

PATCH v3 6/12 perf arm-spe: Fix load-store operation checking

In reply to Leo Yan

The ARM_SPE_OP_LD and ARM_SPE_OP_ST operations are secondary operation type, they are overlapping with other second level's operation types belonging to SVE and branch operations. As a result, a non load-store operation can be parsed for data source and memory sample.

To fix the issue, this commit introduces a is_ldst_op() macro for checking LDST operation, and apply the checking when synthesize data source and memory samples.

Fixes a89dbc9b988f ("perf arm-spe: Set sample's data source field")
---
 tools/perf/util/arm-spe.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
tools/perf/util/arm-spe.c +7 -1
index 251d214adf7f..0e8e05c87fd7 100644
@@ -37,6 +37,8 @@
 #include "../../arch/arm64/include/asm/cputype.h"
 #define MAX_TIMESTAMP (~0ULL)
 
+#define is_ldst_op(op)		(!!((op) & ARM_SPE_OP_LDST))
+
 struct arm_spe {
 	struct auxtrace			auxtrace;
 	struct auxtrace_queues		queues;
@@ -681,6 +683,10 @@ static u64 arm_spe__synth_data_source(struct arm_spe_queue *speq,
 {
 	union perf_mem_data_src	data_src = { .mem_op = PERF_MEM_OP_NA };
 
+	/* Only synthesize data source for LDST operations */
+	if (!is_ldst_op(record->op))
+		return 0;
+
 	if (record->op & ARM_SPE_OP_LD)
 		data_src.mem_op = PERF_MEM_OP_LOAD;
 	else if (record->op & ARM_SPE_OP_ST)
@@ -779,7 +785,7 @@ static int arm_spe_sample(struct arm_spe_queue *speq)
 	 * When data_src is zero it means the record is not a memory operation,
 	 * skip to synthesize memory sample for this case.
 	 */
-	if (spe->sample_memory && data_src) {
+	if (spe->sample_memory && is_ldst_op(record->op)) {
 		err = arm_spe__synth_mem_sample(speq, spe->memory_id, data_src);
 		if (err)
 			return err;
Signature · 1 line
2.34.1
reviewer Reviewed-by on v3 6/12
#13 lore raw

Re: [PATCH v3 06/12] perf arm-spe: Fix load-store operation checking

Quoted 40 lines · Leo Yan

The ARM_SPE_OP_LD and ARM_SPE_OP_ST operations are secondary operation type, they are overlapping with other second level's operation types belonging to SVE and branch operations. As a result, a non load-store operation can be parsed for data source and memory sample.

To fix the issue, this commit introduces a is_ldst_op() macro for checking LDST operation, and apply the checking when synthesize data source and memory samples.

Fixes a89dbc9b988f ("perf arm-spe: Set sample's data source field")
---
  tools/perf/util/arm-spe.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
tools/perf/util/arm-spe.c +6 -0
index 251d214adf7f..0e8e05c87fd7 100644
@@ -37,6 +37,8 @@
  #include "../../arch/arm64/include/asm/cputype.h"
  #define MAX_TIMESTAMP (~0ULL)
 
+#define is_ldst_op(op)		(!!((op) & ARM_SPE_OP_LDST))
+
  struct arm_spe {
  	struct auxtrace			auxtrace;
  	struct auxtrace_queues		queues;
@@ -681,6 +683,10 @@ static u64 arm_spe__synth_data_source(struct arm_spe_queue *speq,
  {
  	union perf_mem_data_src	data_src = { .mem_op = PERF_MEM_OP_NA };
 
+	/* Only synthesize data source for LDST operations */
+	if (!is_ldst_op(record->op))
+		return 0;
+
  	if (record->op & ARM_SPE_OP_LD)
  		data_src.mem_op = PERF_MEM_OP_LOAD;
  	else if (record->op & ARM_SPE_OP_ST)

tools/perf/util/arm-spe.c static int arm_spe_sample(struct arm_spe_queue *speq) · line 791

Quoted 8 lines
tools/perf/util/arm-spe.c +1 -1
@@ -779,7 +785,7 @@ static int arm_spe_sample(struct arm_spe_queue *speq)
  	 * When data_src is zero it means the record is not a memory operation,
  	 * skip to synthesize memory sample for this case.
  	 */
-	if (spe->sample_memory && data_src) {
+	if (spe->sample_memory && is_ldst_op(record->op)) {
  		err = arm_spe__synth_mem_sample(speq, spe->memory_id, data_src);
  		if (err)
  			return err;
author
#14 lore raw

PATCH v3 7/12 perf arm-spe: Extend branch operations

In reply to Leo Yan

In 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
index 4cef10a83962..625834da7e20 100644
@@ -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
index 464a912b221c..32d760ede701 100644
@@ -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
author
#15 lore raw

PATCH v3 8/12 perf arm-spe: Decode transactional event

In reply to Leo Yan

The 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
index 625834da7e20..13cadb2f1cea 100644
@@ -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
index 32d760ede701..2cdf9f6da268 100644
@@ -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
author
#16 lore raw

PATCH v3 9/12 perf arm-spe: Fill branch operations and events to record

In reply to Leo Yan

The 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
index ba807071d3c1..52bd0a4ea96d 100644
@@ -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
index 4bcd627e859f..85b688a97436 100644
@@ -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
author
#17 lore raw

PATCH v3 10/12 perf arm-spe: Set sample flags with supplement info

In reply to Leo Yan

Based 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
index 0e8e05c87fd7..daecf9e1017a 100644
@@ -484,6 +484,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
author
#18 lore raw

PATCH v3 11/12 perf arm-spe: Add branch stack

In reply to Leo Yan

Although 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
index daecf9e1017a..ed89b7dbc244 100644
@@ -103,6 +103,7 @@ struct arm_spe_queue {
 	struct thread			*thread;
 	u64				period_instructions;
 	u32				flags;
+	struct branch_stack		*last_branch;
 };
 
 struct data_source_handle {
@@ -233,6 +234,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(&params);
 	if (!speq->decoder)
@@ -242,6 +253,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;
@@ -348,6 +360,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);
@@ -416,6 +495,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);
@@ -450,6 +530,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);
@@ -787,6 +868,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)
@@ -1278,6 +1363,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);
 }
 
@@ -1497,6 +1583,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
author
#19 lore raw

PATCH v3 12/12 perf arm-spe: Support previous branch target (PBT) address

In reply to Leo Yan

When 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                   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
index 52bd0a4ea96d..688fe6d75244 100644
@@ -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
index 85b688a97436..5d232188643b 100644
@@ -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
author
#20 lore raw
In reply to Leo Yan

Hi Arnaldo, Namhyung,

Quoted 3 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.

James has given review tags (including the new added patch 06). Could you kindly pick up this series?

Sorry for pushing a bit, as we have dependency on it.

Thanks,
Leo

maintainer
#21 lore raw
Quoted 11 lines · Leo Yan

Hi Arnaldo, Namhyung,

Quoted 3 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.

James has given review tags (including the new added patch 06). Could you kindly pick up this series?

Sorry for pushing a bit, as we have dependency on it.

No problem, I'll add it to the tmp branch and run some tests.

Thanks,
Namhyung

author
#22 lore raw
Quoted 8 lines · Namhyung Kim
Quoted 7 lines · Leo Yan

Hi Arnaldo, Namhyung,

Quoted 3 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.

Quoted 6 lines
Quoted 4 lines

James has given review tags (including the new added patch 06). Could you kindly pick up this series?

Sorry for pushing a bit, as we have dependency on it.

No problem, I'll add it to the tmp branch and run some tests.

Thanks a lot, Namhyung!

I saw you have picked patch series v3 in the tmp.perf-tools-next branch. On the other hand, to follow up on Adrian's comment, I have sent out v4 to remove an unnecessary change in patch 01, and the rest of the patches are exactly the same as v3.

Please consider picking the latest change (either the whole v4 series or just the updated patch 01 would be fine).

Thanks,
Leo

maintainer
#23 lore raw
Quoted 18 lines · Leo Yan
Quoted 8 lines · Namhyung Kim
Quoted 7 lines · Leo Yan

Hi Arnaldo, Namhyung,

Quoted 3 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.

Quoted 6 lines
Quoted 4 lines

James has given review tags (including the new added patch 06). Could you kindly pick up this series?

Sorry for pushing a bit, as we have dependency on it.

No problem, I'll add it to the tmp branch and run some tests.

Thanks a lot, Namhyung!

Quoted 7 lines

I saw you have picked patch series v3 in the tmp.perf-tools-next branch. On the other hand, to follow up on Adrian's comment, I have sent out v4 to remove an unnecessary change in patch 01, and the rest of the patches are exactly the same as v3.

Please consider picking the latest change (either the whole v4 series or just the updated patch 01 would be fine).

Sure, I'll add v4 to the tmp branch soon.

Thanks,
Namhyung

author
#24 lore raw

On Tue, Mar 04, 2025 at 10:43:31AM -0800, Namhyung Kim wrote:

[...]

Quoted 4 lines
Quoted 2 lines

Please consider picking the latest change (either the whole v4 series or just the updated patch 01 would be fine).

Sure, I'll add v4 to the tmp branch soon.

Thanks, Namhyung! Also thanks Adrian's review for v4!

Leo