Pārlūkot izejas kodu

do not record function multiple times in topproto (#388)

Margaret Nolan 6 gadus atpakaļ
vecāks
revīzija
be90f33229

+ 1
- 0
internal/driver/driver_test.go Parādīt failu

@@ -63,6 +63,7 @@ func TestParse(t *testing.T) {
63 63
 		{"text,lines,cum,show=[12]00", "cpu"},
64 64
 		{"text,lines,cum,hide=line[X3]0,focus=[12]00", "cpu"},
65 65
 		{"topproto,lines,cum,hide=mangled[X3]0", "cpu"},
66
+		{"topproto,lines", "cpu"},
66 67
 		{"tree,lines,cum,focus=[24]00", "heap"},
67 68
 		{"tree,relative_percentages,cum,focus=[24]00", "heap"},
68 69
 		{"tree,lines,cum,show_from=line2", "cpu"},

+ 3
- 0
internal/driver/testdata/pprof.cpu.lines.topproto Parādīt failu

@@ -0,0 +1,3 @@
1
+Showing nodes accounting for 1s, 100% of 1s total
2
+      flat  flat%   sum%        cum   cum%
3
+        1s   100%   100%         1s   100%  mangled1000 testdata/file1000.src:1

+ 11
- 5
internal/report/report.go Parādīt failu

@@ -309,7 +309,10 @@ func printTopProto(w io.Writer, rpt *Report) error {
309 309
 	}
310 310
 	functionMap := make(functionMap)
311 311
 	for i, n := range g.Nodes {
312
-		f := functionMap.FindOrAdd(n.Info)
312
+		f, added := functionMap.findOrAdd(n.Info)
313
+		if added {
314
+			out.Function = append(out.Function, f)
315
+		}
313 316
 		flat, cum := n.FlatValue(), n.CumValue()
314 317
 		l := &profile.Location{
315 318
 			ID:      uint64(i + 1),
@@ -328,7 +331,6 @@ func printTopProto(w io.Writer, rpt *Report) error {
328 331
 			Location: []*profile.Location{l},
329 332
 			Value:    []int64{int64(cv), int64(fv)},
330 333
 		}
331
-		out.Function = append(out.Function, f)
332 334
 		out.Location = append(out.Location, l)
333 335
 		out.Sample = append(out.Sample, s)
334 336
 	}
@@ -338,11 +340,15 @@ func printTopProto(w io.Writer, rpt *Report) error {
338 340
 
339 341
 type functionMap map[string]*profile.Function
340 342
 
341
-func (fm functionMap) FindOrAdd(ni graph.NodeInfo) *profile.Function {
343
+// findOrAdd takes a node representing a function, adds the function
344
+// represented by the node to the map if the function is not already present,
345
+// and returns the function the node represents. This also returns a boolean,
346
+// which is true if the function was added and false otherwise.
347
+func (fm functionMap) findOrAdd(ni graph.NodeInfo) (*profile.Function, bool) {
342 348
 	fName := fmt.Sprintf("%q%q%q%d", ni.Name, ni.OrigName, ni.File, ni.StartLine)
343 349
 
344 350
 	if f := fm[fName]; f != nil {
345
-		return f
351
+		return f, false
346 352
 	}
347 353
 
348 354
 	f := &profile.Function{
@@ -353,7 +359,7 @@ func (fm functionMap) FindOrAdd(ni graph.NodeInfo) *profile.Function {
353 359
 		StartLine:  int64(ni.StartLine),
354 360
 	}
355 361
 	fm[fName] = f
356
-	return f
362
+	return f, true
357 363
 }
358 364
 
359 365
 // printAssembly prints an annotated assembly listing.

+ 14
- 7
internal/report/report_test.go Parādīt failu

@@ -253,16 +253,23 @@ func TestFunctionMap(t *testing.T) {
253 253
 		{Name: "fun2", File: "filename2"},
254 254
 	}
255 255
 
256
-	want := []profile.Function{
257
-		{ID: 1, Name: "fun1"},
258
-		{ID: 2, Name: "fun2", Filename: "filename"},
259
-		{ID: 1, Name: "fun1"},
260
-		{ID: 3, Name: "fun2", Filename: "filename2"},
256
+	want := []struct {
257
+		wantFunction profile.Function
258
+		wantAdded    bool
259
+	}{
260
+		{profile.Function{ID: 1, Name: "fun1"}, true},
261
+		{profile.Function{ID: 2, Name: "fun2", Filename: "filename"}, true},
262
+		{profile.Function{ID: 1, Name: "fun1"}, false},
263
+		{profile.Function{ID: 3, Name: "fun2", Filename: "filename2"}, true},
261 264
 	}
262 265
 
263 266
 	for i, tc := range nodes {
264
-		if got, want := fm.FindOrAdd(tc), want[i]; *got != want {
265
-			t.Errorf("%d: want %v, got %v", i, want, got)
267
+		gotFunc, gotAdded := fm.findOrAdd(tc)
268
+		if got, want := gotFunc, want[i].wantFunction; *got != want {
269
+			t.Errorf("%d: got %v, want %v", i, got, want)
270
+		}
271
+		if got, want := gotAdded, want[i].wantAdded; got != want {
272
+			t.Errorf("%d: got %v, want %v", i, got, want)
266 273
 		}
267 274
 	}
268 275
 }