[PATCH v1 05/11] perf script: Add not taken event for branch stack

From: Leo Yan
Date: Wed Feb 05 2025 - 07:17:26 EST


The branch stack has an existed field for printing mispredict, extend
the field for printing events and add support not-taken event.

Signed-off-by: Leo Yan <leo.yan@xxxxxxx>
---
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 e7f79a3f1fd2..5e1c9a803d66 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -929,19 +929,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..99338f26f5e5 100644
--- a/tools/perf/util/branch.h
+++ b/tools/perf/util/branch.h
@@ -18,6 +18,7 @@ struct branch_flags {
struct {
u64 mispred:1;
u64 predicted:1;
+ u64 not_taken:1;
u64 in_tx:1;
u64 abort:1;
u64 cycles:16;
@@ -25,7 +26,7 @@ struct branch_flags {
u64 spec:2;
u64 new_type:4;
u64 priv:3;
- u64 reserved:31;
+ u64 reserved:30;
};
};
};
--
2.34.1