-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmonitor.go
55 lines (48 loc) · 1.08 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
package utest
import (
"io/ioutil"
"os"
"runtime/pprof"
"strings"
"time"
)
// Command handler
var CommandHandler func(string) bool
func init() {
go func() {
for {
if input, err := ioutil.ReadFile("utest.cmd"); err == nil && len(input) > 0 {
ioutil.WriteFile("utest.cmd", []byte(""), 0744)
cmd := strings.Trim(string(input), " \n\r\t")
var (
profile *pprof.Profile
filename string
)
switch cmd {
case "lookup goroutine":
profile = pprof.Lookup("goroutine")
filename = "utest.goroutine"
case "lookup heap":
profile = pprof.Lookup("heap")
filename = "utest.heap"
case "lookup threadcreate":
profile = pprof.Lookup("threadcreate")
filename = "utest.thread"
default:
if CommandHandler == nil || !CommandHandler(cmd) {
println("unknow command: '" + cmd + "'")
}
}
if profile != nil {
file, err := os.Create(filename)
if err != nil {
println("couldn't create " + filename)
} else {
profile.WriteTo(file, 2)
}
}
}
time.Sleep(2 * time.Second)
}
}()
}