暫無描述

report.go 26KB

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