Aucune description

graph.go 27KB

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