cmd: fix cpu profile starter

This commit is contained in:
c9s 2022-08-07 01:13:57 +08:00
parent ba7b6f82e2
commit 4e4ffe83e5
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 3 additions and 23 deletions

View File

@ -62,15 +62,15 @@ var RootCmd = &cobra.Command{
}
if cpuProfile != "" {
log.Infof("starting cpu profiler...")
log.Infof("starting cpu profiler, recording at %s", cpuProfile)
cpuProfileFile, err = os.Create(cpuProfile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
return errors.Wrap(err, "can not create file for CPU profile")
}
if err := pprof.StartCPUProfile(cpuProfileFile); err != nil {
log.Fatal("could not start CPU profile: ", err)
return errors.Wrap(err, "can not start CPU profile")
}
}

View File

@ -6,7 +6,6 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime/pprof"
"syscall"
"github.com/pkg/errors"
@ -244,11 +243,6 @@ func run(cmd *cobra.Command, args []string) error {
return err
}
cpuProfile, err := cmd.Flags().GetString("cpu-profile")
if err != nil {
return err
}
if !setup {
// if it's not setup, then the config file option is required.
if len(configFile) == 0 {
@ -280,20 +274,6 @@ func run(cmd *cobra.Command, args []string) error {
return err
}
if cpuProfile != "" {
f, err := os.Create(cpuProfile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
defer f.Close() // error handling omitted for example
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
return runConfig(ctx, cmd, userConfig)
}