/
signal.go
168 lines (143 loc) · 3.2 KB
/
signal.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package sigx
import (
"bytes"
"context"
"io/ioutil"
"log"
"os"
"os/signal"
"runtime/pprof"
"syscall"
"time"
"github.com/bingoohuang/gg/pkg/iox"
"github.com/bingoohuang/gg/pkg/osx"
"github.com/bingoohuang/gg/pkg/profile"
)
// RegisterSignals registers signal handlers.
func RegisterSignals(c context.Context, signals ...os.Signal) (context.Context, context.CancelFunc) {
if c == nil {
c = context.Background()
}
cc, cancel := context.WithCancel(c)
sig := make(chan os.Signal, 1)
if len(signals) == 0 {
// syscall.SIGINT: ctl + c, syscall.SIGTERM: kill pid
signals = []os.Signal{syscall.SIGINT, syscall.SIGTERM}
}
signal.Notify(sig, signals...)
go func() {
<-sig
cancel()
}()
return cc, cancel
}
func RegisterSignalCallback(f func(), signals ...os.Signal) {
sig := make(chan os.Signal, 1)
signal.Notify(sig, signals...)
go func() {
for range sig {
f()
}
}()
}
func RegisterSignalProfile(signals ...os.Signal) {
if len(signals) == 0 {
signals = defaultSignals
}
RegisterSignalCallback(func() {
val, ok := ReadCmdOK("jj.cpu", false)
if ok {
collectCpuProfile()
if val = bytes.TrimSpace(val); len(val) > 0 {
if duration, err := time.ParseDuration(string(val)); err != nil {
log.Printf("ignore duration %s in jj.cpu, parse failed: %v", val, err)
} else if duration > 0 {
log.Printf("after %s, cpu.profile will be generated ", val)
go func() {
time.Sleep(duration)
collectCpuProfile()
}()
}
}
}
if HasCmd("jj.mem", true) {
if err := CollectMemProfile("mem.profile"); err != nil {
log.Printf("failed to collect profile: %v", err)
}
}
if v := ReadCmd("jj.profile"); len(v) > 0 {
go profile.Start(profile.Specs(string(v)))
}
}, signals...)
}
func collectCpuProfile() {
if completed, err := CollectCpuProfile("cpu.profile"); err != nil {
log.Printf("failed to collect profile: %v", err)
} else if completed {
osx.Remove("jj.cpu")
}
}
var cpuProfileFile *os.File
func HasCmd(f string, remove bool) bool {
s, err := os.Stat(f)
if err == nil && !s.IsDir() {
if remove {
osx.Remove(f)
}
return true
}
return false
}
func ReadCmdOK(f string, remove bool) ([]byte, bool) {
s, err := os.Stat(f)
if err == nil && !s.IsDir() {
data, _ := ioutil.ReadFile(f)
if remove {
osx.Remove(f)
}
return data, true
}
return nil, false
}
func ReadCmd(f string) []byte {
s, err := os.Stat(f)
if err == nil && !s.IsDir() {
data, _ := ioutil.ReadFile(f)
osx.Remove(f)
return data
}
return nil
}
func CollectCpuProfile(cpuProfile string) (bool, error) {
if cpuProfile == "" {
return false, nil
}
if cpuProfileFile != nil {
pprof.StopCPUProfile()
iox.Close(cpuProfileFile)
log.Printf("%s collected", cpuProfileFile.Name())
cpuProfileFile = nil
return true, nil
}
f, err := os.Create(cpuProfile)
if err != nil {
return false, err
}
cpuProfileFile = f
if err := pprof.StartCPUProfile(f); err != nil {
return false, err
}
log.Printf("%s started", cpuProfile)
return false, nil
}
func CollectMemProfile(memProfile string) error {
if memProfile == "" {
return nil
}
f, err := os.Create(memProfile)
if err != nil {
return err
}
defer f.Close()
return pprof.WriteHeapProfile(f)
}