2017-09-01 14:09:11 +00:00
|
|
|
package profiling
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime/pprof"
|
|
|
|
)
|
|
|
|
|
2018-02-02 12:23:47 +00:00
|
|
|
// CPUFilename is a filename in which the CPU profiling is stored.
|
|
|
|
const CPUFilename = "status_cpu.prof"
|
2017-09-01 14:09:11 +00:00
|
|
|
|
2018-02-02 12:23:47 +00:00
|
|
|
var cpuFile *os.File
|
2017-09-01 14:09:11 +00:00
|
|
|
|
|
|
|
// StartCPUProfile enables CPU profiling for the current process. While profiling,
|
|
|
|
// the profile will be buffered and written to the file in folder dataDir.
|
|
|
|
func StartCPUProfile(dataDir string) error {
|
|
|
|
if cpuFile == nil {
|
|
|
|
var err error
|
|
|
|
cpuFile, err = os.Create(filepath.Join(dataDir, CPUFilename))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pprof.StartCPUProfile(cpuFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopCPUProfile stops the current CPU profile, if any, and closes the file.
|
|
|
|
func StopCPUProfile() error {
|
|
|
|
if cpuFile == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
pprof.StopCPUProfile()
|
|
|
|
return cpuFile.Close()
|
|
|
|
}
|