Ei kuvausta

report.go 35KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  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. "path/filepath"
  21. "regexp"
  22. "sort"
  23. "strconv"
  24. "strings"
  25. "text/tabwriter"
  26. "time"
  27. "github.com/google/pprof/internal/graph"
  28. "github.com/google/pprof/internal/measurement"
  29. "github.com/google/pprof/internal/plugin"
  30. "github.com/google/pprof/profile"
  31. )
  32. // Output formats.
  33. const (
  34. Callgrind = iota
  35. Comments
  36. Dis
  37. Dot
  38. List
  39. Proto
  40. Raw
  41. Tags
  42. Text
  43. TopProto
  44. Traces
  45. Tree
  46. WebList
  47. )
  48. // Options are the formatting and filtering options used to generate a
  49. // profile.
  50. type Options struct {
  51. OutputFormat int
  52. CumSort bool
  53. CallTree bool
  54. DropNegative bool
  55. PositivePercentages bool
  56. CompactLabels bool
  57. Ratio float64
  58. Title string
  59. ProfileLabels []string
  60. ActiveFilters []string
  61. NumLabelUnits map[string]string
  62. NodeCount int
  63. NodeFraction float64
  64. EdgeFraction float64
  65. SampleValue func(s []int64) int64
  66. SampleMeanDivisor func(s []int64) int64
  67. SampleType string
  68. SampleUnit string // Unit for the sample data from the profile.
  69. OutputUnit string // Units for data formatting in report.
  70. Symbol *regexp.Regexp // Symbols to include on disassembly report.
  71. SourcePath string // Search path for source files.
  72. }
  73. // Generate generates a report as directed by the Report.
  74. func Generate(w io.Writer, rpt *Report, obj plugin.ObjTool) error {
  75. o := rpt.options
  76. switch o.OutputFormat {
  77. case Comments:
  78. return printComments(w, rpt)
  79. case Dot:
  80. return printDOT(w, rpt)
  81. case Tree:
  82. return printTree(w, rpt)
  83. case Text:
  84. return printText(w, rpt)
  85. case Traces:
  86. return printTraces(w, rpt)
  87. case Raw:
  88. fmt.Fprint(w, rpt.prof.String())
  89. return nil
  90. case Tags:
  91. return printTags(w, rpt)
  92. case Proto:
  93. return rpt.prof.Write(w)
  94. case TopProto:
  95. return printTopProto(w, rpt)
  96. case Dis:
  97. return printAssembly(w, rpt, obj)
  98. case List:
  99. return printSource(w, rpt)
  100. case WebList:
  101. return printWebSource(w, rpt, obj)
  102. case Callgrind:
  103. return printCallgrind(w, rpt)
  104. }
  105. return fmt.Errorf("unexpected output format")
  106. }
  107. // newTrimmedGraph creates a graph for this report, trimmed according
  108. // to the report options.
  109. func (rpt *Report) newTrimmedGraph() (g *graph.Graph, origCount, droppedNodes, droppedEdges int) {
  110. o := rpt.options
  111. // Build a graph and refine it. On each refinement step we must rebuild the graph from the samples,
  112. // as the graph itself doesn't contain enough information to preserve full precision.
  113. visualMode := o.OutputFormat == Dot
  114. cumSort := o.CumSort
  115. // The call_tree option is only honored when generating visual representations of the callgraph.
  116. callTree := o.CallTree && (o.OutputFormat == Dot || o.OutputFormat == Callgrind)
  117. // First step: Build complete graph to identify low frequency nodes, based on their cum weight.
  118. g = rpt.newGraph(nil)
  119. totalValue, _ := g.Nodes.Sum()
  120. nodeCutoff := abs64(int64(float64(totalValue) * o.NodeFraction))
  121. edgeCutoff := abs64(int64(float64(totalValue) * o.EdgeFraction))
  122. // Filter out nodes with cum value below nodeCutoff.
  123. if nodeCutoff > 0 {
  124. if callTree {
  125. if nodesKept := g.DiscardLowFrequencyNodePtrs(nodeCutoff); len(g.Nodes) != len(nodesKept) {
  126. droppedNodes = len(g.Nodes) - len(nodesKept)
  127. g.TrimTree(nodesKept)
  128. }
  129. } else {
  130. if nodesKept := g.DiscardLowFrequencyNodes(nodeCutoff); len(g.Nodes) != len(nodesKept) {
  131. droppedNodes = len(g.Nodes) - len(nodesKept)
  132. g = rpt.newGraph(nodesKept)
  133. }
  134. }
  135. }
  136. origCount = len(g.Nodes)
  137. // Second step: Limit the total number of nodes. Apply specialized heuristics to improve
  138. // visualization when generating dot output.
  139. g.SortNodes(cumSort, visualMode)
  140. if nodeCount := o.NodeCount; nodeCount > 0 {
  141. // Remove low frequency tags and edges as they affect selection.
  142. g.TrimLowFrequencyTags(nodeCutoff)
  143. g.TrimLowFrequencyEdges(edgeCutoff)
  144. if callTree {
  145. if nodesKept := g.SelectTopNodePtrs(nodeCount, visualMode); len(g.Nodes) != len(nodesKept) {
  146. g.TrimTree(nodesKept)
  147. g.SortNodes(cumSort, visualMode)
  148. }
  149. } else {
  150. if nodesKept := g.SelectTopNodes(nodeCount, visualMode); len(g.Nodes) != len(nodesKept) {
  151. g = rpt.newGraph(nodesKept)
  152. g.SortNodes(cumSort, visualMode)
  153. }
  154. }
  155. }
  156. // Final step: Filter out low frequency tags and edges, and remove redundant edges that clutter
  157. // the graph.
  158. g.TrimLowFrequencyTags(nodeCutoff)
  159. droppedEdges = g.TrimLowFrequencyEdges(edgeCutoff)
  160. if visualMode {
  161. g.RemoveRedundantEdges()
  162. }
  163. return
  164. }
  165. func (rpt *Report) selectOutputUnit(g *graph.Graph) {
  166. o := rpt.options
  167. // Select best unit for profile output.
  168. // Find the appropriate units for the smallest non-zero sample
  169. if o.OutputUnit != "minimum" || len(g.Nodes) == 0 {
  170. return
  171. }
  172. var minValue int64
  173. for _, n := range g.Nodes {
  174. nodeMin := abs64(n.FlatValue())
  175. if nodeMin == 0 {
  176. nodeMin = abs64(n.CumValue())
  177. }
  178. if nodeMin > 0 && (minValue == 0 || nodeMin < minValue) {
  179. minValue = nodeMin
  180. }
  181. }
  182. maxValue := rpt.total
  183. if minValue == 0 {
  184. minValue = maxValue
  185. }
  186. if r := o.Ratio; r > 0 && r != 1 {
  187. minValue = int64(float64(minValue) * r)
  188. maxValue = int64(float64(maxValue) * r)
  189. }
  190. _, minUnit := measurement.Scale(minValue, o.SampleUnit, "minimum")
  191. _, maxUnit := measurement.Scale(maxValue, o.SampleUnit, "minimum")
  192. unit := minUnit
  193. if minUnit != maxUnit && minValue*100 < maxValue && o.OutputFormat != Callgrind {
  194. // Minimum and maximum values have different units. Scale
  195. // minimum by 100 to use larger units, allowing minimum value to
  196. // be scaled down to 0.01, except for callgrind reports since
  197. // they can only represent integer values.
  198. _, unit = measurement.Scale(100*minValue, o.SampleUnit, "minimum")
  199. }
  200. if unit != "" {
  201. o.OutputUnit = unit
  202. } else {
  203. o.OutputUnit = o.SampleUnit
  204. }
  205. }
  206. // newGraph creates a new graph for this report. If nodes is non-nil,
  207. // only nodes whose info matches are included. Otherwise, all nodes
  208. // are included, without trimming.
  209. func (rpt *Report) newGraph(nodes graph.NodeSet) *graph.Graph {
  210. o := rpt.options
  211. // Clean up file paths using heuristics.
  212. prof := rpt.prof
  213. for _, f := range prof.Function {
  214. f.Filename = trimPath(f.Filename)
  215. }
  216. // Removes all numeric tags except for the bytes tag prior
  217. // to making graph.
  218. // TODO: modify to select first numeric tag if no bytes tag
  219. for _, s := range prof.Sample {
  220. numLabels := make(map[string][]int64, len(s.NumLabel))
  221. numUnits := make(map[string][]string, len(s.NumLabel))
  222. for k, vs := range s.NumLabel {
  223. if k == "bytes" {
  224. unit := o.NumLabelUnits[k]
  225. numValues := make([]int64, len(vs))
  226. numUnit := make([]string, len(vs))
  227. for i, v := range vs {
  228. numValues[i] = v
  229. numUnit[i] = unit
  230. }
  231. numLabels[k] = append(numLabels[k], numValues...)
  232. numUnits[k] = append(numUnits[k], numUnit...)
  233. }
  234. }
  235. s.NumLabel = numLabels
  236. s.NumUnit = numUnits
  237. }
  238. formatTag := func(v int64, key string) string {
  239. return measurement.ScaledLabel(v, key, o.OutputUnit)
  240. }
  241. gopt := &graph.Options{
  242. SampleValue: o.SampleValue,
  243. SampleMeanDivisor: o.SampleMeanDivisor,
  244. FormatTag: formatTag,
  245. CallTree: o.CallTree && (o.OutputFormat == Dot || o.OutputFormat == Callgrind),
  246. DropNegative: o.DropNegative,
  247. KeptNodes: nodes,
  248. }
  249. // Only keep binary names for disassembly-based reports, otherwise
  250. // remove it to allow merging of functions across binaries.
  251. switch o.OutputFormat {
  252. case Raw, List, WebList, Dis, Callgrind:
  253. gopt.ObjNames = true
  254. }
  255. return graph.New(rpt.prof, gopt)
  256. }
  257. func printTopProto(w io.Writer, rpt *Report) error {
  258. p := rpt.prof
  259. o := rpt.options
  260. g, _, _, _ := rpt.newTrimmedGraph()
  261. rpt.selectOutputUnit(g)
  262. out := profile.Profile{
  263. SampleType: []*profile.ValueType{
  264. {Type: "cum", Unit: o.OutputUnit},
  265. {Type: "flat", Unit: o.OutputUnit},
  266. },
  267. TimeNanos: p.TimeNanos,
  268. DurationNanos: p.DurationNanos,
  269. PeriodType: p.PeriodType,
  270. Period: p.Period,
  271. }
  272. functionMap := make(functionMap)
  273. for i, n := range g.Nodes {
  274. f := functionMap.FindOrAdd(n.Info)
  275. flat, cum := n.FlatValue(), n.CumValue()
  276. l := &profile.Location{
  277. ID: uint64(i + 1),
  278. Address: n.Info.Address,
  279. Line: []profile.Line{
  280. {
  281. Line: int64(n.Info.Lineno),
  282. Function: f,
  283. },
  284. },
  285. }
  286. fv, _ := measurement.Scale(flat, o.SampleUnit, o.OutputUnit)
  287. cv, _ := measurement.Scale(cum, o.SampleUnit, o.OutputUnit)
  288. s := &profile.Sample{
  289. Location: []*profile.Location{l},
  290. Value: []int64{int64(cv), int64(fv)},
  291. }
  292. out.Function = append(out.Function, f)
  293. out.Location = append(out.Location, l)
  294. out.Sample = append(out.Sample, s)
  295. }
  296. return out.Write(w)
  297. }
  298. type functionMap map[string]*profile.Function
  299. func (fm functionMap) FindOrAdd(ni graph.NodeInfo) *profile.Function {
  300. fName := fmt.Sprintf("%q%q%q%d", ni.Name, ni.OrigName, ni.File, ni.StartLine)
  301. if f := fm[fName]; f != nil {
  302. return f
  303. }
  304. f := &profile.Function{
  305. ID: uint64(len(fm) + 1),
  306. Name: ni.Name,
  307. SystemName: ni.OrigName,
  308. Filename: ni.File,
  309. StartLine: int64(ni.StartLine),
  310. }
  311. fm[fName] = f
  312. return f
  313. }
  314. // printAssembly prints an annotated assembly listing.
  315. func printAssembly(w io.Writer, rpt *Report, obj plugin.ObjTool) error {
  316. return PrintAssembly(w, rpt, obj, -1)
  317. }
  318. // PrintAssembly prints annotated disasssembly of rpt to w.
  319. func PrintAssembly(w io.Writer, rpt *Report, obj plugin.ObjTool, maxFuncs int) error {
  320. o := rpt.options
  321. prof := rpt.prof
  322. g := rpt.newGraph(nil)
  323. // If the regexp source can be parsed as an address, also match
  324. // functions that land on that address.
  325. var address *uint64
  326. if hex, err := strconv.ParseUint(o.Symbol.String(), 0, 64); err == nil {
  327. address = &hex
  328. }
  329. fmt.Fprintln(w, "Total:", rpt.formatValue(rpt.total))
  330. symbols := symbolsFromBinaries(prof, g, o.Symbol, address, obj)
  331. symNodes := nodesPerSymbol(g.Nodes, symbols)
  332. // Sort for printing.
  333. var syms []*objSymbol
  334. for s := range symNodes {
  335. syms = append(syms, s)
  336. }
  337. byName := func(a, b *objSymbol) bool {
  338. if na, nb := a.sym.Name[0], b.sym.Name[0]; na != nb {
  339. return na < nb
  340. }
  341. return a.sym.Start < b.sym.Start
  342. }
  343. if maxFuncs < 0 {
  344. sort.Sort(orderSyms{syms, byName})
  345. } else {
  346. byFlatSum := func(a, b *objSymbol) bool {
  347. suma, _ := symNodes[a].Sum()
  348. sumb, _ := symNodes[b].Sum()
  349. if suma != sumb {
  350. return suma > sumb
  351. }
  352. return byName(a, b)
  353. }
  354. sort.Sort(orderSyms{syms, byFlatSum})
  355. if len(syms) > maxFuncs {
  356. syms = syms[:maxFuncs]
  357. }
  358. }
  359. // Correlate the symbols from the binary with the profile samples.
  360. for _, s := range syms {
  361. sns := symNodes[s]
  362. // Gather samples for this symbol.
  363. flatSum, cumSum := sns.Sum()
  364. // Get the function assembly.
  365. insts, err := obj.Disasm(s.sym.File, s.sym.Start, s.sym.End)
  366. if err != nil {
  367. return err
  368. }
  369. ns := annotateAssembly(insts, sns, s.base)
  370. fmt.Fprintf(w, "ROUTINE ======================== %s\n", s.sym.Name[0])
  371. for _, name := range s.sym.Name[1:] {
  372. fmt.Fprintf(w, " AKA ======================== %s\n", name)
  373. }
  374. fmt.Fprintf(w, "%10s %10s (flat, cum) %s of Total\n",
  375. rpt.formatValue(flatSum), rpt.formatValue(cumSum),
  376. measurement.Percentage(cumSum, rpt.total))
  377. function, file, line := "", "", 0
  378. for _, n := range ns {
  379. locStr := ""
  380. // Skip loc information if it hasn't changed from previous instruction.
  381. if n.function != function || n.file != file || n.line != line {
  382. function, file, line = n.function, n.file, n.line
  383. if n.function != "" {
  384. locStr = n.function + " "
  385. }
  386. if n.file != "" {
  387. locStr += n.file
  388. if n.line != 0 {
  389. locStr += fmt.Sprintf(":%d", n.line)
  390. }
  391. }
  392. }
  393. switch {
  394. case locStr == "":
  395. // No location info, just print the instruction.
  396. fmt.Fprintf(w, "%10s %10s %10x: %s\n",
  397. valueOrDot(n.flatValue(), rpt),
  398. valueOrDot(n.cumValue(), rpt),
  399. n.address, n.instruction,
  400. )
  401. case len(n.instruction) < 40:
  402. // Short instruction, print loc on the same line.
  403. fmt.Fprintf(w, "%10s %10s %10x: %-40s;%s\n",
  404. valueOrDot(n.flatValue(), rpt),
  405. valueOrDot(n.cumValue(), rpt),
  406. n.address, n.instruction,
  407. locStr,
  408. )
  409. default:
  410. // Long instruction, print loc on a separate line.
  411. fmt.Fprintf(w, "%74s;%s\n", "", locStr)
  412. fmt.Fprintf(w, "%10s %10s %10x: %s\n",
  413. valueOrDot(n.flatValue(), rpt),
  414. valueOrDot(n.cumValue(), rpt),
  415. n.address, n.instruction,
  416. )
  417. }
  418. }
  419. }
  420. return nil
  421. }
  422. // symbolsFromBinaries examines the binaries listed on the profile
  423. // that have associated samples, and identifies symbols matching rx.
  424. func symbolsFromBinaries(prof *profile.Profile, g *graph.Graph, rx *regexp.Regexp, address *uint64, obj plugin.ObjTool) []*objSymbol {
  425. hasSamples := make(map[string]bool)
  426. // Only examine mappings that have samples that match the
  427. // regexp. This is an optimization to speed up pprof.
  428. for _, n := range g.Nodes {
  429. if name := n.Info.PrintableName(); rx.MatchString(name) && n.Info.Objfile != "" {
  430. hasSamples[n.Info.Objfile] = true
  431. }
  432. }
  433. // Walk all mappings looking for matching functions with samples.
  434. var objSyms []*objSymbol
  435. for _, m := range prof.Mapping {
  436. if !hasSamples[m.File] {
  437. if address == nil || !(m.Start <= *address && *address <= m.Limit) {
  438. continue
  439. }
  440. }
  441. f, err := obj.Open(m.File, m.Start, m.Limit, m.Offset)
  442. if err != nil {
  443. fmt.Printf("%v\n", err)
  444. continue
  445. }
  446. // Find symbols in this binary matching the user regexp.
  447. var addr uint64
  448. if address != nil {
  449. addr = *address
  450. }
  451. msyms, err := f.Symbols(rx, addr)
  452. base := f.Base()
  453. f.Close()
  454. if err != nil {
  455. continue
  456. }
  457. for _, ms := range msyms {
  458. objSyms = append(objSyms,
  459. &objSymbol{
  460. sym: ms,
  461. base: base,
  462. file: f,
  463. },
  464. )
  465. }
  466. }
  467. return objSyms
  468. }
  469. // objSym represents a symbol identified from a binary. It includes
  470. // the SymbolInfo from the disasm package and the base that must be
  471. // added to correspond to sample addresses
  472. type objSymbol struct {
  473. sym *plugin.Sym
  474. base uint64
  475. file plugin.ObjFile
  476. }
  477. // orderSyms is a wrapper type to sort []*objSymbol by a supplied comparator.
  478. type orderSyms struct {
  479. v []*objSymbol
  480. less func(a, b *objSymbol) bool
  481. }
  482. func (o orderSyms) Len() int { return len(o.v) }
  483. func (o orderSyms) Less(i, j int) bool { return o.less(o.v[i], o.v[j]) }
  484. func (o orderSyms) Swap(i, j int) { o.v[i], o.v[j] = o.v[j], o.v[i] }
  485. // nodesPerSymbol classifies nodes into a group of symbols.
  486. func nodesPerSymbol(ns graph.Nodes, symbols []*objSymbol) map[*objSymbol]graph.Nodes {
  487. symNodes := make(map[*objSymbol]graph.Nodes)
  488. for _, s := range symbols {
  489. // Gather samples for this symbol.
  490. for _, n := range ns {
  491. address := n.Info.Address - s.base
  492. if address >= s.sym.Start && address < s.sym.End {
  493. symNodes[s] = append(symNodes[s], n)
  494. }
  495. }
  496. }
  497. return symNodes
  498. }
  499. type assemblyInstruction struct {
  500. address uint64
  501. instruction string
  502. function string
  503. file string
  504. line int
  505. flat, cum int64
  506. flatDiv, cumDiv int64
  507. startsBlock bool
  508. inlineCalls []callID
  509. }
  510. type callID struct {
  511. file string
  512. line int
  513. }
  514. func (a *assemblyInstruction) flatValue() int64 {
  515. if a.flatDiv != 0 {
  516. return a.flat / a.flatDiv
  517. }
  518. return a.flat
  519. }
  520. func (a *assemblyInstruction) cumValue() int64 {
  521. if a.cumDiv != 0 {
  522. return a.cum / a.cumDiv
  523. }
  524. return a.cum
  525. }
  526. // annotateAssembly annotates a set of assembly instructions with a
  527. // set of samples. It returns a set of nodes to display. base is an
  528. // offset to adjust the sample addresses.
  529. func annotateAssembly(insts []plugin.Inst, samples graph.Nodes, base uint64) []assemblyInstruction {
  530. // Add end marker to simplify printing loop.
  531. insts = append(insts, plugin.Inst{
  532. Addr: ^uint64(0),
  533. })
  534. // Ensure samples are sorted by address.
  535. samples.Sort(graph.AddressOrder)
  536. s := 0
  537. asm := make([]assemblyInstruction, 0, len(insts))
  538. for ix, in := range insts[:len(insts)-1] {
  539. n := assemblyInstruction{
  540. address: in.Addr,
  541. instruction: in.Text,
  542. function: in.Function,
  543. line: in.Line,
  544. }
  545. if in.File != "" {
  546. n.file = filepath.Base(in.File)
  547. }
  548. // Sum all the samples until the next instruction (to account
  549. // for samples attributed to the middle of an instruction).
  550. for next := insts[ix+1].Addr; s < len(samples) && samples[s].Info.Address-base < next; s++ {
  551. sample := samples[s]
  552. n.flatDiv += sample.FlatDiv
  553. n.flat += sample.Flat
  554. n.cumDiv += sample.CumDiv
  555. n.cum += sample.Cum
  556. if f := sample.Info.File; f != "" && n.file == "" {
  557. n.file = filepath.Base(f)
  558. }
  559. if ln := sample.Info.Lineno; ln != 0 && n.line == 0 {
  560. n.line = ln
  561. }
  562. if f := sample.Info.Name; f != "" && n.function == "" {
  563. n.function = f
  564. }
  565. }
  566. asm = append(asm, n)
  567. }
  568. return asm
  569. }
  570. // valueOrDot formats a value according to a report, intercepting zero
  571. // values.
  572. func valueOrDot(value int64, rpt *Report) string {
  573. if value == 0 {
  574. return "."
  575. }
  576. return rpt.formatValue(value)
  577. }
  578. // printTags collects all tags referenced in the profile and prints
  579. // them in a sorted table.
  580. func printTags(w io.Writer, rpt *Report) error {
  581. p := rpt.prof
  582. o := rpt.options
  583. formatTag := func(v int64, key string) string {
  584. return measurement.ScaledLabel(v, key, o.OutputUnit)
  585. }
  586. // Hashtable to keep accumulate tags as key,value,count.
  587. tagMap := make(map[string]map[string]int64)
  588. for _, s := range p.Sample {
  589. for key, vals := range s.Label {
  590. for _, val := range vals {
  591. valueMap, ok := tagMap[key]
  592. if !ok {
  593. valueMap = make(map[string]int64)
  594. tagMap[key] = valueMap
  595. }
  596. valueMap[val] += o.SampleValue(s.Value)
  597. }
  598. }
  599. for key, vals := range s.NumLabel {
  600. unit := o.NumLabelUnits[key]
  601. for _, nval := range vals {
  602. val := formatTag(nval, unit)
  603. valueMap, ok := tagMap[key]
  604. if !ok {
  605. valueMap = make(map[string]int64)
  606. tagMap[key] = valueMap
  607. }
  608. valueMap[val] += o.SampleValue(s.Value)
  609. }
  610. }
  611. }
  612. tagKeys := make([]*graph.Tag, 0, len(tagMap))
  613. for key := range tagMap {
  614. tagKeys = append(tagKeys, &graph.Tag{Name: key})
  615. }
  616. tabw := tabwriter.NewWriter(w, 0, 0, 1, ' ', tabwriter.AlignRight)
  617. for _, tagKey := range graph.SortTags(tagKeys, true) {
  618. var total int64
  619. key := tagKey.Name
  620. tags := make([]*graph.Tag, 0, len(tagMap[key]))
  621. for t, c := range tagMap[key] {
  622. total += c
  623. tags = append(tags, &graph.Tag{Name: t, Flat: c})
  624. }
  625. f, u := measurement.Scale(total, o.SampleUnit, o.OutputUnit)
  626. fmt.Fprintf(tabw, "%s:\t Total %.1f%s\n", key, f, u)
  627. for _, t := range graph.SortTags(tags, true) {
  628. f, u := measurement.Scale(t.FlatValue(), o.SampleUnit, o.OutputUnit)
  629. if total > 0 {
  630. fmt.Fprintf(tabw, " \t%.1f%s (%s):\t %s\n", f, u, measurement.Percentage(t.FlatValue(), total), t.Name)
  631. } else {
  632. fmt.Fprintf(tabw, " \t%.1f%s:\t %s\n", f, u, t.Name)
  633. }
  634. }
  635. fmt.Fprintln(tabw)
  636. }
  637. return tabw.Flush()
  638. }
  639. // printComments prints all freeform comments in the profile.
  640. func printComments(w io.Writer, rpt *Report) error {
  641. p := rpt.prof
  642. for _, c := range p.Comments {
  643. fmt.Fprintln(w, c)
  644. }
  645. return nil
  646. }
  647. // TextItem holds a single text report entry.
  648. type TextItem struct {
  649. Name string
  650. InlineLabel string // Not empty if inlined
  651. Flat, Cum int64 // Raw values
  652. FlatFormat, CumFormat string // Formatted values
  653. }
  654. // TextItems returns a list of text items from the report and a list
  655. // of labels that describe the report.
  656. func TextItems(rpt *Report) ([]TextItem, []string) {
  657. g, origCount, droppedNodes, _ := rpt.newTrimmedGraph()
  658. rpt.selectOutputUnit(g)
  659. labels := reportLabels(rpt, g, origCount, droppedNodes, 0, false)
  660. var items []TextItem
  661. var flatSum int64
  662. for _, n := range g.Nodes {
  663. name, flat, cum := n.Info.PrintableName(), n.FlatValue(), n.CumValue()
  664. var inline, noinline bool
  665. for _, e := range n.In {
  666. if e.Inline {
  667. inline = true
  668. } else {
  669. noinline = true
  670. }
  671. }
  672. var inl string
  673. if inline {
  674. if noinline {
  675. inl = "(partial-inline)"
  676. } else {
  677. inl = "(inline)"
  678. }
  679. }
  680. flatSum += flat
  681. items = append(items, TextItem{
  682. Name: name,
  683. InlineLabel: inl,
  684. Flat: flat,
  685. Cum: cum,
  686. FlatFormat: rpt.formatValue(flat),
  687. CumFormat: rpt.formatValue(cum),
  688. })
  689. }
  690. return items, labels
  691. }
  692. // printText prints a flat text report for a profile.
  693. func printText(w io.Writer, rpt *Report) error {
  694. items, labels := TextItems(rpt)
  695. fmt.Fprintln(w, strings.Join(labels, "\n"))
  696. fmt.Fprintf(w, "%10s %5s%% %5s%% %10s %5s%%\n",
  697. "flat", "flat", "sum", "cum", "cum")
  698. var flatSum int64
  699. for _, item := range items {
  700. inl := item.InlineLabel
  701. if inl != "" {
  702. inl = " " + inl
  703. }
  704. flatSum += item.Flat
  705. fmt.Fprintf(w, "%10s %s %s %10s %s %s%s\n",
  706. item.FlatFormat, measurement.Percentage(item.Flat, rpt.total),
  707. measurement.Percentage(flatSum, rpt.total),
  708. item.CumFormat, measurement.Percentage(item.Cum, rpt.total),
  709. item.Name, inl)
  710. }
  711. return nil
  712. }
  713. // printTraces prints all traces from a profile.
  714. func printTraces(w io.Writer, rpt *Report) error {
  715. fmt.Fprintln(w, strings.Join(ProfileLabels(rpt), "\n"))
  716. prof := rpt.prof
  717. o := rpt.options
  718. const separator = "-----------+-------------------------------------------------------"
  719. _, locations := graph.CreateNodes(prof, &graph.Options{})
  720. for _, sample := range prof.Sample {
  721. var stack graph.Nodes
  722. for _, loc := range sample.Location {
  723. id := loc.ID
  724. stack = append(stack, locations[id]...)
  725. }
  726. if len(stack) == 0 {
  727. continue
  728. }
  729. fmt.Fprintln(w, separator)
  730. // Print any text labels for the sample.
  731. var labels []string
  732. for s, vs := range sample.Label {
  733. labels = append(labels, fmt.Sprintf("%10s: %s\n", s, strings.Join(vs, " ")))
  734. }
  735. sort.Strings(labels)
  736. fmt.Fprint(w, strings.Join(labels, ""))
  737. // Print any numeric labels for the sample
  738. var numLabels []string
  739. for key, vals := range sample.NumLabel {
  740. unit := o.NumLabelUnits[key]
  741. numValues := make([]string, len(vals))
  742. for i, vv := range vals {
  743. numValues[i] = measurement.Label(vv, unit)
  744. }
  745. numLabels = append(numLabels, fmt.Sprintf("%10s: %s\n", key, strings.Join(numValues, " ")))
  746. }
  747. sort.Strings(numLabels)
  748. fmt.Fprint(w, strings.Join(numLabels, ""))
  749. var d, v int64
  750. v = o.SampleValue(sample.Value)
  751. if o.SampleMeanDivisor != nil {
  752. d = o.SampleMeanDivisor(sample.Value)
  753. }
  754. // Print call stack.
  755. if d != 0 {
  756. v = v / d
  757. }
  758. fmt.Fprintf(w, "%10s %s\n",
  759. rpt.formatValue(v), stack[0].Info.PrintableName())
  760. for _, s := range stack[1:] {
  761. fmt.Fprintf(w, "%10s %s\n", "", s.Info.PrintableName())
  762. }
  763. }
  764. fmt.Fprintln(w, separator)
  765. return nil
  766. }
  767. // printCallgrind prints a graph for a profile on callgrind format.
  768. func printCallgrind(w io.Writer, rpt *Report) error {
  769. o := rpt.options
  770. rpt.options.NodeFraction = 0
  771. rpt.options.EdgeFraction = 0
  772. rpt.options.NodeCount = 0
  773. g, _, _, _ := rpt.newTrimmedGraph()
  774. rpt.selectOutputUnit(g)
  775. nodeNames := getDisambiguatedNames(g)
  776. fmt.Fprintln(w, "positions: instr line")
  777. fmt.Fprintln(w, "events:", o.SampleType+"("+o.OutputUnit+")")
  778. objfiles := make(map[string]int)
  779. files := make(map[string]int)
  780. names := make(map[string]int)
  781. // prevInfo points to the previous NodeInfo.
  782. // It is used to group cost lines together as much as possible.
  783. var prevInfo *graph.NodeInfo
  784. for _, n := range g.Nodes {
  785. if prevInfo == nil || n.Info.Objfile != prevInfo.Objfile || n.Info.File != prevInfo.File || n.Info.Name != prevInfo.Name {
  786. fmt.Fprintln(w)
  787. fmt.Fprintln(w, "ob="+callgrindName(objfiles, n.Info.Objfile))
  788. fmt.Fprintln(w, "fl="+callgrindName(files, n.Info.File))
  789. fmt.Fprintln(w, "fn="+callgrindName(names, n.Info.Name))
  790. }
  791. addr := callgrindAddress(prevInfo, n.Info.Address)
  792. sv, _ := measurement.Scale(n.FlatValue(), o.SampleUnit, o.OutputUnit)
  793. fmt.Fprintf(w, "%s %d %d\n", addr, n.Info.Lineno, int64(sv))
  794. // Print outgoing edges.
  795. for _, out := range n.Out.Sort() {
  796. c, _ := measurement.Scale(out.Weight, o.SampleUnit, o.OutputUnit)
  797. callee := out.Dest
  798. fmt.Fprintln(w, "cfl="+callgrindName(files, callee.Info.File))
  799. fmt.Fprintln(w, "cfn="+callgrindName(names, nodeNames[callee]))
  800. // pprof doesn't have a flat weight for a call, leave as 0.
  801. fmt.Fprintf(w, "calls=0 %s %d\n", callgrindAddress(prevInfo, callee.Info.Address), callee.Info.Lineno)
  802. // TODO: This address may be in the middle of a call
  803. // instruction. It would be best to find the beginning
  804. // of the instruction, but the tools seem to handle
  805. // this OK.
  806. fmt.Fprintf(w, "* * %d\n", int64(c))
  807. }
  808. prevInfo = &n.Info
  809. }
  810. return nil
  811. }
  812. // getDisambiguatedNames returns a map from each node in the graph to
  813. // the name to use in the callgrind output. Callgrind merges all
  814. // functions with the same [file name, function name]. Add a [%d/n]
  815. // suffix to disambiguate nodes with different values of
  816. // node.Function, which we want to keep separate. In particular, this
  817. // affects graphs created with --call_tree, where nodes from different
  818. // contexts are associated to different Functions.
  819. func getDisambiguatedNames(g *graph.Graph) map[*graph.Node]string {
  820. nodeName := make(map[*graph.Node]string, len(g.Nodes))
  821. type names struct {
  822. file, function string
  823. }
  824. // nameFunctionIndex maps the callgrind names (filename, function)
  825. // to the node.Function values found for that name, and each
  826. // node.Function value to a sequential index to be used on the
  827. // disambiguated name.
  828. nameFunctionIndex := make(map[names]map[*graph.Node]int)
  829. for _, n := range g.Nodes {
  830. nm := names{n.Info.File, n.Info.Name}
  831. p, ok := nameFunctionIndex[nm]
  832. if !ok {
  833. p = make(map[*graph.Node]int)
  834. nameFunctionIndex[nm] = p
  835. }
  836. if _, ok := p[n.Function]; !ok {
  837. p[n.Function] = len(p)
  838. }
  839. }
  840. for _, n := range g.Nodes {
  841. nm := names{n.Info.File, n.Info.Name}
  842. nodeName[n] = n.Info.Name
  843. if p := nameFunctionIndex[nm]; len(p) > 1 {
  844. // If there is more than one function, add suffix to disambiguate.
  845. nodeName[n] += fmt.Sprintf(" [%d/%d]", p[n.Function]+1, len(p))
  846. }
  847. }
  848. return nodeName
  849. }
  850. // callgrindName implements the callgrind naming compression scheme.
  851. // For names not previously seen returns "(N) name", where N is a
  852. // unique index. For names previously seen returns "(N)" where N is
  853. // the index returned the first time.
  854. func callgrindName(names map[string]int, name string) string {
  855. if name == "" {
  856. return ""
  857. }
  858. if id, ok := names[name]; ok {
  859. return fmt.Sprintf("(%d)", id)
  860. }
  861. id := len(names) + 1
  862. names[name] = id
  863. return fmt.Sprintf("(%d) %s", id, name)
  864. }
  865. // callgrindAddress implements the callgrind subposition compression scheme if
  866. // possible. If prevInfo != nil, it contains the previous address. The current
  867. // address can be given relative to the previous address, with an explicit +/-
  868. // to indicate it is relative, or * for the same address.
  869. func callgrindAddress(prevInfo *graph.NodeInfo, curr uint64) string {
  870. abs := fmt.Sprintf("%#x", curr)
  871. if prevInfo == nil {
  872. return abs
  873. }
  874. prev := prevInfo.Address
  875. if prev == curr {
  876. return "*"
  877. }
  878. diff := int64(curr - prev)
  879. relative := fmt.Sprintf("%+d", diff)
  880. // Only bother to use the relative address if it is actually shorter.
  881. if len(relative) < len(abs) {
  882. return relative
  883. }
  884. return abs
  885. }
  886. // printTree prints a tree-based report in text form.
  887. func printTree(w io.Writer, rpt *Report) error {
  888. const separator = "----------------------------------------------------------+-------------"
  889. const legend = " flat flat% sum% cum cum% calls calls% + context "
  890. g, origCount, droppedNodes, _ := rpt.newTrimmedGraph()
  891. rpt.selectOutputUnit(g)
  892. fmt.Fprintln(w, strings.Join(reportLabels(rpt, g, origCount, droppedNodes, 0, false), "\n"))
  893. fmt.Fprintln(w, separator)
  894. fmt.Fprintln(w, legend)
  895. var flatSum int64
  896. rx := rpt.options.Symbol
  897. for _, n := range g.Nodes {
  898. name, flat, cum := n.Info.PrintableName(), n.FlatValue(), n.CumValue()
  899. // Skip any entries that do not match the regexp (for the "peek" command).
  900. if rx != nil && !rx.MatchString(name) {
  901. continue
  902. }
  903. fmt.Fprintln(w, separator)
  904. // Print incoming edges.
  905. inEdges := n.In.Sort()
  906. for _, in := range inEdges {
  907. var inline string
  908. if in.Inline {
  909. inline = " (inline)"
  910. }
  911. fmt.Fprintf(w, "%50s %s | %s%s\n", rpt.formatValue(in.Weight),
  912. measurement.Percentage(in.Weight, cum), in.Src.Info.PrintableName(), inline)
  913. }
  914. // Print current node.
  915. flatSum += flat
  916. fmt.Fprintf(w, "%10s %s %s %10s %s | %s\n",
  917. rpt.formatValue(flat),
  918. measurement.Percentage(flat, rpt.total),
  919. measurement.Percentage(flatSum, rpt.total),
  920. rpt.formatValue(cum),
  921. measurement.Percentage(cum, rpt.total),
  922. name)
  923. // Print outgoing edges.
  924. outEdges := n.Out.Sort()
  925. for _, out := range outEdges {
  926. var inline string
  927. if out.Inline {
  928. inline = " (inline)"
  929. }
  930. fmt.Fprintf(w, "%50s %s | %s%s\n", rpt.formatValue(out.Weight),
  931. measurement.Percentage(out.Weight, cum), out.Dest.Info.PrintableName(), inline)
  932. }
  933. }
  934. if len(g.Nodes) > 0 {
  935. fmt.Fprintln(w, separator)
  936. }
  937. return nil
  938. }
  939. // GetDOT returns a graph suitable for dot processing along with some
  940. // configuration information.
  941. func GetDOT(rpt *Report) (*graph.Graph, *graph.DotConfig) {
  942. g, origCount, droppedNodes, droppedEdges := rpt.newTrimmedGraph()
  943. rpt.selectOutputUnit(g)
  944. labels := reportLabels(rpt, g, origCount, droppedNodes, droppedEdges, true)
  945. c := &graph.DotConfig{
  946. Title: rpt.options.Title,
  947. Labels: labels,
  948. FormatValue: rpt.formatValue,
  949. Total: rpt.total,
  950. }
  951. return g, c
  952. }
  953. // printDOT prints an annotated callgraph in DOT format.
  954. func printDOT(w io.Writer, rpt *Report) error {
  955. g, c := GetDOT(rpt)
  956. graph.ComposeDot(w, g, &graph.DotAttributes{}, c)
  957. return nil
  958. }
  959. // ProfileLabels returns printable labels for a profile.
  960. func ProfileLabels(rpt *Report) []string {
  961. label := []string{}
  962. prof := rpt.prof
  963. o := rpt.options
  964. if len(prof.Mapping) > 0 {
  965. if prof.Mapping[0].File != "" {
  966. label = append(label, "File: "+filepath.Base(prof.Mapping[0].File))
  967. }
  968. if prof.Mapping[0].BuildID != "" {
  969. label = append(label, "Build ID: "+prof.Mapping[0].BuildID)
  970. }
  971. }
  972. // Only include comments that do not start with '#'.
  973. for _, c := range prof.Comments {
  974. if !strings.HasPrefix(c, "#") {
  975. label = append(label, c)
  976. }
  977. }
  978. if o.SampleType != "" {
  979. label = append(label, "Type: "+o.SampleType)
  980. }
  981. if prof.TimeNanos != 0 {
  982. const layout = "Jan 2, 2006 at 3:04pm (MST)"
  983. label = append(label, "Time: "+time.Unix(0, prof.TimeNanos).Format(layout))
  984. }
  985. if prof.DurationNanos != 0 {
  986. duration := measurement.Label(prof.DurationNanos, "nanoseconds")
  987. totalNanos, totalUnit := measurement.Scale(rpt.total, o.SampleUnit, "nanoseconds")
  988. var ratio string
  989. if totalUnit == "ns" && totalNanos != 0 {
  990. ratio = "(" + measurement.Percentage(int64(totalNanos), prof.DurationNanos) + ")"
  991. }
  992. label = append(label, fmt.Sprintf("Duration: %s, Total samples = %s %s", duration, rpt.formatValue(rpt.total), ratio))
  993. }
  994. return label
  995. }
  996. // reportLabels returns printable labels for a report. Includes
  997. // profileLabels.
  998. func reportLabels(rpt *Report, g *graph.Graph, origCount, droppedNodes, droppedEdges int, fullHeaders bool) []string {
  999. nodeFraction := rpt.options.NodeFraction
  1000. edgeFraction := rpt.options.EdgeFraction
  1001. nodeCount := len(g.Nodes)
  1002. var label []string
  1003. if len(rpt.options.ProfileLabels) > 0 {
  1004. label = append(label, rpt.options.ProfileLabels...)
  1005. } else if fullHeaders || !rpt.options.CompactLabels {
  1006. label = ProfileLabels(rpt)
  1007. }
  1008. var flatSum int64
  1009. for _, n := range g.Nodes {
  1010. flatSum = flatSum + n.FlatValue()
  1011. }
  1012. if len(rpt.options.ActiveFilters) > 0 {
  1013. activeFilters := legendActiveFilters(rpt.options.ActiveFilters)
  1014. label = append(label, activeFilters...)
  1015. }
  1016. label = append(label, fmt.Sprintf("Showing nodes accounting for %s, %s of %s total", rpt.formatValue(flatSum), strings.TrimSpace(measurement.Percentage(flatSum, rpt.total)), rpt.formatValue(rpt.total)))
  1017. if rpt.total != 0 {
  1018. if droppedNodes > 0 {
  1019. label = append(label, genLabel(droppedNodes, "node", "cum",
  1020. rpt.formatValue(abs64(int64(float64(rpt.total)*nodeFraction)))))
  1021. }
  1022. if droppedEdges > 0 {
  1023. label = append(label, genLabel(droppedEdges, "edge", "freq",
  1024. rpt.formatValue(abs64(int64(float64(rpt.total)*edgeFraction)))))
  1025. }
  1026. if nodeCount > 0 && nodeCount < origCount {
  1027. label = append(label, fmt.Sprintf("Showing top %d nodes out of %d",
  1028. nodeCount, origCount))
  1029. }
  1030. }
  1031. return label
  1032. }
  1033. func legendActiveFilters(activeFilters []string) []string {
  1034. legendActiveFilters := make([]string, len(activeFilters)+1)
  1035. legendActiveFilters[0] = "Active filters:"
  1036. for i, s := range activeFilters {
  1037. if len(s) > 80 {
  1038. s = s[:80] + "…"
  1039. }
  1040. legendActiveFilters[i+1] = " " + s
  1041. }
  1042. return legendActiveFilters
  1043. }
  1044. func genLabel(d int, n, l, f string) string {
  1045. if d > 1 {
  1046. n = n + "s"
  1047. }
  1048. return fmt.Sprintf("Dropped %d %s (%s <= %s)", d, n, l, f)
  1049. }
  1050. // New builds a new report indexing the sample values interpreting the
  1051. // samples with the provided function.
  1052. func New(prof *profile.Profile, o *Options) *Report {
  1053. format := func(v int64) string {
  1054. if r := o.Ratio; r > 0 && r != 1 {
  1055. fv := float64(v) * r
  1056. v = int64(fv)
  1057. }
  1058. return measurement.ScaledLabel(v, o.SampleUnit, o.OutputUnit)
  1059. }
  1060. return &Report{prof, computeTotal(prof, o.SampleValue, o.SampleMeanDivisor, !o.PositivePercentages),
  1061. o, format}
  1062. }
  1063. // NewDefault builds a new report indexing the last sample value
  1064. // available.
  1065. func NewDefault(prof *profile.Profile, options Options) *Report {
  1066. index := len(prof.SampleType) - 1
  1067. o := &options
  1068. if o.Title == "" && len(prof.Mapping) > 0 && prof.Mapping[0].File != "" {
  1069. o.Title = filepath.Base(prof.Mapping[0].File)
  1070. }
  1071. o.SampleType = prof.SampleType[index].Type
  1072. o.SampleUnit = strings.ToLower(prof.SampleType[index].Unit)
  1073. o.SampleValue = func(v []int64) int64 {
  1074. return v[index]
  1075. }
  1076. return New(prof, o)
  1077. }
  1078. // computeTotal computes the sum of all sample values. This will be
  1079. // used to compute percentages. If includeNegative is set, use use
  1080. // absolute values to provide a meaningful percentage for both
  1081. // negative and positive values. Otherwise only use positive values,
  1082. // which is useful when comparing profiles from different jobs.
  1083. func computeTotal(prof *profile.Profile, value, meanDiv func(v []int64) int64, includeNegative bool) int64 {
  1084. var div, ret int64
  1085. for _, sample := range prof.Sample {
  1086. var d, v int64
  1087. v = value(sample.Value)
  1088. if meanDiv != nil {
  1089. d = meanDiv(sample.Value)
  1090. }
  1091. if v >= 0 {
  1092. ret += v
  1093. div += d
  1094. } else if includeNegative {
  1095. ret -= v
  1096. div += d
  1097. }
  1098. }
  1099. if div != 0 {
  1100. return ret / div
  1101. }
  1102. return ret
  1103. }
  1104. // Report contains the data and associated routines to extract a
  1105. // report from a profile.
  1106. type Report struct {
  1107. prof *profile.Profile
  1108. total int64
  1109. options *Options
  1110. formatValue func(int64) string
  1111. }
  1112. // Total returns the total number of samples in a report.
  1113. func (rpt *Report) Total() int64 { return rpt.total }
  1114. func abs64(i int64) int64 {
  1115. if i < 0 {
  1116. return -i
  1117. }
  1118. return i
  1119. }