I am trying to profile a go application and below is the code:
func main() {
defer pprof.StopCPUProfile()
f, err := os.Create("./profile.tar.gz")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
...
I am able to see the file was created on the disk once the application exits. Is there a way to flush the profile to disk without quitting the application? My application is a rest API which will keep running for a long time. I'd like to see profile file during the run time. I may create an internal rest path in the application to flush profile data into file.
Reading through the code of
pprof.NewCPUProfileit doesn't seem to be able to do that. It only writes (and stops profiling) to whatever writer you have passed if it receives an EOF signal when trying to do an internalreadProfile. The function that actually writes data into the file is calledbuildand it is only called three times in the package and one of the times it's from theprofileWriter(a goroutine spawned by pprof'sNewCPUProfilefunction.I'd use the net/http/pprof package and from them export whatever profile file you want.