Geen omschrijving

graph.go 24KB

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