Skip to contentSkip to conversation

perf: Fix missing branch counter information in script and annotate

PATCH Superseded Thomas Falcon 3 participants 7 messages Apr 29 – May 11 perf events

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover perf: Fix missing branch counter information in script and annotate

This series fixes two issues related to branch counter (br_cntr) display in perf.

The first patch fixes a missing '+' indicator in 'perf script' output. The '+' suffix is used to signal that event occurrences may have been lost due to branch counter overflow.

The second patch fixes a regression in 'perf annotate' TUI mode where the Branch Count column was never displayed. The check for whether to enable branch counter display was performed before events were processed and the counter data was populated, so the condition was never satisfied. Moving the check to after event processing resolves this.

v2: Added a cover letter and expanded commit message for patch 1/2.
Patch 2/2 was missing some conditional checks to enable branch counter display, which have been added back.

Dapeng Mi (1):
perf script: Fix missing '+' indicator when branch counter reaches
upper limit

Thomas Falcon (1):
perf annotate: Fix missing branch counter column in TUI mode

 tools/perf/builtin-annotate.c | 9 +++++----
 tools/perf/builtin-script.c   | 8 ++++++--
 2 files changed, 11 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 perf script: Fix missing '+' indicator when branch counter reaches upper limit

From: Dapeng Mi <[email protected]>

When displaying branch counter (br_cntr) information, a "+" suffix represents that event occurrences may have been lost due to branch counter saturation. However, this indicator was missing in perf script. Add it back.

Before:

# Branch counter abbr list:
# cpu_core/event=0xc4,umask=0x20/ppp = A

 # cpu_core/instructions/ = B
 # cpu_core/MEM_INST_RETIRED.ALL_LOADS/ = C
 # cpu_core/MEM_LOAD_RETIRED.L2_MISS/ = D
 # '-' No event occurs
 # '+' Event occurrences may be lost due to branch counter saturated
...
        datasym+190:
        00005567f9951676        jz 0x5567f995162dr_cntr: BBBC   # PRED 1 cycles [1]
...
After:
...
        datasym+190:
        00005567f9951676        jz 0x5567f995162dr_cntr: BBB+C  # PRED 1 cycles [1]
...
---
 tools/perf/builtin-script.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/perf/builtin-script.c +6 -2
index c8ac9f01a36b..f865c8b2f95f 100644
@@ -1287,8 +1287,12 @@ static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
 			if (!verbose) {
 				for (j = 0; j < num; j++)
 					printed += fprintf(fp, "%s", pos->abbr_name);
-			} else
-				printed += fprintf(fp, "%s %d ", pos->name, num);
+				if (num == mask)
+					printed += fprintf(fp, "+");
+			} else {
+				printed += fprintf(fp, "%s %d%s", pos->name,
+						   num, num == mask ? "+ " : " ");
+			}
 		}
 		if (numprinted == 0 && !verbose)
 			printed += fprintf(fp, "-");
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/2 v2] perf script: Fix missing '+' indicator when branch counter reaches upper limit

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The patch incorrectly prints the saturation indicator '+' when branch counter width is 0. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch incorrectly prints the saturation indicator '+' when branch counter width is 0. --

commit 7ec212bf30c80539ef5e37c2e9ff148610e094d5
Author: Dapeng Mi <[email protected]>

perf script: Fix missing '+' indicator when branch counter reaches upper limit

This commit restores the missing "+" suffix indicator in perf script which represents that branch counter events may have been lost due to saturation.

tools/perf/builtin-script.c static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en, · line 1290

Quoted 11 lines
tools/perf/builtin-script.c +1 -2
index c8ac9f01a36b..f865c8b2f95f 100644
@@ -1287,8 +1287,12 @@ static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
 			if (!verbose) {
 				for (j = 0; j < num; j++)
 					printed += fprintf(fp, "%s", pos->abbr_name);
-			} else
-				printed += fprintf(fp, "%s %d ", pos->name, num);
+				if (num == mask)

Could this incorrectly print the saturation indicator if the branch counter width is 0?

Since mask is derived via (1L << width) - 1 earlier in the function:

    perf_env__find_br_cntr_info(evsel__env(evsel), NULL, &width);
    mask = (1L << width) - 1;

A width of 0 results in a mask of 0. In this scenario, num will also evaluate to 0, making the condition num == mask evaluate to true.

Would it be better to verify that mask > 0 before checking for saturation?

tools/perf/builtin-script.c static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en, · line 1294

Quoted 4 lines
tools/perf/builtin-script.c +4 -0
+					printed += fprintf(fp, "+");
+			} else {
+				printed += fprintf(fp, "%s %d%s", pos->name,
+						   num, num == mask ? "+ " : " ");

Is a similar check needed here to prevent the same false positive in verbose mode?

tools/perf/builtin-script.c static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en, · line 1298

Quoted 4 lines
tools/perf/builtin-script.c +1 -0
+			}
 		}
 		if (numprinted == 0 && !verbose)
 			printed += fprintf(fp, "-");

In non-verbose mode, a false positive on the mask check introduces a regression where the output becomes +- since the numprinted == 0 condition above will also append a "-", contradicting the intention that "-" means no event occurred.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v2 1/2
#3 lore raw

Re: [PATCH 1/2 v2] perf script: Fix missing '+' indicator when branch counter reaches upper limit

In reply to Thomas Falcon
Quoted 41 lines · Thomas Falcon

From: Dapeng Mi <[email protected]>

When displaying branch counter (br_cntr) information, a "+" suffix represents that event occurrences may have been lost due to branch counter saturation. However, this indicator was missing in perf script. Add it back.

Before:

# Branch counter abbr list:
# cpu_core/event=0xc4,umask=0x20/ppp = A

 # cpu_core/instructions/ = B
 # cpu_core/MEM_INST_RETIRED.ALL_LOADS/ = C
 # cpu_core/MEM_LOAD_RETIRED.L2_MISS/ = D
 # '-' No event occurs
 # '+' Event occurrences may be lost due to branch counter saturated
...
        datasym+190:
        00005567f9951676        jz 0x5567f995162dr_cntr: BBBC   # PRED 1 cycles [1]
...
After:
...
        datasym+190:
        00005567f9951676        jz 0x5567f995162dr_cntr: BBB+C  # PRED 1 cycles [1]
...
---
 tools/perf/builtin-script.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/perf/builtin-script.c +0 -1
index c8ac9f01a36b..f865c8b2f95f 100644
@@ -1287,8 +1287,12 @@ static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
 			if (!verbose) {
 				for (j = 0; j < num; j++)
 					printed += fprintf(fp, "%s", pos->abbr_name);
-			} else

tools/perf/builtin-script.c static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en, · line 1298

Quoted 10 lines
tools/perf/builtin-script.c +6 -1
-				printed += fprintf(fp, "%s %d ", pos->name, num);
+				if (num == mask)
+					printed += fprintf(fp, "+");
+			} else {
+				printed += fprintf(fp, "%s %d%s", pos->name,
+						   num, num == mask ? "+ " : " ");
+			}
 		}
 		if (numprinted == 0 && !verbose)
 			printed += fprintf(fp, "-");
#4 lore raw

PATCH v2 2/2 perf annotate: Fix missing branch counter column in TUI mode

In reply to Thomas Falcon

perf annotate checks that evlist->nr_br_cntr has been incremented to determine whether to show branch counter information. However, this data is not populated until after the check when events are processed. Therefore, this counter will always be less than zero and the Branch Count column is never shown. Do this check after events have been processed and branch counter data is updated.

---
 tools/perf/builtin-annotate.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
tools/perf/builtin-annotate.c +5 -4
index 5e57b78548f4..7bddbaee1cde 100644
@@ -563,6 +563,10 @@ static int __cmd_annotate(struct perf_annotate *ann)
 	if (ret)
 		goto out;
 
+	if ((use_browser == 1 || ann->use_stdio2) && ann->has_br_stack)
+		if (session->evlist->nr_br_cntr > 0)
+			annotate_opts.show_br_cntr = true;
+
 	if (dump_trace) {
 		perf_session__fprintf_nr_events(session, stdout);
 		evlist__fprintf_nr_events(session->evlist, stdout);
@@ -926,11 +930,8 @@ int cmd_annotate(int argc, const char **argv)
 	 * branch counters, if the corresponding branch info is available
 	 * in the perf data in the TUI mode.
 	 */
-	if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack) {
+	if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack)
 		sort__mode = SORT_MODE__BRANCH;
-		if (annotate.session->evlist->nr_br_cntr > 0)
-			annotate_opts.show_br_cntr = true;
-	}
 
 	if (setup_sorting(/*evlist=*/NULL, perf_session__env(annotate.session)) < 0)
 		usage_with_options(annotate_usage, options);
Signature · 1 line
2.43.0
Reviewed-by on v2 2/2
#5 lore raw

Re: [PATCH 2/2 v2] perf annotate: Fix missing branch counter column in TUI mode

Quoted 30 lines · Thomas Falcon

perf annotate checks that evlist->nr_br_cntr has been incremented to determine whether to show branch counter information. However, this data is not populated until after the check when events are processed. Therefore, this counter will always be less than zero and the Branch Count column is never shown. Do this check after events have been processed and branch counter data is updated.

---
 tools/perf/builtin-annotate.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
tools/perf/builtin-annotate.c +4 -0
index 5e57b78548f4..7bddbaee1cde 100644
@@ -563,6 +563,10 @@ static int __cmd_annotate(struct perf_annotate *ann)
 	if (ret)
 		goto out;
 
+	if ((use_browser == 1 || ann->use_stdio2) && ann->has_br_stack)
+		if (session->evlist->nr_br_cntr > 0)
+			annotate_opts.show_br_cntr = true;
+
 	if (dump_trace) {
 		perf_session__fprintf_nr_events(session, stdout);
 		evlist__fprintf_nr_events(session->evlist, stdout);
@@ -926,11 +930,8 @@ int cmd_annotate(int argc, const char **argv)
 	 * branch counters, if the corresponding branch info is available
 	 * in the perf data in the TUI mode.

tools/perf/builtin-annotate.c int cmd_annotate(int argc, const char **argv) · line 937

Quoted 10 lines
tools/perf/builtin-annotate.c +1 -4
 	 */
-	if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack) {
+	if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack)
 		sort__mode = SORT_MODE__BRANCH;
-		if (annotate.session->evlist->nr_br_cntr > 0)
-			annotate_opts.show_br_cntr = true;
-	}
 
 	if (setup_sorting(/*evlist=*/NULL, perf_session__env(annotate.session)) < 0)
 		usage_with_options(annotate_usage, options);

LGTM.

#6 lore raw
In reply to Thomas Falcon
Quoted 8 lines · Thomas Falcon

This series fixes two issues related to branch counter (br_cntr) display in perf.

The first patch fixes a missing '+' indicator in 'perf script' output.
The '+' suffix is used to signal that event occurrences may have been lost due to branch counter overflow.

Quoted 7 lines

The second patch fixes a regression in 'perf annotate' TUI mode where the Branch Count column was never displayed. The check for whether to enable branch counter display was performed before events were processed
and the counter data was populated, so the condition was never satisfied.
Moving the check to after event processing resolves this.

Ping.

Thanks,
Tom

Quoted 18 lines

v2: Added a cover letter and expanded commit message for patch 1/2. Patch 2/2 was missing some conditional checks to enable branch counter
    display, which have been added back.

Dapeng Mi (1):
  perf script: Fix missing '+' indicator when branch counter reaches upper limit

Thomas Falcon (1):
  perf annotate: Fix missing branch counter column in TUI mode

 tools/perf/builtin-annotate.c | 9 +++++----
 tools/perf/builtin-script.c   | 8 ++++++--

 2 files changed, 11 insertions(+), 6 deletions(-)