Nav apraksta

report.go 29KB

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