No Description

report.go 30KB

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