/
pprofM.go
141 lines (125 loc) · 2.51 KB
/
pprofM.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
package pprof
// add by stefan 20190606 16:12
import (
"github.com/Peakchen/xgameCommon/akLog"
"github.com/Peakchen/xgameCommon/aktime"
"fmt"
"os"
"path"
"runtime/pprof"
"strings"
"time"
//"log"
"github.com/Peakchen/xgameCommon/utls"
"context"
"sync"
)
const (
const_PProfWriteInterval = int32(60 * 1)
)
type TPProfMgr struct {
ctx context.Context
wg sync.WaitGroup
cpu *os.File
mem *os.File
}
var (
_pprofobj *TPProfMgr
)
func init() {
_pprofobj = &TPProfMgr{}
}
func Run(ctx context.Context) {
_pprofobj.StartPProf(ctx)
}
func Exit() {
_pprofobj.Exit()
}
func (this *TPProfMgr) StartPProf(ctx context.Context) {
this.ctx = ctx
this.wg.Add(1)
checkcreateTempDir()
this.cpu = createCpu()
this.mem = createMem()
go this.loop()
}
func (this *TPProfMgr) Exit() {
akLog.FmtPrintln("pprof exist.")
this.flush()
if this.cpu != nil {
this.cpu.Close()
}
if this.mem != nil {
pprof.WriteHeapProfile(this.mem)
}
}
func (this *TPProfMgr) flush() {
if this.cpu != nil {
pprof.StopCPUProfile()
}
if this.mem != nil {
pprof.WriteHeapProfile(this.mem)
}
}
func (this *TPProfMgr) loop() {
defer this.wg.Done()
t := time.NewTicker(time.Duration(const_PProfWriteInterval) * time.Second)
for {
select {
case <-this.ctx.Done():
this.Exit()
return
case <-t.C:
// do nothing...
this.flush()
}
}
}
func Newpprof(file string) (retfile string) {
timeformat := aktime.Now().Format("2006-01-02")
retfile = timeformat + "_" + file
execpath, err := os.Executable()
if err != nil {
return
}
execpath = strings.Replace(execpath, "\\", "/", -1)
_, sfile := path.Split(execpath)
arrfile := strings.Split(sfile, ".")
retfile = fmt.Sprintf("./pprof/%s_%v.prof", arrfile[0], retfile)
return
}
func checkcreateTempDir() {
exepath := utls.GetExeFilePath()
filepath := exepath + "/pprof"
exist, err := utls.IsPathExisted(filepath)
if err != nil {
panic("check path exist err: " + err.Error())
return
}
if false == exist {
err = os.Mkdir(filepath, os.ModePerm)
if err != nil {
panic("log mkdir fail, err: " + err.Error())
return
}
}
}
func createCpu() (file *os.File) {
cpuf := Newpprof("cpu")
f, err := os.OpenFile(cpuf, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
akLog.FmtPrintln("cpu pprof open fail, err: ", err)
return
}
pprof.StartCPUProfile(f)
return f
}
func createMem() (file *os.File) {
cpuf := Newpprof("mem")
f, err := os.OpenFile(cpuf, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
akLog.FmtPrintln("mem pprof open fail, err: ", err)
return
}
return f
}