暫無描述

report.go 29KB

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