Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion internal/report/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/report/testdata/source.rpt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading