소스 검색

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

Margaret Nolan 6 년 전
부모
커밋
be90f33229
4개의 변경된 파일29개의 추가작업 그리고 12개의 파일을 삭제
  1. 1
    0
      internal/driver/driver_test.go
  2. 3
    0
      internal/driver/testdata/pprof.cpu.lines.topproto
  3. 11
    5
      internal/report/report.go
  4. 14
    7
      internal/report/report_test.go

+ 1
- 0
internal/driver/driver_test.go 파일 보기

63
 		{"text,lines,cum,show=[12]00", "cpu"},
63
 		{"text,lines,cum,show=[12]00", "cpu"},
64
 		{"text,lines,cum,hide=line[X3]0,focus=[12]00", "cpu"},
64
 		{"text,lines,cum,hide=line[X3]0,focus=[12]00", "cpu"},
65
 		{"topproto,lines,cum,hide=mangled[X3]0", "cpu"},
65
 		{"topproto,lines,cum,hide=mangled[X3]0", "cpu"},
66
+		{"topproto,lines", "cpu"},
66
 		{"tree,lines,cum,focus=[24]00", "heap"},
67
 		{"tree,lines,cum,focus=[24]00", "heap"},
67
 		{"tree,relative_percentages,cum,focus=[24]00", "heap"},
68
 		{"tree,relative_percentages,cum,focus=[24]00", "heap"},
68
 		{"tree,lines,cum,show_from=line2", "cpu"},
69
 		{"tree,lines,cum,show_from=line2", "cpu"},

+ 3
- 0
internal/driver/testdata/pprof.cpu.lines.topproto 파일 보기

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 파일 보기

309
 	}
309
 	}
310
 	functionMap := make(functionMap)
310
 	functionMap := make(functionMap)
311
 	for i, n := range g.Nodes {
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
 		flat, cum := n.FlatValue(), n.CumValue()
316
 		flat, cum := n.FlatValue(), n.CumValue()
314
 		l := &profile.Location{
317
 		l := &profile.Location{
315
 			ID:      uint64(i + 1),
318
 			ID:      uint64(i + 1),
328
 			Location: []*profile.Location{l},
331
 			Location: []*profile.Location{l},
329
 			Value:    []int64{int64(cv), int64(fv)},
332
 			Value:    []int64{int64(cv), int64(fv)},
330
 		}
333
 		}
331
-		out.Function = append(out.Function, f)
332
 		out.Location = append(out.Location, l)
334
 		out.Location = append(out.Location, l)
333
 		out.Sample = append(out.Sample, s)
335
 		out.Sample = append(out.Sample, s)
334
 	}
336
 	}
338
 
340
 
339
 type functionMap map[string]*profile.Function
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
 	fName := fmt.Sprintf("%q%q%q%d", ni.Name, ni.OrigName, ni.File, ni.StartLine)
348
 	fName := fmt.Sprintf("%q%q%q%d", ni.Name, ni.OrigName, ni.File, ni.StartLine)
343
 
349
 
344
 	if f := fm[fName]; f != nil {
350
 	if f := fm[fName]; f != nil {
345
-		return f
351
+		return f, false
346
 	}
352
 	}
347
 
353
 
348
 	f := &profile.Function{
354
 	f := &profile.Function{
353
 		StartLine:  int64(ni.StartLine),
359
 		StartLine:  int64(ni.StartLine),
354
 	}
360
 	}
355
 	fm[fName] = f
361
 	fm[fName] = f
356
-	return f
362
+	return f, true
357
 }
363
 }
358
 
364
 
359
 // printAssembly prints an annotated assembly listing.
365
 // printAssembly prints an annotated assembly listing.

+ 14
- 7
internal/report/report_test.go 파일 보기

253
 		{Name: "fun2", File: "filename2"},
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
 	for i, tc := range nodes {
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
 }