-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathcpu.go
40 lines (33 loc) · 869 Bytes
/
cpu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package profiling
import (
"errors"
"os"
"path/filepath"
"runtime/pprof"
)
// CPUFilename is a filename in which the CPU profiling is stored.
const CPUFilename = "status_cpu.prof"
var cpuFile *os.File
// 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 {
return errors.New("cpu profiling is already started")
}
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()
err := cpuFile.Close()
cpuFile = nil
return err
}