暫無描述

graph.go 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. // FindOrInsertNode takes the info for a node and either returns a matching node
  125. // from the node map if one exists, or adds one to the map if one does not.
  126. // If kept is non-nil, nodes are only added if they can be located on it.
  127. func (nm NodeMap) FindOrInsertNode(info NodeInfo, kept NodeSet) *Node {
  128. if kept != nil {
  129. if _, ok := kept[info]; !ok {
  130. return nil
  131. }
  132. }
  133. if n, ok := nm[info]; ok {
  134. return n
  135. }
  136. n := &Node{
  137. Info: info,
  138. In: make(EdgeMap),
  139. Out: make(EdgeMap),
  140. LabelTags: make(TagMap),
  141. NumericTags: make(map[string]TagMap),
  142. }
  143. nm[info] = n
  144. return n
  145. }
  146. // EdgeMap is used to represent the incoming/outgoing edges from a node.
  147. type EdgeMap map[*Node]*Edge
  148. // Edge contains any attributes to be represented about edges in a graph.
  149. type Edge struct {
  150. Src, Dest *Node
  151. // The summary weight of the edge
  152. Weight int64
  153. // residual edges connect nodes that were connected through a
  154. // separate node, which has been removed from the report.
  155. Residual bool
  156. // An inline edge represents a call that was inlined into the caller.
  157. Inline bool
  158. }
  159. // Tag represent sample annotations
  160. type Tag struct {
  161. Name string
  162. Unit string // Describe the value, "" for non-numeric tags
  163. Value int64
  164. Flat int64
  165. Cum int64
  166. }
  167. // TagMap is a collection of tags, classified by their name.
  168. type TagMap map[string]*Tag
  169. // SortTags sorts a slice of tags based on their weight.
  170. func SortTags(t []*Tag, flat bool) []*Tag {
  171. ts := tags{t, flat}
  172. sort.Sort(ts)
  173. return ts.t
  174. }
  175. // New summarizes performance data from a profile into a graph.
  176. func New(prof *profile.Profile, o *Options) *Graph {
  177. if o.CallTree {
  178. return newTree(prof, o)
  179. }
  180. g, _ := newGraph(prof, o)
  181. return g
  182. }
  183. // newGraph computes a graph from a profile. It returns the graph, and
  184. // a map from the profile location indices to the corresponding graph
  185. // nodes.
  186. func newGraph(prof *profile.Profile, o *Options) (*Graph, map[uint64]Nodes) {
  187. nodes, locationMap := CreateNodes(prof, o.ObjNames, o.KeptNodes)
  188. for _, sample := range prof.Sample {
  189. weight := o.SampleValue(sample.Value)
  190. if weight == 0 {
  191. continue
  192. }
  193. seenNode := make(map[*Node]bool, len(sample.Location))
  194. seenEdge := make(map[nodePair]bool, len(sample.Location))
  195. var parent *Node
  196. // A residual edge goes over one or more nodes that were not kept.
  197. residual := false
  198. labels := joinLabels(sample)
  199. // Group the sample frames, based on a global map.
  200. for i := len(sample.Location) - 1; i >= 0; i-- {
  201. l := sample.Location[i]
  202. locNodes := locationMap[l.ID]
  203. for ni := len(locNodes) - 1; ni >= 0; ni-- {
  204. n := locNodes[ni]
  205. if n == nil {
  206. residual = true
  207. continue
  208. }
  209. // Add cum weight to all nodes in stack, avoiding double counting.
  210. if _, ok := seenNode[n]; !ok {
  211. seenNode[n] = true
  212. n.addSample(weight, labels, sample.NumLabel, o.FormatTag, false)
  213. }
  214. // Update edge weights for all edges in stack, avoiding double counting.
  215. if _, ok := seenEdge[nodePair{n, parent}]; !ok && parent != nil && n != parent {
  216. seenEdge[nodePair{n, parent}] = true
  217. parent.AddToEdge(n, weight, residual, ni != len(locNodes)-1)
  218. }
  219. parent = n
  220. residual = false
  221. }
  222. }
  223. if parent != nil && !residual {
  224. // Add flat weight to leaf node.
  225. parent.addSample(weight, labels, sample.NumLabel, o.FormatTag, true)
  226. }
  227. }
  228. return selectNodesForGraph(nodes, o.DropNegative), locationMap
  229. }
  230. func selectNodesForGraph(nodes Nodes, dropNegative bool) *Graph {
  231. // Collect nodes into a graph.
  232. gNodes := make(Nodes, 0, len(nodes))
  233. for _, n := range nodes {
  234. if n == nil {
  235. continue
  236. }
  237. if n.Cum == 0 && n.Flat == 0 {
  238. continue
  239. }
  240. if dropNegative && isNegative(n) {
  241. continue
  242. }
  243. gNodes = append(gNodes, n)
  244. }
  245. return &Graph{gNodes}
  246. }
  247. type nodePair struct {
  248. src, dest *Node
  249. }
  250. func newTree(prof *profile.Profile, o *Options) (g *Graph) {
  251. kept := o.KeptNodes
  252. keepBinary := o.ObjNames
  253. parentNodeMap := make(map[*Node]NodeMap, len(prof.Sample))
  254. nextSample:
  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 nextSample
  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. func joinLabels(s *profile.Sample) string {
  297. if len(s.Label) == 0 {
  298. return ""
  299. }
  300. var labels []string
  301. for key, vals := range s.Label {
  302. for _, v := range vals {
  303. labels = append(labels, key+":"+v)
  304. }
  305. }
  306. sort.Strings(labels)
  307. return strings.Join(labels, `\n`)
  308. }
  309. // isNegative returns true if the node is considered as "negative" for the
  310. // purposes of drop_negative.
  311. func isNegative(n *Node) bool {
  312. switch {
  313. case n.Flat < 0:
  314. return true
  315. case n.Flat == 0 && n.Cum < 0:
  316. return true
  317. default:
  318. return false
  319. }
  320. }
  321. // CreateNodes creates graph nodes for all locations in a profile. It
  322. // returns set of all nodes, plus a mapping of each location to the
  323. // set of corresponding nodes (one per location.Line). If kept is
  324. // non-nil, only nodes in that set are included; nodes that do not
  325. // match are represented as a nil.
  326. func CreateNodes(prof *profile.Profile, keepBinary bool, kept NodeSet) (Nodes, map[uint64]Nodes) {
  327. locations := make(map[uint64]Nodes, len(prof.Location))
  328. nm := make(NodeMap, len(prof.Location))
  329. for _, l := range prof.Location {
  330. lines := l.Line
  331. if len(lines) == 0 {
  332. lines = []profile.Line{{}} // Create empty line to include location info.
  333. }
  334. nodes := make(Nodes, len(lines))
  335. for ln := range lines {
  336. nodes[ln] = nm.findOrInsertLine(l, lines[ln], keepBinary, kept)
  337. }
  338. locations[l.ID] = nodes
  339. }
  340. return nm.nodes(), locations
  341. }
  342. func (nm NodeMap) nodes() Nodes {
  343. nodes := make(Nodes, 0, len(nm))
  344. for _, n := range nm {
  345. nodes = append(nodes, n)
  346. }
  347. return nodes
  348. }
  349. func (nm NodeMap) findOrInsertLine(l *profile.Location, li profile.Line, keepBinary bool, kept NodeSet) *Node {
  350. var objfile string
  351. if m := l.Mapping; m != nil && m.File != "" {
  352. objfile = filepath.Base(m.File)
  353. }
  354. if ni := nodeInfo(l, li, objfile, keepBinary); ni != nil {
  355. return nm.FindOrInsertNode(*ni, kept)
  356. }
  357. return nil
  358. }
  359. func nodeInfo(l *profile.Location, line profile.Line, objfile string, keepBinary bool) *NodeInfo {
  360. if line.Function == nil {
  361. return &NodeInfo{Address: l.Address, Objfile: objfile}
  362. }
  363. ni := &NodeInfo{
  364. Address: l.Address,
  365. Lineno: int(line.Line),
  366. Name: line.Function.Name,
  367. OrigName: line.Function.SystemName,
  368. }
  369. if fname := line.Function.Filename; fname != "" {
  370. ni.File = filepath.Clean(fname)
  371. }
  372. if keepBinary {
  373. ni.Objfile = objfile
  374. ni.StartLine = int(line.Function.StartLine)
  375. }
  376. return ni
  377. }
  378. type tags struct {
  379. t []*Tag
  380. flat bool
  381. }
  382. func (t tags) Len() int { return len(t.t) }
  383. func (t tags) Swap(i, j int) { t.t[i], t.t[j] = t.t[j], t.t[i] }
  384. func (t tags) Less(i, j int) bool {
  385. if !t.flat {
  386. if t.t[i].Cum != t.t[j].Cum {
  387. return abs64(t.t[i].Cum) > abs64(t.t[j].Cum)
  388. }
  389. }
  390. if t.t[i].Flat != t.t[j].Flat {
  391. return abs64(t.t[i].Flat) > abs64(t.t[j].Flat)
  392. }
  393. return t.t[i].Name < t.t[j].Name
  394. }
  395. // Sum adds the flat and cum values of a set of nodes.
  396. func (ns Nodes) Sum() (flat int64, cum int64) {
  397. for _, n := range ns {
  398. flat += n.Flat
  399. cum += n.Cum
  400. }
  401. return
  402. }
  403. func (n *Node) addSample(value int64, labels string, numLabel map[string][]int64, format func(int64, string) string, flat bool) {
  404. // Update sample value
  405. if flat {
  406. n.Flat += value
  407. } else {
  408. n.Cum += value
  409. }
  410. // Add string tags
  411. if labels != "" {
  412. t := n.LabelTags.findOrAddTag(labels, "", 0)
  413. if flat {
  414. t.Flat += value
  415. } else {
  416. t.Cum += value
  417. }
  418. }
  419. numericTags := n.NumericTags[labels]
  420. if numericTags == nil {
  421. numericTags = TagMap{}
  422. n.NumericTags[labels] = numericTags
  423. }
  424. // Add numeric tags
  425. if format == nil {
  426. format = defaultLabelFormat
  427. }
  428. for key, nvals := range numLabel {
  429. for _, v := range nvals {
  430. t := numericTags.findOrAddTag(format(v, key), key, v)
  431. if flat {
  432. t.Flat += value
  433. } else {
  434. t.Cum += value
  435. }
  436. }
  437. }
  438. }
  439. func defaultLabelFormat(v int64, key string) string {
  440. return strconv.FormatInt(v, 10)
  441. }
  442. func (m TagMap) findOrAddTag(label, unit string, value int64) *Tag {
  443. l := m[label]
  444. if l == nil {
  445. l = &Tag{
  446. Name: label,
  447. Unit: unit,
  448. Value: value,
  449. }
  450. m[label] = l
  451. }
  452. return l
  453. }
  454. // String returns a text representation of a graph, for debugging purposes.
  455. func (g *Graph) String() string {
  456. var s []string
  457. nodeIndex := make(map[*Node]int, len(g.Nodes))
  458. for i, n := range g.Nodes {
  459. nodeIndex[n] = i + 1
  460. }
  461. for i, n := range g.Nodes {
  462. name := n.Info.PrintableName()
  463. var in, out []int
  464. for _, from := range n.In {
  465. in = append(in, nodeIndex[from.Src])
  466. }
  467. for _, to := range n.Out {
  468. out = append(out, nodeIndex[to.Dest])
  469. }
  470. s = append(s, fmt.Sprintf("%d: %s[flat=%d cum=%d] %x -> %v ", i+1, name, n.Flat, n.Cum, in, out))
  471. }
  472. return strings.Join(s, "\n")
  473. }
  474. // DiscardLowFrequencyNodes returns a set of the nodes at or over a
  475. // specific cum value cutoff.
  476. func (g *Graph) DiscardLowFrequencyNodes(nodeCutoff int64) NodeSet {
  477. return makeNodeSet(g.Nodes, nodeCutoff)
  478. }
  479. func makeNodeSet(nodes Nodes, nodeCutoff int64) NodeSet {
  480. kept := make(NodeSet, len(nodes))
  481. for _, n := range nodes {
  482. if abs64(n.Cum) < nodeCutoff {
  483. continue
  484. }
  485. kept[n.Info] = true
  486. }
  487. return kept
  488. }
  489. // TrimLowFrequencyTags removes tags that have less than
  490. // the specified weight.
  491. func (g *Graph) TrimLowFrequencyTags(tagCutoff int64) {
  492. // Remove nodes with value <= total*nodeFraction
  493. for _, n := range g.Nodes {
  494. n.LabelTags = trimLowFreqTags(n.LabelTags, tagCutoff)
  495. for s, nt := range n.NumericTags {
  496. n.NumericTags[s] = trimLowFreqTags(nt, tagCutoff)
  497. }
  498. }
  499. }
  500. func trimLowFreqTags(tags TagMap, minValue int64) TagMap {
  501. kept := TagMap{}
  502. for s, t := range tags {
  503. if abs64(t.Flat) >= minValue || abs64(t.Cum) >= minValue {
  504. kept[s] = t
  505. }
  506. }
  507. return kept
  508. }
  509. // TrimLowFrequencyEdges removes edges that have less than
  510. // the specified weight. Returns the number of edges removed
  511. func (g *Graph) TrimLowFrequencyEdges(edgeCutoff int64) int {
  512. var droppedEdges int
  513. for _, n := range g.Nodes {
  514. for src, e := range n.In {
  515. if abs64(e.Weight) < edgeCutoff {
  516. delete(n.In, src)
  517. delete(src.Out, n)
  518. droppedEdges++
  519. }
  520. }
  521. }
  522. return droppedEdges
  523. }
  524. // SortNodes sorts the nodes in a graph based on a specific heuristic.
  525. func (g *Graph) SortNodes(cum bool, visualMode bool) {
  526. // Sort nodes based on requested mode
  527. switch {
  528. case visualMode:
  529. // Specialized sort to produce a more visually-interesting graph
  530. g.Nodes.Sort(EntropyOrder)
  531. case cum:
  532. g.Nodes.Sort(CumNameOrder)
  533. default:
  534. g.Nodes.Sort(FlatNameOrder)
  535. }
  536. }
  537. // SelectTopNodes returns a set of the top maxNodes nodes in a graph.
  538. func (g *Graph) SelectTopNodes(maxNodes int, visualMode bool) NodeSet {
  539. if maxNodes > 0 {
  540. if visualMode {
  541. var count int
  542. // If generating a visual graph, count tags as nodes. Update
  543. // maxNodes to account for them.
  544. for i, n := range g.Nodes {
  545. if count += countTags(n) + 1; count >= maxNodes {
  546. maxNodes = i + 1
  547. break
  548. }
  549. }
  550. }
  551. }
  552. if maxNodes > len(g.Nodes) {
  553. maxNodes = len(g.Nodes)
  554. }
  555. return makeNodeSet(g.Nodes[:maxNodes], 0)
  556. }
  557. // countTags counts the tags with flat count. This underestimates the
  558. // number of tags being displayed, but in practice is close enough.
  559. func countTags(n *Node) int {
  560. count := 0
  561. for _, e := range n.LabelTags {
  562. if e.Flat != 0 {
  563. count++
  564. }
  565. }
  566. for _, t := range n.NumericTags {
  567. for _, e := range t {
  568. if e.Flat != 0 {
  569. count++
  570. }
  571. }
  572. }
  573. return count
  574. }
  575. // countEdges counts the number of edges below the specified cutoff.
  576. func countEdges(el EdgeMap, cutoff int64) int {
  577. count := 0
  578. for _, e := range el {
  579. if e.Weight > cutoff {
  580. count++
  581. }
  582. }
  583. return count
  584. }
  585. // RemoveRedundantEdges removes residual edges if the destination can
  586. // be reached through another path. This is done to simplify the graph
  587. // while preserving connectivity.
  588. func (g *Graph) RemoveRedundantEdges() {
  589. // Walk the nodes and outgoing edges in reverse order to prefer
  590. // removing edges with the lowest weight.
  591. for i := len(g.Nodes); i > 0; i-- {
  592. n := g.Nodes[i-1]
  593. in := n.In.Sort()
  594. for j := len(in); j > 0; j-- {
  595. e := in[j-1]
  596. if !e.Residual {
  597. // Do not remove edges heavier than a non-residual edge, to
  598. // avoid potential confusion.
  599. break
  600. }
  601. if isRedundant(e) {
  602. delete(e.Src.Out, e.Dest)
  603. delete(e.Dest.In, e.Src)
  604. }
  605. }
  606. }
  607. }
  608. // isRedundant determines if an edge can be removed without impacting
  609. // connectivity of the whole graph. This is implemented by checking if the
  610. // nodes have a common ancestor after removing the edge.
  611. func isRedundant(e *Edge) bool {
  612. destPred := predecessors(e, e.Dest)
  613. if len(destPred) == 1 {
  614. return false
  615. }
  616. srcPred := predecessors(e, e.Src)
  617. for n := range srcPred {
  618. if destPred[n] && n != e.Dest {
  619. return true
  620. }
  621. }
  622. return false
  623. }
  624. // predecessors collects all the predecessors to node n, excluding edge e.
  625. func predecessors(e *Edge, n *Node) map[*Node]bool {
  626. seen := map[*Node]bool{n: true}
  627. queue := Nodes{n}
  628. for len(queue) > 0 {
  629. n := queue[0]
  630. queue = queue[1:]
  631. for _, ie := range n.In {
  632. if e == ie || seen[ie.Src] {
  633. continue
  634. }
  635. seen[ie.Src] = true
  636. queue = append(queue, ie.Src)
  637. }
  638. }
  639. return seen
  640. }
  641. // nodeSorter is a mechanism used to allow a report to be sorted
  642. // in different ways.
  643. type nodeSorter struct {
  644. rs Nodes
  645. less func(l, r *Node) bool
  646. }
  647. func (s nodeSorter) Len() int { return len(s.rs) }
  648. func (s nodeSorter) Swap(i, j int) { s.rs[i], s.rs[j] = s.rs[j], s.rs[i] }
  649. func (s nodeSorter) Less(i, j int) bool { return s.less(s.rs[i], s.rs[j]) }
  650. // Sort reorders a slice of nodes based on the specified ordering
  651. // criteria. The result is sorted in decreasing order for (absolute)
  652. // numeric quantities, alphabetically for text, and increasing for
  653. // addresses.
  654. func (ns Nodes) Sort(o NodeOrder) error {
  655. var s nodeSorter
  656. switch o {
  657. case FlatNameOrder:
  658. s = nodeSorter{ns,
  659. func(l, r *Node) bool {
  660. if iv, jv := abs64(l.Flat), abs64(r.Flat); iv != jv {
  661. return iv > jv
  662. }
  663. if iv, jv := l.Info.PrintableName(), r.Info.PrintableName(); iv != jv {
  664. return iv < jv
  665. }
  666. if iv, jv := abs64(l.Cum), abs64(r.Cum); iv != jv {
  667. return iv > jv
  668. }
  669. return compareNodes(l, r)
  670. },
  671. }
  672. case FlatCumNameOrder:
  673. s = nodeSorter{ns,
  674. func(l, r *Node) bool {
  675. if iv, jv := abs64(l.Flat), abs64(r.Flat); iv != jv {
  676. return iv > jv
  677. }
  678. if iv, jv := abs64(l.Cum), abs64(r.Cum); iv != jv {
  679. return iv > jv
  680. }
  681. if iv, jv := l.Info.PrintableName(), r.Info.PrintableName(); iv != jv {
  682. return iv < jv
  683. }
  684. return compareNodes(l, r)
  685. },
  686. }
  687. case NameOrder:
  688. s = nodeSorter{ns,
  689. func(l, r *Node) bool {
  690. if iv, jv := l.Info.Name, r.Info.Name; iv != jv {
  691. return iv < jv
  692. }
  693. return compareNodes(l, r)
  694. },
  695. }
  696. case FileOrder:
  697. s = nodeSorter{ns,
  698. func(l, r *Node) bool {
  699. if iv, jv := l.Info.File, r.Info.File; iv != jv {
  700. return iv < jv
  701. }
  702. return compareNodes(l, r)
  703. },
  704. }
  705. case AddressOrder:
  706. s = nodeSorter{ns,
  707. func(l, r *Node) bool {
  708. if iv, jv := l.Info.Address, r.Info.Address; iv != jv {
  709. return iv < jv
  710. }
  711. return compareNodes(l, r)
  712. },
  713. }
  714. case CumNameOrder, EntropyOrder:
  715. // Hold scoring for score-based ordering
  716. var score map[*Node]int64
  717. scoreOrder := func(l, r *Node) bool {
  718. if iv, jv := abs64(score[l]), abs64(score[r]); iv != jv {
  719. return iv > jv
  720. }
  721. if iv, jv := l.Info.PrintableName(), r.Info.PrintableName(); iv != jv {
  722. return iv < jv
  723. }
  724. if iv, jv := abs64(l.Flat), abs64(r.Flat); iv != jv {
  725. return iv > jv
  726. }
  727. return compareNodes(l, r)
  728. }
  729. switch o {
  730. case CumNameOrder:
  731. score = make(map[*Node]int64, len(ns))
  732. for _, n := range ns {
  733. score[n] = n.Cum
  734. }
  735. s = nodeSorter{ns, scoreOrder}
  736. case EntropyOrder:
  737. score = make(map[*Node]int64, len(ns))
  738. for _, n := range ns {
  739. score[n] = entropyScore(n)
  740. }
  741. s = nodeSorter{ns, scoreOrder}
  742. }
  743. default:
  744. return fmt.Errorf("report: unrecognized sort ordering: %d", o)
  745. }
  746. sort.Sort(s)
  747. return nil
  748. }
  749. // compareNodes compares two nodes to provide a deterministic ordering
  750. // between them. Two nodes cannot have the same Node.Info value.
  751. func compareNodes(l, r *Node) bool {
  752. return fmt.Sprint(l.Info) < fmt.Sprint(r.Info)
  753. }
  754. // entropyScore computes a score for a node representing how important
  755. // it is to include this node on a graph visualization. It is used to
  756. // sort the nodes and select which ones to display if we have more
  757. // nodes than desired in the graph. This number is computed by looking
  758. // at the flat and cum weights of the node and the incoming/outgoing
  759. // edges. The fundamental idea is to penalize nodes that have a simple
  760. // fallthrough from their incoming to the outgoing edge.
  761. func entropyScore(n *Node) int64 {
  762. score := float64(0)
  763. if len(n.In) == 0 {
  764. score++ // Favor entry nodes
  765. } else {
  766. score += edgeEntropyScore(n, n.In, 0)
  767. }
  768. if len(n.Out) == 0 {
  769. score++ // Favor leaf nodes
  770. } else {
  771. score += edgeEntropyScore(n, n.Out, n.Flat)
  772. }
  773. return int64(score*float64(n.Cum)) + n.Flat
  774. }
  775. // edgeEntropyScore computes the entropy value for a set of edges
  776. // coming in or out of a node. Entropy (as defined in information
  777. // theory) refers to the amount of information encoded by the set of
  778. // edges. A set of edges that have a more interesting distribution of
  779. // samples gets a higher score.
  780. func edgeEntropyScore(n *Node, edges EdgeMap, self int64) float64 {
  781. score := float64(0)
  782. total := self
  783. for _, e := range edges {
  784. if e.Weight > 0 {
  785. total += abs64(e.Weight)
  786. }
  787. }
  788. if total != 0 {
  789. for _, e := range edges {
  790. frac := float64(abs64(e.Weight)) / float64(total)
  791. score += -frac * math.Log2(frac)
  792. }
  793. if self > 0 {
  794. frac := float64(abs64(self)) / float64(total)
  795. score += -frac * math.Log2(frac)
  796. }
  797. }
  798. return score
  799. }
  800. // NodeOrder sets the ordering for a Sort operation
  801. type NodeOrder int
  802. // Sorting options for node sort.
  803. const (
  804. FlatNameOrder NodeOrder = iota
  805. FlatCumNameOrder
  806. CumNameOrder
  807. NameOrder
  808. FileOrder
  809. AddressOrder
  810. EntropyOrder
  811. )
  812. // Sort returns a slice of the edges in the map, in a consistent
  813. // order. The sort order is first based on the edge weight
  814. // (higher-to-lower) and then by the node names to avoid flakiness.
  815. func (e EdgeMap) Sort() []*Edge {
  816. el := make(edgeList, 0, len(e))
  817. for _, w := range e {
  818. el = append(el, w)
  819. }
  820. sort.Sort(el)
  821. return el
  822. }
  823. // Sum returns the total weight for a set of nodes.
  824. func (e EdgeMap) Sum() int64 {
  825. var ret int64
  826. for _, edge := range e {
  827. ret += edge.Weight
  828. }
  829. return ret
  830. }
  831. type edgeList []*Edge
  832. func (el edgeList) Len() int {
  833. return len(el)
  834. }
  835. func (el edgeList) Less(i, j int) bool {
  836. if el[i].Weight != el[j].Weight {
  837. return abs64(el[i].Weight) > abs64(el[j].Weight)
  838. }
  839. from1 := el[i].Src.Info.PrintableName()
  840. from2 := el[j].Src.Info.PrintableName()
  841. if from1 != from2 {
  842. return from1 < from2
  843. }
  844. to1 := el[i].Dest.Info.PrintableName()
  845. to2 := el[j].Dest.Info.PrintableName()
  846. return to1 < to2
  847. }
  848. func (el edgeList) Swap(i, j int) {
  849. el[i], el[j] = el[j], el[i]
  850. }
  851. func abs64(i int64) int64 {
  852. if i < 0 {
  853. return -i
  854. }
  855. return i
  856. }