Aucune description

graph.go 27KB

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