-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.go
255 lines (231 loc) · 6.81 KB
/
api.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2018 The uranus Authors
// This file is part of the uranus library.
//
// The uranus library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The uranus library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the uranus library. If not, see <http://www.gnu.org/licenses/>.
package debug
import (
"bytes"
"errors"
"io"
"os"
"os/user"
"path/filepath"
"runtime"
"runtime/debug"
"runtime/pprof"
"runtime/trace"
"strings"
"sync"
"time"
"github.com/UranusBlockStack/uranus/common/log"
)
// Handler is the global debugging handler.
var Handler = new(HandlerT)
// HandlerT implements the debugging API.
// Do not create values of this type, use the one
// in the Handler variable instead.
type HandlerT struct {
mu sync.Mutex
cpuW io.WriteCloser
cpuFile string
traceW io.WriteCloser
traceFile string
}
// MemStats returns detailed runtime memory statistics.
func (*HandlerT) MemStats(ignore string, reply *runtime.MemStats) error {
runtime.ReadMemStats(reply)
return nil
}
// GcStats returns GC statistics.
func (*HandlerT) GcStats(ignore string, reply *debug.GCStats) error {
debug.ReadGCStats(reply)
return nil
}
type DebugArgs struct {
File string
Nsec uint64
}
// CpuProfile turns on CPU profiling for nsec seconds and writesprofile data to file.
func (h *HandlerT) CpuProfile(args DebugArgs, reply *bool) error {
if err := h.StartCPUProfile(args.File, reply); err != nil {
return err
}
time.Sleep(time.Duration(args.Nsec) * time.Second)
err := h.StopCPUProfile("", reply)
*reply = err == nil
return err
}
// StartCPUProfile turns on CPU profiling, writing to the given file.
func (h *HandlerT) StartCPUProfile(file string, reply *bool) error {
h.mu.Lock()
defer h.mu.Unlock()
if h.cpuW != nil {
return errors.New("CPU profiling already in progress")
}
f, err := os.Create(expandHome(file))
if err != nil {
return err
}
if err := pprof.StartCPUProfile(f); err != nil {
f.Close()
return err
}
h.cpuW = f
h.cpuFile = file
log.Infof("CPU profiling started dump: %v", h.cpuFile)
*reply = true
return nil
}
// StopCPUProfile stops an ongoing CPU profile.
func (h *HandlerT) StopCPUProfile(ignore string, reply *bool) error {
h.mu.Lock()
defer h.mu.Unlock()
pprof.StopCPUProfile()
if h.cpuW == nil {
return errors.New("CPU profiling not in progress")
}
log.Infof("Done writing CPU profile dump: %v", h.cpuFile)
h.cpuW.Close()
h.cpuW = nil
h.cpuFile = ""
*reply = true
return nil
}
// GoTrace turns on tracing for nsec seconds and writes
// trace data to file.
func (h *HandlerT) GoTrace(args DebugArgs, reply *bool) error {
if err := h.StartGoTrace(args.File, reply); err != nil {
return err
}
time.Sleep(time.Duration(args.Nsec) * time.Second)
err := h.StopGoTrace("", reply)
*reply = err == nil
return err
}
// StartGoTrace turns on tracing, writing to the given file.
func (h *HandlerT) StartGoTrace(file string, reply *bool) error {
h.mu.Lock()
defer h.mu.Unlock()
if h.traceW != nil {
return errors.New("trace already in progress")
}
f, err := os.Create(expandHome(file))
if err != nil {
return err
}
if err := trace.Start(f); err != nil {
f.Close()
return err
}
h.traceW = f
h.traceFile = file
log.Infof("Go tracing started dump: %v", h.traceFile)
*reply = true
return nil
}
// StopGoTrace stops an ongoing trace.
func (h *HandlerT) StopGoTrace(ignore string, reply *bool) error {
h.mu.Lock()
defer h.mu.Unlock()
trace.Stop()
if h.traceW == nil {
return errors.New("trace not in progress")
}
log.Infof("Done writing Go trace dump: %v", h.traceFile)
h.traceW.Close()
h.traceW = nil
h.traceFile = ""
*reply = true
return nil
}
// BlockProfile turns on goroutine profiling for nsec seconds and writes profile data to
// file. It uses a profile rate of 1 for most accurate information. If a different rate is
// desired, set the rate and write the profile manually.
func (*HandlerT) BlockProfile(args DebugArgs, reply *bool) error {
runtime.SetBlockProfileRate(1)
time.Sleep(time.Duration(args.Nsec) * time.Second)
defer runtime.SetBlockProfileRate(0)
err := writeProfile("block", args.File)
*reply = err == nil
return err
}
// MutexProfile turns on mutex profiling for nsec seconds and writes profile data to file.
// It uses a profile rate of 1 for most accurate information. If a different rate is
// desired, set the rate and write the profile manually.
func (*HandlerT) MutexProfile(args DebugArgs, reply *bool) error {
runtime.SetMutexProfileFraction(1)
time.Sleep(time.Duration(args.Nsec) * time.Second)
defer runtime.SetMutexProfileFraction(0)
err := writeProfile("mutex", args.File)
*reply = err == nil
return err
}
// WriteMemProfile writes an allocation profile to the given file.
// Note that the profiling rate cannot be set through the API,
// it must be set on the command line.
func (*HandlerT) WriteMemProfile(file string, reply *bool) error {
err := writeProfile("heap", file)
*reply = err == nil
return err
}
// Stacks returns a printed representation of the stacks of all goroutines.
func (*HandlerT) Stacks(ignore string, reply *[]byte) error {
buf := new(bytes.Buffer)
pprof.Lookup("goroutine").WriteTo(buf, 2)
*reply = buf.Bytes()
return nil
}
// FreeOSMemory returns unused memory to the OS.
func (*HandlerT) FreeOSMemory(ignore string, reply *bool) error {
debug.FreeOSMemory()
*reply = true
return nil
}
// SetGCPercent sets the garbage collection target percentage. It returns the previous
// setting. A negative value disables GC.
func (*HandlerT) SetGCPercent(v int, reply *int) error {
*reply = debug.SetGCPercent(v)
return nil
}
// setBlockProfileRate sets the rate of goroutine block profile data collection.
// rate 0 disables block profiling.
func (*HandlerT) setBlockProfileRate(rate int) {
runtime.SetBlockProfileRate(rate)
}
func writeProfile(name, file string) error {
p := pprof.Lookup(name)
log.Infof("Writing profile records count: %v, type: %v, dump: %v", p.Count(), name, file)
f, err := os.Create(expandHome(file))
if err != nil {
return err
}
defer f.Close()
return p.WriteTo(f, 0)
}
// expands home directory in file paths.
// ~someuser/tmp will not be expanded.
func expandHome(p string) string {
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
home := os.Getenv("HOME")
if home == "" {
if usr, err := user.Current(); err == nil {
home = usr.HomeDir
}
}
if home != "" {
p = home + p[1:]
}
}
return filepath.Clean(p)
}