forked from linuxdeepin/dde-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
159 lines (138 loc) · 3.46 KB
/
manager.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
package calltrace
import (
"fmt"
"os"
"path/filepath"
"pkg.deepin.io/lib/xdg/basedir"
"runtime/pprof"
"time"
)
// Manager manage calltrace files
type Manager struct {
cpuFile *os.File
stackFile *os.File
duration uint32
quit chan bool
}
// NewManager create Manager and launch calltrace module
func NewManager(duration uint32) (*Manager, error) {
dir, err := ensureDirExist()
if err != nil {
return nil, err
}
timestamp := time.Now().UnixNano()
cpu, err := os.Create(filepath.Join(dir,
fmt.Sprintf("cpu_%v.prof", timestamp)))
if err != nil {
return nil, err
}
stack, err := os.OpenFile(filepath.Join(dir,
fmt.Sprintf("stack_%v.log", timestamp)),
os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
if err != nil {
_ = cpu.Close()
return nil, err
}
ct := new(Manager)
ct.cpuFile = cpu
ct.stackFile = stack
ct.quit = make(chan bool)
ct.duration = duration
err = pprof.StartCPUProfile(ct.cpuFile)
if err != nil {
logger.Warning("Failed to start cpu profile:", err)
_ = ct.cpuFile.Close()
ct.cpuFile = nil
}
logger.Infof("[Manager] Will record profiles, once per %d second", ct.duration)
ct.writeHeap()
ct.recordStack()
go ct.loop()
return ct, nil
}
// SetAutoDestroy auto destroy manager after the special seconds
func (ct *Manager) SetAutoDestroy(seconds uint32) {
if seconds == 0 {
return
}
go func() {
time.Sleep(time.Second * time.Duration(seconds))
ct.stop()
}()
}
// Stop terminate calltrace module
func (ct *Manager) stop() {
if ct.quit != nil {
ct.quit <- true
}
if ct.cpuFile != nil {
pprof.StopCPUProfile()
_ = ct.cpuFile.Close()
}
if ct.stackFile != nil {
_ = ct.stackFile.Close()
}
logger.Info("[Manager] Terminated!")
ct = nil
}
func (ct *Manager) loop() {
var ticker = time.NewTicker(time.Second * time.Duration(ct.duration))
for {
select {
case _, ok := <-ticker.C:
if !ok {
logger.Error("Invalid ticker event")
return
}
ct.writeHeap()
ct.recordStack()
case <-ct.quit:
ticker.Stop()
close(ct.quit)
ct.quit = nil
return
}
}
}
func (ct *Manager) writeHeap() {
dir, _ := ensureDirExist()
mem, err := os.Create(filepath.Join(dir,
fmt.Sprintf("memory_%v.prof", time.Now().UnixNano())))
if err != nil {
logger.Warning("Failed to create memory file:", err)
return
}
err = pprof.WriteHeapProfile(mem)
_ = mem.Close()
if err != nil {
logger.Warning("Failed to write head profile:", err)
}
}
func (ct *Manager) recordStack() {
_, err := ct.stackFile.WriteString("=== BEGIN " + time.Now().String() + " ===\n")
if err != nil {
logger.Warning("Failed to start record stack:", err)
return
}
_, _ = ct.stackFile.WriteString("--- DUMP [goroutine] ---\n")
_ = pprof.Lookup("goroutine").WriteTo(ct.stackFile, 2)
_, _ =ct.stackFile.WriteString("\n--- DUMP [heap] ---\n")
_ = pprof.Lookup("heap").WriteTo(ct.stackFile, 2)
_, _ =ct.stackFile.WriteString("\n--- DUMP [threadcreate] ---\n")
_ = pprof.Lookup("threadcreate").WriteTo(ct.stackFile, 2)
_, _ =ct.stackFile.WriteString("\n--- DUMP [block] ---\n")
_ = pprof.Lookup("block").WriteTo(ct.stackFile, 2)
_, _ =ct.stackFile.WriteString("\n--- DUMP [mutex] ---\n")
_ = pprof.Lookup("mutex").WriteTo(ct.stackFile, 2)
_, _ =ct.stackFile.WriteString("=== END " + time.Now().String() + " ===\n\n\n")
_ = ct.stackFile.Sync()
}
func ensureDirExist() (string, error) {
dir := filepath.Join(basedir.GetUserCacheDir(),
"deepin", "dde-daemon", "calltrace")
err := os.MkdirAll(dir, 0755)
if err != nil {
return "", err
}
return dir, nil
}