You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1274 lines
35 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 report summarizes a performance profile into a
  15. // human-readable report.
  16. package report
  17. import (
  18. "fmt"
  19. "io"
  20. "path/filepath"
  21. "regexp"
  22. "sort"
  23. "strconv"
  24. "strings"
  25. "text/tabwriter"
  26. "time"
  27. "github.com/google/pprof/internal/graph"
  28. "github.com/google/pprof/internal/measurement"
  29. "github.com/google/pprof/internal/plugin"
  30. "github.com/google/pprof/profile"
  31. )
  32. // Output formats.
  33. const (
  34. Callgrind = iota
  35. Comments
  36. Dis
  37. Dot
  38. List
  39. Proto
  40. Raw
  41. Tags
  42. Text
  43. TopProto
  44. Traces
  45. Tree
  46. WebList
  47. )
  48. // Options are the formatting and filtering options used to generate a
  49. // profile.
  50. type Options struct {
  51. OutputFormat int
  52. CumSort bool
  53. CallTree bool
  54. DropNegative bool
  55. CompactLabels bool
  56. Ratio float64
  57. Title string
  58. ProfileLabels []string
  59. ActiveFilters []string
  60. NumLabelUnits map[string]string
  61. NodeCount int
  62. NodeFraction float64
  63. EdgeFraction float64
  64. SampleValue func(s []int64) int64
  65. SampleMeanDivisor func(s []int64) int64
  66. SampleType string
  67. SampleUnit string // Unit for the sample data from the profile.
  68. OutputUnit string // Units for data formatting in report.
  69. Symbol *regexp.Regexp // Symbols to include on disassembly report.
  70. SourcePath string // Search path for source files.
  71. TrimPath string // Paths to trim from source file paths.
  72. }
  73. // Generate generates a report as directed by the Report.
  74. func Generate(w io.Writer, rpt *Report, obj plugin.ObjTool) error {
  75. o := rpt.options
  76. switch o.OutputFormat {
  77. case Comments:
  78. return printComments(w, rpt)
  79. case Dot:
  80. return printDOT(w, rpt)
  81. case Tree:
  82. return printTree(w, rpt)
  83. case Text:
  84. return printText(w, rpt)
  85. case Traces:
  86. return printTraces(w, rpt)
  87. case Raw:
  88. fmt.Fprint(w, rpt.prof.String())
  89. return nil
  90. case Tags:
  91. return printTags(w, rpt)
  92. case Proto:
  93. return rpt.prof.Write(w)
  94. case TopProto:
  95. return printTopProto(w, rpt)
  96. case Dis:
  97. return printAssembly(w, rpt, obj)
  98. case List:
  99. return printSource(w, rpt)
  100. case WebList:
  101. return printWebSource(w, rpt, obj)
  102. case Callgrind:
  103. return printCallgrind(w, rpt)
  104. }
  105. return fmt.Errorf("unexpected output format")
  106. }
  107. // newTrimmedGraph creates a graph for this report, trimmed according
  108. // to the report options.
  109. func (rpt *Report) newTrimmedGraph() (g *graph.Graph, origCount, droppedNodes, droppedEdges int) {
  110. o := rpt.options
  111. // Build a graph and refine it. On each refinement step we must rebuild the graph from the samples,
  112. // as the graph itself doesn't contain enough information to preserve full precision.
  113. visualMode := o.OutputFormat == Dot
  114. cumSort := o.CumSort
  115. // The call_tree option is only honored when generating visual representations of the callgraph.
  116. callTree := o.CallTree && (o.OutputFormat == Dot || o.OutputFormat == Callgrind)
  117. // First step: Build complete graph to identify low frequency nodes, based on their cum weight.
  118. g = rpt.newGraph(nil)
  119. totalValue, _ := g.Nodes.Sum()
  120. nodeCutoff := abs64(int64(float64(totalValue) * o.NodeFraction))
  121. edgeCutoff := abs64(int64(float64(totalValue) * o.EdgeFraction))
  122. // Filter out nodes with cum value below nodeCutoff.
  123. if nodeCutoff > 0 {
  124. if callTree {
  125. if nodesKept := g.DiscardLowFrequencyNodePtrs(nodeCutoff); len(g.Nodes) != len(nodesKept) {
  126. droppedNodes = len(g.Nodes) - len(nodesKept)
  127. g.TrimTree(nodesKept)
  128. }
  129. } else {
  130. if nodesKept := g.DiscardLowFrequencyNodes(nodeCutoff); len(g.Nodes) != len(nodesKept) {
  131. droppedNodes = len(g.Nodes) - len(nodesKept)
  132. g = rpt.newGraph(nodesKept)
  133. }
  134. }
  135. }
  136. origCount = len(g.Nodes)
  137. // Second step: Limit the total number of nodes. Apply specialized heuristics to improve
  138. // visualization when generating dot output.
  139. g.SortNodes(cumSort, visualMode)
  140. if nodeCount := o.NodeCount; nodeCount > 0 {
  141. // Remove low frequency tags and edges as they affect selection.
  142. g.TrimLowFrequencyTags(nodeCutoff)
  143. g.TrimLowFrequencyEdges(edgeCutoff)
  144. if callTree {
  145. if nodesKept := g.SelectTopNodePtrs(nodeCount, visualMode); len(g.Nodes) != len(nodesKept) {
  146. g.TrimTree(nodesKept)
  147. g.SortNodes(cumSort, visualMode)
  148. }
  149. } else {
  150. if nodesKept := g.SelectTopNodes(nodeCount, visualMode); len(g.Nodes) != len(nodesKept) {
  151. g = rpt.newGraph(nodesKept)
  152. g.SortNodes(cumSort, visualMode)
  153. }
  154. }
  155. }
  156. // Final step: Filter out low frequency tags and edges, and remove redundant edges that clutter
  157. // the graph.
  158. g.TrimLowFrequencyTags(nodeCutoff)
  159. droppedEdges = g.TrimLowFrequencyEdges(edgeCutoff)
  160. if visualMode {
  161. g.RemoveRedundantEdges()
  162. }
  163. return
  164. }
  165. func (rpt *Report) selectOutputUnit(g *graph.Graph) {
  166. o := rpt.options
  167. // Select best unit for profile output.
  168. // Find the appropriate units for the smallest non-zero sample
  169. if o.OutputUnit != "minimum" || len(g.Nodes) == 0 {
  170. return
  171. }
  172. var minValue int64
  173. for _, n := range g.Nodes {
  174. nodeMin := abs64(n.FlatValue())
  175. if nodeMin == 0 {
  176. nodeMin = abs64(n.CumValue())
  177. }
  178. if nodeMin > 0 && (minValue == 0 || nodeMin < minValue) {
  179. minValue = nodeMin
  180. }
  181. }
  182. maxValue := rpt.total
  183. if minValue == 0 {
  184. minValue = maxValue
  185. }
  186. if r := o.Ratio; r > 0 && r != 1 {
  187. minValue = int64(float64(minValue) * r)
  188. maxValue = int64(float64(maxValue) * r)
  189. }
  190. _, minUnit := measurement.Scale(minValue, o.SampleUnit, "minimum")
  191. _, maxUnit := measurement.Scale(maxValue, o.SampleUnit, "minimum")
  192. unit := minUnit
  193. if minUnit != maxUnit && minValue*100 < maxValue && o.OutputFormat != Callgrind {
  194. // Minimum and maximum values have different units. Scale
  195. // minimum by 100 to use larger units, allowing minimum value to
  196. // be scaled down to 0.01, except for callgrind reports since
  197. // they can only represent integer values.
  198. _, unit = measurement.Scale(100*minValue, o.SampleUnit, "minimum")
  199. }
  200. if unit != "" {
  201. o.OutputUnit = unit
  202. } else {
  203. o.OutputUnit = o.SampleUnit
  204. }
  205. }
  206. // newGraph creates a new graph for this report. If nodes is non-nil,
  207. // only nodes whose info matches are included. Otherwise, all nodes
  208. // are included, without trimming.
  209. func (rpt *Report) newGraph(nodes graph.NodeSet) *graph.Graph {
  210. o := rpt.options
  211. // Clean up file paths using heuristics.
  212. prof := rpt.prof
  213. for _, f := range prof.Function {
  214. f.Filename = trimPath(f.Filename, o.TrimPath, o.SourcePath)
  215. }
  216. // Removes all numeric tags except for the bytes tag prior
  217. // to making graph.
  218. // TODO: modify to select first numeric tag if no bytes tag
  219. for _, s := range prof.Sample {
  220. numLabels := make(map[string][]int64, len(s.NumLabel))
  221. numUnits := make(map[string][]string, len(s.NumLabel))
  222. for k, vs := range s.NumLabel {
  223. if k == "bytes" {
  224. unit := o.NumLabelUnits[k]
  225. numValues := make([]int64, len(vs))
  226. numUnit := make([]string, len(vs))
  227. for i, v := range vs {
  228. numValues[i] = v
  229. numUnit[i] = unit
  230. }
  231. numLabels[k] = append(numLabels[k], numValues...)
  232. numUnits[k] = append(numUnits[k], numUnit...)
  233. }
  234. }
  235. s.NumLabel = numLabels
  236. s.NumUnit = numUnits
  237. }
  238. // Remove label marking samples from the base profiles, so it does not appear
  239. // as a nodelet in the graph view.
  240. prof.RemoveLabel("pprof::base")
  241. formatTag := func(v int64, key string) string {
  242. return measurement.ScaledLabel(v, key, o.OutputUnit)
  243. }
  244. gopt := &graph.Options{
  245. SampleValue: o.SampleValue,
  246. SampleMeanDivisor: o.SampleMeanDivisor,
  247. FormatTag: formatTag,
  248. CallTree: o.CallTree && (o.OutputFormat == Dot || o.OutputFormat == Callgrind),
  249. DropNegative: o.DropNegative,
  250. KeptNodes: nodes,
  251. }
  252. // Only keep binary names for disassembly-based reports, otherwise
  253. // remove it to allow merging of functions across binaries.
  254. switch o.OutputFormat {
  255. case Raw, List, WebList, Dis, Callgrind:
  256. gopt.ObjNames = true
  257. }
  258. return graph.New(rpt.prof, gopt)
  259. }
  260. func printTopProto(w io.Writer, rpt *Report) error {
  261. p := rpt.prof
  262. o := rpt.options
  263. g, _, _, _ := rpt.newTrimmedGraph()
  264. rpt.selectOutputUnit(g)
  265. out := profile.Profile{
  266. SampleType: []*profile.ValueType{
  267. {Type: "cum", Unit: o.OutputUnit},
  268. {Type: "flat", Unit: o.OutputUnit},
  269. },
  270. TimeNanos: p.TimeNanos,
  271. DurationNanos: p.DurationNanos,
  272. PeriodType: p.PeriodType,
  273. Period: p.Period,
  274. }
  275. functionMap := make(functionMap)
  276. for i, n := range g.Nodes {
  277. f, added := functionMap.findOrAdd(n.Info)
  278. if added {
  279. out.Function = append(out.Function, f)
  280. }
  281. flat, cum := n.FlatValue(), n.CumValue()
  282. l := &profile.Location{
  283. ID: uint64(i + 1),
  284. Address: n.Info.Address,
  285. Line: []profile.Line{
  286. {
  287. Line: int64(n.Info.Lineno),
  288. Function: f,
  289. },
  290. },
  291. }
  292. fv, _ := measurement.Scale(flat, o.SampleUnit, o.OutputUnit)
  293. cv, _ := measurement.Scale(cum, o.SampleUnit, o.OutputUnit)
  294. s := &profile.Sample{
  295. Location: []*profile.Location{l},
  296. Value: []int64{int64(cv), int64(fv)},
  297. }
  298. out.Location = append(out.Location, l)
  299. out.Sample = append(out.Sample, s)
  300. }
  301. return out.Write(w)
  302. }
  303. type functionMap map[string]*profile.Function
  304. // findOrAdd takes a node representing a function, adds the function
  305. // represented by the node to the map if the function is not already present,
  306. // and returns the function the node represents. This also returns a boolean,
  307. // which is true if the function was added and false otherwise.
  308. func (fm functionMap) findOrAdd(ni graph.NodeInfo) (*profile.Function, bool) {
  309. fName := fmt.Sprintf("%q%q%q%d", ni.Name, ni.OrigName, ni.File, ni.StartLine)
  310. if f := fm[fName]; f != nil {
  311. return f, false
  312. }
  313. f := &profile.Function{
  314. ID: uint64(len(fm) + 1),
  315. Name: ni.Name,
  316. SystemName: ni.OrigName,
  317. Filename: ni.File,
  318. StartLine: int64(ni.StartLine),
  319. }
  320. fm[fName] = f
  321. return f, true
  322. }
  323. // printAssembly prints an annotated assembly listing.
  324. func printAssembly(w io.Writer, rpt *Report, obj plugin.ObjTool) error {
  325. return PrintAssembly(w, rpt, obj, -1)
  326. }
  327. // PrintAssembly prints annotated disassembly of rpt to w.
  328. func PrintAssembly(w io.Writer, rpt *Report, obj plugin.ObjTool, maxFuncs int) error {
  329. o := rpt.options
  330. prof := rpt.prof
  331. g := rpt.newGraph(nil)
  332. // If the regexp source can be parsed as an address, also match
  333. // functions that land on that address.
  334. var address *uint64
  335. if hex, err := strconv.ParseUint(o.Symbol.String(), 0, 64); err == nil {
  336. address = &hex
  337. }
  338. fmt.Fprintln(w, "Total:", rpt.formatValue(rpt.total))
  339. symbols := symbolsFromBinaries(prof, g, o.Symbol, address, obj)
  340. symNodes := nodesPerSymbol(g.Nodes, symbols)
  341. // Sort for printing.
  342. var syms []*objSymbol
  343. for s := range symNodes {
  344. syms = append(syms, s)
  345. }
  346. byName := func(a, b *objSymbol) bool {
  347. if na, nb := a.sym.Name[0], b.sym.Name[0]; na != nb {
  348. return na < nb
  349. }
  350. return a.sym.Start < b.sym.Start
  351. }
  352. if maxFuncs < 0 {
  353. sort.Sort(orderSyms{syms, byName})
  354. } else {
  355. byFlatSum := func(a, b *objSymbol) bool {
  356. suma, _ := symNodes[a].Sum()
  357. sumb, _ := symNodes[b].Sum()
  358. if suma != sumb {
  359. return suma > sumb
  360. }
  361. return byName(a, b)
  362. }
  363. sort.Sort(orderSyms{syms, byFlatSum})
  364. if len(syms) > maxFuncs {
  365. syms = syms[:maxFuncs]
  366. }
  367. }
  368. // Correlate the symbols from the binary with the profile samples.
  369. for _, s := range syms {
  370. sns := symNodes[s]
  371. // Gather samples for this symbol.
  372. flatSum, cumSum := sns.Sum()
  373. // Get the function assembly.
  374. insts, err := obj.Disasm(s.sym.File, s.sym.Start, s.sym.End)
  375. if err != nil {
  376. return err
  377. }
  378. ns := annotateAssembly(insts, sns, s.base)
  379. fmt.Fprintf(w, "ROUTINE ======================== %s\n", s.sym.Name[0])
  380. for _, name := range s.sym.Name[1:] {
  381. fmt.Fprintf(w, " AKA ======================== %s\n", name)
  382. }
  383. fmt.Fprintf(w, "%10s %10s (flat, cum) %s of Total\n",
  384. rpt.formatValue(flatSum), rpt.formatValue(cumSum),
  385. measurement.Percentage(cumSum, rpt.total))
  386. function, file, line := "", "", 0
  387. for _, n := range ns {
  388. locStr := ""
  389. // Skip loc information if it hasn't changed from previous instruction.
  390. if n.function != function || n.file != file || n.line != line {
  391. function, file, line = n.function, n.file, n.line
  392. if n.function != "" {
  393. locStr = n.function + " "
  394. }
  395. if n.file != "" {
  396. locStr += n.file
  397. if n.line != 0 {
  398. locStr += fmt.Sprintf(":%d", n.line)
  399. }
  400. }
  401. }
  402. switch {
  403. case locStr == "":
  404. // No location info, just print the instruction.
  405. fmt.Fprintf(w, "%10s %10s %10x: %s\n",
  406. valueOrDot(n.flatValue(), rpt),
  407. valueOrDot(n.cumValue(), rpt),
  408. n.address, n.instruction,
  409. )
  410. case len(n.instruction) < 40:
  411. // Short instruction, print loc on the same line.
  412. fmt.Fprintf(w, "%10s %10s %10x: %-40s;%s\n",
  413. valueOrDot(n.flatValue(), rpt),
  414. valueOrDot(n.cumValue(), rpt),
  415. n.address, n.instruction,
  416. locStr,
  417. )
  418. default:
  419. // Long instruction, print loc on a separate line.
  420. fmt.Fprintf(w, "%74s;%s\n", "", locStr)
  421. fmt.Fprintf(w, "%10s %10s %10x: %s\n",
  422. valueOrDot(n.flatValue(), rpt),
  423. valueOrDot(n.cumValue(), rpt),
  424. n.address, n.instruction,
  425. )
  426. }
  427. }
  428. }
  429. return nil
  430. }
  431. // symbolsFromBinaries examines the binaries listed on the profile
  432. // that have associated samples, and identifies symbols matching rx.
  433. func symbolsFromBinaries(prof *profile.Profile, g *graph.Graph, rx *regexp.Regexp, address *uint64, obj plugin.ObjTool) []*objSymbol {
  434. hasSamples := make(map[string]bool)
  435. // Only examine mappings that have samples that match the
  436. // regexp. This is an optimization to speed up pprof.
  437. for _, n := range g.Nodes {
  438. if name := n.Info.PrintableName(); rx.MatchString(name) && n.Info.Objfile != "" {
  439. hasSamples[n.Info.Objfile] = true
  440. }
  441. }
  442. // Walk all mappings looking for matching functions with samples.
  443. var objSyms []*objSymbol
  444. for _, m := range prof.Mapping {
  445. if !hasSamples[m.File] {
  446. if address == nil || !(m.Start <= *address && *address <= m.Limit) {
  447. continue
  448. }
  449. }
  450. f, err := obj.Open(m.File, m.Start, m.Limit, m.Offset)
  451. if err != nil {
  452. fmt.Printf("%v\n", err)
  453. continue
  454. }
  455. // Find symbols in this binary matching the user regexp.
  456. var addr uint64
  457. if address != nil {
  458. addr = *address
  459. }
  460. msyms, err := f.Symbols(rx, addr)
  461. base := f.Base()
  462. f.Close()
  463. if err != nil {
  464. continue
  465. }
  466. for _, ms := range msyms {
  467. objSyms = append(objSyms,
  468. &objSymbol{
  469. sym: ms,
  470. base: base,
  471. file: f,
  472. },
  473. )
  474. }
  475. }
  476. return objSyms
  477. }
  478. // objSym represents a symbol identified from a binary. It includes
  479. // the SymbolInfo from the disasm package and the base that must be
  480. // added to correspond to sample addresses
  481. type objSymbol struct {
  482. sym *plugin.Sym
  483. base uint64
  484. file plugin.ObjFile
  485. }
  486. // orderSyms is a wrapper type to sort []*objSymbol by a supplied comparator.
  487. type orderSyms struct {
  488. v []*objSymbol
  489. less func(a, b *objSymbol) bool
  490. }
  491. func (o orderSyms) Len() int { return len(o.v) }
  492. func (o orderSyms) Less(i, j int) bool { return o.less(o.v[i], o.v[j]) }
  493. func (o orderSyms) Swap(i, j int) { o.v[i], o.v[j] = o.v[j], o.v[i] }
  494. // nodesPerSymbol classifies nodes into a group of symbols.
  495. func nodesPerSymbol(ns graph.Nodes, symbols []*objSymbol) map[*objSymbol]graph.Nodes {
  496. symNodes := make(map[*objSymbol]graph.Nodes)
  497. for _, s := range symbols {
  498. // Gather samples for this symbol.
  499. for _, n := range ns {
  500. address := n.Info.Address - s.base
  501. if address >= s.sym.Start && address < s.sym.End {
  502. symNodes[s] = append(symNodes[s], n)
  503. }
  504. }
  505. }
  506. return symNodes
  507. }
  508. type assemblyInstruction struct {
  509. address uint64
  510. instruction string
  511. function string
  512. file string
  513. line int
  514. flat, cum int64
  515. flatDiv, cumDiv int64
  516. startsBlock bool
  517. inlineCalls []callID
  518. }
  519. type callID struct {
  520. file string
  521. line int
  522. }
  523. func (a *assemblyInstruction) flatValue() int64 {
  524. if a.flatDiv != 0 {
  525. return a.flat / a.flatDiv
  526. }
  527. return a.flat
  528. }
  529. func (a *assemblyInstruction) cumValue() int64 {
  530. if a.cumDiv != 0 {
  531. return a.cum / a.cumDiv
  532. }
  533. return a.cum
  534. }
  535. // annotateAssembly annotates a set of assembly instructions with a
  536. // set of samples. It returns a set of nodes to display. base is an
  537. // offset to adjust the sample addresses.
  538. func annotateAssembly(insts []plugin.Inst, samples graph.Nodes, base uint64) []assemblyInstruction {
  539. // Add end marker to simplify printing loop.
  540. insts = append(insts, plugin.Inst{
  541. Addr: ^uint64(0),
  542. })
  543. // Ensure samples are sorted by address.
  544. samples.Sort(graph.AddressOrder)
  545. s := 0
  546. asm := make([]assemblyInstruction, 0, len(insts))
  547. for ix, in := range insts[:len(insts)-1] {
  548. n := assemblyInstruction{
  549. address: in.Addr,
  550. instruction: in.Text,
  551. function: in.Function,
  552. line: in.Line,
  553. }
  554. if in.File != "" {
  555. n.file = filepath.Base(in.File)
  556. }
  557. // Sum all the samples until the next instruction (to account
  558. // for samples attributed to the middle of an instruction).
  559. for next := insts[ix+1].Addr; s < len(samples) && samples[s].Info.Address-base < next; s++ {
  560. sample := samples[s]
  561. n.flatDiv += sample.FlatDiv
  562. n.flat += sample.Flat
  563. n.cumDiv += sample.CumDiv
  564. n.cum += sample.Cum
  565. if f := sample.Info.File; f != "" && n.file == "" {
  566. n.file = filepath.Base(f)
  567. }
  568. if ln := sample.Info.Lineno; ln != 0 && n.line == 0 {
  569. n.line = ln
  570. }
  571. if f := sample.Info.Name; f != "" && n.function == "" {
  572. n.function = f
  573. }
  574. }
  575. asm = append(asm, n)
  576. }
  577. return asm
  578. }
  579. // valueOrDot formats a value according to a report, intercepting zero
  580. // values.
  581. func valueOrDot(value int64, rpt *Report) string {
  582. if value == 0 {
  583. return "."
  584. }
  585. return rpt.formatValue(value)
  586. }
  587. // printTags collects all tags referenced in the profile and prints
  588. // them in a sorted table.
  589. func printTags(w io.Writer, rpt *Report) error {
  590. p := rpt.prof
  591. o := rpt.options
  592. formatTag := func(v int64, key string) string {
  593. return measurement.ScaledLabel(v, key, o.OutputUnit)
  594. }
  595. // Hashtable to keep accumulate tags as key,value,count.
  596. tagMap := make(map[string]map[string]int64)
  597. for _, s := range p.Sample {
  598. for key, vals := range s.Label {
  599. for _, val := range vals {
  600. valueMap, ok := tagMap[key]
  601. if !ok {
  602. valueMap = make(map[string]int64)
  603. tagMap[key] = valueMap
  604. }
  605. valueMap[val] += o.SampleValue(s.Value)
  606. }
  607. }
  608. for key, vals := range s.NumLabel {
  609. unit := o.NumLabelUnits[key]
  610. for _, nval := range vals {
  611. val := formatTag(nval, unit)
  612. valueMap, ok := tagMap[key]
  613. if !ok {
  614. valueMap = make(map[string]int64)
  615. tagMap[key] = valueMap
  616. }
  617. valueMap[val] += o.SampleValue(s.Value)
  618. }
  619. }
  620. }
  621. tagKeys := make([]*graph.Tag, 0, len(tagMap))
  622. for key := range tagMap {
  623. tagKeys = append(tagKeys, &graph.Tag{Name: key})
  624. }
  625. tabw := tabwriter.NewWriter(w, 0, 0, 1, ' ', tabwriter.AlignRight)
  626. for _, tagKey := range graph.SortTags(tagKeys, true) {
  627. var total int64
  628. key := tagKey.Name
  629. tags := make([]*graph.Tag, 0, len(tagMap[key]))
  630. for t, c := range tagMap[key] {
  631. total += c
  632. tags = append(tags, &graph.Tag{Name: t, Flat: c})
  633. }
  634. f, u := measurement.Scale(total, o.SampleUnit, o.OutputUnit)
  635. fmt.Fprintf(tabw, "%s:\t Total %.1f%s\n", key, f, u)
  636. for _, t := range graph.SortTags(tags, true) {
  637. f, u := measurement.Scale(t.FlatValue(), o.SampleUnit, o.OutputUnit)
  638. if total > 0 {
  639. fmt.Fprintf(tabw, " \t%.1f%s (%s):\t %s\n", f, u, measurement.Percentage(t.FlatValue(), total), t.Name)
  640. } else {
  641. fmt.Fprintf(tabw, " \t%.1f%s:\t %s\n", f, u, t.Name)
  642. }
  643. }
  644. fmt.Fprintln(tabw)
  645. }
  646. return tabw.Flush()
  647. }
  648. // printComments prints all freeform comments in the profile.
  649. func printComments(w io.Writer, rpt *Report) error {
  650. p := rpt.prof
  651. for _, c := range p.Comments {
  652. fmt.Fprintln(w, c)
  653. }
  654. return nil
  655. }
  656. // TextItem holds a single text report entry.
  657. type TextItem struct {
  658. Name string
  659. InlineLabel string // Not empty if inlined
  660. Flat, Cum int64 // Raw values
  661. FlatFormat, CumFormat string // Formatted values
  662. }
  663. // TextItems returns a list of text items from the report and a list
  664. // of labels that describe the report.
  665. func TextItems(rpt *Report) ([]TextItem, []string) {
  666. g, origCount, droppedNodes, _ := rpt.newTrimmedGraph()
  667. rpt.selectOutputUnit(g)
  668. labels := reportLabels(rpt, g, origCount, droppedNodes, 0, false)
  669. var items []TextItem
  670. var flatSum int64
  671. for _, n := range g.Nodes {
  672. name, flat, cum := n.Info.PrintableName(), n.FlatValue(), n.CumValue()
  673. var inline, noinline bool
  674. for _, e := range n.In {
  675. if e.Inline {
  676. inline = true
  677. } else {
  678. noinline = true
  679. }
  680. }
  681. var inl string
  682. if inline {
  683. if noinline {
  684. inl = "(partial-inline)"
  685. } else {
  686. inl = "(inline)"
  687. }
  688. }
  689. flatSum += flat
  690. items = append(items, TextItem{
  691. Name: name,
  692. InlineLabel: inl,
  693. Flat: flat,
  694. Cum: cum,
  695. FlatFormat: rpt.formatValue(flat),
  696. CumFormat: rpt.formatValue(cum),
  697. })
  698. }
  699. return items, labels
  700. }
  701. // printText prints a flat text report for a profile.
  702. func printText(w io.Writer, rpt *Report) error {
  703. items, labels := TextItems(rpt)
  704. fmt.Fprintln(w, strings.Join(labels, "\n"))
  705. fmt.Fprintf(w, "%10s %5s%% %5s%% %10s %5s%%\n",
  706. "flat", "flat", "sum", "cum", "cum")
  707. var flatSum int64
  708. for _, item := range items {
  709. inl := item.InlineLabel
  710. if inl != "" {
  711. inl = " " + inl
  712. }
  713. flatSum += item.Flat
  714. fmt.Fprintf(w, "%10s %s %s %10s %s %s%s\n",
  715. item.FlatFormat, measurement.Percentage(item.Flat, rpt.total),
  716. measurement.Percentage(flatSum, rpt.total),
  717. item.CumFormat, measurement.Percentage(item.Cum, rpt.total),
  718. item.Name, inl)
  719. }
  720. return nil
  721. }
  722. // printTraces prints all traces from a profile.
  723. func printTraces(w io.Writer, rpt *Report) error {
  724. fmt.Fprintln(w, strings.Join(ProfileLabels(rpt), "\n"))
  725. prof := rpt.prof
  726. o := rpt.options
  727. const separator = "-----------+-------------------------------------------------------"
  728. _, locations := graph.CreateNodes(prof, &graph.Options{})
  729. for _, sample := range prof.Sample {
  730. var stack graph.Nodes
  731. for _, loc := range sample.Location {
  732. id := loc.ID
  733. stack = append(stack, locations[id]...)
  734. }
  735. if len(stack) == 0 {
  736. continue
  737. }
  738. fmt.Fprintln(w, separator)
  739. // Print any text labels for the sample.
  740. var labels []string
  741. for s, vs := range sample.Label {
  742. labels = append(labels, fmt.Sprintf("%10s: %s\n", s, strings.Join(vs, " ")))
  743. }
  744. sort.Strings(labels)
  745. fmt.Fprint(w, strings.Join(labels, ""))
  746. // Print any numeric labels for the sample
  747. var numLabels []string
  748. for key, vals := range sample.NumLabel {
  749. unit := o.NumLabelUnits[key]
  750. numValues := make([]string, len(vals))
  751. for i, vv := range vals {
  752. numValues[i] = measurement.Label(vv, unit)
  753. }
  754. numLabels = append(numLabels, fmt.Sprintf("%10s: %s\n", key, strings.Join(numValues, " ")))
  755. }
  756. sort.Strings(numLabels)
  757. fmt.Fprint(w, strings.Join(numLabels, ""))
  758. var d, v int64
  759. v = o.SampleValue(sample.Value)
  760. if o.SampleMeanDivisor != nil {
  761. d = o.SampleMeanDivisor(sample.Value)
  762. }
  763. // Print call stack.
  764. if d != 0 {
  765. v = v / d
  766. }
  767. fmt.Fprintf(w, "%10s %s\n",
  768. rpt.formatValue(v), stack[0].Info.PrintableName())
  769. for _, s := range stack[1:] {
  770. fmt.Fprintf(w, "%10s %s\n", "", s.Info.PrintableName())
  771. }
  772. }
  773. fmt.Fprintln(w, separator)
  774. return nil
  775. }
  776. // printCallgrind prints a graph for a profile on callgrind format.
  777. func printCallgrind(w io.Writer, rpt *Report) error {
  778. o := rpt.options
  779. rpt.options.NodeFraction = 0
  780. rpt.options.EdgeFraction = 0
  781. rpt.options.NodeCount = 0
  782. g, _, _, _ := rpt.newTrimmedGraph()
  783. rpt.selectOutputUnit(g)
  784. nodeNames := getDisambiguatedNames(g)
  785. fmt.Fprintln(w, "positions: instr line")
  786. fmt.Fprintln(w, "events:", o.SampleType+"("+o.OutputUnit+")")
  787. objfiles := make(map[string]int)
  788. files := make(map[string]int)
  789. names := make(map[string]int)
  790. // prevInfo points to the previous NodeInfo.
  791. // It is used to group cost lines together as much as possible.
  792. var prevInfo *graph.NodeInfo
  793. for _, n := range g.Nodes {
  794. if prevInfo == nil || n.Info.Objfile != prevInfo.Objfile || n.Info.File != prevInfo.File || n.Info.Name != prevInfo.Name {
  795. fmt.Fprintln(w)
  796. fmt.Fprintln(w, "ob="+callgrindName(objfiles, n.Info.Objfile))
  797. fmt.Fprintln(w, "fl="+callgrindName(files, n.Info.File))
  798. fmt.Fprintln(w, "fn="+callgrindName(names, n.Info.Name))
  799. }
  800. addr := callgrindAddress(prevInfo, n.Info.Address)
  801. sv, _ := measurement.Scale(n.FlatValue(), o.SampleUnit, o.OutputUnit)
  802. fmt.Fprintf(w, "%s %d %d\n", addr, n.Info.Lineno, int64(sv))
  803. // Print outgoing edges.
  804. for _, out := range n.Out.Sort() {
  805. c, _ := measurement.Scale(out.Weight, o.SampleUnit, o.OutputUnit)
  806. callee := out.Dest
  807. fmt.Fprintln(w, "cfl="+callgrindName(files, callee.Info.File))
  808. fmt.Fprintln(w, "cfn="+callgrindName(names, nodeNames[callee]))
  809. // pprof doesn't have a flat weight for a call, leave as 0.
  810. fmt.Fprintf(w, "calls=0 %s %d\n", callgrindAddress(prevInfo, callee.Info.Address), callee.Info.Lineno)
  811. // TODO: This address may be in the middle of a call
  812. // instruction. It would be best to find the beginning
  813. // of the instruction, but the tools seem to handle
  814. // this OK.
  815. fmt.Fprintf(w, "* * %d\n", int64(c))
  816. }
  817. prevInfo = &n.Info
  818. }
  819. return nil
  820. }
  821. // getDisambiguatedNames returns a map from each node in the graph to
  822. // the name to use in the callgrind output. Callgrind merges all
  823. // functions with the same [file name, function name]. Add a [%d/n]
  824. // suffix to disambiguate nodes with different values of
  825. // node.Function, which we want to keep separate. In particular, this
  826. // affects graphs created with --call_tree, where nodes from different
  827. // contexts are associated to different Functions.
  828. func getDisambiguatedNames(g *graph.Graph) map[*graph.Node]string {
  829. nodeName := make(map[*graph.Node]string, len(g.Nodes))
  830. type names struct {
  831. file, function string
  832. }
  833. // nameFunctionIndex maps the callgrind names (filename, function)
  834. // to the node.Function values found for that name, and each
  835. // node.Function value to a sequential index to be used on the
  836. // disambiguated name.
  837. nameFunctionIndex := make(map[names]map[*graph.Node]int)
  838. for _, n := range g.Nodes {
  839. nm := names{n.Info.File, n.Info.Name}
  840. p, ok := nameFunctionIndex[nm]
  841. if !ok {
  842. p = make(map[*graph.Node]int)
  843. nameFunctionIndex[nm] = p
  844. }
  845. if _, ok := p[n.Function]; !ok {
  846. p[n.Function] = len(p)
  847. }
  848. }
  849. for _, n := range g.Nodes {
  850. nm := names{n.Info.File, n.Info.Name}
  851. nodeName[n] = n.Info.Name
  852. if p := nameFunctionIndex[nm]; len(p) > 1 {
  853. // If there is more than one function, add suffix to disambiguate.
  854. nodeName[n] += fmt.Sprintf(" [%d/%d]", p[n.Function]+1, len(p))
  855. }
  856. }
  857. return nodeName
  858. }
  859. // callgrindName implements the callgrind naming compression scheme.
  860. // For names not previously seen returns "(N) name", where N is a
  861. // unique index. For names previously seen returns "(N)" where N is
  862. // the index returned the first time.
  863. func callgrindName(names map[string]int, name string) string {
  864. if name == "" {
  865. return ""
  866. }
  867. if id, ok := names[name]; ok {
  868. return fmt.Sprintf("(%d)", id)
  869. }
  870. id := len(names) + 1
  871. names[name] = id
  872. return fmt.Sprintf("(%d) %s", id, name)
  873. }
  874. // callgrindAddress implements the callgrind subposition compression scheme if
  875. // possible. If prevInfo != nil, it contains the previous address. The current
  876. // address can be given relative to the previous address, with an explicit +/-
  877. // to indicate it is relative, or * for the same address.
  878. func callgrindAddress(prevInfo *graph.NodeInfo, curr uint64) string {
  879. abs := fmt.Sprintf("%#x", curr)
  880. if prevInfo == nil {
  881. return abs
  882. }
  883. prev := prevInfo.Address
  884. if prev == curr {
  885. return "*"
  886. }
  887. diff := int64(curr - prev)
  888. relative := fmt.Sprintf("%+d", diff)
  889. // Only bother to use the relative address if it is actually shorter.
  890. if len(relative) < len(abs) {
  891. return relative
  892. }
  893. return abs
  894. }
  895. // printTree prints a tree-based report in text form.
  896. func printTree(w io.Writer, rpt *Report) error {
  897. const separator = "----------------------------------------------------------+-------------"
  898. const legend = " flat flat% sum% cum cum% calls calls% + context "
  899. g, origCount, droppedNodes, _ := rpt.newTrimmedGraph()
  900. rpt.selectOutputUnit(g)
  901. fmt.Fprintln(w, strings.Join(reportLabels(rpt, g, origCount, droppedNodes, 0, false), "\n"))
  902. fmt.Fprintln(w, separator)
  903. fmt.Fprintln(w, legend)
  904. var flatSum int64
  905. rx := rpt.options.Symbol
  906. for _, n := range g.Nodes {
  907. name, flat, cum := n.Info.PrintableName(), n.FlatValue(), n.CumValue()
  908. // Skip any entries that do not match the regexp (for the "peek" command).
  909. if rx != nil && !rx.MatchString(name) {
  910. continue
  911. }
  912. fmt.Fprintln(w, separator)
  913. // Print incoming edges.
  914. inEdges := n.In.Sort()
  915. for _, in := range inEdges {
  916. var inline string
  917. if in.Inline {
  918. inline = " (inline)"
  919. }
  920. fmt.Fprintf(w, "%50s %s | %s%s\n", rpt.formatValue(in.Weight),
  921. measurement.Percentage(in.Weight, cum), in.Src.Info.PrintableName(), inline)
  922. }
  923. // Print current node.
  924. flatSum += flat
  925. fmt.Fprintf(w, "%10s %s %s %10s %s | %s\n",
  926. rpt.formatValue(flat),
  927. measurement.Percentage(flat, rpt.total),
  928. measurement.Percentage(flatSum, rpt.total),
  929. rpt.formatValue(cum),
  930. measurement.Percentage(cum, rpt.total),
  931. name)
  932. // Print outgoing edges.
  933. outEdges := n.Out.Sort()
  934. for _, out := range outEdges {
  935. var inline string
  936. if out.Inline {
  937. inline = " (inline)"
  938. }
  939. fmt.Fprintf(w, "%50s %s | %s%s\n", rpt.formatValue(out.Weight),
  940. measurement.Percentage(out.Weight, cum), out.Dest.Info.PrintableName(), inline)
  941. }
  942. }
  943. if len(g.Nodes) > 0 {
  944. fmt.Fprintln(w, separator)
  945. }
  946. return nil
  947. }
  948. // GetDOT returns a graph suitable for dot processing along with some
  949. // configuration information.
  950. func GetDOT(rpt *Report) (*graph.Graph, *graph.DotConfig) {
  951. g, origCount, droppedNodes, droppedEdges := rpt.newTrimmedGraph()
  952. rpt.selectOutputUnit(g)
  953. labels := reportLabels(rpt, g, origCount, droppedNodes, droppedEdges, true)
  954. c := &graph.DotConfig{
  955. Title: rpt.options.Title,
  956. Labels: labels,
  957. FormatValue: rpt.formatValue,
  958. Total: rpt.total,
  959. }
  960. return g, c
  961. }
  962. // printDOT prints an annotated callgraph in DOT format.
  963. func printDOT(w io.Writer, rpt *Report) error {
  964. g, c := GetDOT(rpt)
  965. graph.ComposeDot(w, g, &graph.DotAttributes{}, c)
  966. return nil
  967. }
  968. // ProfileLabels returns printable labels for a profile.
  969. func ProfileLabels(rpt *Report) []string {
  970. label := []string{}
  971. prof := rpt.prof
  972. o := rpt.options
  973. if len(prof.Mapping) > 0 {
  974. if prof.Mapping[0].File != "" {
  975. label = append(label, "File: "+filepath.Base(prof.Mapping[0].File))
  976. }
  977. if prof.Mapping[0].BuildID != "" {
  978. label = append(label, "Build ID: "+prof.Mapping[0].BuildID)
  979. }
  980. }
  981. // Only include comments that do not start with '#'.
  982. for _, c := range prof.Comments {
  983. if !strings.HasPrefix(c, "#") {
  984. label = append(label, c)
  985. }
  986. }
  987. if o.SampleType != "" {
  988. label = append(label, "Type: "+o.SampleType)
  989. }
  990. if prof.TimeNanos != 0 {
  991. const layout = "Jan 2, 2006 at 3:04pm (MST)"
  992. label = append(label, "Time: "+time.Unix(0, prof.TimeNanos).Format(layout))
  993. }
  994. if prof.DurationNanos != 0 {
  995. duration := measurement.Label(prof.DurationNanos, "nanoseconds")
  996. totalNanos, totalUnit := measurement.Scale(rpt.total, o.SampleUnit, "nanoseconds")
  997. var ratio string
  998. if totalUnit == "ns" && totalNanos != 0 {
  999. ratio = "(" + measurement.Percentage(int64(totalNanos), prof.DurationNanos) + ")"
  1000. }
  1001. label = append(label, fmt.Sprintf("Duration: %s, Total samples = %s %s", duration, rpt.formatValue(rpt.total), ratio))
  1002. }
  1003. return label
  1004. }
  1005. // reportLabels returns printable labels for a report. Includes
  1006. // profileLabels.
  1007. func reportLabels(rpt *Report, g *graph.Graph, origCount, droppedNodes, droppedEdges int, fullHeaders bool) []string {
  1008. nodeFraction := rpt.options.NodeFraction
  1009. edgeFraction := rpt.options.EdgeFraction
  1010. nodeCount := len(g.Nodes)
  1011. var label []string
  1012. if len(rpt.options.ProfileLabels) > 0 {
  1013. label = append(label, rpt.options.ProfileLabels...)
  1014. } else if fullHeaders || !rpt.options.CompactLabels {
  1015. label = ProfileLabels(rpt)
  1016. }
  1017. var flatSum int64
  1018. for _, n := range g.Nodes {
  1019. flatSum = flatSum + n.FlatValue()
  1020. }
  1021. if len(rpt.options.ActiveFilters) > 0 {
  1022. activeFilters := legendActiveFilters(rpt.options.ActiveFilters)
  1023. label = append(label, activeFilters...)
  1024. }
  1025. label = append(label, fmt.Sprintf("Showing nodes accounting for %s, %s of %s total", rpt.formatValue(flatSum), strings.TrimSpace(measurement.Percentage(flatSum, rpt.total)), rpt.formatValue(rpt.total)))
  1026. if rpt.total != 0 {
  1027. if droppedNodes > 0 {
  1028. label = append(label, genLabel(droppedNodes, "node", "cum",
  1029. rpt.formatValue(abs64(int64(float64(rpt.total)*nodeFraction)))))
  1030. }
  1031. if droppedEdges > 0 {
  1032. label = append(label, genLabel(droppedEdges, "edge", "freq",
  1033. rpt.formatValue(abs64(int64(float64(rpt.total)*edgeFraction)))))
  1034. }
  1035. if nodeCount > 0 && nodeCount < origCount {
  1036. label = append(label, fmt.Sprintf("Showing top %d nodes out of %d",
  1037. nodeCount, origCount))
  1038. }
  1039. }
  1040. return label
  1041. }
  1042. func legendActiveFilters(activeFilters []string) []string {
  1043. legendActiveFilters := make([]string, len(activeFilters)+1)
  1044. legendActiveFilters[0] = "Active filters:"
  1045. for i, s := range activeFilters {
  1046. if len(s) > 80 {
  1047. s = s[:80] + "…"
  1048. }
  1049. legendActiveFilters[i+1] = " " + s
  1050. }
  1051. return legendActiveFilters
  1052. }
  1053. func genLabel(d int, n, l, f string) string {
  1054. if d > 1 {
  1055. n = n + "s"
  1056. }
  1057. return fmt.Sprintf("Dropped %d %s (%s <= %s)", d, n, l, f)
  1058. }
  1059. // New builds a new report indexing the sample values interpreting the
  1060. // samples with the provided function.
  1061. func New(prof *profile.Profile, o *Options) *Report {
  1062. format := func(v int64) string {
  1063. if r := o.Ratio; r > 0 && r != 1 {
  1064. fv := float64(v) * r
  1065. v = int64(fv)
  1066. }
  1067. return measurement.ScaledLabel(v, o.SampleUnit, o.OutputUnit)
  1068. }
  1069. return &Report{prof, computeTotal(prof, o.SampleValue, o.SampleMeanDivisor),
  1070. o, format}
  1071. }
  1072. // NewDefault builds a new report indexing the last sample value
  1073. // available.
  1074. func NewDefault(prof *profile.Profile, options Options) *Report {
  1075. index := len(prof.SampleType) - 1
  1076. o := &options
  1077. if o.Title == "" && len(prof.Mapping) > 0 && prof.Mapping[0].File != "" {
  1078. o.Title = filepath.Base(prof.Mapping[0].File)
  1079. }
  1080. o.SampleType = prof.SampleType[index].Type
  1081. o.SampleUnit = strings.ToLower(prof.SampleType[index].Unit)
  1082. o.SampleValue = func(v []int64) int64 {
  1083. return v[index]
  1084. }
  1085. return New(prof, o)
  1086. }
  1087. // computeTotal computes the sum of the absolute value of all sample values.
  1088. // If any samples have label indicating they belong to the diff base, then the
  1089. // total will only include samples with that label.
  1090. func computeTotal(prof *profile.Profile, value, meanDiv func(v []int64) int64) int64 {
  1091. var div, total, diffDiv, diffTotal int64
  1092. for _, sample := range prof.Sample {
  1093. var d, v int64
  1094. v = value(sample.Value)
  1095. if meanDiv != nil {
  1096. d = meanDiv(sample.Value)
  1097. }
  1098. if v < 0 {
  1099. v = -v
  1100. }
  1101. total += v
  1102. div += d
  1103. if sample.DiffBaseSample() {
  1104. diffTotal += v
  1105. diffDiv += d
  1106. }
  1107. }
  1108. if diffTotal > 0 {
  1109. total = diffTotal
  1110. div = diffDiv
  1111. }
  1112. if div != 0 {
  1113. return total / div
  1114. }
  1115. return total
  1116. }
  1117. // Report contains the data and associated routines to extract a
  1118. // report from a profile.
  1119. type Report struct {
  1120. prof *profile.Profile
  1121. total int64
  1122. options *Options
  1123. formatValue func(int64) string
  1124. }
  1125. // Total returns the total number of samples in a report.
  1126. func (rpt *Report) Total() int64 { return rpt.total }
  1127. func abs64(i int64) int64 {
  1128. if i < 0 {
  1129. return -i
  1130. }
  1131. return i
  1132. }