Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

1151 строка
30 KiB

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