No Description

report.go 26KB

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