No Description

report.go 26KB

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