Brak opisu

report.go 27KB

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