6
6
//
7
7
// Usage:
8
8
//
9
- // go tool preprofile [-v] [-o output] [ -i (pprof) input]
9
+ // go tool preprofile [-v] [-o output] -i input
10
10
//
11
11
//
12
12
@@ -32,45 +32,50 @@ import (
32
32
// in current Go Compiler.
33
33
// The format of the pre-processed output is as follows.
34
34
//
35
- // Header
36
- // caller_name
35
+ // Header
36
+ // caller_name
37
37
// callee_name
38
38
// "call site offset" "call edge weight"
39
39
// ...
40
- // caller_name
40
+ // caller_name
41
41
// callee_name
42
42
// "call site offset" "call edge weight"
43
43
44
44
func usage () {
45
- fmt .Fprintf (os .Stderr , "MUST have (pprof) input file \n " )
46
- fmt .Fprintf (os .Stderr , "usage: go tool preprofile [-v] [-o output] [-i (pprof)input] \n \n " )
45
+ fmt .Fprintf (os .Stderr , "usage: go tool preprofile [-v] [-o output] -i input\n \n " )
47
46
flag .PrintDefaults ()
48
47
os .Exit (2 )
49
48
}
50
49
50
+ var (
51
+ output = flag .String ("o" , "" , "output file path" )
52
+ input = flag .String ("i" , "" , "input pprof file path" )
53
+ verbose = flag .Bool ("v" , false , "enable verbose logging" )
54
+ )
55
+
51
56
type NodeMapKey struct {
52
57
CallerName string
53
58
CalleeName string
54
59
CallSiteOffset int // Line offset from function start line.
55
60
}
56
61
57
- func readPprofFile (profileFile string , outputFile string , verbose bool ) bool {
62
+ func preprocess (profileFile string , outputFile string , verbose bool ) error {
58
63
// open the pprof profile file
59
64
f , err := os .Open (profileFile )
60
65
if err != nil {
61
- log .Fatal ("failed to open file " + profileFile )
62
- return false
66
+ return fmt .Errorf ("error opening profile: %w" , err )
63
67
}
64
68
defer f .Close ()
65
69
p , err := profile .Parse (f )
66
70
if err != nil {
67
- log .Fatal ("failed to Parse profile file." )
68
- return false
71
+ return fmt .Errorf ("error parsing profile: %w" , err )
69
72
}
70
73
71
74
if len (p .Sample ) == 0 {
72
75
// We accept empty profiles, but there is nothing to do.
73
- return false
76
+ //
77
+ // TODO(prattmic): write an "empty" preprocessed file.
78
+ return nil
74
79
}
75
80
76
81
valueIndex := - 1
@@ -85,8 +90,7 @@ func readPprofFile(profileFile string, outputFile string, verbose bool) bool {
85
90
}
86
91
87
92
if valueIndex == - 1 {
88
- log .Fatal ("failed to find CPU samples count or CPU nanoseconds value-types in profile." )
89
- return false
93
+ return fmt .Errorf ("failed to find CPU samples count or CPU nanoseconds value-types in profile." )
90
94
}
91
95
92
96
// The processing here is equivalent to cmd/compile/internal/pgo.createNamedEdgeMap.
@@ -131,16 +135,15 @@ func readPprofFile(profileFile string, outputFile string, verbose bool) bool {
131
135
dirPath := filepath .Dir (outputFile )
132
136
_ , err := os .Stat (dirPath )
133
137
if err != nil {
134
- log . Fatal ( "Directory does not exist: " , dirPath )
138
+ return fmt . Errorf ( "directory does not exist: %s " , dirPath )
135
139
}
136
140
base := filepath .Base (outputFile )
137
141
outputFile = filepath .Join (dirPath , base )
138
142
139
143
// write out NodeMap to a file
140
144
fNodeMap , err = os .Create (outputFile )
141
145
if err != nil {
142
- log .Fatal ("Error creating output file:" , err )
143
- return false
146
+ return fmt .Errorf ("Error creating output file: %w" , err )
144
147
}
145
148
146
149
defer fNodeMap .Close () // Close the file when done writing
@@ -162,26 +165,21 @@ func readPprofFile(profileFile string, outputFile string, verbose bool) bool {
162
165
count += 1
163
166
}
164
167
165
- if TotalEdgeWeight == 0 {
166
- return false
167
- }
168
-
169
- return true
168
+ return nil
170
169
}
171
170
172
- var dumpCode = flag .String ("o" , "" , "dump output file " )
173
- var input = flag .String ("i" , "" , "input pprof file " )
174
- var verbose = flag .Bool ("v" , false , "verbose log" )
175
-
176
171
func main () {
177
172
log .SetFlags (0 )
178
173
log .SetPrefix ("preprofile: " )
179
174
180
175
flag .Usage = usage
181
176
flag .Parse ()
182
177
if * input == "" {
178
+ log .Print ("Input pprof path required (-i)" )
183
179
usage ()
184
- } else {
185
- readPprofFile (* input , * dumpCode , * verbose )
180
+ }
181
+
182
+ if err := preprocess (* input , * output , * verbose ); err != nil {
183
+ log .Fatal (err )
186
184
}
187
185
}
0 commit comments