No Description

graph.go 23KB

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