暫無描述

graph.go 28KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package graph collects a set of samples into a directed graph.
  15. package graph
  16. import (
  17. "fmt"
  18. "math"
  19. "path/filepath"
  20. "sort"
  21. "strconv"
  22. "strings"
  23. "github.com/google/pprof/profile"
  24. )
  25. // Graph summarizes a performance profile into a format that is
  26. // suitable for visualization.
  27. type Graph struct {
  28. Nodes Nodes
  29. }
  30. // Options encodes the options for constructing a graph
  31. type Options struct {
  32. SampleValue func(s []int64) int64 // Function to compute the value of a sample
  33. FormatTag func(int64, string) string // Function to format a sample tag value into a string
  34. ObjNames bool // Always preserve obj filename
  35. CallTree bool // Build a tree instead of a graph
  36. DropNegative bool // Drop nodes with overall negative values
  37. KeptNodes NodeSet // If non-nil, only use nodes in this set
  38. }
  39. // Nodes is an ordered collection of graph nodes.
  40. type Nodes []*Node
  41. // Node is an entry on a profiling report. It represents a unique
  42. // program location.
  43. type Node struct {
  44. // Info describes the source location associated to this node.
  45. Info NodeInfo
  46. // Function represents the function that this node belongs to. On
  47. // graphs with sub-function resolution (eg line number or
  48. // addresses), two nodes in a NodeMap that are part of the same
  49. // function have the same value of Node.Function. If the Node
  50. // represents the whole function, it points back to itself.
  51. Function *Node
  52. // Values associated to this node. Flat is exclusive to this node,
  53. // Cum includes all descendents.
  54. Flat, Cum int64
  55. // In and out Contains the nodes immediately reaching or reached by
  56. // this node.
  57. In, Out EdgeMap
  58. // LabelTags provide additional information about subsets of a sample.
  59. LabelTags TagMap
  60. // NumericTags provide additional values for subsets of a sample.
  61. // Numeric tags are optionally associated to a label tag. The key
  62. // for NumericTags is the name of the LabelTag they are associated
  63. // to, or "" for numeric tags not associated to a label tag.
  64. NumericTags map[string]TagMap
  65. }
  66. // AddToEdge increases the weight of an edge between two nodes. If
  67. // there isn't such an edge one is created.
  68. func (n *Node) AddToEdge(to *Node, w int64, residual, inline bool) {
  69. if n.Out[to] != to.In[n] {
  70. panic(fmt.Errorf("asymmetric edges %v %v", *n, *to))
  71. }
  72. if e := n.Out[to]; e != nil {
  73. e.Weight += w
  74. if residual {
  75. e.Residual = true
  76. }
  77. if !inline {
  78. e.Inline = false
  79. }
  80. return
  81. }
  82. info := &Edge{Src: n, Dest: to, Weight: w, Residual: residual, Inline: inline}
  83. n.Out[to] = info
  84. to.In[n] = info
  85. }
  86. // NodeInfo contains the attributes for a node.
  87. type NodeInfo struct {
  88. Name string
  89. OrigName string
  90. Address uint64
  91. File string
  92. StartLine, Lineno int
  93. Objfile string
  94. }
  95. // PrintableName calls the Node's Formatter function with a single space separator.
  96. func (i *NodeInfo) PrintableName() string {
  97. return strings.Join(i.NameComponents(), " ")
  98. }
  99. // NameComponents returns the components of the printable name to be used for a node.
  100. func (i *NodeInfo) NameComponents() []string {
  101. var name []string
  102. if i.Address != 0 {
  103. name = append(name, fmt.Sprintf("%016x", i.Address))
  104. }
  105. if fun := i.Name; fun != "" {
  106. name = append(name, fun)
  107. }
  108. switch {
  109. case i.Lineno != 0:
  110. // User requested line numbers, provide what we have.
  111. name = append(name, fmt.Sprintf("%s:%d", i.File, i.Lineno))
  112. case i.File != "":
  113. // User requested file name, provide it.
  114. name = append(name, i.File)
  115. case i.Name != "":
  116. // User requested function name. It was already included.
  117. case i.Objfile != "":
  118. // Only binary name is available
  119. name = append(name, "["+i.Objfile+"]")
  120. default:
  121. // Do not leave it empty if there is no information at all.
  122. name = append(name, "<unknown>")
  123. }
  124. return name
  125. }
  126. // NodeMap maps from a node info struct to a node. It is used to merge
  127. // report entries with the same info.
  128. type NodeMap map[NodeInfo]*Node
  129. // NodeSet is a collection of node info structs.
  130. type NodeSet map[NodeInfo]bool
  131. // NodePtrSet is a collection of nodes. Trimming a graph or tree requires a set
  132. // of objects which uniquely identify the nodes to keep. In a graph, NodeInfo
  133. // works as a unique identifier; however, in a tree multiple nodes may share
  134. // identical NodeInfos. A *Node does uniquely identify a node so we can use that
  135. // instead. Though a *Node also uniquely identifies a node in a graph,
  136. // currently, during trimming, graphs are rebult from scratch using only the
  137. // NodeSet, so there would not be the required context of the initial graph to
  138. // allow for the use of *Node.
  139. type NodePtrSet map[*Node]bool
  140. // FindOrInsertNode takes the info for a node and either returns a matching node
  141. // from the node map if one exists, or adds one to the map if one does not.
  142. // If kept is non-nil, nodes are only added if they can be located on it.
  143. func (nm NodeMap) FindOrInsertNode(info NodeInfo, kept NodeSet) *Node {
  144. if kept != nil {
  145. if _, ok := kept[info]; !ok {
  146. return nil
  147. }
  148. }
  149. if n, ok := nm[info]; ok {
  150. return n
  151. }
  152. n := &Node{
  153. Info: info,
  154. In: make(EdgeMap),
  155. Out: make(EdgeMap),
  156. LabelTags: make(TagMap),
  157. NumericTags: make(map[string]TagMap),
  158. }
  159. nm[info] = n
  160. if info.Address == 0 && info.Lineno == 0 {
  161. // This node represents the whole function, so point Function
  162. // back to itself.
  163. n.Function = n
  164. return n
  165. }
  166. // Find a node that represents the whole function.
  167. info.Address = 0
  168. info.Lineno = 0
  169. n.Function = nm.FindOrInsertNode(info, nil)
  170. return n
  171. }
  172. // EdgeMap is used to represent the incoming/outgoing edges from a node.
  173. type EdgeMap map[*Node]*Edge
  174. // Edge contains any attributes to be represented about edges in a graph.
  175. type Edge struct {
  176. Src, Dest *Node
  177. // The summary weight of the edge
  178. Weight int64
  179. // residual edges connect nodes that were connected through a
  180. // separate node, which has been removed from the report.
  181. Residual bool
  182. // An inline edge represents a call that was inlined into the caller.
  183. Inline bool
  184. }
  185. // Tag represent sample annotations
  186. type Tag struct {
  187. Name string
  188. Unit string // Describe the value, "" for non-numeric tags
  189. Value int64
  190. Flat int64
  191. Cum int64
  192. }
  193. // TagMap is a collection of tags, classified by their name.
  194. type TagMap map[string]*Tag
  195. // SortTags sorts a slice of tags based on their weight.
  196. func SortTags(t []*Tag, flat bool) []*Tag {
  197. ts := tags{t, flat}
  198. sort.Sort(ts)
  199. return ts.t
  200. }
  201. // New summarizes performance data from a profile into a graph.
  202. func New(prof *profile.Profile, o *Options) *Graph {
  203. if o.CallTree {
  204. return newTree(prof, o)
  205. }
  206. g, _ := newGraph(prof, o)
  207. return g
  208. }
  209. // newGraph computes a graph from a profile. It returns the graph, and
  210. // a map from the profile location indices to the corresponding graph
  211. // nodes.
  212. func newGraph(prof *profile.Profile, o *Options) (*Graph, map[uint64]Nodes) {
  213. nodes, locationMap := CreateNodes(prof, o.ObjNames, o.KeptNodes)
  214. for _, sample := range prof.Sample {
  215. weight := o.SampleValue(sample.Value)
  216. if weight == 0 {
  217. continue
  218. }
  219. seenNode := make(map[*Node]bool, len(sample.Location))
  220. seenEdge := make(map[nodePair]bool, len(sample.Location))
  221. var parent *Node
  222. // A residual edge goes over one or more nodes that were not kept.
  223. residual := false
  224. labels := joinLabels(sample)
  225. // Group the sample frames, based on a global map.
  226. for i := len(sample.Location) - 1; i >= 0; i-- {
  227. l := sample.Location[i]
  228. locNodes := locationMap[l.ID]
  229. for ni := len(locNodes) - 1; ni >= 0; ni-- {
  230. n := locNodes[ni]
  231. if n == nil {
  232. residual = true
  233. continue
  234. }
  235. // Add cum weight to all nodes in stack, avoiding double counting.
  236. if _, ok := seenNode[n]; !ok {
  237. seenNode[n] = true
  238. n.addSample(weight, labels, sample.NumLabel, o.FormatTag, false)
  239. }
  240. // Update edge weights for all edges in stack, avoiding double counting.
  241. if _, ok := seenEdge[nodePair{n, parent}]; !ok && parent != nil && n != parent {
  242. seenEdge[nodePair{n, parent}] = true
  243. parent.AddToEdge(n, weight, residual, ni != len(locNodes)-1)
  244. }
  245. parent = n
  246. residual = false
  247. }
  248. }
  249. if parent != nil && !residual {
  250. // Add flat weight to leaf node.
  251. parent.addSample(weight, labels, sample.NumLabel, o.FormatTag, true)
  252. }
  253. }
  254. return selectNodesForGraph(nodes, o.DropNegative), locationMap
  255. }
  256. func selectNodesForGraph(nodes Nodes, dropNegative bool) *Graph {
  257. // Collect nodes into a graph.
  258. gNodes := make(Nodes, 0, len(nodes))
  259. for _, n := range nodes {
  260. if n == nil {
  261. continue
  262. }
  263. if n.Cum == 0 && n.Flat == 0 {
  264. continue
  265. }
  266. if dropNegative && isNegative(n) {
  267. continue
  268. }
  269. gNodes = append(gNodes, n)
  270. }
  271. return &Graph{gNodes}
  272. }
  273. type nodePair struct {
  274. src, dest *Node
  275. }
  276. func newTree(prof *profile.Profile, o *Options) (g *Graph) {
  277. kept := o.KeptNodes
  278. keepBinary := o.ObjNames
  279. parentNodeMap := make(map[*Node]NodeMap, len(prof.Sample))
  280. for _, sample := range prof.Sample {
  281. weight := o.SampleValue(sample.Value)
  282. if weight == 0 {
  283. continue
  284. }
  285. var parent *Node
  286. labels := joinLabels(sample)
  287. // Group the sample frames, based on a per-node map.
  288. for i := len(sample.Location) - 1; i >= 0; i-- {
  289. l := sample.Location[i]
  290. lines := l.Line
  291. if len(lines) == 0 {
  292. lines = []profile.Line{{}} // Create empty line to include location info.
  293. }
  294. for lidx := len(lines) - 1; lidx >= 0; lidx-- {
  295. nodeMap := parentNodeMap[parent]
  296. if nodeMap == nil {
  297. nodeMap = make(NodeMap)
  298. parentNodeMap[parent] = nodeMap
  299. }
  300. n := nodeMap.findOrInsertLine(l, lines[lidx], keepBinary, kept)
  301. if n == nil {
  302. continue
  303. }
  304. n.addSample(weight, labels, sample.NumLabel, o.FormatTag, false)
  305. if parent != nil {
  306. parent.AddToEdge(n, weight, false, lidx != len(lines)-1)
  307. }
  308. parent = n
  309. }
  310. }
  311. if parent != nil {
  312. parent.addSample(weight, labels, sample.NumLabel, o.FormatTag, true)
  313. }
  314. }
  315. nodes := make(Nodes, len(prof.Location))
  316. for _, nm := range parentNodeMap {
  317. nodes = append(nodes, nm.nodes()...)
  318. }
  319. return selectNodesForGraph(nodes, o.DropNegative)
  320. }
  321. // TrimTree trims a Graph in forest form, keeping only the nodes in kept. This
  322. // will not work correctly if even a single node has multiple parents.
  323. func (g *Graph) TrimTree(kept NodePtrSet) {
  324. // Creates a new list of nodes
  325. oldNodes := g.Nodes
  326. g.Nodes = make(Nodes, 0, len(kept))
  327. for _, cur := range oldNodes {
  328. // A node may not have multiple parents
  329. if len(cur.In) > 1 {
  330. panic("TrimTree only works on trees")
  331. }
  332. // If a node should be kept, add it to the new list of nodes
  333. if _, ok := kept[cur]; ok {
  334. g.Nodes = append(g.Nodes, cur)
  335. continue
  336. }
  337. // If a node has no parents, then delete all of the in edges of its
  338. // children to make them each roots of their own trees.
  339. if len(cur.In) == 0 {
  340. for _, outEdge := range cur.Out {
  341. delete(outEdge.Dest.In, cur)
  342. }
  343. continue
  344. }
  345. // Get the parent. This works since at this point cur.In must contain only
  346. // one element.
  347. if len(cur.In) != 1 {
  348. panic("Get parent assertion failed. cur.In expected to be of length 1.")
  349. }
  350. var parent *Node
  351. for _, edge := range cur.In {
  352. parent = edge.Src
  353. }
  354. parentEdgeInline := parent.Out[cur].Inline
  355. // Remove the edge from the parent to this node
  356. delete(parent.Out, cur)
  357. // Reconfigure every edge from the current node to now begin at the parent.
  358. for _, outEdge := range cur.Out {
  359. child := outEdge.Dest
  360. delete(child.In, cur)
  361. child.In[parent] = outEdge
  362. parent.Out[child] = outEdge
  363. outEdge.Src = parent
  364. outEdge.Residual = true
  365. // If the edge from the parent to the current node and the edge from the
  366. // current node to the child are both inline, then this resulting residual
  367. // edge should also be inline
  368. outEdge.Inline = parentEdgeInline && outEdge.Inline
  369. }
  370. }
  371. g.RemoveRedundantEdges()
  372. }
  373. func joinLabels(s *profile.Sample) string {
  374. if len(s.Label) == 0 {
  375. return ""
  376. }
  377. var labels []string
  378. for key, vals := range s.Label {
  379. for _, v := range vals {
  380. labels = append(labels, key+":"+v)
  381. }
  382. }
  383. sort.Strings(labels)
  384. return strings.Join(labels, `\n`)
  385. }
  386. // isNegative returns true if the node is considered as "negative" for the
  387. // purposes of drop_negative.
  388. func isNegative(n *Node) bool {
  389. switch {
  390. case n.Flat < 0:
  391. return true
  392. case n.Flat == 0 && n.Cum < 0:
  393. return true
  394. default:
  395. return false
  396. }
  397. }
  398. // CreateNodes creates graph nodes for all locations in a profile. It
  399. // returns set of all nodes, plus a mapping of each location to the
  400. // set of corresponding nodes (one per location.Line). If kept is
  401. // non-nil, only nodes in that set are included; nodes that do not
  402. // match are represented as a nil.
  403. func CreateNodes(prof *profile.Profile, keepBinary bool, kept NodeSet) (Nodes, map[uint64]Nodes) {
  404. locations := make(map[uint64]Nodes, len(prof.Location))
  405. nm := make(NodeMap, len(prof.Location))
  406. for _, l := range prof.Location {
  407. lines := l.Line
  408. if len(lines) == 0 {
  409. lines = []profile.Line{{}} // Create empty line to include location info.
  410. }
  411. nodes := make(Nodes, len(lines))
  412. for ln := range lines {
  413. nodes[ln] = nm.findOrInsertLine(l, lines[ln], keepBinary, kept)
  414. }
  415. locations[l.ID] = nodes
  416. }
  417. return nm.nodes(), locations
  418. }
  419. func (nm NodeMap) nodes() Nodes {
  420. nodes := make(Nodes, 0, len(nm))
  421. for _, n := range nm {
  422. nodes = append(nodes, n)
  423. }
  424. return nodes
  425. }
  426. func (nm NodeMap) findOrInsertLine(l *profile.Location, li profile.Line, keepBinary bool, kept NodeSet) *Node {
  427. var objfile string
  428. if m := l.Mapping; m != nil && m.File != "" {
  429. objfile = filepath.Base(m.File)
  430. }
  431. if ni := nodeInfo(l, li, objfile, keepBinary); ni != nil {
  432. return nm.FindOrInsertNode(*ni, kept)
  433. }
  434. return nil
  435. }
  436. func nodeInfo(l *profile.Location, line profile.Line, objfile string, keepBinary bool) *NodeInfo {
  437. if line.Function == nil {
  438. return &NodeInfo{Address: l.Address, Objfile: objfile}
  439. }
  440. ni := &NodeInfo{
  441. Address: l.Address,
  442. Lineno: int(line.Line),
  443. Name: line.Function.Name,
  444. OrigName: line.Function.SystemName,
  445. }
  446. if fname := line.Function.Filename; fname != "" {
  447. ni.File = filepath.Clean(fname)
  448. }
  449. if keepBinary {
  450. ni.Objfile = objfile
  451. ni.StartLine = int(line.Function.StartLine)
  452. }
  453. return ni
  454. }
  455. type tags struct {
  456. t []*Tag
  457. flat bool
  458. }
  459. func (t tags) Len() int { return len(t.t) }
  460. func (t tags) Swap(i, j int) { t.t[i], t.t[j] = t.t[j], t.t[i] }
  461. func (t tags) Less(i, j int) bool {
  462. if !t.flat {
  463. if t.t[i].Cum != t.t[j].Cum {
  464. return abs64(t.t[i].Cum) > abs64(t.t[j].Cum)
  465. }
  466. }
  467. if t.t[i].Flat != t.t[j].Flat {
  468. return abs64(t.t[i].Flat) > abs64(t.t[j].Flat)
  469. }
  470. return t.t[i].Name < t.t[j].Name
  471. }
  472. // Sum adds the flat and cum values of a set of nodes.
  473. func (ns Nodes) Sum() (flat int64, cum int64) {
  474. for _, n := range ns {
  475. flat += n.Flat
  476. cum += n.Cum
  477. }
  478. return
  479. }
  480. func (n *Node) addSample(value int64, labels string, numLabel map[string][]int64, format func(int64, string) string, flat bool) {
  481. // Update sample value
  482. if flat {
  483. n.Flat += value
  484. } else {
  485. n.Cum += value
  486. }
  487. // Add string tags
  488. if labels != "" {
  489. t := n.LabelTags.findOrAddTag(labels, "", 0)
  490. if flat {
  491. t.Flat += value
  492. } else {
  493. t.Cum += value
  494. }
  495. }
  496. numericTags := n.NumericTags[labels]
  497. if numericTags == nil {
  498. numericTags = TagMap{}
  499. n.NumericTags[labels] = numericTags
  500. }
  501. // Add numeric tags
  502. if format == nil {
  503. format = defaultLabelFormat
  504. }
  505. for key, nvals := range numLabel {
  506. for _, v := range nvals {
  507. t := numericTags.findOrAddTag(format(v, key), key, v)
  508. if flat {
  509. t.Flat += value
  510. } else {
  511. t.Cum += value
  512. }
  513. }
  514. }
  515. }
  516. func defaultLabelFormat(v int64, key string) string {
  517. return strconv.FormatInt(v, 10)
  518. }
  519. func (m TagMap) findOrAddTag(label, unit string, value int64) *Tag {
  520. l := m[label]
  521. if l == nil {
  522. l = &Tag{
  523. Name: label,
  524. Unit: unit,
  525. Value: value,
  526. }
  527. m[label] = l
  528. }
  529. return l
  530. }
  531. // String returns a text representation of a graph, for debugging purposes.
  532. func (g *Graph) String() string {
  533. var s []string
  534. nodeIndex := make(map[*Node]int, len(g.Nodes))
  535. for i, n := range g.Nodes {
  536. nodeIndex[n] = i + 1
  537. }
  538. for i, n := range g.Nodes {
  539. name := n.Info.PrintableName()
  540. var in, out []int
  541. for _, from := range n.In {
  542. in = append(in, nodeIndex[from.Src])
  543. }
  544. for _, to := range n.Out {
  545. out = append(out, nodeIndex[to.Dest])
  546. }
  547. s = append(s, fmt.Sprintf("%d: %s[flat=%d cum=%d] %x -> %v ", i+1, name, n.Flat, n.Cum, in, out))
  548. }
  549. return strings.Join(s, "\n")
  550. }
  551. // DiscardLowFrequencyNodes returns a set of the nodes at or over a
  552. // specific cum value cutoff.
  553. func (g *Graph) DiscardLowFrequencyNodes(nodeCutoff int64) NodeSet {
  554. return makeNodeSet(g.Nodes, nodeCutoff)
  555. }
  556. // DiscardLowFrequencyNodePtrs returns a NodePtrSet of nodes at or over a
  557. // specific cum value cutoff.
  558. func (g *Graph) DiscardLowFrequencyNodePtrs(nodeCutoff int64) NodePtrSet {
  559. cutNodes := getNodesAboveCumCutoff(g.Nodes, nodeCutoff)
  560. kept := make(NodePtrSet, len(cutNodes))
  561. for _, n := range cutNodes {
  562. kept[n] = true
  563. }
  564. return kept
  565. }
  566. func makeNodeSet(nodes Nodes, nodeCutoff int64) NodeSet {
  567. cutNodes := getNodesAboveCumCutoff(nodes, nodeCutoff)
  568. kept := make(NodeSet, len(cutNodes))
  569. for _, n := range cutNodes {
  570. kept[n.Info] = true
  571. }
  572. return kept
  573. }
  574. // getNodesAboveCumCutoff returns all the nodes which have a Cum value greater
  575. // than or equal to cutoff.
  576. func getNodesAboveCumCutoff(nodes Nodes, nodeCutoff int64) Nodes {
  577. cutoffNodes := make(Nodes, 0, len(nodes))
  578. for _, n := range nodes {
  579. if abs64(n.Cum) < nodeCutoff {
  580. continue
  581. }
  582. cutoffNodes = append(cutoffNodes, n)
  583. }
  584. return cutoffNodes
  585. }
  586. // TrimLowFrequencyTags removes tags that have less than
  587. // the specified weight.
  588. func (g *Graph) TrimLowFrequencyTags(tagCutoff int64) {
  589. // Remove nodes with value <= total*nodeFraction
  590. for _, n := range g.Nodes {
  591. n.LabelTags = trimLowFreqTags(n.LabelTags, tagCutoff)
  592. for s, nt := range n.NumericTags {
  593. n.NumericTags[s] = trimLowFreqTags(nt, tagCutoff)
  594. }
  595. }
  596. }
  597. func trimLowFreqTags(tags TagMap, minValue int64) TagMap {
  598. kept := TagMap{}
  599. for s, t := range tags {
  600. if abs64(t.Flat) >= minValue || abs64(t.Cum) >= minValue {
  601. kept[s] = t
  602. }
  603. }
  604. return kept
  605. }
  606. // TrimLowFrequencyEdges removes edges that have less than
  607. // the specified weight. Returns the number of edges removed
  608. func (g *Graph) TrimLowFrequencyEdges(edgeCutoff int64) int {
  609. var droppedEdges int
  610. for _, n := range g.Nodes {
  611. for src, e := range n.In {
  612. if abs64(e.Weight) < edgeCutoff {
  613. delete(n.In, src)
  614. delete(src.Out, n)
  615. droppedEdges++
  616. }
  617. }
  618. }
  619. return droppedEdges
  620. }
  621. // SortNodes sorts the nodes in a graph based on a specific heuristic.
  622. func (g *Graph) SortNodes(cum bool, visualMode bool) {
  623. // Sort nodes based on requested mode
  624. switch {
  625. case visualMode:
  626. // Specialized sort to produce a more visually-interesting graph
  627. g.Nodes.Sort(EntropyOrder)
  628. case cum:
  629. g.Nodes.Sort(CumNameOrder)
  630. default:
  631. g.Nodes.Sort(FlatNameOrder)
  632. }
  633. }
  634. // SelectTopNodePtrs returns a set of the top maxNodes *Node in a graph.
  635. func (g *Graph) SelectTopNodePtrs(maxNodes int, visualMode bool) NodePtrSet {
  636. set := make(NodePtrSet)
  637. for _, node := range g.selectTopNodes(maxNodes, visualMode) {
  638. set[node] = true
  639. }
  640. return set
  641. }
  642. // SelectTopNodes returns a set of the top maxNodes nodes in a graph.
  643. func (g *Graph) SelectTopNodes(maxNodes int, visualMode bool) NodeSet {
  644. return makeNodeSet(g.selectTopNodes(maxNodes, visualMode), 0)
  645. }
  646. // selectTopNodes returns a slice of the top maxNodes nodes in a graph.
  647. func (g *Graph) selectTopNodes(maxNodes int, visualMode bool) Nodes {
  648. if maxNodes > 0 {
  649. if visualMode {
  650. var count int
  651. // If generating a visual graph, count tags as nodes. Update
  652. // maxNodes to account for them.
  653. for i, n := range g.Nodes {
  654. if count += countTags(n) + 1; count >= maxNodes {
  655. maxNodes = i + 1
  656. break
  657. }
  658. }
  659. }
  660. }
  661. if maxNodes > len(g.Nodes) {
  662. maxNodes = len(g.Nodes)
  663. }
  664. return g.Nodes[:maxNodes]
  665. }
  666. // countTags counts the tags with flat count. This underestimates the
  667. // number of tags being displayed, but in practice is close enough.
  668. func countTags(n *Node) int {
  669. count := 0
  670. for _, e := range n.LabelTags {
  671. if e.Flat != 0 {
  672. count++
  673. }
  674. }
  675. for _, t := range n.NumericTags {
  676. for _, e := range t {
  677. if e.Flat != 0 {
  678. count++
  679. }
  680. }
  681. }
  682. return count
  683. }
  684. // countEdges counts the number of edges below the specified cutoff.
  685. func countEdges(el EdgeMap, cutoff int64) int {
  686. count := 0
  687. for _, e := range el {
  688. if e.Weight > cutoff {
  689. count++
  690. }
  691. }
  692. return count
  693. }
  694. // RemoveRedundantEdges removes residual edges if the destination can
  695. // be reached through another path. This is done to simplify the graph
  696. // while preserving connectivity.
  697. func (g *Graph) RemoveRedundantEdges() {
  698. // Walk the nodes and outgoing edges in reverse order to prefer
  699. // removing edges with the lowest weight.
  700. for i := len(g.Nodes); i > 0; i-- {
  701. n := g.Nodes[i-1]
  702. in := n.In.Sort()
  703. for j := len(in); j > 0; j-- {
  704. e := in[j-1]
  705. if !e.Residual {
  706. // Do not remove edges heavier than a non-residual edge, to
  707. // avoid potential confusion.
  708. break
  709. }
  710. if isRedundant(e) {
  711. delete(e.Src.Out, e.Dest)
  712. delete(e.Dest.In, e.Src)
  713. }
  714. }
  715. }
  716. }
  717. // isRedundant determines if an edge can be removed without impacting
  718. // connectivity of the whole graph. This is implemented by checking if the
  719. // nodes have a common ancestor after removing the edge.
  720. func isRedundant(e *Edge) bool {
  721. destPred := predecessors(e, e.Dest)
  722. if len(destPred) == 1 {
  723. return false
  724. }
  725. srcPred := predecessors(e, e.Src)
  726. for n := range srcPred {
  727. if destPred[n] && n != e.Dest {
  728. return true
  729. }
  730. }
  731. return false
  732. }
  733. // predecessors collects all the predecessors to node n, excluding edge e.
  734. func predecessors(e *Edge, n *Node) map[*Node]bool {
  735. seen := map[*Node]bool{n: true}
  736. queue := Nodes{n}
  737. for len(queue) > 0 {
  738. n := queue[0]
  739. queue = queue[1:]
  740. for _, ie := range n.In {
  741. if e == ie || seen[ie.Src] {
  742. continue
  743. }
  744. seen[ie.Src] = true
  745. queue = append(queue, ie.Src)
  746. }
  747. }
  748. return seen
  749. }
  750. // nodeSorter is a mechanism used to allow a report to be sorted
  751. // in different ways.
  752. type nodeSorter struct {
  753. rs Nodes
  754. less func(l, r *Node) bool
  755. }
  756. func (s nodeSorter) Len() int { return len(s.rs) }
  757. func (s nodeSorter) Swap(i, j int) { s.rs[i], s.rs[j] = s.rs[j], s.rs[i] }
  758. func (s nodeSorter) Less(i, j int) bool { return s.less(s.rs[i], s.rs[j]) }
  759. // Sort reorders a slice of nodes based on the specified ordering
  760. // criteria. The result is sorted in decreasing order for (absolute)
  761. // numeric quantities, alphabetically for text, and increasing for
  762. // addresses.
  763. func (ns Nodes) Sort(o NodeOrder) error {
  764. var s nodeSorter
  765. switch o {
  766. case FlatNameOrder:
  767. s = nodeSorter{ns,
  768. func(l, r *Node) bool {
  769. if iv, jv := abs64(l.Flat), abs64(r.Flat); iv != jv {
  770. return iv > jv
  771. }
  772. if iv, jv := l.Info.PrintableName(), r.Info.PrintableName(); iv != jv {
  773. return iv < jv
  774. }
  775. if iv, jv := abs64(l.Cum), abs64(r.Cum); iv != jv {
  776. return iv > jv
  777. }
  778. return compareNodes(l, r)
  779. },
  780. }
  781. case FlatCumNameOrder:
  782. s = nodeSorter{ns,
  783. func(l, r *Node) bool {
  784. if iv, jv := abs64(l.Flat), abs64(r.Flat); iv != jv {
  785. return iv > jv
  786. }
  787. if iv, jv := abs64(l.Cum), abs64(r.Cum); iv != jv {
  788. return iv > jv
  789. }
  790. if iv, jv := l.Info.PrintableName(), r.Info.PrintableName(); iv != jv {
  791. return iv < jv
  792. }
  793. return compareNodes(l, r)
  794. },
  795. }
  796. case NameOrder:
  797. s = nodeSorter{ns,
  798. func(l, r *Node) bool {
  799. if iv, jv := l.Info.Name, r.Info.Name; iv != jv {
  800. return iv < jv
  801. }
  802. return compareNodes(l, r)
  803. },
  804. }
  805. case FileOrder:
  806. s = nodeSorter{ns,
  807. func(l, r *Node) bool {
  808. if iv, jv := l.Info.File, r.Info.File; iv != jv {
  809. return iv < jv
  810. }
  811. if iv, jv := l.Info.StartLine, r.Info.StartLine; iv != jv {
  812. return iv < jv
  813. }
  814. return compareNodes(l, r)
  815. },
  816. }
  817. case AddressOrder:
  818. s = nodeSorter{ns,
  819. func(l, r *Node) bool {
  820. if iv, jv := l.Info.Address, r.Info.Address; iv != jv {
  821. return iv < jv
  822. }
  823. return compareNodes(l, r)
  824. },
  825. }
  826. case CumNameOrder, EntropyOrder:
  827. // Hold scoring for score-based ordering
  828. var score map[*Node]int64
  829. scoreOrder := func(l, r *Node) bool {
  830. if iv, jv := abs64(score[l]), abs64(score[r]); iv != jv {
  831. return iv > jv
  832. }
  833. if iv, jv := l.Info.PrintableName(), r.Info.PrintableName(); iv != jv {
  834. return iv < jv
  835. }
  836. if iv, jv := abs64(l.Flat), abs64(r.Flat); iv != jv {
  837. return iv > jv
  838. }
  839. return compareNodes(l, r)
  840. }
  841. switch o {
  842. case CumNameOrder:
  843. score = make(map[*Node]int64, len(ns))
  844. for _, n := range ns {
  845. score[n] = n.Cum
  846. }
  847. s = nodeSorter{ns, scoreOrder}
  848. case EntropyOrder:
  849. score = make(map[*Node]int64, len(ns))
  850. for _, n := range ns {
  851. score[n] = entropyScore(n)
  852. }
  853. s = nodeSorter{ns, scoreOrder}
  854. }
  855. default:
  856. return fmt.Errorf("report: unrecognized sort ordering: %d", o)
  857. }
  858. sort.Sort(s)
  859. return nil
  860. }
  861. // compareNodes compares two nodes to provide a deterministic ordering
  862. // between them. Two nodes cannot have the same Node.Info value.
  863. func compareNodes(l, r *Node) bool {
  864. return fmt.Sprint(l.Info) < fmt.Sprint(r.Info)
  865. }
  866. // entropyScore computes a score for a node representing how important
  867. // it is to include this node on a graph visualization. It is used to
  868. // sort the nodes and select which ones to display if we have more
  869. // nodes than desired in the graph. This number is computed by looking
  870. // at the flat and cum weights of the node and the incoming/outgoing
  871. // edges. The fundamental idea is to penalize nodes that have a simple
  872. // fallthrough from their incoming to the outgoing edge.
  873. func entropyScore(n *Node) int64 {
  874. score := float64(0)
  875. if len(n.In) == 0 {
  876. score++ // Favor entry nodes
  877. } else {
  878. score += edgeEntropyScore(n, n.In, 0)
  879. }
  880. if len(n.Out) == 0 {
  881. score++ // Favor leaf nodes
  882. } else {
  883. score += edgeEntropyScore(n, n.Out, n.Flat)
  884. }
  885. return int64(score*float64(n.Cum)) + n.Flat
  886. }
  887. // edgeEntropyScore computes the entropy value for a set of edges
  888. // coming in or out of a node. Entropy (as defined in information
  889. // theory) refers to the amount of information encoded by the set of
  890. // edges. A set of edges that have a more interesting distribution of
  891. // samples gets a higher score.
  892. func edgeEntropyScore(n *Node, edges EdgeMap, self int64) float64 {
  893. score := float64(0)
  894. total := self
  895. for _, e := range edges {
  896. if e.Weight > 0 {
  897. total += abs64(e.Weight)
  898. }
  899. }
  900. if total != 0 {
  901. for _, e := range edges {
  902. frac := float64(abs64(e.Weight)) / float64(total)
  903. score += -frac * math.Log2(frac)
  904. }
  905. if self > 0 {
  906. frac := float64(abs64(self)) / float64(total)
  907. score += -frac * math.Log2(frac)
  908. }
  909. }
  910. return score
  911. }
  912. // NodeOrder sets the ordering for a Sort operation
  913. type NodeOrder int
  914. // Sorting options for node sort.
  915. const (
  916. FlatNameOrder NodeOrder = iota
  917. FlatCumNameOrder
  918. CumNameOrder
  919. NameOrder
  920. FileOrder
  921. AddressOrder
  922. EntropyOrder
  923. )
  924. // Sort returns a slice of the edges in the map, in a consistent
  925. // order. The sort order is first based on the edge weight
  926. // (higher-to-lower) and then by the node names to avoid flakiness.
  927. func (e EdgeMap) Sort() []*Edge {
  928. el := make(edgeList, 0, len(e))
  929. for _, w := range e {
  930. el = append(el, w)
  931. }
  932. sort.Sort(el)
  933. return el
  934. }
  935. // Sum returns the total weight for a set of nodes.
  936. func (e EdgeMap) Sum() int64 {
  937. var ret int64
  938. for _, edge := range e {
  939. ret += edge.Weight
  940. }
  941. return ret
  942. }
  943. type edgeList []*Edge
  944. func (el edgeList) Len() int {
  945. return len(el)
  946. }
  947. func (el edgeList) Less(i, j int) bool {
  948. if el[i].Weight != el[j].Weight {
  949. return abs64(el[i].Weight) > abs64(el[j].Weight)
  950. }
  951. from1 := el[i].Src.Info.PrintableName()
  952. from2 := el[j].Src.Info.PrintableName()
  953. if from1 != from2 {
  954. return from1 < from2
  955. }
  956. to1 := el[i].Dest.Info.PrintableName()
  957. to2 := el[j].Dest.Info.PrintableName()
  958. return to1 < to2
  959. }
  960. func (el edgeList) Swap(i, j int) {
  961. el[i], el[j] = el[j], el[i]
  962. }
  963. func abs64(i int64) int64 {
  964. if i < 0 {
  965. return -i
  966. }
  967. return i
  968. }