15
15
package driver
16
16
17
17
import (
18
+ "errors"
18
19
"fmt"
19
20
"os"
20
21
"strings"
@@ -28,6 +29,7 @@ type source struct {
28
29
ExecName string
29
30
BuildID string
30
31
Base []string
32
+ DiffBase bool
31
33
Normalize bool
32
34
33
35
Seconds int
@@ -43,7 +45,8 @@ type source struct {
43
45
func parseFlags (o * plugin.Options ) (* source , []string , error ) {
44
46
flag := o .Flagset
45
47
// Comparisons.
46
- flagBase := flag .StringList ("base" , "" , "Source for base profile for comparison" )
48
+ flagBase := flag .StringList ("base" , "" , "Source for base profile for profile subtraction" )
49
+ flagDiffBase := flag .StringList ("diff_base" , "" , "Source for diff base profile for comparison" )
47
50
// Source options.
48
51
flagSymbolize := flag .String ("symbolize" , "" , "Options for profile symbolization" )
49
52
flagBuildID := flag .String ("buildid" , "" , "Override build id for first mapping" )
@@ -85,7 +88,7 @@ func parseFlags(o *plugin.Options) (*source, []string, error) {
85
88
usageMsgVars )
86
89
})
87
90
if len (args ) == 0 {
88
- return nil , nil , fmt . Errorf ("no profile source specified" )
91
+ return nil , nil , errors . New ("no profile source specified" )
89
92
}
90
93
91
94
var execName string
@@ -112,7 +115,7 @@ func parseFlags(o *plugin.Options) (*source, []string, error) {
112
115
return nil , nil , err
113
116
}
114
117
if cmd != nil && * flagHTTP != "" {
115
- return nil , nil , fmt . Errorf ("-http is not compatible with an output format on the command line" )
118
+ return nil , nil , errors . New ("-http is not compatible with an output format on the command line" )
116
119
}
117
120
118
121
si := pprofVariables ["sample_index" ].value
@@ -140,15 +143,13 @@ func parseFlags(o *plugin.Options) (*source, []string, error) {
140
143
Comment : * flagAddComment ,
141
144
}
142
145
143
- for _ , s := range * flagBase {
144
- if * s != "" {
145
- source .Base = append (source .Base , * s )
146
- }
146
+ if err := source .addBaseProfiles (* flagBase , * flagDiffBase ); err != nil {
147
+ return nil , nil , err
147
148
}
148
149
149
150
normalize := pprofVariables ["normalize" ].boolValue ()
150
151
if normalize && len (source .Base ) == 0 {
151
- return nil , nil , fmt . Errorf ( "Must have base profile to normalize by" )
152
+ return nil , nil , errors . New ( "must have base profile to normalize by" )
152
153
}
153
154
source .Normalize = normalize
154
155
@@ -158,6 +159,34 @@ func parseFlags(o *plugin.Options) (*source, []string, error) {
158
159
return source , cmd , nil
159
160
}
160
161
162
+ // addBaseProfiles adds the list of base profiles or diff base profiles to
163
+ // the source. This function will return an error if both base and diff base
164
+ // profiles are specified.
165
+ func (source * source ) addBaseProfiles (flagBase , flagDiffBase []* string ) error {
166
+ base , diffBase := dropEmpty (flagBase ), dropEmpty (flagDiffBase )
167
+ if len (base ) > 0 && len (diffBase ) > 0 {
168
+ return errors .New ("-base and -diff_base flags cannot both be specified" )
169
+ }
170
+
171
+ source .Base = base
172
+ if len (diffBase ) > 0 {
173
+ source .Base , source .DiffBase = diffBase , true
174
+ }
175
+ return nil
176
+ }
177
+
178
+ // dropEmpty list takes a slice of string pointers, and outputs a slice of
179
+ // non-empty strings associated with the flag.
180
+ func dropEmpty (list []* string ) []string {
181
+ var l []string
182
+ for _ , s := range list {
183
+ if * s != "" {
184
+ l = append (l , * s )
185
+ }
186
+ }
187
+ return l
188
+ }
189
+
161
190
// installFlags creates command line flags for pprof variables.
162
191
func installFlags (flag plugin.FlagSet ) flagsInstalled {
163
192
f := flagsInstalled {
@@ -240,15 +269,15 @@ func outputFormat(bcmd map[string]*bool, acmd map[string]*string) (cmd []string,
240
269
for n , b := range bcmd {
241
270
if * b {
242
271
if cmd != nil {
243
- return nil , fmt . Errorf ("must set at most one output format" )
272
+ return nil , errors . New ("must set at most one output format" )
244
273
}
245
274
cmd = []string {n }
246
275
}
247
276
}
248
277
for n , s := range acmd {
249
278
if * s != "" {
250
279
if cmd != nil {
251
- return nil , fmt . Errorf ("must set at most one output format" )
280
+ return nil , errors . New ("must set at most one output format" )
252
281
}
253
282
cmd = []string {n , * s }
254
283
}
0 commit comments