暫無描述

report.go 34KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  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. // TextItem holds a single text report entry.
  609. type TextItem struct {
  610. Name string
  611. InlineLabel string // Not empty if inlined
  612. Flat, FlatPercent string
  613. SumPercent string
  614. Cum, CumPercent string
  615. }
  616. // TextItems returns a list of text items from the report and a list
  617. // of labels that describe the report.
  618. func TextItems(rpt *Report) ([]TextItem, []string) {
  619. g, origCount, droppedNodes, _ := rpt.newTrimmedGraph()
  620. rpt.selectOutputUnit(g)
  621. labels := reportLabels(rpt, g, origCount, droppedNodes, 0, false)
  622. var items []TextItem
  623. var flatSum int64
  624. for _, n := range g.Nodes {
  625. name, flat, cum := n.Info.PrintableName(), n.FlatValue(), n.CumValue()
  626. var inline, noinline bool
  627. for _, e := range n.In {
  628. if e.Inline {
  629. inline = true
  630. } else {
  631. noinline = true
  632. }
  633. }
  634. var inl string
  635. if inline {
  636. if noinline {
  637. inl = "(partial-inline)"
  638. } else {
  639. inl = "(inline)"
  640. }
  641. }
  642. flatSum += flat
  643. items = append(items, TextItem{
  644. Name: name,
  645. InlineLabel: inl,
  646. Flat: rpt.formatValue(flat),
  647. FlatPercent: percentage(flat, rpt.total),
  648. SumPercent: percentage(flatSum, rpt.total),
  649. Cum: rpt.formatValue(cum),
  650. CumPercent: percentage(cum, rpt.total),
  651. })
  652. }
  653. return items, labels
  654. }
  655. // printText prints a flat text report for a profile.
  656. func printText(w io.Writer, rpt *Report) error {
  657. items, labels := TextItems(rpt)
  658. fmt.Fprintln(w, strings.Join(labels, "\n"))
  659. fmt.Fprintf(w, "%10s %5s%% %5s%% %10s %5s%%\n",
  660. "flat", "flat", "sum", "cum", "cum")
  661. for _, item := range items {
  662. inl := item.InlineLabel
  663. if inl != "" {
  664. inl = " " + inl
  665. }
  666. fmt.Fprintf(w, "%10s %s %s %10s %s %s%s\n",
  667. item.Flat, item.FlatPercent,
  668. item.SumPercent,
  669. item.Cum, item.CumPercent,
  670. item.Name, inl)
  671. }
  672. return nil
  673. }
  674. // printTraces prints all traces from a profile.
  675. func printTraces(w io.Writer, rpt *Report) error {
  676. fmt.Fprintln(w, strings.Join(ProfileLabels(rpt), "\n"))
  677. prof := rpt.prof
  678. o := rpt.options
  679. const separator = "-----------+-------------------------------------------------------"
  680. _, locations := graph.CreateNodes(prof, &graph.Options{})
  681. for _, sample := range prof.Sample {
  682. var stack graph.Nodes
  683. for _, loc := range sample.Location {
  684. id := loc.ID
  685. stack = append(stack, locations[id]...)
  686. }
  687. if len(stack) == 0 {
  688. continue
  689. }
  690. fmt.Fprintln(w, separator)
  691. // Print any text labels for the sample.
  692. var labels []string
  693. for s, vs := range sample.Label {
  694. labels = append(labels, fmt.Sprintf("%10s: %s\n", s, strings.Join(vs, " ")))
  695. }
  696. sort.Strings(labels)
  697. fmt.Fprint(w, strings.Join(labels, ""))
  698. // Print any numeric labels for the sample
  699. var numLabels []string
  700. for k, v := range sample.NumLabel {
  701. numLabels = append(numLabels, fmt.Sprintf("%10s: %s\n", k, strings.Trim(fmt.Sprintf("%d", v), "[]")))
  702. }
  703. sort.Strings(numLabels)
  704. fmt.Fprint(w, strings.Join(numLabels, ""))
  705. var d, v int64
  706. v = o.SampleValue(sample.Value)
  707. if o.SampleMeanDivisor != nil {
  708. d = o.SampleMeanDivisor(sample.Value)
  709. }
  710. // Print call stack.
  711. if d != 0 {
  712. v = v / d
  713. }
  714. fmt.Fprintf(w, "%10s %s\n",
  715. rpt.formatValue(v), stack[0].Info.PrintableName())
  716. for _, s := range stack[1:] {
  717. fmt.Fprintf(w, "%10s %s\n", "", s.Info.PrintableName())
  718. }
  719. }
  720. fmt.Fprintln(w, separator)
  721. return nil
  722. }
  723. // printCallgrind prints a graph for a profile on callgrind format.
  724. func printCallgrind(w io.Writer, rpt *Report) error {
  725. o := rpt.options
  726. rpt.options.NodeFraction = 0
  727. rpt.options.EdgeFraction = 0
  728. rpt.options.NodeCount = 0
  729. g, _, _, _ := rpt.newTrimmedGraph()
  730. rpt.selectOutputUnit(g)
  731. nodeNames := getDisambiguatedNames(g)
  732. fmt.Fprintln(w, "positions: instr line")
  733. fmt.Fprintln(w, "events:", o.SampleType+"("+o.OutputUnit+")")
  734. objfiles := make(map[string]int)
  735. files := make(map[string]int)
  736. names := make(map[string]int)
  737. // prevInfo points to the previous NodeInfo.
  738. // It is used to group cost lines together as much as possible.
  739. var prevInfo *graph.NodeInfo
  740. for _, n := range g.Nodes {
  741. if prevInfo == nil || n.Info.Objfile != prevInfo.Objfile || n.Info.File != prevInfo.File || n.Info.Name != prevInfo.Name {
  742. fmt.Fprintln(w)
  743. fmt.Fprintln(w, "ob="+callgrindName(objfiles, n.Info.Objfile))
  744. fmt.Fprintln(w, "fl="+callgrindName(files, n.Info.File))
  745. fmt.Fprintln(w, "fn="+callgrindName(names, n.Info.Name))
  746. }
  747. addr := callgrindAddress(prevInfo, n.Info.Address)
  748. sv, _ := measurement.Scale(n.FlatValue(), o.SampleUnit, o.OutputUnit)
  749. fmt.Fprintf(w, "%s %d %d\n", addr, n.Info.Lineno, int64(sv))
  750. // Print outgoing edges.
  751. for _, out := range n.Out.Sort() {
  752. c, _ := measurement.Scale(out.Weight, o.SampleUnit, o.OutputUnit)
  753. callee := out.Dest
  754. fmt.Fprintln(w, "cfl="+callgrindName(files, callee.Info.File))
  755. fmt.Fprintln(w, "cfn="+callgrindName(names, nodeNames[callee]))
  756. // pprof doesn't have a flat weight for a call, leave as 0.
  757. fmt.Fprintf(w, "calls=0 %s %d\n", callgrindAddress(prevInfo, callee.Info.Address), callee.Info.Lineno)
  758. // TODO: This address may be in the middle of a call
  759. // instruction. It would be best to find the beginning
  760. // of the instruction, but the tools seem to handle
  761. // this OK.
  762. fmt.Fprintf(w, "* * %d\n", int64(c))
  763. }
  764. prevInfo = &n.Info
  765. }
  766. return nil
  767. }
  768. // getDisambiguatedNames returns a map from each node in the graph to
  769. // the name to use in the callgrind output. Callgrind merges all
  770. // functions with the same [file name, function name]. Add a [%d/n]
  771. // suffix to disambiguate nodes with different values of
  772. // node.Function, which we want to keep separate. In particular, this
  773. // affects graphs created with --call_tree, where nodes from different
  774. // contexts are associated to different Functions.
  775. func getDisambiguatedNames(g *graph.Graph) map[*graph.Node]string {
  776. nodeName := make(map[*graph.Node]string, len(g.Nodes))
  777. type names struct {
  778. file, function string
  779. }
  780. // nameFunctionIndex maps the callgrind names (filename, function)
  781. // to the node.Function values found for that name, and each
  782. // node.Function value to a sequential index to be used on the
  783. // disambiguated name.
  784. nameFunctionIndex := make(map[names]map[*graph.Node]int)
  785. for _, n := range g.Nodes {
  786. nm := names{n.Info.File, n.Info.Name}
  787. p, ok := nameFunctionIndex[nm]
  788. if !ok {
  789. p = make(map[*graph.Node]int)
  790. nameFunctionIndex[nm] = p
  791. }
  792. if _, ok := p[n.Function]; !ok {
  793. p[n.Function] = len(p)
  794. }
  795. }
  796. for _, n := range g.Nodes {
  797. nm := names{n.Info.File, n.Info.Name}
  798. nodeName[n] = n.Info.Name
  799. if p := nameFunctionIndex[nm]; len(p) > 1 {
  800. // If there is more than one function, add suffix to disambiguate.
  801. nodeName[n] += fmt.Sprintf(" [%d/%d]", p[n.Function]+1, len(p))
  802. }
  803. }
  804. return nodeName
  805. }
  806. // callgrindName implements the callgrind naming compression scheme.
  807. // For names not previously seen returns "(N) name", where N is a
  808. // unique index. For names previously seen returns "(N)" where N is
  809. // the index returned the first time.
  810. func callgrindName(names map[string]int, name string) string {
  811. if name == "" {
  812. return ""
  813. }
  814. if id, ok := names[name]; ok {
  815. return fmt.Sprintf("(%d)", id)
  816. }
  817. id := len(names) + 1
  818. names[name] = id
  819. return fmt.Sprintf("(%d) %s", id, name)
  820. }
  821. // callgrindAddress implements the callgrind subposition compression scheme if
  822. // possible. If prevInfo != nil, it contains the previous address. The current
  823. // address can be given relative to the previous address, with an explicit +/-
  824. // to indicate it is relative, or * for the same address.
  825. func callgrindAddress(prevInfo *graph.NodeInfo, curr uint64) string {
  826. abs := fmt.Sprintf("%#x", curr)
  827. if prevInfo == nil {
  828. return abs
  829. }
  830. prev := prevInfo.Address
  831. if prev == curr {
  832. return "*"
  833. }
  834. diff := int64(curr - prev)
  835. relative := fmt.Sprintf("%+d", diff)
  836. // Only bother to use the relative address if it is actually shorter.
  837. if len(relative) < len(abs) {
  838. return relative
  839. }
  840. return abs
  841. }
  842. // printTree prints a tree-based report in text form.
  843. func printTree(w io.Writer, rpt *Report) error {
  844. const separator = "----------------------------------------------------------+-------------"
  845. const legend = " flat flat% sum% cum cum% calls calls% + context "
  846. g, origCount, droppedNodes, _ := rpt.newTrimmedGraph()
  847. rpt.selectOutputUnit(g)
  848. fmt.Fprintln(w, strings.Join(reportLabels(rpt, g, origCount, droppedNodes, 0, false), "\n"))
  849. fmt.Fprintln(w, separator)
  850. fmt.Fprintln(w, legend)
  851. var flatSum int64
  852. rx := rpt.options.Symbol
  853. for _, n := range g.Nodes {
  854. name, flat, cum := n.Info.PrintableName(), n.FlatValue(), n.CumValue()
  855. // Skip any entries that do not match the regexp (for the "peek" command).
  856. if rx != nil && !rx.MatchString(name) {
  857. continue
  858. }
  859. fmt.Fprintln(w, separator)
  860. // Print incoming edges.
  861. inEdges := n.In.Sort()
  862. for _, in := range inEdges {
  863. var inline string
  864. if in.Inline {
  865. inline = " (inline)"
  866. }
  867. fmt.Fprintf(w, "%50s %s | %s%s\n", rpt.formatValue(in.Weight),
  868. percentage(in.Weight, cum), in.Src.Info.PrintableName(), inline)
  869. }
  870. // Print current node.
  871. flatSum += flat
  872. fmt.Fprintf(w, "%10s %s %s %10s %s | %s\n",
  873. rpt.formatValue(flat),
  874. percentage(flat, rpt.total),
  875. percentage(flatSum, rpt.total),
  876. rpt.formatValue(cum),
  877. percentage(cum, rpt.total),
  878. name)
  879. // Print outgoing edges.
  880. outEdges := n.Out.Sort()
  881. for _, out := range outEdges {
  882. var inline string
  883. if out.Inline {
  884. inline = " (inline)"
  885. }
  886. fmt.Fprintf(w, "%50s %s | %s%s\n", rpt.formatValue(out.Weight),
  887. percentage(out.Weight, cum), out.Dest.Info.PrintableName(), inline)
  888. }
  889. }
  890. if len(g.Nodes) > 0 {
  891. fmt.Fprintln(w, separator)
  892. }
  893. return nil
  894. }
  895. // GetDOT returns a graph suitable for dot processing along with some
  896. // configuration information.
  897. func GetDOT(rpt *Report) (*graph.Graph, *graph.DotConfig) {
  898. g, origCount, droppedNodes, droppedEdges := rpt.newTrimmedGraph()
  899. rpt.selectOutputUnit(g)
  900. labels := reportLabels(rpt, g, origCount, droppedNodes, droppedEdges, true)
  901. c := &graph.DotConfig{
  902. Title: rpt.options.Title,
  903. Labels: labels,
  904. FormatValue: rpt.formatValue,
  905. Total: rpt.total,
  906. }
  907. return g, c
  908. }
  909. // printDOT prints an annotated callgraph in DOT format.
  910. func printDOT(w io.Writer, rpt *Report) error {
  911. g, c := GetDOT(rpt)
  912. graph.ComposeDot(w, g, &graph.DotAttributes{}, c)
  913. return nil
  914. }
  915. // percentage computes the percentage of total of a value, and encodes
  916. // it as a string. At least two digits of precision are printed.
  917. func percentage(value, total int64) string {
  918. var ratio float64
  919. if total != 0 {
  920. ratio = math.Abs(float64(value)/float64(total)) * 100
  921. }
  922. switch {
  923. case math.Abs(ratio) >= 99.95 && math.Abs(ratio) <= 100.05:
  924. return " 100%"
  925. case math.Abs(ratio) >= 1.0:
  926. return fmt.Sprintf("%5.2f%%", ratio)
  927. default:
  928. return fmt.Sprintf("%5.2g%%", ratio)
  929. }
  930. }
  931. // ProfileLabels returns printable labels for a profile.
  932. func ProfileLabels(rpt *Report) []string {
  933. label := []string{}
  934. prof := rpt.prof
  935. o := rpt.options
  936. if len(prof.Mapping) > 0 {
  937. if prof.Mapping[0].File != "" {
  938. label = append(label, "File: "+filepath.Base(prof.Mapping[0].File))
  939. }
  940. if prof.Mapping[0].BuildID != "" {
  941. label = append(label, "Build ID: "+prof.Mapping[0].BuildID)
  942. }
  943. }
  944. // Only include comments that do not start with '#'.
  945. for _, c := range prof.Comments {
  946. if !strings.HasPrefix(c, "#") {
  947. label = append(label, c)
  948. }
  949. }
  950. if o.SampleType != "" {
  951. label = append(label, "Type: "+o.SampleType)
  952. }
  953. if prof.TimeNanos != 0 {
  954. const layout = "Jan 2, 2006 at 3:04pm (MST)"
  955. label = append(label, "Time: "+time.Unix(0, prof.TimeNanos).Format(layout))
  956. }
  957. if prof.DurationNanos != 0 {
  958. duration := measurement.Label(prof.DurationNanos, "nanoseconds")
  959. totalNanos, totalUnit := measurement.Scale(rpt.total, o.SampleUnit, "nanoseconds")
  960. var ratio string
  961. if totalUnit == "ns" && totalNanos != 0 {
  962. ratio = "(" + percentage(int64(totalNanos), prof.DurationNanos) + ")"
  963. }
  964. label = append(label, fmt.Sprintf("Duration: %s, Total samples = %s %s", duration, rpt.formatValue(rpt.total), ratio))
  965. }
  966. return label
  967. }
  968. // reportLabels returns printable labels for a report. Includes
  969. // profileLabels.
  970. func reportLabels(rpt *Report, g *graph.Graph, origCount, droppedNodes, droppedEdges int, fullHeaders bool) []string {
  971. nodeFraction := rpt.options.NodeFraction
  972. edgeFraction := rpt.options.EdgeFraction
  973. nodeCount := len(g.Nodes)
  974. var label []string
  975. if len(rpt.options.ProfileLabels) > 0 {
  976. label = append(label, rpt.options.ProfileLabels...)
  977. } else if fullHeaders || !rpt.options.CompactLabels {
  978. label = ProfileLabels(rpt)
  979. }
  980. var flatSum int64
  981. for _, n := range g.Nodes {
  982. flatSum = flatSum + n.FlatValue()
  983. }
  984. if len(rpt.options.ActiveFilters) > 0 {
  985. activeFilters := legendActiveFilters(rpt.options.ActiveFilters)
  986. label = append(label, activeFilters...)
  987. }
  988. 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)))
  989. if rpt.total != 0 {
  990. if droppedNodes > 0 {
  991. label = append(label, genLabel(droppedNodes, "node", "cum",
  992. rpt.formatValue(abs64(int64(float64(rpt.total)*nodeFraction)))))
  993. }
  994. if droppedEdges > 0 {
  995. label = append(label, genLabel(droppedEdges, "edge", "freq",
  996. rpt.formatValue(abs64(int64(float64(rpt.total)*edgeFraction)))))
  997. }
  998. if nodeCount > 0 && nodeCount < origCount {
  999. label = append(label, fmt.Sprintf("Showing top %d nodes out of %d",
  1000. nodeCount, origCount))
  1001. }
  1002. }
  1003. return label
  1004. }
  1005. func legendActiveFilters(activeFilters []string) []string {
  1006. legendActiveFilters := make([]string, len(activeFilters)+1)
  1007. legendActiveFilters[0] = "Active filters:"
  1008. for i, s := range activeFilters {
  1009. if len(s) > 80 {
  1010. s = s[:80] + "…"
  1011. }
  1012. legendActiveFilters[i+1] = " " + s
  1013. }
  1014. return legendActiveFilters
  1015. }
  1016. func genLabel(d int, n, l, f string) string {
  1017. if d > 1 {
  1018. n = n + "s"
  1019. }
  1020. return fmt.Sprintf("Dropped %d %s (%s <= %s)", d, n, l, f)
  1021. }
  1022. // New builds a new report indexing the sample values interpreting the
  1023. // samples with the provided function.
  1024. func New(prof *profile.Profile, o *Options) *Report {
  1025. format := func(v int64) string {
  1026. if r := o.Ratio; r > 0 && r != 1 {
  1027. fv := float64(v) * r
  1028. v = int64(fv)
  1029. }
  1030. return measurement.ScaledLabel(v, o.SampleUnit, o.OutputUnit)
  1031. }
  1032. return &Report{prof, computeTotal(prof, o.SampleValue, o.SampleMeanDivisor, !o.PositivePercentages),
  1033. o, format}
  1034. }
  1035. // NewDefault builds a new report indexing the last sample value
  1036. // available.
  1037. func NewDefault(prof *profile.Profile, options Options) *Report {
  1038. index := len(prof.SampleType) - 1
  1039. o := &options
  1040. if o.Title == "" && len(prof.Mapping) > 0 && prof.Mapping[0].File != "" {
  1041. o.Title = filepath.Base(prof.Mapping[0].File)
  1042. }
  1043. o.SampleType = prof.SampleType[index].Type
  1044. o.SampleUnit = strings.ToLower(prof.SampleType[index].Unit)
  1045. o.SampleValue = func(v []int64) int64 {
  1046. return v[index]
  1047. }
  1048. return New(prof, o)
  1049. }
  1050. // computeTotal computes the sum of all sample values. This will be
  1051. // used to compute percentages. If includeNegative is set, use use
  1052. // absolute values to provide a meaningful percentage for both
  1053. // negative and positive values. Otherwise only use positive values,
  1054. // which is useful when comparing profiles from different jobs.
  1055. func computeTotal(prof *profile.Profile, value, meanDiv func(v []int64) int64, includeNegative bool) int64 {
  1056. var div, ret int64
  1057. for _, sample := range prof.Sample {
  1058. var d, v int64
  1059. v = value(sample.Value)
  1060. if meanDiv != nil {
  1061. d = meanDiv(sample.Value)
  1062. }
  1063. if v >= 0 {
  1064. ret += v
  1065. div += d
  1066. } else if includeNegative {
  1067. ret -= v
  1068. div += d
  1069. }
  1070. }
  1071. if div != 0 {
  1072. return ret / div
  1073. }
  1074. return ret
  1075. }
  1076. // Report contains the data and associated routines to extract a
  1077. // report from a profile.
  1078. type Report struct {
  1079. prof *profile.Profile
  1080. total int64
  1081. options *Options
  1082. formatValue func(int64) string
  1083. }
  1084. func abs64(i int64) int64 {
  1085. if i < 0 {
  1086. return -i
  1087. }
  1088. return i
  1089. }