Sin descripción

report.go 31KB

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