Skip to content

Commit 01ea337

Browse files
Merge pull request #62 from rauls5382/toppproto
Populate more carefully the profile.proto in the topproto output
2 parents 2d104b1 + 6c7cca8 commit 01ea337

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

internal/report/report.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,20 +286,16 @@ func printTopProto(w io.Writer, rpt *Report) error {
286286
PeriodType: p.PeriodType,
287287
Period: p.Period,
288288
}
289-
var flatSum int64
289+
functionMap := make(functionMap)
290290
for i, n := range g.Nodes {
291-
name, flat, cum := n.Info.PrintableName(), n.FlatValue(), n.CumValue()
292-
293-
flatSum += flat
294-
f := &profile.Function{
295-
ID: uint64(i + 1),
296-
Name: name,
297-
SystemName: name,
298-
}
291+
f := functionMap.FindOrAdd(n.Info)
292+
flat, cum := n.FlatValue(), n.CumValue()
299293
l := &profile.Location{
300-
ID: uint64(i + 1),
294+
ID: uint64(i + 1),
295+
Address: n.Info.Address,
301296
Line: []profile.Line{
302297
{
298+
Line: int64(n.Info.Lineno),
303299
Function: f,
304300
},
305301
},
@@ -319,6 +315,26 @@ func printTopProto(w io.Writer, rpt *Report) error {
319315
return out.Write(w)
320316
}
321317

318+
type functionMap map[string]*profile.Function
319+
320+
func (fm functionMap) FindOrAdd(ni graph.NodeInfo) *profile.Function {
321+
fName := fmt.Sprintf("%q%q%q%d", ni.Name, ni.OrigName, ni.File, ni.StartLine)
322+
323+
if f := fm[fName]; f != nil {
324+
return f
325+
}
326+
327+
f := &profile.Function{
328+
ID: uint64(len(fm) + 1),
329+
Name: ni.Name,
330+
SystemName: ni.OrigName,
331+
Filename: ni.File,
332+
StartLine: int64(ni.StartLine),
333+
}
334+
fm[fName] = f
335+
return f
336+
}
337+
322338
// printAssembly prints an annotated assembly listing.
323339
func printAssembly(w io.Writer, rpt *Report, obj plugin.ObjTool) error {
324340
o := rpt.options

internal/report/report_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,27 @@ func TestDisambiguation(t *testing.T) {
236236
}
237237
}
238238
}
239+
240+
func TestFunctionMap(t *testing.T) {
241+
242+
fm := make(functionMap)
243+
nodes := []graph.NodeInfo{
244+
{Name: "fun1"},
245+
{Name: "fun2", File: "filename"},
246+
{Name: "fun1"},
247+
{Name: "fun2", File: "filename2"},
248+
}
249+
250+
want := []profile.Function{
251+
{ID: 1, Name: "fun1"},
252+
{ID: 2, Name: "fun2", Filename: "filename"},
253+
{ID: 1, Name: "fun1"},
254+
{ID: 3, Name: "fun2", Filename: "filename2"},
255+
}
256+
257+
for i, tc := range nodes {
258+
if got, want := fm.FindOrAdd(tc), want[i]; *got != want {
259+
t.Errorf("%d: want %v, got %v", i, want, got)
260+
}
261+
}
262+
}

0 commit comments

Comments
 (0)