Source file src/cmd/vendor/github.com/google/pprof/internal/driver/cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package driver
16
17 import (
18 "errors"
19 "fmt"
20 "os"
21 "strings"
22
23 "github.com/google/pprof/internal/binutils"
24 "github.com/google/pprof/internal/plugin"
25 )
26
27 type source struct {
28 Sources []string
29 ExecName string
30 BuildID string
31 Base []string
32 DiffBase bool
33 Normalize bool
34
35 Seconds int
36 Timeout int
37 Symbolize string
38 HTTPHostport string
39 HTTPDisableBrowser bool
40 Comment string
41 }
42
43
44
45
46 func parseFlags(o *plugin.Options) (*source, []string, error) {
47 flag := o.Flagset
48
49 flagDiffBase := flag.StringList("diff_base", "", "Source of base profile for comparison")
50 flagBase := flag.StringList("base", "", "Source of base profile for profile subtraction")
51
52 flagSymbolize := flag.String("symbolize", "", "Options for profile symbolization")
53 flagBuildID := flag.String("buildid", "", "Override build id for first mapping")
54 flagTimeout := flag.Int("timeout", -1, "Timeout in seconds for fetching a profile")
55 flagAddComment := flag.String("add_comment", "", "Annotation string to record in the profile")
56
57 flagSeconds := flag.Int("seconds", -1, "Length of time for dynamic profiles")
58
59 flagInUseSpace := flag.Bool("inuse_space", false, "Display in-use memory size")
60 flagInUseObjects := flag.Bool("inuse_objects", false, "Display in-use object counts")
61 flagAllocSpace := flag.Bool("alloc_space", false, "Display allocated memory size")
62 flagAllocObjects := flag.Bool("alloc_objects", false, "Display allocated object counts")
63
64 flagTotalDelay := flag.Bool("total_delay", false, "Display total delay at each region")
65 flagContentions := flag.Bool("contentions", false, "Display number of delays at each region")
66 flagMeanDelay := flag.Bool("mean_delay", false, "Display mean delay at each region")
67 flagTools := flag.String("tools", os.Getenv("PPROF_TOOLS"), "Path for object tool pathnames")
68
69 flagHTTP := flag.String("http", "", "Present interactive web UI at the specified http host:port")
70 flagNoBrowser := flag.Bool("no_browser", false, "Skip opening a browswer for the interactive web UI")
71
72
73 installedFlags := installFlags(flag)
74
75 flagCommands := make(map[string]*bool)
76 flagParamCommands := make(map[string]*string)
77 for name, cmd := range pprofCommands {
78 if cmd.hasParam {
79 flagParamCommands[name] = flag.String(name, "", "Generate a report in "+name+" format, matching regexp")
80 } else {
81 flagCommands[name] = flag.Bool(name, false, "Generate a report in "+name+" format")
82 }
83 }
84
85 args := flag.Parse(func() {
86 o.UI.Print(usageMsgHdr +
87 usage(true) +
88 usageMsgSrc +
89 flag.ExtraUsage() +
90 usageMsgVars)
91 })
92 if len(args) == 0 {
93 return nil, nil, errors.New("no profile source specified")
94 }
95
96 var execName string
97
98 if len(args) > 1 {
99 arg0 := args[0]
100 if file, err := o.Obj.Open(arg0, 0, ^uint64(0), 0); err == nil {
101 file.Close()
102 execName = arg0
103 args = args[1:]
104 } else if *flagBuildID == "" && isBuildID(arg0) {
105 *flagBuildID = arg0
106 args = args[1:]
107 }
108 }
109
110
111 if err := updateFlags(installedFlags); err != nil {
112 return nil, nil, err
113 }
114
115 cmd, err := outputFormat(flagCommands, flagParamCommands)
116 if err != nil {
117 return nil, nil, err
118 }
119 if cmd != nil && *flagHTTP != "" {
120 return nil, nil, errors.New("-http is not compatible with an output format on the command line")
121 }
122
123 if *flagNoBrowser && *flagHTTP == "" {
124 return nil, nil, errors.New("-no_browser only makes sense with -http")
125 }
126
127 si := pprofVariables["sample_index"].value
128 si = sampleIndex(flagTotalDelay, si, "delay", "-total_delay", o.UI)
129 si = sampleIndex(flagMeanDelay, si, "delay", "-mean_delay", o.UI)
130 si = sampleIndex(flagContentions, si, "contentions", "-contentions", o.UI)
131 si = sampleIndex(flagInUseSpace, si, "inuse_space", "-inuse_space", o.UI)
132 si = sampleIndex(flagInUseObjects, si, "inuse_objects", "-inuse_objects", o.UI)
133 si = sampleIndex(flagAllocSpace, si, "alloc_space", "-alloc_space", o.UI)
134 si = sampleIndex(flagAllocObjects, si, "alloc_objects", "-alloc_objects", o.UI)
135 pprofVariables.set("sample_index", si)
136
137 if *flagMeanDelay {
138 pprofVariables.set("mean", "true")
139 }
140
141 source := &source{
142 Sources: args,
143 ExecName: execName,
144 BuildID: *flagBuildID,
145 Seconds: *flagSeconds,
146 Timeout: *flagTimeout,
147 Symbolize: *flagSymbolize,
148 HTTPHostport: *flagHTTP,
149 HTTPDisableBrowser: *flagNoBrowser,
150 Comment: *flagAddComment,
151 }
152
153 if err := source.addBaseProfiles(*flagBase, *flagDiffBase); err != nil {
154 return nil, nil, err
155 }
156
157 normalize := pprofVariables["normalize"].boolValue()
158 if normalize && len(source.Base) == 0 {
159 return nil, nil, errors.New("must have base profile to normalize by")
160 }
161 source.Normalize = normalize
162
163 if bu, ok := o.Obj.(*binutils.Binutils); ok {
164 bu.SetTools(*flagTools)
165 }
166 return source, cmd, nil
167 }
168
169
170
171
172 func (source *source) addBaseProfiles(flagBase, flagDiffBase []*string) error {
173 base, diffBase := dropEmpty(flagBase), dropEmpty(flagDiffBase)
174 if len(base) > 0 && len(diffBase) > 0 {
175 return errors.New("-base and -diff_base flags cannot both be specified")
176 }
177
178 source.Base = base
179 if len(diffBase) > 0 {
180 source.Base, source.DiffBase = diffBase, true
181 }
182 return nil
183 }
184
185
186
187 func dropEmpty(list []*string) []string {
188 var l []string
189 for _, s := range list {
190 if *s != "" {
191 l = append(l, *s)
192 }
193 }
194 return l
195 }
196
197
198 func installFlags(flag plugin.FlagSet) flagsInstalled {
199 f := flagsInstalled{
200 ints: make(map[string]*int),
201 bools: make(map[string]*bool),
202 floats: make(map[string]*float64),
203 strings: make(map[string]*string),
204 }
205 for n, v := range pprofVariables {
206 switch v.kind {
207 case boolKind:
208 if v.group != "" {
209
210 f.bools[n] = flag.Bool(n, false, v.help)
211 } else {
212 f.bools[n] = flag.Bool(n, v.boolValue(), v.help)
213 }
214 case intKind:
215 f.ints[n] = flag.Int(n, v.intValue(), v.help)
216 case floatKind:
217 f.floats[n] = flag.Float64(n, v.floatValue(), v.help)
218 case stringKind:
219 f.strings[n] = flag.String(n, v.value, v.help)
220 }
221 }
222 return f
223 }
224
225
226
227 func updateFlags(f flagsInstalled) error {
228 vars := pprofVariables
229 groups := map[string]string{}
230 for n, v := range f.bools {
231 vars.set(n, fmt.Sprint(*v))
232 if *v {
233 g := vars[n].group
234 if g != "" && groups[g] != "" {
235 return fmt.Errorf("conflicting options %q and %q set", n, groups[g])
236 }
237 groups[g] = n
238 }
239 }
240 for n, v := range f.ints {
241 vars.set(n, fmt.Sprint(*v))
242 }
243 for n, v := range f.floats {
244 vars.set(n, fmt.Sprint(*v))
245 }
246 for n, v := range f.strings {
247 vars.set(n, *v)
248 }
249 return nil
250 }
251
252 type flagsInstalled struct {
253 ints map[string]*int
254 bools map[string]*bool
255 floats map[string]*float64
256 strings map[string]*string
257 }
258
259
260
261 func isBuildID(id string) bool {
262 return strings.Trim(id, "0123456789abcdefABCDEF") == ""
263 }
264
265 func sampleIndex(flag *bool, si string, sampleType, option string, ui plugin.UI) string {
266 if *flag {
267 if si == "" {
268 return sampleType
269 }
270 ui.PrintErr("Multiple value selections, ignoring ", option)
271 }
272 return si
273 }
274
275 func outputFormat(bcmd map[string]*bool, acmd map[string]*string) (cmd []string, err error) {
276 for n, b := range bcmd {
277 if *b {
278 if cmd != nil {
279 return nil, errors.New("must set at most one output format")
280 }
281 cmd = []string{n}
282 }
283 }
284 for n, s := range acmd {
285 if *s != "" {
286 if cmd != nil {
287 return nil, errors.New("must set at most one output format")
288 }
289 cmd = []string{n, *s}
290 }
291 }
292 return cmd, nil
293 }
294
295 var usageMsgHdr = `usage:
296
297 Produce output in the specified format.
298
299 pprof <format> [options] [binary] <source> ...
300
301 Omit the format to get an interactive shell whose commands can be used
302 to generate various views of a profile
303
304 pprof [options] [binary] <source> ...
305
306 Omit the format and provide the "-http" flag to get an interactive web
307 interface at the specified host:port that can be used to navigate through
308 various views of a profile.
309
310 pprof -http [host]:[port] [options] [binary] <source> ...
311
312 Details:
313 `
314
315 var usageMsgSrc = "\n\n" +
316 " Source options:\n" +
317 " -seconds Duration for time-based profile collection\n" +
318 " -timeout Timeout in seconds for profile collection\n" +
319 " -buildid Override build id for main binary\n" +
320 " -add_comment Free-form annotation to add to the profile\n" +
321 " Displayed on some reports or with pprof -comments\n" +
322 " -diff_base source Source of base profile for comparison\n" +
323 " -base source Source of base profile for profile subtraction\n" +
324 " profile.pb.gz Profile in compressed protobuf format\n" +
325 " legacy_profile Profile in legacy pprof format\n" +
326 " http://host/profile URL for profile handler to retrieve\n" +
327 " -symbolize= Controls source of symbol information\n" +
328 " none Do not attempt symbolization\n" +
329 " local Examine only local binaries\n" +
330 " fastlocal Only get function names from local binaries\n" +
331 " remote Do not examine local binaries\n" +
332 " force Force re-symbolization\n" +
333 " Binary Local path or build id of binary for symbolization\n"
334
335 var usageMsgVars = "\n\n" +
336 " Misc options:\n" +
337 " -http Provide web interface at host:port.\n" +
338 " Host is optional and 'localhost' by default.\n" +
339 " Port is optional and a randomly available port by default.\n" +
340 " -no_browser Skip opening a browser for the interactive web UI.\n" +
341 " -tools Search path for object tools\n" +
342 "\n" +
343 " Legacy convenience options:\n" +
344 " -inuse_space Same as -sample_index=inuse_space\n" +
345 " -inuse_objects Same as -sample_index=inuse_objects\n" +
346 " -alloc_space Same as -sample_index=alloc_space\n" +
347 " -alloc_objects Same as -sample_index=alloc_objects\n" +
348 " -total_delay Same as -sample_index=delay\n" +
349 " -contentions Same as -sample_index=contentions\n" +
350 " -mean_delay Same as -mean -sample_index=delay\n" +
351 "\n" +
352 " Environment Variables:\n" +
353 " PPROF_TMPDIR Location for saved profiles (default $HOME/pprof)\n" +
354 " PPROF_TOOLS Search path for object-level tools\n" +
355 " PPROF_BINARY_PATH Search path for local binary files\n" +
356 " default: $HOME/pprof/binaries\n" +
357 " searches $name, $path, $buildid/$name, $path/$buildid\n" +
358 " * On Windows, %USERPROFILE% is used instead of $HOME"
359
View as plain text