/
monitor.go
103 lines (87 loc) · 1.87 KB
/
monitor.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
package monitor
import (
"errors"
"fmt"
"os"
"path"
"runtime/pprof"
"time"
"github.com/shirou/gopsutil/process"
)
type monitor struct {
config *config
recordRing []recordInfo
idx int
}
func NewMonitor(config *config) *monitor {
return &monitor{
config: config,
recordRing: make([]recordInfo, 6),
}
}
func (m *monitor) Start() error {
// check config
if m.config == nil {
return errors.New("config is nil")
}
// check log path
_, err := os.Stat(m.config.LogPath)
if err != nil {
err = os.Mkdir(m.config.LogPath, os.ModePerm)
if err != nil {
return err
}
}
proce, err := process.NewProcess(int32(os.Getpid()))
if err != nil {
return err
}
go func() {
for {
cpuPercent, err := proce.Percent(5 * time.Second)
if err != nil {
continue
}
m.record(recordInfo{CPUPercent: float32(cpuPercent)})
}
}()
return nil
}
func (m *monitor) GetLastRecordInfo() recordInfo {
return m.recordRing[m.idx]
}
func (m *monitor) record(info recordInfo) {
m.idx++
if m.idx >= len(m.recordRing) {
m.idx = 0
}
m.recordRing[m.idx].CPUPercent = info.CPUPercent
cpuAverage := float32(0)
for _, v := range m.recordRing {
cpuAverage += v.CPUPercent
}
cpuAverage /= float32(len(m.recordRing))
if info.CPUPercent > cpuAverage*1.3 && info.CPUPercent > 30 {
fileName := fmt.Sprintf("%v-%v-%v.cpu.profile", time.Now().Format("2006-01-02_150405"), int(info.CPUPercent), int(cpuAverage))
m.cpuProfile(path.Join(m.config.LogPath, fileName))
}
}
func (m *monitor) cpuProfile(file string) {
go func() {
f, err := os.Create(file)
if err != nil {
fmt.Println(file, err)
return
// log.Fatal(err)
}
if err := pprof.StartCPUProfile(f); err != nil {
// StartCPUProfile failed, so no writes yet.
f.Close()
os.Remove(file)
return
}
defer f.Close()
time.Sleep(time.Duration(5) * time.Second)
pprof.StopCPUProfile()
}()
}