* [PATCH v3 01/12] perf script: Make printing flags reliable
2025-02-17 19:58 [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE Leo Yan
@ 2025-02-17 19:58 ` Leo Yan
2025-03-03 10:44 ` Adrian Hunter
2025-02-17 19:58 ` [PATCH v3 02/12] perf script: Refactor sample_flags_to_name() function Leo Yan
` (11 subsequent siblings)
12 siblings, 1 reply; 25+ messages in thread
From: Leo Yan @ 2025-02-17 19:58 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, James Clark,
Mike Leach, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Adrian Hunter, Liang, Kan, Will Deacon, Graham Woodward,
Paschalis.Mpeis, linux-perf-users, linux-kernel, linux-arm-kernel
Cc: Leo Yan
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.
Reviewed-by: Ian Rogers <irogers@google.com>
Reviewed-by: James Clark <james.clark@linaro.org>
Signed-off-by: Leo Yan <leo.yan@arm.com>
---
tools/perf/builtin-script.c | 10 ++++++++--
tools/perf/util/trace-event.h | 2 ++
2 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index d797cec4f054..2c4b1fb7dc72 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -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 {
diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h
index ac9fde2f980c..71e680bc3d4b 100644
--- a/tools/perf/util/trace-event.h
+++ b/tools/perf/util/trace-event.h
@@ -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)
--
2.34.1
^ permalink raw reply related [flat|nested] 25+ messages in thread
* Re: [PATCH v3 01/12] perf script: Make printing flags reliable
2025-02-17 19:58 ` [PATCH v3 01/12] perf script: Make printing flags reliable Leo Yan
@ 2025-03-03 10:44 ` Adrian Hunter
2025-03-03 16:22 ` Leo Yan
0 siblings, 1 reply; 25+ messages in thread
From: Adrian Hunter @ 2025-03-03 10:44 UTC (permalink / raw)
To: Leo Yan, Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers,
James Clark, Mike Leach, Mark Rutland, Alexander Shishkin,
Jiri Olsa, Liang, Kan, Will Deacon, Graham Woodward,
Paschalis.Mpeis, linux-perf-users, linux-kernel, linux-arm-kernel
On 17/02/25 21:58, Leo Yan wrote:
> Add a check for the generated string of flags. Print out the raw number
> if the string generation fails.
How does it fail?
>
> In another case, if the string length is longer than the aligned size,
> allow the completed string to be printed.
>
> Reviewed-by: Ian Rogers <irogers@google.com>
> Reviewed-by: James Clark <james.clark@linaro.org>
> Signed-off-by: Leo Yan <leo.yan@arm.com>
> ---
> tools/perf/builtin-script.c | 10 ++++++++--
> tools/perf/util/trace-event.h | 2 ++
> 2 files changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
> index d797cec4f054..2c4b1fb7dc72 100644
> --- a/tools/perf/builtin-script.c
> +++ b/tools/perf/builtin-script.c
> @@ -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
> + 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.
> }
>
> struct printer_data {
> diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h
> index ac9fde2f980c..71e680bc3d4b 100644
> --- a/tools/perf/util/trace-event.h
> +++ b/tools/perf/util/trace-event.h
> @@ -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)
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH v3 01/12] perf script: Make printing flags reliable
2025-03-03 10:44 ` Adrian Hunter
@ 2025-03-03 16:22 ` Leo Yan
2025-03-03 15:05 ` Adrian Hunter
0 siblings, 1 reply; 25+ messages in thread
From: Leo Yan @ 2025-03-03 16:22 UTC (permalink / raw)
To: Adrian Hunter
Cc: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, James Clark,
Mike Leach, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Liang, Kan, Will Deacon, Graham Woodward, Paschalis.Mpeis,
linux-perf-users, linux-kernel, linux-arm-kernel
Hi Adrian,
On Mon, Mar 03, 2025 at 12:44:33PM +0200, Adrian Hunter wrote:
> On 17/02/25 21:58, Leo Yan wrote:
> > 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.
> > In another case, if the string length is longer than the aligned size,
> > allow the completed string to be printed.
> >
> > Reviewed-by: Ian Rogers <irogers@google.com>
> > Reviewed-by: James Clark <james.clark@linaro.org>
> > Signed-off-by: Leo Yan <leo.yan@arm.com>
> > ---
> > tools/perf/builtin-script.c | 10 ++++++++--
> > tools/perf/util/trace-event.h | 2 ++
> > 2 files changed, 10 insertions(+), 2 deletions(-)
> >
> > diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
> > index d797cec4f054..2c4b1fb7dc72 100644
> > --- a/tools/perf/builtin-script.c
> > +++ b/tools/perf/builtin-script.c
> > @@ -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.
> > + 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
>
> > }
> >
> > struct printer_data {
> > diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h
> > index ac9fde2f980c..71e680bc3d4b 100644
> > --- a/tools/perf/util/trace-event.h
> > +++ b/tools/perf/util/trace-event.h
> > @@ -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)
>
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH v3 01/12] perf script: Make printing flags reliable
2025-03-03 16:22 ` Leo Yan
@ 2025-03-03 15:05 ` Adrian Hunter
2025-03-03 18:11 ` Leo Yan
0 siblings, 1 reply; 25+ messages in thread
From: Adrian Hunter @ 2025-03-03 15:05 UTC (permalink / raw)
To: Leo Yan
Cc: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, James Clark,
Mike Leach, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Liang, Kan, Will Deacon, Graham Woodward, Paschalis.Mpeis,
linux-perf-users, linux-kernel, linux-arm-kernel
On 3/03/25 18:22, Leo Yan wrote:
> Hi Adrian,
>
> On Mon, Mar 03, 2025 at 12:44:33PM +0200, Adrian Hunter wrote:
>> On 17/02/25 21:58, Leo Yan wrote:
>>> 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.
>
>>> In another case, if the string length is longer than the aligned size,
>>> allow the completed string to be printed.
>>>
>>> Reviewed-by: Ian Rogers <irogers@google.com>
>>> Reviewed-by: James Clark <james.clark@linaro.org>
>>> Signed-off-by: Leo Yan <leo.yan@arm.com>
>>> ---
>>> tools/perf/builtin-script.c | 10 ++++++++--
>>> tools/perf/util/trace-event.h | 2 ++
>>> 2 files changed, 10 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
>>> index d797cec4f054..2c4b1fb7dc72 100644
>>> --- a/tools/perf/builtin-script.c
>>> +++ b/tools/perf/builtin-script.c
>>> @@ -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.
>
>>> + 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.
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.
>
>
>>
>>> }
>>>
>>> struct printer_data {
>>> diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h
>>> index ac9fde2f980c..71e680bc3d4b 100644
>>> --- a/tools/perf/util/trace-event.h
>>> +++ b/tools/perf/util/trace-event.h
>>> @@ -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)
>>
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH v3 01/12] perf script: Make printing flags reliable
2025-03-03 15:05 ` Adrian Hunter
@ 2025-03-03 18:11 ` Leo Yan
2025-03-03 16:56 ` Adrian Hunter
0 siblings, 1 reply; 25+ messages in thread
From: Leo Yan @ 2025-03-03 18:11 UTC (permalink / raw)
To: Adrian Hunter
Cc: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, James Clark,
Mike Leach, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Liang, Kan, Will Deacon, Graham Woodward, Paschalis.Mpeis,
linux-perf-users, linux-kernel, linux-arm-kernel
On Mon, Mar 03, 2025 at 05:05:13PM +0200, Adrian Hunter wrote:
[...]
> >>> + 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.
>
> 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
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH v3 01/12] perf script: Make printing flags reliable
2025-03-03 18:11 ` Leo Yan
@ 2025-03-03 16:56 ` Adrian Hunter
2025-03-03 18:49 ` Leo Yan
0 siblings, 1 reply; 25+ messages in thread
From: Adrian Hunter @ 2025-03-03 16:56 UTC (permalink / raw)
To: Leo Yan
Cc: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, James Clark,
Mike Leach, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Liang, Kan, Will Deacon, Graham Woodward, Paschalis.Mpeis,
linux-perf-users, linux-kernel, linux-arm-kernel
On 3/03/25 20:11, Leo Yan wrote:
> On Mon, Mar 03, 2025 at 05:05:13PM +0200, Adrian Hunter wrote:
>
> [...]
>
>>>>> + 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.
>>
>> 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?
Sure
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH v3 01/12] perf script: Make printing flags reliable
2025-03-03 16:56 ` Adrian Hunter
@ 2025-03-03 18:49 ` Leo Yan
0 siblings, 0 replies; 25+ messages in thread
From: Leo Yan @ 2025-03-03 18:49 UTC (permalink / raw)
To: Adrian Hunter
Cc: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, James Clark,
Mike Leach, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Liang, Kan, Will Deacon, Graham Woodward, Paschalis.Mpeis,
linux-perf-users, linux-kernel, linux-arm-kernel
On Mon, Mar 03, 2025 at 06:56:42PM +0200, Adrian Hunter wrote:
> On 3/03/25 20:11, Leo Yan wrote:
> > On Mon, Mar 03, 2025 at 05:05:13PM +0200, Adrian Hunter wrote:
> >
> > [...]
> >
> >>>>> + 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.
> >>
> >> 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?
>
> Sure
Thanks a lot for confirmation and review!
I will send new version in my tomorrow.
Leo
^ permalink raw reply [flat|nested] 25+ messages in thread
* [PATCH v3 02/12] perf script: Refactor sample_flags_to_name() function
2025-02-17 19:58 [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE Leo Yan
2025-02-17 19:58 ` [PATCH v3 01/12] perf script: Make printing flags reliable Leo Yan
@ 2025-02-17 19:58 ` Leo Yan
2025-02-17 19:58 ` [PATCH v3 03/12] perf script: Separate events from branch types Leo Yan
` (10 subsequent siblings)
12 siblings, 0 replies; 25+ messages in thread
From: Leo Yan @ 2025-02-17 19:58 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, James Clark,
Mike Leach, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Adrian Hunter, Liang, Kan, Will Deacon, Graham Woodward,
Paschalis.Mpeis, linux-perf-users, linux-kernel, linux-arm-kernel
Cc: 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.
Reviewed-by: Ian Rogers <irogers@google.com>
Reviewed-by: James Clark <james.clark@linaro.org>
Signed-off-by: Leo Yan <leo.yan@arm.com>
---
tools/perf/util/event.h | 5 ++
tools/perf/util/trace-event-scripting.c | 85 ++++++++++++++++---------
2 files changed, 59 insertions(+), 31 deletions(-)
diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h
index 2744c54f404e..cd75efc09834 100644
--- a/tools/perf/util/event.h
+++ b/tools/perf/util/event.h
@@ -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 |\
diff --git a/tools/perf/util/trace-event-scripting.c b/tools/perf/util/trace-event-scripting.c
index 4e81e02a4f18..712ba3a51bbe 100644
--- a/tools/perf/util/trace-event-scripting.c
+++ b/tools/perf/util/trace-event-scripting.c
@@ -313,49 +313,72 @@ static const struct {
{0, NULL}
};
-static const char *sample_flags_to_name(u32 flags)
+static int sample_flags_to_name(u32 flags, char *str, size_t size)
{
int i;
-
- for (i = 0; sample_flags[i].name ; i++) {
- if (sample_flags[i].flags == flags)
- return sample_flags[i].name;
+ const char *prefix;
+ int pos = 0, ret;
+ u32 xf = flags & PERF_ADDITIONAL_STATE_MASK;
+ char xs[16] = { 0 };
+
+ /* Clear additional state bits */
+ flags &= ~PERF_ADDITIONAL_STATE_MASK;
+
+ if (flags & PERF_IP_FLAG_TRACE_BEGIN)
+ prefix = "tr strt ";
+ else if (flags & PERF_IP_FLAG_TRACE_END)
+ prefix = "tr end ";
+ else
+ prefix = "";
+
+ ret = snprintf(str + pos, size - pos, "%s", prefix);
+ if (ret < 0)
+ return ret;
+ pos += ret;
+
+ flags &= ~(PERF_IP_FLAG_TRACE_BEGIN | PERF_IP_FLAG_TRACE_END);
+
+ for (i = 0; sample_flags[i].name; i++) {
+ if (sample_flags[i].flags != flags)
+ continue;
+
+ ret = snprintf(str + pos, size - pos, "%s", sample_flags[i].name);
+ if (ret < 0)
+ return ret;
+ pos += ret;
+ break;
}
- return NULL;
+ if (!xf)
+ return pos;
+
+ snprintf(xs, sizeof(xs), "(%s%s%s)",
+ flags & PERF_IP_FLAG_IN_TX ? "x" : "",
+ flags & PERF_IP_FLAG_INTR_DISABLE ? "D" : "",
+ flags & PERF_IP_FLAG_INTR_TOGGLE ? "t" : "");
+
+ /* Right align the string if its length is less than the limit */
+ if ((pos + strlen(xs)) < SAMPLE_FLAGS_STR_ALIGNED_SIZE)
+ ret = snprintf(str + pos, size - pos, "%*s",
+ (int)(SAMPLE_FLAGS_STR_ALIGNED_SIZE - ret), xs);
+ else
+ ret = snprintf(str + pos, size - pos, " %s", xs);
+ if (ret < 0)
+ return ret;
+
+ return pos + ret;
}
int perf_sample__sprintf_flags(u32 flags, char *str, size_t sz)
{
- u32 xf = PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_INTR_DISABLE |
- PERF_IP_FLAG_INTR_TOGGLE;
const char *chars = PERF_IP_FLAG_CHARS;
const size_t n = strlen(PERF_IP_FLAG_CHARS);
- const char *name = NULL;
size_t i, pos = 0;
- char xs[16] = {0};
-
- if (flags & xf)
- snprintf(xs, sizeof(xs), "(%s%s%s)",
- flags & PERF_IP_FLAG_IN_TX ? "x" : "",
- flags & PERF_IP_FLAG_INTR_DISABLE ? "D" : "",
- flags & PERF_IP_FLAG_INTR_TOGGLE ? "t" : "");
-
- name = sample_flags_to_name(flags & ~xf);
- if (name)
- return snprintf(str, sz, "%-15s%6s", name, xs);
-
- if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
- name = sample_flags_to_name(flags & ~(xf | PERF_IP_FLAG_TRACE_BEGIN));
- if (name)
- return snprintf(str, sz, "tr strt %-7s%6s", name, xs);
- }
+ int ret;
- if (flags & PERF_IP_FLAG_TRACE_END) {
- name = sample_flags_to_name(flags & ~(xf | PERF_IP_FLAG_TRACE_END));
- if (name)
- return snprintf(str, sz, "tr end %-7s%6s", name, xs);
- }
+ ret = sample_flags_to_name(flags, str, sz);
+ if (ret > 0)
+ return ret;
for (i = 0; i < n; i++, flags >>= 1) {
if ((flags & 1) && pos < sz)
--
2.34.1
^ permalink raw reply related [flat|nested] 25+ messages in thread
* [PATCH v3 03/12] perf script: Separate events from branch types
2025-02-17 19:58 [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE Leo Yan
2025-02-17 19:58 ` [PATCH v3 01/12] perf script: Make printing flags reliable Leo Yan
2025-02-17 19:58 ` [PATCH v3 02/12] perf script: Refactor sample_flags_to_name() function Leo Yan
@ 2025-02-17 19:58 ` Leo Yan
2025-02-17 19:59 ` [PATCH v3 04/12] perf script: Add not taken event for branches Leo Yan
` (9 subsequent siblings)
12 siblings, 0 replies; 25+ messages in thread
From: Leo Yan @ 2025-02-17 19:58 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, James Clark,
Mike Leach, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Adrian Hunter, Liang, Kan, Will Deacon, Graham Woodward,
Paschalis.Mpeis, linux-perf-users, linux-kernel, linux-arm-kernel
Cc: 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/
Reviewed-by: Ian Rogers <irogers@google.com>
Reviewed-by: James Clark <james.clark@linaro.org>
Signed-off-by: Leo Yan <leo.yan@arm.com>
---
tools/perf/util/event.h | 5 +++-
tools/perf/util/trace-event-scripting.c | 36 ++++++++++++++++++++++---
2 files changed, 37 insertions(+), 4 deletions(-)
diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h
index cd75efc09834..962fbc1714cf 100644
--- a/tools/perf/util/event.h
+++ b/tools/perf/util/event.h
@@ -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) |\
diff --git a/tools/perf/util/trace-event-scripting.c b/tools/perf/util/trace-event-scripting.c
index 712ba3a51bbe..55d7e4e612d5 100644
--- a/tools/perf/util/trace-event-scripting.c
+++ b/tools/perf/util/trace-event-scripting.c
@@ -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;
--
2.34.1
^ permalink raw reply related [flat|nested] 25+ messages in thread
* [PATCH v3 04/12] perf script: Add not taken event for branches
2025-02-17 19:58 [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE Leo Yan
` (2 preceding siblings ...)
2025-02-17 19:58 ` [PATCH v3 03/12] perf script: Separate events from branch types Leo Yan
@ 2025-02-17 19:59 ` Leo Yan
2025-02-17 19:59 ` [PATCH v3 05/12] perf script: Add not taken event for branch stack Leo Yan
` (8 subsequent siblings)
12 siblings, 0 replies; 25+ messages in thread
From: Leo Yan @ 2025-02-17 19:59 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, James Clark,
Mike Leach, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Adrian Hunter, Liang, Kan, Will Deacon, Graham Woodward,
Paschalis.Mpeis, linux-perf-users, linux-kernel, linux-arm-kernel
Cc: 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.
Reviewed-by: Ian Rogers <irogers@google.com>
Reviewed-by: James Clark <james.clark@linaro.org>
Signed-off-by: Leo Yan <leo.yan@arm.com>
---
tools/perf/util/event.h | 6 ++++--
tools/perf/util/trace-event-scripting.c | 1 +
2 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h
index 962fbc1714cf..c7f4b4b841ca 100644
--- a/tools/perf/util/event.h
+++ b/tools/perf/util/event.h
@@ -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) |\
diff --git a/tools/perf/util/trace-event-scripting.c b/tools/perf/util/trace-event-scripting.c
index 55d7e4e612d5..29cc467be14a 100644
--- a/tools/perf/util/trace-event-scripting.c
+++ b/tools/perf/util/trace-event-scripting.c
@@ -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}
};
--
2.34.1
^ permalink raw reply related [flat|nested] 25+ messages in thread
* [PATCH v3 05/12] perf script: Add not taken event for branch stack
2025-02-17 19:58 [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE Leo Yan
` (3 preceding siblings ...)
2025-02-17 19:59 ` [PATCH v3 04/12] perf script: Add not taken event for branches Leo Yan
@ 2025-02-17 19:59 ` Leo Yan
2025-02-17 19:59 ` [PATCH v3 06/12] perf arm-spe: Fix load-store operation checking Leo Yan
` (7 subsequent siblings)
12 siblings, 0 replies; 25+ messages in thread
From: Leo Yan @ 2025-02-17 19:59 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, James Clark,
Mike Leach, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Adrian Hunter, Liang, Kan, Will Deacon, Graham Woodward,
Paschalis.Mpeis, linux-perf-users, linux-kernel, linux-arm-kernel
Cc: Leo Yan
The branch stack has an existed field for printing mispredict, extend
the field for printing events and add support not-taken event.
Reviewed-by: Ian Rogers <irogers@google.com>
Reviewed-by: James Clark <james.clark@linaro.org>
Signed-off-by: Leo Yan <leo.yan@arm.com>
---
tools/perf/builtin-script.c | 20 +++++++++++++-------
tools/perf/util/branch.h | 3 ++-
2 files changed, 15 insertions(+), 8 deletions(-)
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 2c4b1fb7dc72..43bf327bb72b 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -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,
diff --git a/tools/perf/util/branch.h b/tools/perf/util/branch.h
index b80c12c74bbb..7429530fa774 100644
--- a/tools/perf/util/branch.h
+++ b/tools/perf/util/branch.h
@@ -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;
};
};
};
--
2.34.1
^ permalink raw reply related [flat|nested] 25+ messages in thread
* [PATCH v3 06/12] perf arm-spe: Fix load-store operation checking
2025-02-17 19:58 [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE Leo Yan
` (4 preceding siblings ...)
2025-02-17 19:59 ` [PATCH v3 05/12] perf script: Add not taken event for branch stack Leo Yan
@ 2025-02-17 19:59 ` Leo Yan
2025-02-26 13:33 ` James Clark
2025-02-17 19:59 ` [PATCH v3 07/12] perf arm-spe: Extend branch operations Leo Yan
` (6 subsequent siblings)
12 siblings, 1 reply; 25+ messages in thread
From: Leo Yan @ 2025-02-17 19:59 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, James Clark,
Mike Leach, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Adrian Hunter, Liang, Kan, Will Deacon, Graham Woodward,
Paschalis.Mpeis, linux-perf-users, linux-kernel, linux-arm-kernel
Cc: 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")
Signed-off-by: Leo Yan <leo.yan@arm.com>
---
tools/perf/util/arm-spe.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
index 251d214adf7f..0e8e05c87fd7 100644
--- a/tools/perf/util/arm-spe.c
+++ b/tools/perf/util/arm-spe.c
@@ -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;
--
2.34.1
^ permalink raw reply related [flat|nested] 25+ messages in thread
* Re: [PATCH v3 06/12] perf arm-spe: Fix load-store operation checking
2025-02-17 19:59 ` [PATCH v3 06/12] perf arm-spe: Fix load-store operation checking Leo Yan
@ 2025-02-26 13:33 ` James Clark
0 siblings, 0 replies; 25+ messages in thread
From: James Clark @ 2025-02-26 13:33 UTC (permalink / raw)
To: Leo Yan
Cc: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, Mike Leach,
Mark Rutland, Alexander Shishkin, Jiri Olsa, Adrian Hunter,
Liang, Kan, Will Deacon, Graham Woodward, Paschalis.Mpeis,
linux-perf-users, linux-kernel, linux-arm-kernel
On 17/02/2025 7:59 pm, Leo Yan wrote:
> 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")
> Signed-off-by: Leo Yan <leo.yan@arm.com>
> ---
> tools/perf/util/arm-spe.c | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
> index 251d214adf7f..0e8e05c87fd7 100644
> --- a/tools/perf/util/arm-spe.c
> +++ b/tools/perf/util/arm-spe.c
> @@ -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;
Reviewed-by: James Clark <james.clark@linaro.org>
^ permalink raw reply [flat|nested] 25+ messages in thread
* [PATCH v3 07/12] perf arm-spe: Extend branch operations
2025-02-17 19:58 [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE Leo Yan
` (5 preceding siblings ...)
2025-02-17 19:59 ` [PATCH v3 06/12] perf arm-spe: Fix load-store operation checking Leo Yan
@ 2025-02-17 19:59 ` Leo Yan
2025-02-17 19:59 ` [PATCH v3 08/12] perf arm-spe: Decode transactional event Leo Yan
` (5 subsequent siblings)
12 siblings, 0 replies; 25+ messages in thread
From: Leo Yan @ 2025-02-17 19:59 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, James Clark,
Mike Leach, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Adrian Hunter, Liang, Kan, Will Deacon, Graham Woodward,
Paschalis.Mpeis, linux-perf-users, linux-kernel, linux-arm-kernel
Cc: 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.
Reviewed-by: Ian Rogers <irogers@google.com>
Reviewed-by: James Clark <james.clark@linaro.org>
Signed-off-by: Leo Yan <leo.yan@arm.com>
---
.../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(-)
diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index 4cef10a83962..625834da7e20 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -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 */
diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
index 464a912b221c..32d760ede701 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
@@ -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);
--
2.34.1
^ permalink raw reply related [flat|nested] 25+ messages in thread
* [PATCH v3 08/12] perf arm-spe: Decode transactional event
2025-02-17 19:58 [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE Leo Yan
` (6 preceding siblings ...)
2025-02-17 19:59 ` [PATCH v3 07/12] perf arm-spe: Extend branch operations Leo Yan
@ 2025-02-17 19:59 ` Leo Yan
2025-02-17 19:59 ` [PATCH v3 09/12] perf arm-spe: Fill branch operations and events to record Leo Yan
` (4 subsequent siblings)
12 siblings, 0 replies; 25+ messages in thread
From: Leo Yan @ 2025-02-17 19:59 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, James Clark,
Mike Leach, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Adrian Hunter, Liang, Kan, Will Deacon, Graham Woodward,
Paschalis.Mpeis, linux-perf-users, linux-kernel, linux-arm-kernel
Cc: Leo Yan
The bit[16] in an event payload indicates an operation is in
transactional state. Decode the bit.
Reviewed-by: Ian Rogers <irogers@google.com>
Reviewed-by: James Clark <james.clark@linaro.org>
Signed-off-by: Leo Yan <leo.yan@arm.com>
---
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(+)
diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
index 625834da7e20..13cadb2f1cea 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.c
@@ -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))
diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
index 32d760ede701..2cdf9f6da268 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-pkt-decoder.h
@@ -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,
};
--
2.34.1
^ permalink raw reply related [flat|nested] 25+ messages in thread
* [PATCH v3 09/12] perf arm-spe: Fill branch operations and events to record
2025-02-17 19:58 [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE Leo Yan
` (7 preceding siblings ...)
2025-02-17 19:59 ` [PATCH v3 08/12] perf arm-spe: Decode transactional event Leo Yan
@ 2025-02-17 19:59 ` Leo Yan
2025-02-17 19:59 ` [PATCH v3 10/12] perf arm-spe: Set sample flags with supplement info Leo Yan
` (3 subsequent siblings)
12 siblings, 0 replies; 25+ messages in thread
From: Leo Yan @ 2025-02-17 19:59 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, James Clark,
Mike Leach, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Adrian Hunter, Liang, Kan, Will Deacon, Graham Woodward,
Paschalis.Mpeis, linux-perf-users, linux-kernel, linux-arm-kernel
Cc: 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 <irogers@google.com>
Reviewed-by: James Clark <james.clark@linaro.org>
Signed-off-by: Leo Yan <leo.yan@arm.com>
---
.../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(-)
diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c
index ba807071d3c1..52bd0a4ea96d 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c
@@ -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;
diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-decoder.h b/tools/perf/util/arm-spe-decoder/arm-spe-decoder.h
index 4bcd627e859f..85b688a97436 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-decoder.h
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-decoder.h
@@ -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 {
--
2.34.1
^ permalink raw reply related [flat|nested] 25+ messages in thread
* [PATCH v3 10/12] perf arm-spe: Set sample flags with supplement info
2025-02-17 19:58 [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE Leo Yan
` (8 preceding siblings ...)
2025-02-17 19:59 ` [PATCH v3 09/12] perf arm-spe: Fill branch operations and events to record Leo Yan
@ 2025-02-17 19:59 ` Leo Yan
2025-02-17 19:59 ` [PATCH v3 11/12] perf arm-spe: Add branch stack Leo Yan
` (2 subsequent siblings)
12 siblings, 0 replies; 25+ messages in thread
From: Leo Yan @ 2025-02-17 19:59 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, James Clark,
Mike Leach, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Adrian Hunter, Liang, Kan, Will Deacon, Graham Woodward,
Paschalis.Mpeis, linux-perf-users, linux-kernel, linux-arm-kernel
Cc: 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.
Reviewed-by: Ian Rogers <irogers@google.com>
Reviewed-by: James Clark <james.clark@linaro.org>
Signed-off-by: Leo Yan <leo.yan@arm.com>
---
tools/perf/util/arm-spe.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
index 0e8e05c87fd7..daecf9e1017a 100644
--- a/tools/perf/util/arm-spe.c
+++ b/tools/perf/util/arm-spe.c
@@ -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;
}
}
--
2.34.1
^ permalink raw reply related [flat|nested] 25+ messages in thread
* [PATCH v3 11/12] perf arm-spe: Add branch stack
2025-02-17 19:58 [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE Leo Yan
` (9 preceding siblings ...)
2025-02-17 19:59 ` [PATCH v3 10/12] perf arm-spe: Set sample flags with supplement info Leo Yan
@ 2025-02-17 19:59 ` Leo Yan
2025-02-17 19:59 ` [PATCH v3 12/12] perf arm-spe: Support previous branch target (PBT) address Leo Yan
2025-03-03 9:38 ` [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE Leo Yan
12 siblings, 0 replies; 25+ messages in thread
From: Leo Yan @ 2025-02-17 19:59 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, James Clark,
Mike Leach, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Adrian Hunter, Liang, Kan, Will Deacon, Graham Woodward,
Paschalis.Mpeis, linux-perf-users, linux-kernel, linux-arm-kernel
Cc: 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/-
Reviewed-by: Ian Rogers <irogers@google.com>
Reviewed-by: James Clark <james.clark@linaro.org>
Signed-off-by: Leo Yan <leo.yan@arm.com>
---
tools/perf/util/arm-spe.c | 99 +++++++++++++++++++++++++++++++++++++++
1 file changed, 99 insertions(+)
diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
index daecf9e1017a..ed89b7dbc244 100644
--- a/tools/perf/util/arm-spe.c
+++ b/tools/perf/util/arm-spe.c
@@ -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(¶ms);
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;
--
2.34.1
^ permalink raw reply related [flat|nested] 25+ messages in thread
* [PATCH v3 12/12] perf arm-spe: Support previous branch target (PBT) address
2025-02-17 19:58 [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE Leo Yan
` (10 preceding siblings ...)
2025-02-17 19:59 ` [PATCH v3 11/12] perf arm-spe: Add branch stack Leo Yan
@ 2025-02-17 19:59 ` Leo Yan
2025-03-03 9:38 ` [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE Leo Yan
12 siblings, 0 replies; 25+ messages in thread
From: Leo Yan @ 2025-02-17 19:59 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, James Clark,
Mike Leach, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Adrian Hunter, Liang, Kan, Will Deacon, Graham Woodward,
Paschalis.Mpeis, linux-perf-users, linux-kernel, linux-arm-kernel
Cc: 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//-
Reviewed-by: Ian Rogers <irogers@google.com>
Reviewed-by: James Clark <james.clark@linaro.org>
Signed-off-by: Leo Yan <leo.yan@arm.com>
---
.../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(-)
diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c b/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c
index 52bd0a4ea96d..688fe6d75244 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-decoder.c
@@ -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)
diff --git a/tools/perf/util/arm-spe-decoder/arm-spe-decoder.h b/tools/perf/util/arm-spe-decoder/arm-spe-decoder.h
index 85b688a97436..5d232188643b 100644
--- a/tools/perf/util/arm-spe-decoder/arm-spe-decoder.h
+++ b/tools/perf/util/arm-spe-decoder/arm-spe-decoder.h
@@ -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;
diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
index ed89b7dbc244..2a9775649cc2 100644
--- a/tools/perf/util/arm-spe.c
+++ b/tools/perf/util/arm-spe.c
@@ -237,8 +237,9 @@ static struct arm_spe_queue *arm_spe__alloc_queue(struct arm_spe *spe,
if (spe->synth_opts.last_branch) {
size_t sz = sizeof(struct branch_stack);
- /* Allocate one entry for TGT */
- sz += sizeof(struct branch_entry);
+ /* Allocate up to two entries for PBT + TGT */
+ sz += sizeof(struct branch_entry) *
+ min(spe->synth_opts.last_branch_sz, 2U);
speq->last_branch = zalloc(sz);
if (!speq->last_branch)
goto out_free;
@@ -362,68 +363,83 @@ static void arm_spe_prep_sample(struct arm_spe *spe,
static void arm_spe__prep_branch_stack(struct arm_spe_queue *speq)
{
+ struct arm_spe *spe = speq->spe;
struct arm_spe_record *record = &speq->decoder->record;
struct branch_stack *bstack = speq->last_branch;
struct branch_flags *bs_flags;
+ unsigned int last_branch_sz = spe->synth_opts.last_branch_sz;
+ bool have_tgt = !!(speq->flags & PERF_IP_FLAG_BRANCH);
+ bool have_pbt = last_branch_sz >= (have_tgt + 1U) && record->prev_br_tgt;
size_t sz = sizeof(struct branch_stack) +
- sizeof(struct branch_entry) /* TGT */;
+ sizeof(struct branch_entry) * min(last_branch_sz, 2U) /* PBT + TGT */;
+ int i = 0;
/* Clean up branch stack */
memset(bstack, 0x0, sz);
- if (!(speq->flags & PERF_IP_FLAG_BRANCH))
+ if (!have_tgt && !have_pbt)
return;
- bstack->entries[0].from = record->from_ip;
- bstack->entries[0].to = record->to_ip;
+ if (have_tgt) {
+ bstack->entries[i].from = record->from_ip;
+ bstack->entries[i].to = record->to_ip;
- bs_flags = &bstack->entries[0].flags;
- bs_flags->value = 0;
+ bs_flags = &bstack->entries[i].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->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_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_BRANCH_NOT_TAKEN)
- bs_flags->not_taken = 1;
+ if (record->type & ARM_SPE_IN_TXN)
+ bs_flags->in_tx = 1;
- if (record->type & ARM_SPE_IN_TXN)
- bs_flags->in_tx = 1;
+ bs_flags->cycles = min(record->latency, 0xFFFFU);
+ i++;
+ }
- bs_flags->cycles = min(record->latency, 0xFFFFU);
+ if (have_pbt) {
+ bs_flags = &bstack->entries[i].flags;
+ bs_flags->type |= PERF_BR_UNKNOWN;
+ bstack->entries[i].to = record->prev_br_tgt;
+ i++;
+ }
- bstack->nr = 1;
+ bstack->nr = i;
bstack->hw_idx = -1ULL;
}
@@ -1584,8 +1600,8 @@ arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
}
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");
+ if (spe->synth_opts.last_branch_sz > 2)
+ pr_debug("Arm SPE supports only two bstack entries (PBT+TGT).\n");
attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
/*
--
2.34.1
^ permalink raw reply related [flat|nested] 25+ messages in thread
* Re: [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE
2025-02-17 19:58 [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE Leo Yan
` (11 preceding siblings ...)
2025-02-17 19:59 ` [PATCH v3 12/12] perf arm-spe: Support previous branch target (PBT) address Leo Yan
@ 2025-03-03 9:38 ` Leo Yan
2025-03-03 21:00 ` Namhyung Kim
12 siblings, 1 reply; 25+ messages in thread
From: Leo Yan @ 2025-03-03 9:38 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo, Namhyung Kim, Ian Rogers, James Clark,
Mike Leach, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Adrian Hunter, Liang, Kan, Will Deacon, Graham Woodward,
Paschalis.Mpeis, linux-perf-users, linux-kernel, linux-arm-kernel
Hi Arnaldo, Namhyung,
On Mon, Feb 17, 2025 at 07:58:56PM +0000, Leo Yan wrote:
> 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
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE
2025-03-03 9:38 ` [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE Leo Yan
@ 2025-03-03 21:00 ` Namhyung Kim
2025-03-04 11:33 ` Leo Yan
0 siblings, 1 reply; 25+ messages in thread
From: Namhyung Kim @ 2025-03-03 21:00 UTC (permalink / raw)
To: Leo Yan
Cc: Arnaldo Carvalho de Melo, Ian Rogers, James Clark, Mike Leach,
Mark Rutland, Alexander Shishkin, Jiri Olsa, Adrian Hunter,
Liang, Kan, Will Deacon, Graham Woodward, Paschalis.Mpeis,
linux-perf-users, linux-kernel, linux-arm-kernel
On Mon, Mar 03, 2025 at 09:38:53AM +0000, Leo Yan wrote:
> Hi Arnaldo, Namhyung,
>
> On Mon, Feb 17, 2025 at 07:58:56PM +0000, Leo Yan wrote:
> > 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
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE
2025-03-03 21:00 ` Namhyung Kim
@ 2025-03-04 11:33 ` Leo Yan
2025-03-04 18:43 ` Namhyung Kim
0 siblings, 1 reply; 25+ messages in thread
From: Leo Yan @ 2025-03-04 11:33 UTC (permalink / raw)
To: Namhyung Kim
Cc: Arnaldo Carvalho de Melo, Ian Rogers, James Clark, Mike Leach,
Mark Rutland, Alexander Shishkin, Jiri Olsa, Adrian Hunter,
Liang, Kan, Will Deacon, Graham Woodward, Paschalis.Mpeis,
linux-perf-users, linux-kernel, linux-arm-kernel
On Mon, Mar 03, 2025 at 01:00:19PM -0800, Namhyung Kim wrote:
> On Mon, Mar 03, 2025 at 09:38:53AM +0000, Leo Yan wrote:
> > Hi Arnaldo, Namhyung,
> >
> > On Mon, Feb 17, 2025 at 07:58:56PM +0000, Leo Yan wrote:
> > > 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 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
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE
2025-03-04 11:33 ` Leo Yan
@ 2025-03-04 18:43 ` Namhyung Kim
2025-03-05 9:53 ` Leo Yan
0 siblings, 1 reply; 25+ messages in thread
From: Namhyung Kim @ 2025-03-04 18:43 UTC (permalink / raw)
To: Leo Yan
Cc: Arnaldo Carvalho de Melo, Ian Rogers, James Clark, Mike Leach,
Mark Rutland, Alexander Shishkin, Jiri Olsa, Adrian Hunter,
Liang, Kan, Will Deacon, Graham Woodward, Paschalis.Mpeis,
linux-perf-users, linux-kernel, linux-arm-kernel
On Tue, Mar 04, 2025 at 11:33:50AM +0000, Leo Yan wrote:
> On Mon, Mar 03, 2025 at 01:00:19PM -0800, Namhyung Kim wrote:
> > On Mon, Mar 03, 2025 at 09:38:53AM +0000, Leo Yan wrote:
> > > Hi Arnaldo, Namhyung,
> > >
> > > On Mon, Feb 17, 2025 at 07:58:56PM +0000, Leo Yan wrote:
> > > > 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 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).
Sure, I'll add v4 to the tmp branch soon.
Thanks,
Namhyung
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH v3 00/12] perf script: Refactor branch flags for Arm SPE
2025-03-04 18:43 ` Namhyung Kim
@ 2025-03-05 9:53 ` Leo Yan
0 siblings, 0 replies; 25+ messages in thread
From: Leo Yan @ 2025-03-05 9:53 UTC (permalink / raw)
To: Namhyung Kim
Cc: Arnaldo Carvalho de Melo, Ian Rogers, James Clark, Mike Leach,
Mark Rutland, Alexander Shishkin, Jiri Olsa, Adrian Hunter,
Liang, Kan, Will Deacon, Graham Woodward, Paschalis.Mpeis,
linux-perf-users, linux-kernel, linux-arm-kernel
On Tue, Mar 04, 2025 at 10:43:31AM -0800, Namhyung Kim wrote:
[...]
> > 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
^ permalink raw reply [flat|nested] 25+ messages in thread