Aucune description

graph.go 26KB

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