Няма описание

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