/
profiling.go
150 lines (135 loc) · 3.75 KB
/
profiling.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package pprof
import (
"flag"
"fmt"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"sync"
)
// ProfileType 代表概要类型。
type ProfileType string
var ( // profile flags
memProfile = flag.String("memprofile", "", "write a memory profile to the named file after execution")
memProfileRate = flag.Int("memprofilerate", 0, "if > 0, sets runtime.MemProfileRate")
cpuProfile = flag.String("cpuprofile", "", "write a cpu profile to the named file during execution")
blockProfile = flag.String("blockprofile", "", "write a goroutine blocking profile to the named file after execution")
blockProfileRate = flag.Int("blockprofilerate", 1, "if > 0, calls runtime.SetBlockProfileRate()")
)
var running bool
var lock = new(sync.Mutex)
func parseProfFlags() {
if !flag.Parsed() {
flag.Parse()
}
*cpuProfile = getAbsFilePath(*cpuProfile)
*blockProfile = getAbsFilePath(*blockProfile)
*memProfile = getAbsFilePath(*memProfile)
}
func getAbsFilePath(path string) string {
if path == "" {
return ""
}
path = filepath.FromSlash(path)
if !filepath.IsAbs(path) {
baseDir, err := os.Getwd()
if err != nil {
panic(fmt.Errorf("Couldn't get current work dir: %s\n", err))
}
path = filepath.Join(baseDir, path)
}
return path
}
// Start 用于开始采样并生成概要文件。
func Start() {
lock.Lock()
defer lock.Unlock()
parseProfFlags()
startBlockProfile()
startCPUProfile()
startMemProfile()
running = true
}
func startBlockProfile() {
if *blockProfile != "" && *blockProfileRate > 0 {
runtime.SetBlockProfileRate(*blockProfileRate)
}
}
func startCPUProfile() {
if *cpuProfile != "" {
f, err := os.Create(*cpuProfile)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't create cpu profile output file: %s\n", err)
return
}
if err := pprof.StartCPUProfile(f); err != nil {
fmt.Fprintf(os.Stderr, "Couldn't start cpu profile: %s\n", err)
f.Close()
return
}
}
}
func startMemProfile() {
if *memProfile != "" && *memProfileRate > 0 {
runtime.MemProfileRate = *memProfileRate
}
}
// Stop 用于停止采样并生成概要文件。
func Stop() {
lock.Lock()
defer lock.Unlock()
stopBlockProfile()
stopCPUProfile()
stopMemProfile()
running = false
}
func stopBlockProfile() {
if *blockProfile != "" && *blockProfileRate >= 0 {
f, err := os.Create(*blockProfile)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't create block profile output file: %s\n", err)
return
}
if err = pprof.Lookup("block").WriteTo(f, 0); err != nil {
fmt.Fprintf(os.Stderr, "Couldn't write %s: %s\n", *blockProfile, err)
}
f.Close()
}
}
func stopCPUProfile() {
if *cpuProfile != "" {
pprof.StopCPUProfile() // 把记录的概要信息写到已指定的文件
}
}
func stopMemProfile() {
if *memProfile != "" {
f, err := os.Create(*memProfile)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't create mem profile output file: %s\n", err)
return
}
if err = pprof.WriteHeapProfile(f); err != nil {
fmt.Fprintf(os.Stderr, "Couldn't write %s: %s\n", *memProfile, err)
}
f.Close()
}
}
// SaveProfile 用于存储概要文件。
func SaveProfile(workDir string, profileName string, ptype ProfileType, debug int) {
absWorkDir := getAbsFilePath(workDir)
if profileName == "" {
profileName = string(ptype)
}
profileName += ".out"
profilePath := filepath.Join(absWorkDir, profileName)
f, err := os.Create(profilePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't create profile output file: %s\n", err)
return
}
if err = pprof.Lookup(string(ptype)).WriteTo(f, debug); err != nil {
fmt.Fprintf(os.Stderr, "Couldn't write %s: %s\n", profilePath, err)
}
f.Close()
}