Skip to content

Commit a16b4bf

Browse files
committed
cmd/preprofile: clean up error handling
This CL adjusts error handling to be a bit more idiomatic. The processing function returns errors, leaving main to log and exit on error. This CL contains no functional changes. For #58102. Change-Id: I9074127cc675e177d046474b7f01fbc37d0bd4c0 Reviewed-on: https://go-review.googlesource.com/c/go/+/569335 Reviewed-by: Cherry Mui <cherryyz@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 50dcffb commit a16b4bf

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

src/cmd/preprofile/main.go

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
// Usage:
88
//
9-
// go tool preprofile [-v] [-o output] [-i (pprof)input]
9+
// go tool preprofile [-v] [-o output] -i input
1010
//
1111
//
1212

@@ -32,45 +32,50 @@ import (
3232
// in current Go Compiler.
3333
// The format of the pre-processed output is as follows.
3434
//
35-
// Header
36-
// caller_name
35+
// Header
36+
// caller_name
3737
// callee_name
3838
// "call site offset" "call edge weight"
3939
// ...
40-
// caller_name
40+
// caller_name
4141
// callee_name
4242
// "call site offset" "call edge weight"
4343

4444
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")
4746
flag.PrintDefaults()
4847
os.Exit(2)
4948
}
5049

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+
5156
type NodeMapKey struct {
5257
CallerName string
5358
CalleeName string
5459
CallSiteOffset int // Line offset from function start line.
5560
}
5661

57-
func readPprofFile(profileFile string, outputFile string, verbose bool) bool {
62+
func preprocess(profileFile string, outputFile string, verbose bool) error {
5863
// open the pprof profile file
5964
f, err := os.Open(profileFile)
6065
if err != nil {
61-
log.Fatal("failed to open file " + profileFile)
62-
return false
66+
return fmt.Errorf("error opening profile: %w", err)
6367
}
6468
defer f.Close()
6569
p, err := profile.Parse(f)
6670
if err != nil {
67-
log.Fatal("failed to Parse profile file.")
68-
return false
71+
return fmt.Errorf("error parsing profile: %w", err)
6972
}
7073

7174
if len(p.Sample) == 0 {
7275
// 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
7479
}
7580

7681
valueIndex := -1
@@ -85,8 +90,7 @@ func readPprofFile(profileFile string, outputFile string, verbose bool) bool {
8590
}
8691

8792
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.")
9094
}
9195

9296
// The processing here is equivalent to cmd/compile/internal/pgo.createNamedEdgeMap.
@@ -131,16 +135,15 @@ func readPprofFile(profileFile string, outputFile string, verbose bool) bool {
131135
dirPath := filepath.Dir(outputFile)
132136
_, err := os.Stat(dirPath)
133137
if err != nil {
134-
log.Fatal("Directory does not exist: ", dirPath)
138+
return fmt.Errorf("directory does not exist: %s", dirPath)
135139
}
136140
base := filepath.Base(outputFile)
137141
outputFile = filepath.Join(dirPath, base)
138142

139143
// write out NodeMap to a file
140144
fNodeMap, err = os.Create(outputFile)
141145
if err != nil {
142-
log.Fatal("Error creating output file:", err)
143-
return false
146+
return fmt.Errorf("Error creating output file: %w", err)
144147
}
145148

146149
defer fNodeMap.Close() // Close the file when done writing
@@ -162,26 +165,21 @@ func readPprofFile(profileFile string, outputFile string, verbose bool) bool {
162165
count += 1
163166
}
164167

165-
if TotalEdgeWeight == 0 {
166-
return false
167-
}
168-
169-
return true
168+
return nil
170169
}
171170

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-
176171
func main() {
177172
log.SetFlags(0)
178173
log.SetPrefix("preprofile: ")
179174

180175
flag.Usage = usage
181176
flag.Parse()
182177
if *input == "" {
178+
log.Print("Input pprof path required (-i)")
183179
usage()
184-
} else {
185-
readPprofFile(*input, *dumpCode, *verbose)
180+
}
181+
182+
if err := preprocess(*input, *output, *verbose); err != nil {
183+
log.Fatal(err)
186184
}
187185
}

0 commit comments

Comments
 (0)