|
@@ -34,10 +34,11 @@ type Graph struct {
|
34
|
34
|
|
35
|
35
|
// Options encodes the options for constructing a graph
|
36
|
36
|
type Options struct {
|
37
|
|
- SampleValue func(s []int64) int64 // Function to compute the value of a sample
|
38
|
|
- FormatTag func(int64, string) string // Function to format a sample tag value into a string
|
39
|
|
- ObjNames bool // Always preserve obj filename
|
40
|
|
- OrigFnNames bool // Preserve original (eg mangled) function names
|
|
37
|
+ SampleValue func(s []int64) int64 // Function to compute the value of a sample
|
|
38
|
+ SampleMeanDivisor func(s []int64) int64 // Function to compute the divisor for mean graphs, or nil
|
|
39
|
+ FormatTag func(int64, string) string // Function to format a sample tag value into a string
|
|
40
|
+ ObjNames bool // Always preserve obj filename
|
|
41
|
+ OrigFnNames bool // Preserve original (eg mangled) function names
|
41
|
42
|
|
42
|
43
|
CallTree bool // Build a tree instead of a graph
|
43
|
44
|
DropNegative bool // Drop nodes with overall negative values
|
|
@@ -63,7 +64,7 @@ type Node struct {
|
63
|
64
|
|
64
|
65
|
// Values associated to this node. Flat is exclusive to this node,
|
65
|
66
|
// Cum includes all descendents.
|
66
|
|
- Flat, Cum int64
|
|
67
|
+ Flat, FlatDiv, Cum, CumDiv int64
|
67
|
68
|
|
68
|
69
|
// In and out Contains the nodes immediately reaching or reached by
|
69
|
70
|
// this node.
|
|
@@ -79,15 +80,40 @@ type Node struct {
|
79
|
80
|
NumericTags map[string]TagMap
|
80
|
81
|
}
|
81
|
82
|
|
|
83
|
+// FlatValue returns the exclusive value for this node, computing the
|
|
84
|
+// mean if a divisor is available.
|
|
85
|
+func (n *Node) FlatValue() int64 {
|
|
86
|
+ if n.FlatDiv == 0 {
|
|
87
|
+ return n.Flat
|
|
88
|
+ }
|
|
89
|
+ return n.Flat / n.FlatDiv
|
|
90
|
+}
|
|
91
|
+
|
|
92
|
+// CumValue returns the inclusive value for this node, computing the
|
|
93
|
+// mean if a divisor is available.
|
|
94
|
+func (n *Node) CumValue() int64 {
|
|
95
|
+ if n.CumDiv == 0 {
|
|
96
|
+ return n.Cum
|
|
97
|
+ }
|
|
98
|
+ return n.Cum / n.CumDiv
|
|
99
|
+}
|
|
100
|
+
|
82
|
101
|
// AddToEdge increases the weight of an edge between two nodes. If
|
83
|
102
|
// there isn't such an edge one is created.
|
84
|
|
-func (n *Node) AddToEdge(to *Node, w int64, residual, inline bool) {
|
|
103
|
+func (n *Node) AddToEdge(to *Node, v int64, residual, inline bool) {
|
|
104
|
+ n.AddToEdgeDiv(to, 0, v, residual, inline)
|
|
105
|
+}
|
|
106
|
+
|
|
107
|
+// AddToEdgeDiv increases the weight of an edge between two nodes. If
|
|
108
|
+// there isn't such an edge one is created.
|
|
109
|
+func (n *Node) AddToEdgeDiv(to *Node, dv, v int64, residual, inline bool) {
|
85
|
110
|
if n.Out[to] != to.In[n] {
|
86
|
111
|
panic(fmt.Errorf("asymmetric edges %v %v", *n, *to))
|
87
|
112
|
}
|
88
|
113
|
|
89
|
114
|
if e := n.Out[to]; e != nil {
|
90
|
|
- e.Weight += w
|
|
115
|
+ e.WeightDiv += dv
|
|
116
|
+ e.Weight += v
|
91
|
117
|
if residual {
|
92
|
118
|
e.Residual = true
|
93
|
119
|
}
|
|
@@ -97,7 +123,7 @@ func (n *Node) AddToEdge(to *Node, w int64, residual, inline bool) {
|
97
|
123
|
return
|
98
|
124
|
}
|
99
|
125
|
|
100
|
|
- info := &Edge{Src: n, Dest: to, Weight: w, Residual: residual, Inline: inline}
|
|
126
|
+ info := &Edge{Src: n, Dest: to, WeightDiv: dv, Weight: v, Residual: residual, Inline: inline}
|
101
|
127
|
n.Out[to] = info
|
102
|
128
|
to.In[n] = info
|
103
|
129
|
}
|
|
@@ -205,7 +231,8 @@ type EdgeMap map[*Node]*Edge
|
205
|
231
|
type Edge struct {
|
206
|
232
|
Src, Dest *Node
|
207
|
233
|
// The summary weight of the edge
|
208
|
|
- Weight int64
|
|
234
|
+ Weight, WeightDiv int64
|
|
235
|
+
|
209
|
236
|
// residual edges connect nodes that were connected through a
|
210
|
237
|
// separate node, which has been removed from the report.
|
211
|
238
|
Residual bool
|
|
@@ -213,13 +240,38 @@ type Edge struct {
|
213
|
240
|
Inline bool
|
214
|
241
|
}
|
215
|
242
|
|
|
243
|
+func (e *Edge) WeightValue() int64 {
|
|
244
|
+ if e.WeightDiv == 0 {
|
|
245
|
+ return e.Weight
|
|
246
|
+ }
|
|
247
|
+ return e.Weight / e.WeightDiv
|
|
248
|
+}
|
|
249
|
+
|
216
|
250
|
// Tag represent sample annotations
|
217
|
251
|
type Tag struct {
|
218
|
|
- Name string
|
219
|
|
- Unit string // Describe the value, "" for non-numeric tags
|
220
|
|
- Value int64
|
221
|
|
- Flat int64
|
222
|
|
- Cum int64
|
|
252
|
+ Name string
|
|
253
|
+ Unit string // Describe the value, "" for non-numeric tags
|
|
254
|
+ Value int64
|
|
255
|
+ Flat, FlatDiv int64
|
|
256
|
+ Cum, CumDiv int64
|
|
257
|
+}
|
|
258
|
+
|
|
259
|
+// FlatValue returns the exclusive value for this tag, computing the
|
|
260
|
+// mean if a divisor is available.
|
|
261
|
+func (t *Tag) FlatValue() int64 {
|
|
262
|
+ if t.FlatDiv == 0 {
|
|
263
|
+ return t.Flat
|
|
264
|
+ }
|
|
265
|
+ return t.Flat / t.FlatDiv
|
|
266
|
+}
|
|
267
|
+
|
|
268
|
+// CumValue returns the inclusive value for this tag, computing the
|
|
269
|
+// mean if a divisor is available.
|
|
270
|
+func (t *Tag) CumValue() int64 {
|
|
271
|
+ if t.CumDiv == 0 {
|
|
272
|
+ return t.Cum
|
|
273
|
+ }
|
|
274
|
+ return t.Cum / t.CumDiv
|
223
|
275
|
}
|
224
|
276
|
|
225
|
277
|
// TagMap is a collection of tags, classified by their name.
|
|
@@ -247,8 +299,12 @@ func New(prof *profile.Profile, o *Options) *Graph {
|
247
|
299
|
func newGraph(prof *profile.Profile, o *Options) (*Graph, map[uint64]Nodes) {
|
248
|
300
|
nodes, locationMap := CreateNodes(prof, o)
|
249
|
301
|
for _, sample := range prof.Sample {
|
250
|
|
- weight := o.SampleValue(sample.Value)
|
251
|
|
- if weight == 0 {
|
|
302
|
+ var w, dw int64
|
|
303
|
+ w = o.SampleValue(sample.Value)
|
|
304
|
+ if o.SampleMeanDivisor != nil {
|
|
305
|
+ dw = o.SampleMeanDivisor(sample.Value)
|
|
306
|
+ }
|
|
307
|
+ if dw == 0 && w == 0 {
|
252
|
308
|
continue
|
253
|
309
|
}
|
254
|
310
|
seenNode := make(map[*Node]bool, len(sample.Location))
|
|
@@ -271,12 +327,12 @@ func newGraph(prof *profile.Profile, o *Options) (*Graph, map[uint64]Nodes) {
|
271
|
327
|
// Add cum weight to all nodes in stack, avoiding double counting.
|
272
|
328
|
if _, ok := seenNode[n]; !ok {
|
273
|
329
|
seenNode[n] = true
|
274
|
|
- n.addSample(weight, labels, sample.NumLabel, o.FormatTag, false)
|
|
330
|
+ n.addSample(dw, w, labels, sample.NumLabel, o.FormatTag, false)
|
275
|
331
|
}
|
276
|
332
|
// Update edge weights for all edges in stack, avoiding double counting.
|
277
|
333
|
if _, ok := seenEdge[nodePair{n, parent}]; !ok && parent != nil && n != parent {
|
278
|
334
|
seenEdge[nodePair{n, parent}] = true
|
279
|
|
- parent.AddToEdge(n, weight, residual, ni != len(locNodes)-1)
|
|
335
|
+ parent.AddToEdgeDiv(n, dw, w, residual, ni != len(locNodes)-1)
|
280
|
336
|
}
|
281
|
337
|
parent = n
|
282
|
338
|
residual = false
|
|
@@ -284,7 +340,7 @@ func newGraph(prof *profile.Profile, o *Options) (*Graph, map[uint64]Nodes) {
|
284
|
340
|
}
|
285
|
341
|
if parent != nil && !residual {
|
286
|
342
|
// Add flat weight to leaf node.
|
287
|
|
- parent.addSample(weight, labels, sample.NumLabel, o.FormatTag, true)
|
|
343
|
+ parent.addSample(dw, w, labels, sample.NumLabel, o.FormatTag, true)
|
288
|
344
|
}
|
289
|
345
|
}
|
290
|
346
|
|
|
@@ -316,8 +372,12 @@ type nodePair struct {
|
316
|
372
|
func newTree(prof *profile.Profile, o *Options) (g *Graph) {
|
317
|
373
|
parentNodeMap := make(map[*Node]NodeMap, len(prof.Sample))
|
318
|
374
|
for _, sample := range prof.Sample {
|
319
|
|
- weight := o.SampleValue(sample.Value)
|
320
|
|
- if weight == 0 {
|
|
375
|
+ var w, dw int64
|
|
376
|
+ w = o.SampleValue(sample.Value)
|
|
377
|
+ if o.SampleMeanDivisor != nil {
|
|
378
|
+ dw = o.SampleMeanDivisor(sample.Value)
|
|
379
|
+ }
|
|
380
|
+ if dw == 0 && w == 0 {
|
321
|
381
|
continue
|
322
|
382
|
}
|
323
|
383
|
var parent *Node
|
|
@@ -339,15 +399,15 @@ func newTree(prof *profile.Profile, o *Options) (g *Graph) {
|
339
|
399
|
if n == nil {
|
340
|
400
|
continue
|
341
|
401
|
}
|
342
|
|
- n.addSample(weight, labels, sample.NumLabel, o.FormatTag, false)
|
|
402
|
+ n.addSample(dw, w, labels, sample.NumLabel, o.FormatTag, false)
|
343
|
403
|
if parent != nil {
|
344
|
|
- parent.AddToEdge(n, weight, false, lidx != len(lines)-1)
|
|
404
|
+ parent.AddToEdgeDiv(n, dw, w, false, lidx != len(lines)-1)
|
345
|
405
|
}
|
346
|
406
|
parent = n
|
347
|
407
|
}
|
348
|
408
|
}
|
349
|
409
|
if parent != nil {
|
350
|
|
- parent.addSample(weight, labels, sample.NumLabel, o.FormatTag, true)
|
|
410
|
+ parent.addSample(dw, w, labels, sample.NumLabel, o.FormatTag, true)
|
351
|
411
|
}
|
352
|
412
|
}
|
353
|
413
|
|
|
@@ -540,21 +600,25 @@ func (ns Nodes) Sum() (flat int64, cum int64) {
|
540
|
600
|
return
|
541
|
601
|
}
|
542
|
602
|
|
543
|
|
-func (n *Node) addSample(value int64, labels string, numLabel map[string][]int64, format func(int64, string) string, flat bool) {
|
|
603
|
+func (n *Node) addSample(dw, w int64, labels string, numLabel map[string][]int64, format func(int64, string) string, flat bool) {
|
544
|
604
|
// Update sample value
|
545
|
605
|
if flat {
|
546
|
|
- n.Flat += value
|
|
606
|
+ n.FlatDiv += dw
|
|
607
|
+ n.Flat += w
|
547
|
608
|
} else {
|
548
|
|
- n.Cum += value
|
|
609
|
+ n.CumDiv += dw
|
|
610
|
+ n.Cum += w
|
549
|
611
|
}
|
550
|
612
|
|
551
|
613
|
// Add string tags
|
552
|
614
|
if labels != "" {
|
553
|
615
|
t := n.LabelTags.findOrAddTag(labels, "", 0)
|
554
|
616
|
if flat {
|
555
|
|
- t.Flat += value
|
|
617
|
+ t.FlatDiv += dw
|
|
618
|
+ t.Flat += w
|
556
|
619
|
} else {
|
557
|
|
- t.Cum += value
|
|
620
|
+ t.CumDiv += dw
|
|
621
|
+ t.Cum += w
|
558
|
622
|
}
|
559
|
623
|
}
|
560
|
624
|
|
|
@@ -571,9 +635,11 @@ func (n *Node) addSample(value int64, labels string, numLabel map[string][]int64
|
571
|
635
|
for _, v := range nvals {
|
572
|
636
|
t := numericTags.findOrAddTag(format(v, key), key, v)
|
573
|
637
|
if flat {
|
574
|
|
- t.Flat += value
|
|
638
|
+ t.FlatDiv += dw
|
|
639
|
+ t.Flat += w
|
575
|
640
|
} else {
|
576
|
|
- t.Cum += value
|
|
641
|
+ t.CumDiv += dw
|
|
642
|
+ t.Cum += w
|
577
|
643
|
}
|
578
|
644
|
}
|
579
|
645
|
}
|