-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.go
65 lines (60 loc) · 1.86 KB
/
profile.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package golib
import (
"flag"
"os"
"runtime"
"runtime/pprof"
)
var (
// CpuProfileFile will be used to output a CPU profile of the running program,
// if ProfileCpu() is called. This field is configured by the '-profile-cpu' flag
// created by RegisterProfileFlags().
CpuProfileFile = ""
// MemProfileFile will be used to output a memory usage profile of the running program,
// if ProfileCpu() is called. This field is configured by the '-profile-mem' flag
// created by RegisterProfileFlags().
MemProfileFile = ""
)
// RegisterProfileFlags registers flags to configure the CpuProfileFile and MemProfileFile
// by user-provided flags.
func RegisterProfileFlags() {
flag.StringVar(&CpuProfileFile, "profile-cpu", CpuProfileFile, "Write cpu profile data to file.")
flag.StringVar(&MemProfileFile, "profile-mem", MemProfileFile, "Write memory profile data to file.")
}
// ProfileCpu initiates memory and CPU profiling if any of the CpuProfileFile and MemProfileFile
// is set to non-empty strings, respectively. The function returns a tear-down function
// that must be called before the program exists in order to flush the profiling data to the
// output files.
// It can be used like this:
// defer golib.ProfileCpu()()
func ProfileCpu() func() {
var cpu, mem *os.File
var err error
if CpuProfileFile != "" {
cpu, err = os.Create(CpuProfileFile)
if err != nil {
Log.Fatalln(err)
}
if err := pprof.StartCPUProfile(cpu); err != nil {
Log.Fatalln("Unable to start CPU profile:", err)
}
}
if MemProfileFile != "" {
mem, err = os.Create(MemProfileFile)
if err != nil {
Log.Fatalln(err)
}
}
return func() {
if cpu != nil {
pprof.StopCPUProfile()
}
if mem != nil {
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(mem); err != nil {
Log.Warnln("Failed to write Memory profile:", err)
}
mem.Close()
}
}
}