/
pprof.go
194 lines (155 loc) · 5.21 KB
/
pprof.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
// Copyright 2018 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package service
import (
"fmt"
"io"
"os"
"path/filepath"
"runtime/pprof"
"runtime/trace"
"time"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/logger"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
"golang.org/x/net/context"
)
type PprofHandler struct {
libkb.Contextified
*BaseHandler
}
func NewPprofHandler(xp rpc.Transporter, g *libkb.GlobalContext) *PprofHandler {
return &PprofHandler{
BaseHandler: NewBaseHandler(g, xp),
Contextified: libkb.NewContextified(g),
}
}
func durationSecToDuration(s keybase1.DurationSec) time.Duration {
return time.Duration(float64(s) * float64(time.Second))
}
type timedProfiler interface {
Name() string
MaxFileCount() int
MakeFilename(dir string, start time.Time, duration time.Duration) string
GetSortedFiles(dir string) ([]string, error)
Start(w io.Writer) error
Stop()
}
// doTimedProfile asynchronously runs a profile with the given
// timedProfiler and associated parameters.
func doTimedProfile(log libkb.LogUI, delayedLog logger.Logger,
profiler timedProfiler, outputFile string,
durationSeconds keybase1.DurationSec) (err error) {
if !filepath.IsAbs(outputFile) {
return fmt.Errorf("%q is not an absolute path", outputFile)
}
close := func(c io.Closer) {
err := c.Close()
if err != nil {
log.Warning("Failed to close %s: %s", outputFile, err)
}
}
name := profiler.Name()
defer func() {
if err != nil {
log.Warning("Failed to do %s profile to %s for %.2f second(s): %s", name, outputFile, durationSeconds, err)
}
}()
f, err := os.Create(outputFile)
if err != nil {
return err
}
err = profiler.Start(f)
if err != nil {
close(f)
return err
}
log.Info("Doing %s profile to %s for %.2f second(s)", name, outputFile, durationSeconds)
go func() {
time.Sleep(durationSecToDuration(durationSeconds))
profiler.Stop()
close(f)
delayedLog.Info("%s profile to %s done", name, outputFile)
}()
return nil
}
func doTimedProfileInDir(log libkb.LogUI, delayedLog logger.Logger,
profiler timedProfiler, dir string,
durationSeconds keybase1.DurationSec) (err error) {
name := profiler.Name()
maxFileCount := profiler.MaxFileCount()
files, err := profiler.GetSortedFiles(dir)
if err != nil {
log.Warning("Error getting %s profile files in %q: %s",
name, dir, err)
} else if len(files)+1 > maxFileCount {
// Remove old trace files.
toRemove := files[:len(files)+1-maxFileCount]
for _, path := range toRemove {
log.Info("Removing old %s profile file %q", name, path)
err := os.Remove(path)
if err != nil {
log.Warning("Error on os.Remove(%q): %s", path, err)
}
}
}
outputFile := profiler.MakeFilename(dir, time.Now(), durationSecToDuration(durationSeconds))
return doTimedProfile(log, delayedLog, profiler, outputFile, durationSeconds)
}
type cpuProfiler struct{}
func (cpuProfiler) Name() string { return "CPU" }
func (cpuProfiler) MaxFileCount() int {
return libkb.MaxCPUProfileFileCount
}
func (cpuProfiler) MakeFilename(dir string, start time.Time, duration time.Duration) string {
return libkb.MakeCPUProfileFilename(dir, start, duration)
}
func (cpuProfiler) GetSortedFiles(dir string) ([]string, error) {
return libkb.GetSortedCPUProfileFiles(dir)
}
func (cpuProfiler) Start(w io.Writer) error {
return pprof.StartCPUProfile(w)
}
func (cpuProfiler) Stop() {
pprof.StopCPUProfile()
}
func (c *PprofHandler) ProcessorProfile(_ context.Context, arg keybase1.ProcessorProfileArg) (err error) {
logui := c.getLogUI(arg.SessionID)
return doTimedProfile(logui, c.G().Log, cpuProfiler{}, arg.ProfileFile, arg.ProfileDurationSeconds)
}
func (c *PprofHandler) logDir(logDirForMobile string) string {
// Assume the returned directory already exists, i.e. we've
// already started logging.
if len(logDirForMobile) > 0 {
return logDirForMobile
}
return c.G().Env.GetLogDir()
}
func (c *PprofHandler) LogProcessorProfile(_ context.Context, arg keybase1.LogProcessorProfileArg) (err error) {
logui := c.getLogUI(arg.SessionID)
return doTimedProfileInDir(logui, c.G().Log, cpuProfiler{}, c.logDir(arg.LogDirForMobile), arg.ProfileDurationSeconds)
}
type traceProfiler struct{}
func (traceProfiler) Name() string { return "trace" }
func (traceProfiler) MaxFileCount() int { return libkb.MaxTraceFileCount }
func (traceProfiler) MakeFilename(dir string, start time.Time, duration time.Duration) string {
return libkb.MakeTraceFilename(dir, start, duration)
}
func (traceProfiler) GetSortedFiles(dir string) ([]string, error) {
return libkb.GetSortedTraceFiles(dir)
}
func (traceProfiler) Start(w io.Writer) error {
return trace.Start(w)
}
func (traceProfiler) Stop() {
trace.Stop()
}
func (c *PprofHandler) Trace(_ context.Context, arg keybase1.TraceArg) (err error) {
logui := c.getLogUI(arg.SessionID)
return doTimedProfile(logui, c.G().Log, traceProfiler{}, arg.TraceFile, arg.TraceDurationSeconds)
}
func (c *PprofHandler) LogTrace(_ context.Context, arg keybase1.LogTraceArg) (err error) {
logui := c.getLogUI(arg.SessionID)
return doTimedProfileInDir(logui, c.G().Log, traceProfiler{}, c.logDir(arg.LogDirForMobile), arg.TraceDurationSeconds)
}