暂无描述

report.go 35KB

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