Re: [PATCH] perf hist: Fix branch_info index for sample weight
From: James Clark
Date: Wed Nov 04 2026 - 04:24:05 EST
On 04/11/2026 09:02, Gu Bin wrote:
iter_add_next_branch_entry() is called once per branch and uses &bi[i]
for the hist entry, but reads the weight from bi->flags.cycles (i.e.
bi[0]) for every branch, so every entry is weighted with the first
branch's cycle count. This is incorrect when the branch stack contains
more than one entry with different cycle counts, as the weight of each
hist entry fails to reflect the cycles of its own branch.
Use bi[i].flags.cycles, as the surrounding code already does.
Fixes: 0e332f033a82 ("perf tools: Add support for cycles, weight branch_info field")
Signed-off-by: Gu Bin <gubin@xxxxxxxxxxxxxxxxxxxx>
---
tools/perf/util/hist.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
index e75a4fcce975..364754e9d12a 100644
--- a/tools/perf/util/hist.c
+++ b/tools/perf/util/hist.c
@@ -1055,7 +1055,7 @@ iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *a
* and not events sampled. Thus we use a pseudo period of 1.
*/
sample->period = 1;
- sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
+ sample->weight = bi[i].flags.cycles ? bi[i].flags.cycles : 1;
he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL, NULL,
sample, true);
Reviewed-by: James Clark <james.clark@xxxxxxxxxx>