暂无描述

report.go 33KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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. "math"
  21. "path/filepath"
  22. "regexp"
  23. "sort"
  24. "strconv"
  25. "strings"
  26. "text/tabwriter"
  27. "time"
  28. "github.com/google/pprof/internal/graph"
  29. "github.com/google/pprof/internal/measurement"
  30. "github.com/google/pprof/internal/plugin"
  31. "github.com/google/pprof/profile"
  32. )
  33. // Output formats.
  34. const (
  35. Callgrind = iota
  36. Comments
  37. Dis
  38. Dot
  39. List
  40. Proto
  41. Raw
  42. Tags
  43. Text
  44. TopProto
  45. Traces
  46. Tree
  47. WebList
  48. )
  49. // Options are the formatting and filtering options used to generate a
  50. // profile.
  51. type Options struct {
  52. OutputFormat int
  53. CumSort bool
  54. CallTree bool
  55. DropNegative bool
  56. PositivePercentages bool
  57. CompactLabels bool
  58. Ratio float64
  59. Title string
  60. ProfileLabels []string
  61. ActiveFilters []string
  62. NodeCount int
  63. NodeFraction float64
  64. EdgeFraction float64
  65. SampleValue func(s []int64) int64
  66. SampleMeanDivisor func(s []int64) int64
  67. SampleType string
  68. SampleUnit string // Unit for the sample data from the profile.
  69. OutputUnit string // Units for data formatting in report.
  70. Symbol *regexp.Regexp // Symbols to include on disassembly report.
  71. SourcePath string // Search path for source files.
  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)
  215. }
  216. // Remove numeric tags not recognized by pprof.
  217. for _, s := range prof.Sample {
  218. numLabels := make(map[string][]int64, len(s.NumLabel))
  219. for k, v := range s.NumLabel {
  220. if k == "bytes" {
  221. numLabels[k] = append(numLabels[k], v...)
  222. }
  223. }
  224. s.NumLabel = numLabels
  225. }
  226. formatTag := func(v int64, key string) string {
  227. return measurement.ScaledLabel(v, key, o.OutputUnit)
  228. }
  229. gopt := &graph.Options{
  230. SampleValue: o.SampleValue,
  231. SampleMeanDivisor: o.SampleMeanDivisor,
  232. FormatTag: formatTag,
  233. CallTree: o.CallTree && (o.OutputFormat == Dot || o.OutputFormat == Callgrind),
  234. DropNegative: o.DropNegative,
  235. KeptNodes: nodes,
  236. }
  237. // Only keep binary names for disassembly-based reports, otherwise
  238. // remove it to allow merging of functions across binaries.
  239. switch o.OutputFormat {
  240. case Raw, List, WebList, Dis, Callgrind:
  241. gopt.ObjNames = true
  242. }
  243. return graph.New(rpt.prof, gopt)
  244. }
  245. func printTopProto(w io.Writer, rpt *Report) error {
  246. p := rpt.prof
  247. o := rpt.options
  248. g, _, _, _ := rpt.newTrimmedGraph()
  249. rpt.selectOutputUnit(g)
  250. out := profile.Profile{
  251. SampleType: []*profile.ValueType{
  252. {Type: "cum", Unit: o.OutputUnit},
  253. {Type: "flat", Unit: o.OutputUnit},
  254. },
  255. TimeNanos: p.TimeNanos,
  256. DurationNanos: p.DurationNanos,
  257. PeriodType: p.PeriodType,
  258. Period: p.Period,
  259. }
  260. functionMap := make(functionMap)
  261. for i, n := range g.Nodes {
  262. f := functionMap.FindOrAdd(n.Info)
  263. flat, cum := n.FlatValue(), n.CumValue()
  264. l := &profile.Location{
  265. ID: uint64(i + 1),
  266. Address: n.Info.Address,
  267. Line: []profile.Line{
  268. {
  269. Line: int64(n.Info.Lineno),
  270. Function: f,
  271. },
  272. },
  273. }
  274. fv, _ := measurement.Scale(flat, o.SampleUnit, o.OutputUnit)
  275. cv, _ := measurement.Scale(cum, o.SampleUnit, o.OutputUnit)
  276. s := &profile.Sample{
  277. Location: []*profile.Location{l},
  278. Value: []int64{int64(cv), int64(fv)},
  279. }
  280. out.Function = append(out.Function, f)
  281. out.Location = append(out.Location, l)
  282. out.Sample = append(out.Sample, s)
  283. }
  284. return out.Write(w)
  285. }
  286. type functionMap map[string]*profile.Function
  287. func (fm functionMap) FindOrAdd(ni graph.NodeInfo) *profile.Function {
  288. fName := fmt.Sprintf("%q%q%q%d", ni.Name, ni.OrigName, ni.File, ni.StartLine)
  289. if f := fm[fName]; f != nil {
  290. return f
  291. }
  292. f := &profile.Function{
  293. ID: uint64(len(fm) + 1),
  294. Name: ni.Name,
  295. SystemName: ni.OrigName,
  296. Filename: ni.File,
  297. StartLine: int64(ni.StartLine),
  298. }
  299. fm[fName] = f
  300. return f
  301. }
  302. // printAssembly prints an annotated assembly listing.
  303. func printAssembly(w io.Writer, rpt *Report, obj plugin.ObjTool) error {
  304. o := rpt.options
  305. prof := rpt.prof
  306. g := rpt.newGraph(nil)
  307. // If the regexp source can be parsed as an address, also match
  308. // functions that land on that address.
  309. var address *uint64
  310. if hex, err := strconv.ParseUint(o.Symbol.String(), 0, 64); err == nil {
  311. address = &hex
  312. }
  313. fmt.Fprintln(w, "Total:", rpt.formatValue(rpt.total))
  314. symbols := symbolsFromBinaries(prof, g, o.Symbol, address, obj)
  315. symNodes := nodesPerSymbol(g.Nodes, symbols)
  316. // Sort function names for printing.
  317. var syms objSymbols
  318. for s := range symNodes {
  319. syms = append(syms, s)
  320. }
  321. sort.Sort(syms)
  322. // Correlate the symbols from the binary with the profile samples.
  323. for _, s := range syms {
  324. sns := symNodes[s]
  325. // Gather samples for this symbol.
  326. flatSum, cumSum := sns.Sum()
  327. // Get the function assembly.
  328. insts, err := obj.Disasm(s.sym.File, s.sym.Start, s.sym.End)
  329. if err != nil {
  330. return err
  331. }
  332. ns := annotateAssembly(insts, sns, s.base)
  333. fmt.Fprintf(w, "ROUTINE ======================== %s\n", s.sym.Name[0])
  334. for _, name := range s.sym.Name[1:] {
  335. fmt.Fprintf(w, " AKA ======================== %s\n", name)
  336. }
  337. fmt.Fprintf(w, "%10s %10s (flat, cum) %s of Total\n",
  338. rpt.formatValue(flatSum), rpt.formatValue(cumSum),
  339. percentage(cumSum, rpt.total))
  340. function, file, line := "", "", 0
  341. for _, n := range ns {
  342. locStr := ""
  343. // Skip loc information if it hasn't changed from previous instruction.
  344. if n.function != function || n.file != file || n.line != line {
  345. function, file, line = n.function, n.file, n.line
  346. if n.function != "" {
  347. locStr = n.function + " "
  348. }
  349. if n.file != "" {
  350. locStr += n.file
  351. if n.line != 0 {
  352. locStr += fmt.Sprintf(":%d", n.line)
  353. }
  354. }
  355. }
  356. switch {
  357. case locStr == "":
  358. // No location info, just print the instruction.
  359. fmt.Fprintf(w, "%10s %10s %10x: %s\n",
  360. valueOrDot(n.flatValue(), rpt),
  361. valueOrDot(n.cumValue(), rpt),
  362. n.address, n.instruction,
  363. )
  364. case len(n.instruction) < 40:
  365. // Short instruction, print loc on the same line.
  366. fmt.Fprintf(w, "%10s %10s %10x: %-40s;%s\n",
  367. valueOrDot(n.flatValue(), rpt),
  368. valueOrDot(n.cumValue(), rpt),
  369. n.address, n.instruction,
  370. locStr,
  371. )
  372. default:
  373. // Long instruction, print loc on a separate line.
  374. fmt.Fprintf(w, "%74s;%s\n", "", locStr)
  375. fmt.Fprintf(w, "%10s %10s %10x: %s\n",
  376. valueOrDot(n.flatValue(), rpt),
  377. valueOrDot(n.cumValue(), rpt),
  378. n.address, n.instruction,
  379. )
  380. }
  381. }
  382. }
  383. return nil
  384. }
  385. // symbolsFromBinaries examines the binaries listed on the profile
  386. // that have associated samples, and identifies symbols matching rx.
  387. func symbolsFromBinaries(prof *profile.Profile, g *graph.Graph, rx *regexp.Regexp, address *uint64, obj plugin.ObjTool) []*objSymbol {
  388. hasSamples := make(map[string]bool)
  389. // Only examine mappings that have samples that match the
  390. // regexp. This is an optimization to speed up pprof.
  391. for _, n := range g.Nodes {
  392. if name := n.Info.PrintableName(); rx.MatchString(name) && n.Info.Objfile != "" {
  393. hasSamples[n.Info.Objfile] = true
  394. }
  395. }
  396. // Walk all mappings looking for matching functions with samples.
  397. var objSyms []*objSymbol
  398. for _, m := range prof.Mapping {
  399. if !hasSamples[m.File] {
  400. if address == nil || !(m.Start <= *address && *address <= m.Limit) {
  401. continue
  402. }
  403. }
  404. f, err := obj.Open(m.File, m.Start, m.Limit, m.Offset)
  405. if err != nil {
  406. fmt.Printf("%v\n", err)
  407. continue
  408. }
  409. // Find symbols in this binary matching the user regexp.
  410. var addr uint64
  411. if address != nil {
  412. addr = *address
  413. }
  414. msyms, err := f.Symbols(rx, addr)
  415. base := f.Base()
  416. f.Close()
  417. if err != nil {
  418. continue
  419. }
  420. for _, ms := range msyms {
  421. objSyms = append(objSyms,
  422. &objSymbol{
  423. sym: ms,
  424. base: base,
  425. },
  426. )
  427. }
  428. }
  429. return objSyms
  430. }
  431. // objSym represents a symbol identified from a binary. It includes
  432. // the SymbolInfo from the disasm package and the base that must be
  433. // added to correspond to sample addresses
  434. type objSymbol struct {
  435. sym *plugin.Sym
  436. base uint64
  437. }
  438. // objSymbols is a wrapper type to enable sorting of []*objSymbol.
  439. type objSymbols []*objSymbol
  440. func (o objSymbols) Len() int {
  441. return len(o)
  442. }
  443. func (o objSymbols) Less(i, j int) bool {
  444. if namei, namej := o[i].sym.Name[0], o[j].sym.Name[0]; namei != namej {
  445. return namei < namej
  446. }
  447. return o[i].sym.Start < o[j].sym.Start
  448. }
  449. func (o objSymbols) Swap(i, j int) {
  450. o[i], o[j] = o[j], o[i]
  451. }
  452. // nodesPerSymbol classifies nodes into a group of symbols.
  453. func nodesPerSymbol(ns graph.Nodes, symbols []*objSymbol) map[*objSymbol]graph.Nodes {
  454. symNodes := make(map[*objSymbol]graph.Nodes)
  455. for _, s := range symbols {
  456. // Gather samples for this symbol.
  457. for _, n := range ns {
  458. address := n.Info.Address - s.base
  459. if address >= s.sym.Start && address < s.sym.End {
  460. symNodes[s] = append(symNodes[s], n)
  461. }
  462. }
  463. }
  464. return symNodes
  465. }
  466. type assemblyInstruction struct {
  467. address uint64
  468. instruction string
  469. function string
  470. file string
  471. line int
  472. flat, cum int64
  473. flatDiv, cumDiv int64
  474. startsBlock bool
  475. }
  476. func (a *assemblyInstruction) flatValue() int64 {
  477. if a.flatDiv != 0 {
  478. return a.flat / a.flatDiv
  479. }
  480. return a.flat
  481. }
  482. func (a *assemblyInstruction) cumValue() int64 {
  483. if a.cumDiv != 0 {
  484. return a.cum / a.cumDiv
  485. }
  486. return a.cum
  487. }
  488. // annotateAssembly annotates a set of assembly instructions with a
  489. // set of samples. It returns a set of nodes to display. base is an
  490. // offset to adjust the sample addresses.
  491. func annotateAssembly(insts []plugin.Inst, samples graph.Nodes, base uint64) []assemblyInstruction {
  492. // Add end marker to simplify printing loop.
  493. insts = append(insts, plugin.Inst{
  494. Addr: ^uint64(0),
  495. })
  496. // Ensure samples are sorted by address.
  497. samples.Sort(graph.AddressOrder)
  498. s := 0
  499. asm := make([]assemblyInstruction, 0, len(insts))
  500. for ix, in := range insts[:len(insts)-1] {
  501. n := assemblyInstruction{
  502. address: in.Addr,
  503. instruction: in.Text,
  504. function: in.Function,
  505. line: in.Line,
  506. }
  507. if in.File != "" {
  508. n.file = filepath.Base(in.File)
  509. }
  510. // Sum all the samples until the next instruction (to account
  511. // for samples attributed to the middle of an instruction).
  512. for next := insts[ix+1].Addr; s < len(samples) && samples[s].Info.Address-base < next; s++ {
  513. sample := samples[s]
  514. n.flatDiv += sample.FlatDiv
  515. n.flat += sample.Flat
  516. n.cumDiv += sample.CumDiv
  517. n.cum += sample.Cum
  518. if f := sample.Info.File; f != "" && n.file == "" {
  519. n.file = filepath.Base(f)
  520. }
  521. if ln := sample.Info.Lineno; ln != 0 && n.line == 0 {
  522. n.line = ln
  523. }
  524. if f := sample.Info.Name; f != "" && n.function == "" {
  525. n.function = f
  526. }
  527. }
  528. asm = append(asm, n)
  529. }
  530. return asm
  531. }
  532. // valueOrDot formats a value according to a report, intercepting zero
  533. // values.
  534. func valueOrDot(value int64, rpt *Report) string {
  535. if value == 0 {
  536. return "."
  537. }
  538. return rpt.formatValue(value)
  539. }
  540. // printTags collects all tags referenced in the profile and prints
  541. // them in a sorted table.
  542. func printTags(w io.Writer, rpt *Report) error {
  543. p := rpt.prof
  544. o := rpt.options
  545. formatTag := func(v int64, key string) string {
  546. return measurement.ScaledLabel(v, key, o.OutputUnit)
  547. }
  548. // Hashtable to keep accumulate tags as key,value,count.
  549. tagMap := make(map[string]map[string]int64)
  550. for _, s := range p.Sample {
  551. for key, vals := range s.Label {
  552. for _, val := range vals {
  553. valueMap, ok := tagMap[key]
  554. if !ok {
  555. valueMap = make(map[string]int64)
  556. tagMap[key] = valueMap
  557. }
  558. valueMap[val] += o.SampleValue(s.Value)
  559. }
  560. }
  561. for key, vals := range s.NumLabel {
  562. for _, nval := range vals {
  563. val := formatTag(nval, key)
  564. valueMap, ok := tagMap[key]
  565. if !ok {
  566. valueMap = make(map[string]int64)
  567. tagMap[key] = valueMap
  568. }
  569. valueMap[val] += o.SampleValue(s.Value)
  570. }
  571. }
  572. }
  573. tagKeys := make([]*graph.Tag, 0, len(tagMap))
  574. for key := range tagMap {
  575. tagKeys = append(tagKeys, &graph.Tag{Name: key})
  576. }
  577. tabw := tabwriter.NewWriter(w, 0, 0, 1, ' ', tabwriter.AlignRight)
  578. for _, tagKey := range graph.SortTags(tagKeys, true) {
  579. var total int64
  580. key := tagKey.Name
  581. tags := make([]*graph.Tag, 0, len(tagMap[key]))
  582. for t, c := range tagMap[key] {
  583. total += c
  584. tags = append(tags, &graph.Tag{Name: t, Flat: c})
  585. }
  586. f, u := measurement.Scale(total, o.SampleUnit, o.OutputUnit)
  587. fmt.Fprintf(tabw, "%s:\t Total %.1f%s\n", key, f, u)
  588. for _, t := range graph.SortTags(tags, true) {
  589. f, u := measurement.Scale(t.FlatValue(), o.SampleUnit, o.OutputUnit)
  590. if total > 0 {
  591. fmt.Fprintf(tabw, " \t%.1f%s (%s):\t %s\n", f, u, percentage(t.FlatValue(), total), t.Name)
  592. } else {
  593. fmt.Fprintf(tabw, " \t%.1f%s:\t %s\n", f, u, t.Name)
  594. }
  595. }
  596. fmt.Fprintln(tabw)
  597. }
  598. return tabw.Flush()
  599. }
  600. // printComments prints all freeform comments in the profile.
  601. func printComments(w io.Writer, rpt *Report) error {
  602. p := rpt.prof
  603. for _, c := range p.Comments {
  604. fmt.Fprintln(w, c)
  605. }
  606. return nil
  607. }
  608. // printText prints a flat text report for a profile.
  609. func printText(w io.Writer, rpt *Report) error {
  610. g, origCount, droppedNodes, _ := rpt.newTrimmedGraph()
  611. rpt.selectOutputUnit(g)
  612. fmt.Fprintln(w, strings.Join(reportLabels(rpt, g, origCount, droppedNodes, 0, false), "\n"))
  613. fmt.Fprintf(w, "%10s %5s%% %5s%% %10s %5s%%\n",
  614. "flat", "flat", "sum", "cum", "cum")
  615. var flatSum int64
  616. for _, n := range g.Nodes {
  617. name, flat, cum := n.Info.PrintableName(), n.FlatValue(), n.CumValue()
  618. var inline, noinline bool
  619. for _, e := range n.In {
  620. if e.Inline {
  621. inline = true
  622. } else {
  623. noinline = true
  624. }
  625. }
  626. if inline {
  627. if noinline {
  628. name = name + " (partial-inline)"
  629. } else {
  630. name = name + " (inline)"
  631. }
  632. }
  633. flatSum += flat
  634. fmt.Fprintf(w, "%10s %s %s %10s %s %s\n",
  635. rpt.formatValue(flat),
  636. percentage(flat, rpt.total),
  637. percentage(flatSum, rpt.total),
  638. rpt.formatValue(cum),
  639. percentage(cum, rpt.total),
  640. name)
  641. }
  642. return nil
  643. }
  644. // printTraces prints all traces from a profile.
  645. func printTraces(w io.Writer, rpt *Report) error {
  646. fmt.Fprintln(w, strings.Join(ProfileLabels(rpt), "\n"))
  647. prof := rpt.prof
  648. o := rpt.options
  649. const separator = "-----------+-------------------------------------------------------"
  650. _, locations := graph.CreateNodes(prof, &graph.Options{})
  651. for _, sample := range prof.Sample {
  652. var stack graph.Nodes
  653. for _, loc := range sample.Location {
  654. id := loc.ID
  655. stack = append(stack, locations[id]...)
  656. }
  657. if len(stack) == 0 {
  658. continue
  659. }
  660. fmt.Fprintln(w, separator)
  661. // Print any text labels for the sample.
  662. var labels []string
  663. for s, vs := range sample.Label {
  664. labels = append(labels, fmt.Sprintf("%10s: %s\n", s, strings.Join(vs, " ")))
  665. }
  666. sort.Strings(labels)
  667. fmt.Fprint(w, strings.Join(labels, ""))
  668. var d, v int64
  669. v = o.SampleValue(sample.Value)
  670. if o.SampleMeanDivisor != nil {
  671. d = o.SampleMeanDivisor(sample.Value)
  672. }
  673. // Print call stack.
  674. if d != 0 {
  675. v = v / d
  676. }
  677. fmt.Fprintf(w, "%10s %s\n",
  678. rpt.formatValue(v), stack[0].Info.PrintableName())
  679. for _, s := range stack[1:] {
  680. fmt.Fprintf(w, "%10s %s\n", "", s.Info.PrintableName())
  681. }
  682. }
  683. fmt.Fprintln(w, separator)
  684. return nil
  685. }
  686. // printCallgrind prints a graph for a profile on callgrind format.
  687. func printCallgrind(w io.Writer, rpt *Report) error {
  688. o := rpt.options
  689. rpt.options.NodeFraction = 0
  690. rpt.options.EdgeFraction = 0
  691. rpt.options.NodeCount = 0
  692. g, _, _, _ := rpt.newTrimmedGraph()
  693. rpt.selectOutputUnit(g)
  694. nodeNames := getDisambiguatedNames(g)
  695. fmt.Fprintln(w, "positions: instr line")
  696. fmt.Fprintln(w, "events:", o.SampleType+"("+o.OutputUnit+")")
  697. objfiles := make(map[string]int)
  698. files := make(map[string]int)
  699. names := make(map[string]int)
  700. // prevInfo points to the previous NodeInfo.
  701. // It is used to group cost lines together as much as possible.
  702. var prevInfo *graph.NodeInfo
  703. for _, n := range g.Nodes {
  704. if prevInfo == nil || n.Info.Objfile != prevInfo.Objfile || n.Info.File != prevInfo.File || n.Info.Name != prevInfo.Name {
  705. fmt.Fprintln(w)
  706. fmt.Fprintln(w, "ob="+callgrindName(objfiles, n.Info.Objfile))
  707. fmt.Fprintln(w, "fl="+callgrindName(files, n.Info.File))
  708. fmt.Fprintln(w, "fn="+callgrindName(names, n.Info.Name))
  709. }
  710. addr := callgrindAddress(prevInfo, n.Info.Address)
  711. sv, _ := measurement.Scale(n.FlatValue(), o.SampleUnit, o.OutputUnit)
  712. fmt.Fprintf(w, "%s %d %d\n", addr, n.Info.Lineno, int64(sv))
  713. // Print outgoing edges.
  714. for _, out := range n.Out.Sort() {
  715. c, _ := measurement.Scale(out.Weight, o.SampleUnit, o.OutputUnit)
  716. callee := out.Dest
  717. fmt.Fprintln(w, "cfl="+callgrindName(files, callee.Info.File))
  718. fmt.Fprintln(w, "cfn="+callgrindName(names, nodeNames[callee]))
  719. // pprof doesn't have a flat weight for a call, leave as 0.
  720. fmt.Fprintf(w, "calls=0 %s %d\n", callgrindAddress(prevInfo, callee.Info.Address), callee.Info.Lineno)
  721. // TODO: This address may be in the middle of a call
  722. // instruction. It would be best to find the beginning
  723. // of the instruction, but the tools seem to handle
  724. // this OK.
  725. fmt.Fprintf(w, "* * %d\n", int64(c))
  726. }
  727. prevInfo = &n.Info
  728. }
  729. return nil
  730. }
  731. // getDisambiguatedNames returns a map from each node in the graph to
  732. // the name to use in the callgrind output. Callgrind merges all
  733. // functions with the same [file name, function name]. Add a [%d/n]
  734. // suffix to disambiguate nodes with different values of
  735. // node.Function, which we want to keep separate. In particular, this
  736. // affects graphs created with --call_tree, where nodes from different
  737. // contexts are associated to different Functions.
  738. func getDisambiguatedNames(g *graph.Graph) map[*graph.Node]string {
  739. nodeName := make(map[*graph.Node]string, len(g.Nodes))
  740. type names struct {
  741. file, function string
  742. }
  743. // nameFunctionIndex maps the callgrind names (filename, function)
  744. // to the node.Function values found for that name, and each
  745. // node.Function value to a sequential index to be used on the
  746. // disambiguated name.
  747. nameFunctionIndex := make(map[names]map[*graph.Node]int)
  748. for _, n := range g.Nodes {
  749. nm := names{n.Info.File, n.Info.Name}
  750. p, ok := nameFunctionIndex[nm]
  751. if !ok {
  752. p = make(map[*graph.Node]int)
  753. nameFunctionIndex[nm] = p
  754. }
  755. if _, ok := p[n.Function]; !ok {
  756. p[n.Function] = len(p)
  757. }
  758. }
  759. for _, n := range g.Nodes {
  760. nm := names{n.Info.File, n.Info.Name}
  761. nodeName[n] = n.Info.Name
  762. if p := nameFunctionIndex[nm]; len(p) > 1 {
  763. // If there is more than one function, add suffix to disambiguate.
  764. nodeName[n] += fmt.Sprintf(" [%d/%d]", p[n.Function]+1, len(p))
  765. }
  766. }
  767. return nodeName
  768. }
  769. // callgrindName implements the callgrind naming compression scheme.
  770. // For names not previously seen returns "(N) name", where N is a
  771. // unique index. For names previously seen returns "(N)" where N is
  772. // the index returned the first time.
  773. func callgrindName(names map[string]int, name string) string {
  774. if name == "" {
  775. return ""
  776. }
  777. if id, ok := names[name]; ok {
  778. return fmt.Sprintf("(%d)", id)
  779. }
  780. id := len(names) + 1
  781. names[name] = id
  782. return fmt.Sprintf("(%d) %s", id, name)
  783. }
  784. // callgrindAddress implements the callgrind subposition compression scheme if
  785. // possible. If prevInfo != nil, it contains the previous address. The current
  786. // address can be given relative to the previous address, with an explicit +/-
  787. // to indicate it is relative, or * for the same address.
  788. func callgrindAddress(prevInfo *graph.NodeInfo, curr uint64) string {
  789. abs := fmt.Sprintf("%#x", curr)
  790. if prevInfo == nil {
  791. return abs
  792. }
  793. prev := prevInfo.Address
  794. if prev == curr {
  795. return "*"
  796. }
  797. diff := int64(curr - prev)
  798. relative := fmt.Sprintf("%+d", diff)
  799. // Only bother to use the relative address if it is actually shorter.
  800. if len(relative) < len(abs) {
  801. return relative
  802. }
  803. return abs
  804. }
  805. // printTree prints a tree-based report in text form.
  806. func printTree(w io.Writer, rpt *Report) error {
  807. const separator = "----------------------------------------------------------+-------------"
  808. const legend = " flat flat% sum% cum cum% calls calls% + context "
  809. g, origCount, droppedNodes, _ := rpt.newTrimmedGraph()
  810. rpt.selectOutputUnit(g)
  811. fmt.Fprintln(w, strings.Join(reportLabels(rpt, g, origCount, droppedNodes, 0, false), "\n"))
  812. fmt.Fprintln(w, separator)
  813. fmt.Fprintln(w, legend)
  814. var flatSum int64
  815. rx := rpt.options.Symbol
  816. for _, n := range g.Nodes {
  817. name, flat, cum := n.Info.PrintableName(), n.FlatValue(), n.CumValue()
  818. // Skip any entries that do not match the regexp (for the "peek" command).
  819. if rx != nil && !rx.MatchString(name) {
  820. continue
  821. }
  822. fmt.Fprintln(w, separator)
  823. // Print incoming edges.
  824. inEdges := n.In.Sort()
  825. for _, in := range inEdges {
  826. var inline string
  827. if in.Inline {
  828. inline = " (inline)"
  829. }
  830. fmt.Fprintf(w, "%50s %s | %s%s\n", rpt.formatValue(in.Weight),
  831. percentage(in.Weight, cum), in.Src.Info.PrintableName(), inline)
  832. }
  833. // Print current node.
  834. flatSum += flat
  835. fmt.Fprintf(w, "%10s %s %s %10s %s | %s\n",
  836. rpt.formatValue(flat),
  837. percentage(flat, rpt.total),
  838. percentage(flatSum, rpt.total),
  839. rpt.formatValue(cum),
  840. percentage(cum, rpt.total),
  841. name)
  842. // Print outgoing edges.
  843. outEdges := n.Out.Sort()
  844. for _, out := range outEdges {
  845. var inline string
  846. if out.Inline {
  847. inline = " (inline)"
  848. }
  849. fmt.Fprintf(w, "%50s %s | %s%s\n", rpt.formatValue(out.Weight),
  850. percentage(out.Weight, cum), out.Dest.Info.PrintableName(), inline)
  851. }
  852. }
  853. if len(g.Nodes) > 0 {
  854. fmt.Fprintln(w, separator)
  855. }
  856. return nil
  857. }
  858. // GetDOT returns a graph suitable for dot processing along with some
  859. // configuration information.
  860. func GetDOT(rpt *Report) (*graph.Graph, *graph.DotConfig) {
  861. g, origCount, droppedNodes, droppedEdges := rpt.newTrimmedGraph()
  862. rpt.selectOutputUnit(g)
  863. labels := reportLabels(rpt, g, origCount, droppedNodes, droppedEdges, true)
  864. o := rpt.options
  865. formatTag := func(v int64, key string) string {
  866. return measurement.ScaledLabel(v, key, o.OutputUnit)
  867. }
  868. c := &graph.DotConfig{
  869. Title: rpt.options.Title,
  870. Labels: labels,
  871. FormatValue: rpt.formatValue,
  872. FormatTag: formatTag,
  873. Total: rpt.total,
  874. }
  875. return g, c
  876. }
  877. // printDOT prints an annotated callgraph in DOT format.
  878. func printDOT(w io.Writer, rpt *Report) error {
  879. g, c := GetDOT(rpt)
  880. graph.ComposeDot(w, g, &graph.DotAttributes{}, c)
  881. return nil
  882. }
  883. // percentage computes the percentage of total of a value, and encodes
  884. // it as a string. At least two digits of precision are printed.
  885. func percentage(value, total int64) string {
  886. var ratio float64
  887. if total != 0 {
  888. ratio = math.Abs(float64(value)/float64(total)) * 100
  889. }
  890. switch {
  891. case math.Abs(ratio) >= 99.95 && math.Abs(ratio) <= 100.05:
  892. return " 100%"
  893. case math.Abs(ratio) >= 1.0:
  894. return fmt.Sprintf("%5.2f%%", ratio)
  895. default:
  896. return fmt.Sprintf("%5.2g%%", ratio)
  897. }
  898. }
  899. // ProfileLabels returns printable labels for a profile.
  900. func ProfileLabels(rpt *Report) []string {
  901. label := []string{}
  902. prof := rpt.prof
  903. o := rpt.options
  904. if len(prof.Mapping) > 0 {
  905. if prof.Mapping[0].File != "" {
  906. label = append(label, "File: "+filepath.Base(prof.Mapping[0].File))
  907. }
  908. if prof.Mapping[0].BuildID != "" {
  909. label = append(label, "Build ID: "+prof.Mapping[0].BuildID)
  910. }
  911. }
  912. // Only include comments that do not start with '#'.
  913. for _, c := range prof.Comments {
  914. if !strings.HasPrefix(c, "#") {
  915. label = append(label, c)
  916. }
  917. }
  918. if o.SampleType != "" {
  919. label = append(label, "Type: "+o.SampleType)
  920. }
  921. if prof.TimeNanos != 0 {
  922. const layout = "Jan 2, 2006 at 3:04pm (MST)"
  923. label = append(label, "Time: "+time.Unix(0, prof.TimeNanos).Format(layout))
  924. }
  925. if prof.DurationNanos != 0 {
  926. duration := measurement.Label(prof.DurationNanos, "nanoseconds")
  927. totalNanos, totalUnit := measurement.Scale(rpt.total, o.SampleUnit, "nanoseconds")
  928. var ratio string
  929. if totalUnit == "ns" && totalNanos != 0 {
  930. ratio = "(" + percentage(int64(totalNanos), prof.DurationNanos) + ")"
  931. }
  932. label = append(label, fmt.Sprintf("Duration: %s, Total samples = %s %s", duration, rpt.formatValue(rpt.total), ratio))
  933. }
  934. return label
  935. }
  936. // reportLabels returns printable labels for a report. Includes
  937. // profileLabels.
  938. func reportLabels(rpt *Report, g *graph.Graph, origCount, droppedNodes, droppedEdges int, fullHeaders bool) []string {
  939. nodeFraction := rpt.options.NodeFraction
  940. edgeFraction := rpt.options.EdgeFraction
  941. nodeCount := len(g.Nodes)
  942. var label []string
  943. if len(rpt.options.ProfileLabels) > 0 {
  944. label = append(label, rpt.options.ProfileLabels...)
  945. } else if fullHeaders || !rpt.options.CompactLabels {
  946. label = ProfileLabels(rpt)
  947. }
  948. var flatSum int64
  949. for _, n := range g.Nodes {
  950. flatSum = flatSum + n.FlatValue()
  951. }
  952. if len(rpt.options.ActiveFilters) > 0 {
  953. activeFilters := legendActiveFilters(rpt.options.ActiveFilters)
  954. label = append(label, activeFilters...)
  955. }
  956. label = append(label, fmt.Sprintf("Showing nodes accounting for %s, %s of %s total", rpt.formatValue(flatSum), strings.TrimSpace(percentage(flatSum, rpt.total)), rpt.formatValue(rpt.total)))
  957. if rpt.total != 0 {
  958. if droppedNodes > 0 {
  959. label = append(label, genLabel(droppedNodes, "node", "cum",
  960. rpt.formatValue(abs64(int64(float64(rpt.total)*nodeFraction)))))
  961. }
  962. if droppedEdges > 0 {
  963. label = append(label, genLabel(droppedEdges, "edge", "freq",
  964. rpt.formatValue(abs64(int64(float64(rpt.total)*edgeFraction)))))
  965. }
  966. if nodeCount > 0 && nodeCount < origCount {
  967. label = append(label, fmt.Sprintf("Showing top %d nodes out of %d",
  968. nodeCount, origCount))
  969. }
  970. }
  971. return label
  972. }
  973. func legendActiveFilters(activeFilters []string) []string {
  974. legendActiveFilters := make([]string, len(activeFilters)+1)
  975. legendActiveFilters[0] = "Active filters:"
  976. for i, s := range activeFilters {
  977. if len(s) > 80 {
  978. s = s[:80] + "…"
  979. }
  980. legendActiveFilters[i+1] = " " + s
  981. }
  982. return legendActiveFilters
  983. }
  984. func genLabel(d int, n, l, f string) string {
  985. if d > 1 {
  986. n = n + "s"
  987. }
  988. return fmt.Sprintf("Dropped %d %s (%s <= %s)", d, n, l, f)
  989. }
  990. // New builds a new report indexing the sample values interpreting the
  991. // samples with the provided function.
  992. func New(prof *profile.Profile, o *Options) *Report {
  993. format := func(v int64) string {
  994. if r := o.Ratio; r > 0 && r != 1 {
  995. fv := float64(v) * r
  996. v = int64(fv)
  997. }
  998. return measurement.ScaledLabel(v, o.SampleUnit, o.OutputUnit)
  999. }
  1000. return &Report{prof, computeTotal(prof, o.SampleValue, o.SampleMeanDivisor, !o.PositivePercentages),
  1001. o, format}
  1002. }
  1003. // NewDefault builds a new report indexing the last sample value
  1004. // available.
  1005. func NewDefault(prof *profile.Profile, options Options) *Report {
  1006. index := len(prof.SampleType) - 1
  1007. o := &options
  1008. if o.Title == "" && len(prof.Mapping) > 0 && prof.Mapping[0].File != "" {
  1009. o.Title = filepath.Base(prof.Mapping[0].File)
  1010. }
  1011. o.SampleType = prof.SampleType[index].Type
  1012. o.SampleUnit = strings.ToLower(prof.SampleType[index].Unit)
  1013. o.SampleValue = func(v []int64) int64 {
  1014. return v[index]
  1015. }
  1016. return New(prof, o)
  1017. }
  1018. // computeTotal computes the sum of all sample values. This will be
  1019. // used to compute percentages. If includeNegative is set, use use
  1020. // absolute values to provide a meaningful percentage for both
  1021. // negative and positive values. Otherwise only use positive values,
  1022. // which is useful when comparing profiles from different jobs.
  1023. func computeTotal(prof *profile.Profile, value, meanDiv func(v []int64) int64, includeNegative bool) int64 {
  1024. var div, ret int64
  1025. for _, sample := range prof.Sample {
  1026. var d, v int64
  1027. v = value(sample.Value)
  1028. if meanDiv != nil {
  1029. d = meanDiv(sample.Value)
  1030. }
  1031. if v >= 0 {
  1032. ret += v
  1033. div += d
  1034. } else if includeNegative {
  1035. ret -= v
  1036. div += d
  1037. }
  1038. }
  1039. if div != 0 {
  1040. return ret / div
  1041. }
  1042. return ret
  1043. }
  1044. // Report contains the data and associated routines to extract a
  1045. // report from a profile.
  1046. type Report struct {
  1047. prof *profile.Profile
  1048. total int64
  1049. options *Options
  1050. formatValue func(int64) string
  1051. }
  1052. func abs64(i int64) int64 {
  1053. if i < 0 {
  1054. return -i
  1055. }
  1056. return i
  1057. }