浏览代码

Merge pull request #52 from rauls5382/tagunit

Have unit= apply to numeric tags
Raul Silvera 8 年前
父节点
当前提交
a691cd088b

+ 5
- 1
internal/driver/driver_test.go 查看文件

@@ -65,6 +65,8 @@ func TestParse(t *testing.T) {
65 65
 		{"dot,files,cum", "contention"},
66 66
 		{"tags", "cpu"},
67 67
 		{"tags,tagignore=tag[13],tagfocus=key[12]", "cpu"},
68
+		{"tags", "heap"},
69
+		{"tags,unit=bytes", "heap"},
68 70
 		{"traces", "cpu"},
69 71
 		{"dot,alloc_space,flat,focus=[234]00", "heap_alloc"},
70 72
 		{"dot,alloc_space,flat,hide=line.*1?23?", "heap_alloc"},
@@ -221,7 +223,9 @@ func solutionFilename(source string, f *testFlags) string {
221 223
 		name = append(name, "ignore")
222 224
 	}
223 225
 	name = addString(name, f, []string{"hide", "show"})
224
-
226
+	if f.strings["unit"] != "minimum" {
227
+		name = addString(name, f, []string{"unit"})
228
+	}
225 229
 	return strings.Join(name, ".")
226 230
 }
227 231
 

+ 6
- 0
internal/driver/testdata/pprof.heap.tags 查看文件

@@ -0,0 +1,6 @@
1
+bytes: Total 150
2
+        80 (53.33%): 400kB
3
+        40 (26.67%): 1.56MB
4
+        20 (13.33%): 200kB
5
+        10 ( 6.67%): 100kB
6
+

+ 6
- 0
internal/driver/testdata/pprof.heap.tags.unit 查看文件

@@ -0,0 +1,6 @@
1
+bytes: Total 150
2
+        80 (53.33%): 409600B
3
+        40 (26.67%): 1638400B
4
+        20 (13.33%): 204800B
5
+        10 ( 6.67%): 102400B
6
+

+ 14
- 8
internal/graph/dotgraph.go 查看文件

@@ -45,8 +45,9 @@ type DotConfig struct {
45 45
 	Title  string   // The title of the DOT graph
46 46
 	Labels []string // The labels for the DOT's legend
47 47
 
48
-	FormatValue func(int64) string // A formatting function for values
49
-	Total       int64              // The total weight of the graph, used to compute percentages
48
+	FormatValue func(int64) string         // A formatting function for values
49
+	FormatTag   func(int64, string) string // A formatting function for numeric tags
50
+	Total       int64                      // The total weight of the graph, used to compute percentages
50 51
 }
51 52
 
52 53
 // Compose creates and writes a in the DOT format to the writer, using
@@ -258,7 +259,7 @@ func (b *builder) numericNodelets(nts []*Tag, maxNumNodelets int, flatTags bool,
258 259
 
259 260
 	// Collapse numeric labels into maxNumNodelets buckets, of the form:
260 261
 	// 1MB..2MB, 3MB..5MB, ...
261
-	for j, t := range collapsedTags(nts, maxNumNodelets, flatTags) {
262
+	for j, t := range b.collapsedTags(nts, maxNumNodelets, flatTags) {
262 263
 		w, attr := t.CumValue(), ` style="dotted"`
263 264
 		if flatTags || t.FlatValue() == t.CumValue() {
264 265
 			w, attr = t.FlatValue(), ""
@@ -399,7 +400,7 @@ func multilinePrintableName(info *NodeInfo) string {
399 400
 }
400 401
 
401 402
 // collapsedTags trims and sorts a slice of tags.
402
-func collapsedTags(ts []*Tag, count int, flatTags bool) []*Tag {
403
+func (b *builder) collapsedTags(ts []*Tag, count int, flatTags bool) []*Tag {
403 404
 	ts = SortTags(ts, flatTags)
404 405
 	if len(ts) <= count {
405 406
 		return ts
@@ -421,7 +422,7 @@ func collapsedTags(ts []*Tag, count int, flatTags bool) []*Tag {
421 422
 
422 423
 	var nts []*Tag
423 424
 	for _, g := range tagGroups {
424
-		l, w, c := tagGroupLabel(g)
425
+		l, w, c := b.tagGroupLabel(g)
425 426
 		nts = append(nts, &Tag{
426 427
 			Name: l,
427 428
 			Flat: w,
@@ -439,10 +440,15 @@ func tagDistance(t, u *Tag) float64 {
439 440
 	return v - float64(t.Value)
440 441
 }
441 442
 
442
-func tagGroupLabel(g []*Tag) (label string, flat, cum int64) {
443
+func (b *builder) tagGroupLabel(g []*Tag) (label string, flat, cum int64) {
444
+	formatTag := b.config.FormatTag
445
+	if formatTag == nil {
446
+		formatTag = measurement.Label
447
+	}
448
+
443 449
 	if len(g) == 1 {
444 450
 		t := g[0]
445
-		return measurement.Label(t.Value, t.Unit), t.FlatValue(), t.CumValue()
451
+		return formatTag(t.Value, t.Unit), t.FlatValue(), t.CumValue()
446 452
 	}
447 453
 	min := g[0]
448 454
 	max := g[0]
@@ -466,7 +472,7 @@ func tagGroupLabel(g []*Tag) (label string, flat, cum int64) {
466 472
 	if dc != 0 {
467 473
 		c = c / dc
468 474
 	}
469
-	return measurement.Label(min.Value, min.Unit) + ".." + measurement.Label(max.Value, max.Unit), f, c
475
+	return formatTag(min.Value, min.Unit) + ".." + formatTag(max.Value, max.Unit), f, c
470 476
 }
471 477
 
472 478
 func min64(a, b int64) int64 {

+ 2
- 1
internal/graph/dotgraph_test.go 查看文件

@@ -263,7 +263,8 @@ func TestTagCollapse(t *testing.T) {
263 263
 
264 264
 	for _, tc := range tagWant {
265 265
 		var got, want []*Tag
266
-		got = collapsedTags(tagSource, len(tc), true)
266
+		b := builder{nil, &DotAttributes{}, &DotConfig{}}
267
+		got = b.collapsedTags(tagSource, len(tc), true)
267 268
 		want = SortTags(tc, true)
268 269
 
269 270
 		if !reflect.DeepEqual(got, want) {

+ 16
- 5
internal/report/report.go 查看文件

@@ -199,6 +199,10 @@ func (rpt *Report) newGraph(nodes graph.NodeSet) *graph.Graph {
199 199
 		s.NumLabel = numLabels
200 200
 	}
201 201
 
202
+	formatTag := func(v int64, key string) string {
203
+		return measurement.ScaledLabel(v, key, o.OutputUnit)
204
+	}
205
+
202 206
 	gopt := &graph.Options{
203 207
 		SampleValue:       o.SampleValue,
204 208
 		SampleMeanDivisor: o.SampleMeanDivisor,
@@ -218,10 +222,6 @@ func (rpt *Report) newGraph(nodes graph.NodeSet) *graph.Graph {
218 222
 	return graph.New(rpt.prof, gopt)
219 223
 }
220 224
 
221
-func formatTag(v int64, key string) string {
222
-	return measurement.Label(v, key)
223
-}
224
-
225 225
 func printTopProto(w io.Writer, rpt *Report) error {
226 226
 	p := rpt.prof
227 227
 	o := rpt.options
@@ -473,6 +473,11 @@ func valueOrDot(value int64, rpt *Report) string {
473 473
 func printTags(w io.Writer, rpt *Report) error {
474 474
 	p := rpt.prof
475 475
 
476
+	o := rpt.options
477
+	formatTag := func(v int64, key string) string {
478
+		return measurement.ScaledLabel(v, key, o.OutputUnit)
479
+	}
480
+
476 481
 	// Hashtable to keep accumulate tags as key,value,count.
477 482
 	tagMap := make(map[string]map[string]int64)
478 483
 	for _, s := range p.Sample {
@@ -489,7 +494,7 @@ func printTags(w io.Writer, rpt *Report) error {
489 494
 		}
490 495
 		for key, vals := range s.NumLabel {
491 496
 			for _, nval := range vals {
492
-				val := measurement.Label(nval, key)
497
+				val := formatTag(nval, key)
493 498
 				if valueMap, ok := tagMap[key]; ok {
494 499
 					valueMap[val] = valueMap[val] + s.Value[0]
495 500
 					continue
@@ -826,10 +831,16 @@ func printDOT(w io.Writer, rpt *Report) error {
826 831
 	rpt.selectOutputUnit(g)
827 832
 	labels := reportLabels(rpt, g, origCount, droppedNodes, droppedEdges, true)
828 833
 
834
+	o := rpt.options
835
+	formatTag := func(v int64, key string) string {
836
+		return measurement.ScaledLabel(v, key, o.OutputUnit)
837
+	}
838
+
829 839
 	c := &graph.DotConfig{
830 840
 		Title:       rpt.options.Title,
831 841
 		Labels:      labels,
832 842
 		FormatValue: rpt.formatValue,
843
+		FormatTag:   formatTag,
833 844
 		Total:       rpt.total,
834 845
 	}
835 846
 	graph.ComposeDot(w, g, &graph.DotAttributes{}, c)