From f35bb8c313d7a4429f0317de5eb27bfcaaad7611 Mon Sep 17 00:00:00 2001 From: "t.hirashima" Date: Sun, 4 Oct 2026 19:11:11 +0900 Subject: [PATCH] internal/report: count each sample once in -list totals --- internal/report/source.go | 29 ++++++++++++++++++++++++++++- internal/report/testdata/source.rpt | 2 +- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/internal/report/source.go b/internal/report/source.go index 26024b7eb0..81539b210e 100644 --- a/internal/report/source.go +++ b/internal/report/source.go @@ -73,6 +73,32 @@ func printSource(w io.Writer, rpt *Report) error { } reader := newSourceReader(sourcePath, o.TrimPath) + // Cumulative value of each function in each file. A sample is counted + // once, even if recursion puts the function on its stack several times. + type funcFile struct{ name, file string } + funcCum := make(map[funcFile]int64) + seen := make(map[funcFile]bool) // Seen in the current sample. + for _, sample := range rpt.prof.Sample { + v := o.SampleValue(sample.Value) + clear(seen) + for _, loc := range sample.Location { + for _, line := range loc.Line { + if line.Function == nil { + continue + } + // Match the name and file of the nodes created by newGraph. + ff := funcFile{name: line.Function.Name} + if fname := line.Function.Filename; fname != "" { + ff.file = filepath.Clean(fname) + } + if !seen[ff] { + seen[ff] = true + funcCum[ff] += v + } + } + } + } + fmt.Fprintf(w, "Total: %s\n", rpt.formatValue(rpt.total)) for _, fn := range functions { name := fn.Info.Name @@ -102,7 +128,8 @@ func printSource(w io.Writer, rpt *Report) error { for _, fl := range sourceFiles { filename := fl.Info.File fns := fileNodes[filename] - flatSum, cumSum := fns.Sum() + flatSum, _ := fns.Sum() + cumSum := funcCum[funcFile{name, filename}] fnodes, _, err := getSourceFromFile(filename, reader, fns, 0, 0) fmt.Fprintf(w, "ROUTINE ======================== %s in %s\n", name, filename) diff --git a/internal/report/testdata/source.rpt b/internal/report/testdata/source.rpt index 9ec7b3b086..7b13a1af0d 100644 --- a/internal/report/testdata/source.rpt +++ b/internal/report/testdata/source.rpt @@ -33,7 +33,7 @@ ROUTINE ======================== main in testdata/source1 . . 6:source1 line 6; . . 7:source1 line 7; ROUTINE ======================== tee in testdata/source2 - 11100 21100 (flat, cum) 189.90% of Total + 11100 11100 (flat, cum) 99.90% of Total . . 1:source2 line 1; 1000 11000 2:source2 line 2; . . 3:source2 line 3;