You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

331 lines
8.9 KiB

  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 driver implements the core pprof functionality. It can be
  15. // parameterized with a flag implementation, fetch and symbolize
  16. // mechanisms.
  17. package driver
  18. import (
  19. "bytes"
  20. "fmt"
  21. "os"
  22. "path/filepath"
  23. "regexp"
  24. "strings"
  25. "github.com/google/pprof/internal/plugin"
  26. "github.com/google/pprof/internal/report"
  27. "github.com/google/pprof/profile"
  28. )
  29. // PProf acquires a profile, and symbolizes it using a profile
  30. // manager. Then it generates a report formatted according to the
  31. // options selected through the flags package.
  32. func PProf(eo *plugin.Options) error {
  33. // Remove any temporary files created during pprof processing.
  34. defer cleanupTempFiles()
  35. o := setDefaults(eo)
  36. src, cmd, err := parseFlags(o)
  37. if err != nil {
  38. return err
  39. }
  40. p, err := fetchProfiles(src, o)
  41. if err != nil {
  42. return err
  43. }
  44. if cmd != nil {
  45. return generateReport(p, cmd, pprofVariables, o)
  46. }
  47. if src.HTTPHostport != "" {
  48. return serveWebInterface(src.HTTPHostport, p, o, src.HTTPDisableBrowser)
  49. }
  50. return interactive(p, o)
  51. }
  52. func generateRawReport(p *profile.Profile, cmd []string, vars variables, o *plugin.Options) (*command, *report.Report, error) {
  53. p = p.Copy() // Prevent modification to the incoming profile.
  54. // Identify units of numeric tags in profile.
  55. numLabelUnits := identifyNumLabelUnits(p, o.UI)
  56. // Get report output format
  57. c := pprofCommands[cmd[0]]
  58. if c == nil {
  59. panic("unexpected nil command")
  60. }
  61. vars = applyCommandOverrides(cmd[0], c.format, vars)
  62. // Delay focus after configuring report to get percentages on all samples.
  63. relative := vars["relative_percentages"].boolValue()
  64. if relative {
  65. if err := applyFocus(p, numLabelUnits, vars, o.UI); err != nil {
  66. return nil, nil, err
  67. }
  68. }
  69. ropt, err := reportOptions(p, numLabelUnits, vars)
  70. if err != nil {
  71. return nil, nil, err
  72. }
  73. ropt.OutputFormat = c.format
  74. if len(cmd) == 2 {
  75. s, err := regexp.Compile(cmd[1])
  76. if err != nil {
  77. return nil, nil, fmt.Errorf("parsing argument regexp %s: %v", cmd[1], err)
  78. }
  79. ropt.Symbol = s
  80. }
  81. rpt := report.New(p, ropt)
  82. if !relative {
  83. if err := applyFocus(p, numLabelUnits, vars, o.UI); err != nil {
  84. return nil, nil, err
  85. }
  86. }
  87. if err := aggregate(p, vars); err != nil {
  88. return nil, nil, err
  89. }
  90. return c, rpt, nil
  91. }
  92. func generateReport(p *profile.Profile, cmd []string, vars variables, o *plugin.Options) error {
  93. c, rpt, err := generateRawReport(p, cmd, vars, o)
  94. if err != nil {
  95. return err
  96. }
  97. // Generate the report.
  98. dst := new(bytes.Buffer)
  99. if err := report.Generate(dst, rpt, o.Obj); err != nil {
  100. return err
  101. }
  102. src := dst
  103. // If necessary, perform any data post-processing.
  104. if c.postProcess != nil {
  105. dst = new(bytes.Buffer)
  106. if err := c.postProcess(src, dst, o.UI); err != nil {
  107. return err
  108. }
  109. src = dst
  110. }
  111. // If no output is specified, use default visualizer.
  112. output := vars["output"].value
  113. if output == "" {
  114. if c.visualizer != nil {
  115. return c.visualizer(src, os.Stdout, o.UI)
  116. }
  117. _, err := src.WriteTo(os.Stdout)
  118. return err
  119. }
  120. // Output to specified file.
  121. o.UI.PrintErr("Generating report in ", output)
  122. out, err := o.Writer.Open(output)
  123. if err != nil {
  124. return err
  125. }
  126. if _, err := src.WriteTo(out); err != nil {
  127. out.Close()
  128. return err
  129. }
  130. return out.Close()
  131. }
  132. func applyCommandOverrides(cmd string, outputFormat int, v variables) variables {
  133. // Some report types override the trim flag to false below. This is to make
  134. // sure the default heuristics of excluding insignificant nodes and edges
  135. // from the call graph do not apply. One example where it is important is
  136. // annotated source or disassembly listing. Those reports run on a specific
  137. // function (or functions), but the trimming is applied before the function
  138. // data is selected. So, with trimming enabled, the report could end up
  139. // showing no data if the specified function is "uninteresting" as far as the
  140. // trimming is concerned.
  141. trim := v["trim"].boolValue()
  142. switch cmd {
  143. case "disasm", "weblist":
  144. trim = false
  145. v.set("addresses", "t")
  146. // Force the 'noinlines' mode so that source locations for a given address
  147. // collapse and there is only one for the given address. Without this
  148. // cumulative metrics would be double-counted when annotating the assembly.
  149. // This is because the merge is done by address and in case of an inlined
  150. // stack each of the inlined entries is a separate callgraph node.
  151. v.set("noinlines", "t")
  152. case "peek":
  153. trim = false
  154. case "list":
  155. trim = false
  156. v.set("lines", "t")
  157. // Do not force 'noinlines' to be false so that specifying
  158. // "-list foo -noinlines" is supported and works as expected.
  159. case "text", "top", "topproto":
  160. if v["nodecount"].intValue() == -1 {
  161. v.set("nodecount", "0")
  162. }
  163. default:
  164. if v["nodecount"].intValue() == -1 {
  165. v.set("nodecount", "80")
  166. }
  167. }
  168. switch outputFormat {
  169. case report.Proto, report.Raw, report.Callgrind:
  170. trim = false
  171. v.set("addresses", "t")
  172. v.set("noinlines", "f")
  173. }
  174. if !trim {
  175. v.set("nodecount", "0")
  176. v.set("nodefraction", "0")
  177. v.set("edgefraction", "0")
  178. }
  179. return v
  180. }
  181. func aggregate(prof *profile.Profile, v variables) error {
  182. var function, filename, linenumber, address bool
  183. inlines := !v["noinlines"].boolValue()
  184. switch {
  185. case v["addresses"].boolValue():
  186. if inlines {
  187. return nil
  188. }
  189. function = true
  190. filename = true
  191. linenumber = true
  192. address = true
  193. case v["lines"].boolValue():
  194. function = true
  195. filename = true
  196. linenumber = true
  197. case v["files"].boolValue():
  198. filename = true
  199. case v["functions"].boolValue():
  200. function = true
  201. case v["filefunctions"].boolValue():
  202. function = true
  203. filename = true
  204. default:
  205. return fmt.Errorf("unexpected granularity")
  206. }
  207. return prof.Aggregate(inlines, function, filename, linenumber, address)
  208. }
  209. func reportOptions(p *profile.Profile, numLabelUnits map[string]string, vars variables) (*report.Options, error) {
  210. si, mean := vars["sample_index"].value, vars["mean"].boolValue()
  211. value, meanDiv, sample, err := sampleFormat(p, si, mean)
  212. if err != nil {
  213. return nil, err
  214. }
  215. stype := sample.Type
  216. if mean {
  217. stype = "mean_" + stype
  218. }
  219. if vars["divide_by"].floatValue() == 0 {
  220. return nil, fmt.Errorf("zero divisor specified")
  221. }
  222. var filters []string
  223. for _, k := range []string{"focus", "ignore", "hide", "show", "show_from", "tagfocus", "tagignore", "tagshow", "taghide"} {
  224. v := vars[k].value
  225. if v != "" {
  226. filters = append(filters, k+"="+v)
  227. }
  228. }
  229. ropt := &report.Options{
  230. CumSort: vars["cum"].boolValue(),
  231. CallTree: vars["call_tree"].boolValue(),
  232. DropNegative: vars["drop_negative"].boolValue(),
  233. CompactLabels: vars["compact_labels"].boolValue(),
  234. Ratio: 1 / vars["divide_by"].floatValue(),
  235. NodeCount: vars["nodecount"].intValue(),
  236. NodeFraction: vars["nodefraction"].floatValue(),
  237. EdgeFraction: vars["edgefraction"].floatValue(),
  238. ActiveFilters: filters,
  239. NumLabelUnits: numLabelUnits,
  240. SampleValue: value,
  241. SampleMeanDivisor: meanDiv,
  242. SampleType: stype,
  243. SampleUnit: sample.Unit,
  244. OutputUnit: vars["unit"].value,
  245. SourcePath: vars["source_path"].stringValue(),
  246. TrimPath: vars["trim_path"].stringValue(),
  247. }
  248. if len(p.Mapping) > 0 && p.Mapping[0].File != "" {
  249. ropt.Title = filepath.Base(p.Mapping[0].File)
  250. }
  251. return ropt, nil
  252. }
  253. // identifyNumLabelUnits returns a map of numeric label keys to the units
  254. // associated with those keys.
  255. func identifyNumLabelUnits(p *profile.Profile, ui plugin.UI) map[string]string {
  256. numLabelUnits, ignoredUnits := p.NumLabelUnits()
  257. // Print errors for tags with multiple units associated with
  258. // a single key.
  259. for k, units := range ignoredUnits {
  260. ui.PrintErr(fmt.Sprintf("For tag %s used unit %s, also encountered unit(s) %s", k, numLabelUnits[k], strings.Join(units, ", ")))
  261. }
  262. return numLabelUnits
  263. }
  264. type sampleValueFunc func([]int64) int64
  265. // sampleFormat returns a function to extract values out of a profile.Sample,
  266. // and the type/units of those values.
  267. func sampleFormat(p *profile.Profile, sampleIndex string, mean bool) (value, meanDiv sampleValueFunc, v *profile.ValueType, err error) {
  268. if len(p.SampleType) == 0 {
  269. return nil, nil, nil, fmt.Errorf("profile has no samples")
  270. }
  271. index, err := p.SampleIndexByName(sampleIndex)
  272. if err != nil {
  273. return nil, nil, nil, err
  274. }
  275. value = valueExtractor(index)
  276. if mean {
  277. meanDiv = valueExtractor(0)
  278. }
  279. v = p.SampleType[index]
  280. return
  281. }
  282. func valueExtractor(ix int) sampleValueFunc {
  283. return func(v []int64) int64 {
  284. return v[ix]
  285. }
  286. }