Nessuna descrizione

report.go 35KB

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