forked from klaytn/klaytn
/
api.go
391 lines (343 loc) · 10.9 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Modifications Copyright 2018 The klaytn Authors
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
//
// This file is derived from internal/debug/api.go (2018/06/04).
// Modified and improved for the klaytn development.
package debug
import (
"errors"
"fmt"
"github.com/klaytn/klaytn/log"
"github.com/klaytn/klaytn/metrics"
"github.com/klaytn/klaytn/metrics/exp"
"github.com/klaytn/klaytn/params"
"io"
"net/http"
"os"
"os/user"
"path/filepath"
"runtime"
"runtime/debug"
"runtime/pprof"
"strings"
"sync"
"time"
)
// Handler is the global debugging handler.
var Handler = new(HandlerT)
var logger = log.NewModuleLogger(log.APIDebug)
// 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
memFile string
traceW io.WriteCloser
traceFile string
// For the pprof http server
handlerInited bool
pprofServer *http.Server
logDir string // log directory path
vmLogFile *os.File // a file descriptor of the vmlog output file
}
// Verbosity sets the log verbosity ceiling. The verbosity of individual packages
// and source files can be raised using Vmodule.
func (*HandlerT) Verbosity(level int) error {
return log.ChangeGlobalLogLevel(glogger, log.Lvl(level))
}
// VerbosityByName sets the verbosity of log module with given name.
// Please note that VerbosityByName only works with zapLogger.
func (*HandlerT) VerbosityByName(mn string, level int) error {
return log.ChangeLogLevelWithName(mn, log.Lvl(level))
}
// VerbosityByID sets the verbosity of log module with given ModuleID.
// Please note that VerbosityByID only works with zapLogger.
func (*HandlerT) VerbosityByID(mi int, level int) error {
return log.ChangeLogLevelWithID(log.ModuleID(mi), log.Lvl(level))
}
// Vmodule sets the log verbosity pattern. See package log for details on the
// pattern syntax.
func (*HandlerT) Vmodule(pattern string) error {
return glogger.Vmodule(pattern)
}
// BacktraceAt sets the log backtrace location. See package log for details on
// the pattern syntax.
func (*HandlerT) BacktraceAt(location string) error {
return glogger.BacktraceAt(location)
}
// MemStats returns detailed runtime memory statistics.
func (*HandlerT) MemStats() *runtime.MemStats {
s := new(runtime.MemStats)
runtime.ReadMemStats(s)
return s
}
// GcStats returns GC statistics.
func (*HandlerT) GcStats() *debug.GCStats {
s := new(debug.GCStats)
debug.ReadGCStats(s)
return s
}
// StartPProf starts the pprof server.
func (h *HandlerT) StartPProf(address string, port int) error {
// Set the default server address and port if they are not set
if address == "" {
address = pprofAddrFlag.Value
}
if port == 0 {
port = pprofPortFlag.Value
}
h.mu.Lock()
defer h.mu.Unlock()
if h.pprofServer != nil {
return errors.New("pprof server is already running")
}
serverAddr := fmt.Sprintf("%s:%d", address, port)
httpServer := &http.Server{Addr: serverAddr}
if !h.handlerInited {
// Hook go-metrics into expvar on any /debug/metrics request, load all vars
// from the registry into expvar, and execute regular expvar handler.
exp.Exp(metrics.DefaultRegistry)
http.Handle("/memsize/", http.StripPrefix("/memsize", &Memsize))
h.handlerInited = true
}
logger.Info("Starting pprof server", "addr", fmt.Sprintf("http://%s/debug/pprof", serverAddr))
go func(handle *HandlerT) {
if err := httpServer.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
logger.Info("pprof server is closed")
} else {
logger.Error("Failure in running pprof server", "err", err)
}
}
h.mu.Lock()
h.pprofServer = nil
h.mu.Unlock()
}(h)
h.pprofServer = httpServer
return nil
}
// StopPProf stops the pprof server.
func (h *HandlerT) StopPProf() error {
h.mu.Lock()
defer h.mu.Unlock()
if h.pprofServer == nil {
return errors.New("pprof server is not running")
}
logger.Info("Shutting down pprof server")
h.pprofServer.Close()
return nil
}
// IsPProfRunning returns true if the pprof HTTP server is running and false otherwise.
func (h *HandlerT) IsPProfRunning() bool {
h.mu.Lock()
defer h.mu.Unlock()
return h.pprofServer != nil
}
// CpuProfile turns on CPU profiling for nsec seconds and writes
// profile data to file.
func (h *HandlerT) CpuProfile(file string, nsec uint) error {
if err := h.StartCPUProfile(file); err != nil {
return err
}
time.Sleep(time.Duration(nsec) * time.Second)
h.StopCPUProfile()
return nil
}
// StartCPUProfile turns on CPU profiling, writing to the given file.
func (h *HandlerT) StartCPUProfile(file string) 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
logger.Info("CPU profiling started", "dump", h.cpuFile)
return nil
}
// StopCPUProfile stops an ongoing CPU profile.
func (h *HandlerT) StopCPUProfile() error {
h.mu.Lock()
defer h.mu.Unlock()
pprof.StopCPUProfile()
if h.cpuW == nil {
return errors.New("CPU profiling not in progress")
}
logger.Info("Done writing CPU profile", "dump", h.cpuFile)
h.cpuW.Close()
h.cpuW = nil
h.cpuFile = ""
return nil
}
// GoTrace turns on tracing for nsec seconds and writes
// trace data to file.
func (h *HandlerT) GoTrace(file string, nsec uint) error {
if err := h.StartGoTrace(file); err != nil {
return err
}
time.Sleep(time.Duration(nsec) * time.Second)
h.StopGoTrace()
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(file string, nsec uint) error {
runtime.SetBlockProfileRate(1)
time.Sleep(time.Duration(nsec) * time.Second)
defer runtime.SetBlockProfileRate(0)
return writeProfile("block", file)
}
// SetBlockProfileRate sets the rate of goroutine block profile data collection.
// rate 0 disables block profiling.
func (*HandlerT) SetBlockProfileRate(rate int) {
runtime.SetBlockProfileRate(rate)
}
// WriteBlockProfile writes a goroutine blocking profile to the given file.
func (*HandlerT) WriteBlockProfile(file string) error {
return writeProfile("block", file)
}
// 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(file string, nsec uint) error {
runtime.SetMutexProfileFraction(1)
time.Sleep(time.Duration(nsec) * time.Second)
defer runtime.SetMutexProfileFraction(0)
return writeProfile("mutex", file)
}
// SetMutexProfileFraction sets the rate of mutex profiling.
func (*HandlerT) SetMutexProfileFraction(rate int) {
runtime.SetMutexProfileFraction(rate)
}
// WriteMutexProfile writes a goroutine blocking profile to the given file.
func (*HandlerT) WriteMutexProfile(file string) error {
return writeProfile("mutex", file)
}
// 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) error {
return writeProfile("heap", file)
}
// Stacks returns a printed representation of the stacks of all goroutines.
func (*HandlerT) Stacks() string {
buf := make([]byte, 1024*1024)
buf = buf[:runtime.Stack(buf, true)]
return string(buf)
}
// FreeOSMemory returns unused memory to the OS.
func (*HandlerT) FreeOSMemory() {
debug.FreeOSMemory()
}
// SetGCPercent sets the garbage collection target percentage. It returns the previous
// setting. A negative value disables GC.
func (*HandlerT) SetGCPercent(v int) int {
return debug.SetGCPercent(v)
}
func writeProfile(name, file string) error {
p := pprof.Lookup(name)
logger.Info("Writing profile records", "count", p.Count(), "type", name, "dump", 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)
}
// WriteVMLog writes msg to a vmlog output file.
func (h *HandlerT) WriteVMLog(msg string) {
h.mu.Lock()
defer h.mu.Unlock()
if h.vmLogFile != nil {
if _, err := h.vmLogFile.WriteString(msg + "\n"); err != nil {
// Since vmlog is a debugging feature, write failure can be treated as a warning.
logger.Warn("Failed to write to a vmlog file", "msg", msg, "err", err)
}
}
}
// openVMLogFile opens a file for vmlog output as the append mode.
func (h *HandlerT) openVMLogFile() {
var err error
filename := filepath.Join(h.logDir, "vm.log")
Handler.vmLogFile, err = os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
logger.Warn("Failed to open a file", "filename", filename, "err", err)
}
}
func vmLogTargetToString(target int) string {
switch target {
case 0:
return "no output"
case params.VMLogToFile:
return "file"
case params.VMLogToStdout:
return "stdout"
case params.VMLogToAll:
return "both file and stdout"
default:
return ""
}
}
// SetVMLogTarget sets the output target of vmlog.
func (h *HandlerT) SetVMLogTarget(target int) (string, error) {
if target < 0 || target > params.VMLogToAll {
return vmLogTargetToString(params.VMLogTarget), fmt.Errorf("target should be between 0 and %d", params.VMLogToAll)
}
h.mu.Lock()
defer h.mu.Unlock()
if (target & params.VMLogToFile) != 0 {
if h.vmLogFile == nil {
h.openVMLogFile()
}
} else {
if h.vmLogFile != nil {
if err := Handler.vmLogFile.Close(); err != nil {
logger.Warn("Failed to close the vmlog file", "err", err)
}
Handler.vmLogFile = nil
}
}
params.VMLogTarget = target
return vmLogTargetToString(target), nil
}