설명 없음

graph.go 26KB

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