Add --memprofile flag

This commit is contained in:
Nick Craig-Wood 2016-01-09 15:25:48 +00:00
parent 5b2efd563a
commit 2646519712

View file

@ -21,7 +21,8 @@ import (
// Globals // Globals
var ( var (
// Flags // Flags
cpuprofile = pflag.StringP("cpuprofile", "", "", "Write cpu profile to file") cpuProfile = pflag.StringP("cpuprofile", "", "", "Write cpu profile to file")
memProfile = pflag.String("memprofile", "", "Write memory profile to file")
statsInterval = pflag.DurationP("stats", "", time.Minute*1, "Interval to print stats (0 to disable)") statsInterval = pflag.DurationP("stats", "", time.Minute*1, "Interval to print stats (0 to disable)")
version = pflag.BoolP("version", "V", false, "Print the version number") version = pflag.BoolP("version", "V", false, "Print the version number")
logFile = pflag.StringP("log-file", "", "", "Log everything to this file") logFile = pflag.StringP("log-file", "", "", "Log everything to this file")
@ -282,21 +283,6 @@ func ParseFlags() {
pflag.Parse() pflag.Parse()
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
fs.LoadConfig() fs.LoadConfig()
// Setup profiling if desired
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
fs.Stats.Error()
log.Fatal(err)
}
err = pprof.StartCPUProfile(f)
if err != nil {
fs.Stats.Error()
log.Fatal(err)
}
defer pprof.StopCPUProfile()
}
} }
// ParseCommand parses the command from the command line // ParseCommand parses the command from the command line
@ -391,6 +377,44 @@ func main() {
redirectStderr(f) redirectStderr(f)
} }
// Setup CPU profiling if desired
if *cpuProfile != "" {
log.Printf("Creating CPU profile %q\n", *cpuProfile)
f, err := os.Create(*cpuProfile)
if err != nil {
fs.Stats.Error()
log.Fatal(err)
}
err = pprof.StartCPUProfile(f)
if err != nil {
fs.Stats.Error()
log.Fatal(err)
}
defer pprof.StopCPUProfile()
}
// Setup memory profiling if desired
if *memProfile != "" {
defer func() {
log.Printf("Saving Memory profile %q\n", *memProfile)
f, err := os.Create(*memProfile)
if err != nil {
fs.Stats.Error()
log.Fatal(err)
}
err = pprof.WriteHeapProfile(f)
if err != nil {
fs.Stats.Error()
log.Fatal(err)
}
err = f.Close()
if err != nil {
fs.Stats.Error()
log.Fatal(err)
}
}()
}
// Make source and destination fs // Make source and destination fs
var fdst, fsrc fs.Fs var fdst, fsrc fs.Fs
if len(args) >= 1 { if len(args) >= 1 {
@ -407,8 +431,12 @@ func main() {
StartStats() StartStats()
} }
// Exit if no command to run
if command.Run == nil {
return
}
// Run the actual command // Run the actual command
if command.Run != nil {
var err error var err error
for try := 1; try <= *retries; try++ { for try := 1; try <= *retries; try++ {
err = command.Run(fdst, fsrc) err = command.Run(fdst, fsrc)
@ -436,8 +464,4 @@ func main() {
if fs.Stats.Errored() { if fs.Stats.Errored() {
os.Exit(1) os.Exit(1)
} }
os.Exit(0)
} else {
}
} }