newGraph computes a graph from a profile. It returns the graph, and a map from the profile location indices to the corresponding graph nodes.
(prof *profile.Profile, o *Options)
| 339 | // a map from the profile location indices to the corresponding graph |
| 340 | // nodes. |
| 341 | func newGraph(prof *profile.Profile, o *Options) (*Graph, map[uint64]Nodes) { |
| 342 | nodes, locationMap := CreateNodes(prof, o) |
| 343 | seenNode := make(map[*Node]bool) |
| 344 | seenEdge := make(map[nodePair]bool) |
| 345 | for _, sample := range prof.Sample { |
| 346 | var w, dw int64 |
| 347 | w = o.SampleValue(sample.Value) |
| 348 | if o.SampleMeanDivisor != nil { |
| 349 | dw = o.SampleMeanDivisor(sample.Value) |
| 350 | } |
| 351 | if dw == 0 && w == 0 { |
| 352 | continue |
| 353 | } |
| 354 | clear(seenNode) |
| 355 | clear(seenEdge) |
| 356 | var parent *Node |
| 357 | // A residual edge goes over one or more nodes that were not kept. |
| 358 | residual := false |
| 359 | |
| 360 | labels := joinLabels(sample) |
| 361 | // Group the sample frames, based on a global map. |
| 362 | for i := len(sample.Location) - 1; i >= 0; i-- { |
| 363 | l := sample.Location[i] |
| 364 | locNodes := locationMap[l.ID] |
| 365 | for ni := len(locNodes) - 1; ni >= 0; ni-- { |
| 366 | n := locNodes[ni] |
| 367 | if n == nil { |
| 368 | residual = true |
| 369 | continue |
| 370 | } |
| 371 | // Add cum weight to all nodes in stack, avoiding double counting. |
| 372 | if _, ok := seenNode[n]; !ok { |
| 373 | seenNode[n] = true |
| 374 | n.addSample(dw, w, labels, sample.NumLabel, sample.NumUnit, o.FormatTag, false) |
| 375 | } |
| 376 | // Update edge weights for all edges in stack, avoiding double counting. |
| 377 | if _, ok := seenEdge[nodePair{n, parent}]; !ok && parent != nil && n != parent { |
| 378 | seenEdge[nodePair{n, parent}] = true |
| 379 | parent.AddToEdgeDiv(n, dw, w, residual, ni != len(locNodes)-1) |
| 380 | } |
| 381 | parent = n |
| 382 | residual = false |
| 383 | } |
| 384 | } |
| 385 | if parent != nil && !residual { |
| 386 | // Add flat weight to leaf node. |
| 387 | parent.addSample(dw, w, labels, sample.NumLabel, sample.NumUnit, o.FormatTag, true) |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | return selectNodesForGraph(nodes, o.DropNegative), locationMap |
| 392 | } |
| 393 | |
| 394 | func selectNodesForGraph(nodes Nodes, dropNegative bool) *Graph { |
| 395 | // Collect nodes into a graph. |
no test coverage detected
searching dependent graphs…