This repository was archived by the owner on Mar 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug_profiles.go
314 lines (270 loc) · 7 KB
/
debug_profiles.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
package go_webserver
import (
"bytes"
"context"
"errors"
"fmt"
"html"
"io"
httpprof "net/http/pprof"
"net/url"
"runtime/pprof"
"strings"
"time"
pprof_profile "github.com/google/pprof/profile"
)
// -----------------------------------------------------------------------------
type debugProfile struct {
name string
profile *pprof.Profile
handler HandlerFunc
}
type flameGraphNode struct {
FunctionName string `json:"functionName"`
FileName string `json:"fileName"`
Line int64 `json:"line"`
Nanoseconds int64 `json:"nanos"`
SelfNanoseconds int64 `json:"selfNanos"`
Children []*flameGraphNode `json:"childs,omitempty"`
}
// -----------------------------------------------------------------------------
var debugProfiles []debugProfile
// -----------------------------------------------------------------------------
// ServeDebugProfiles adds the GO runtime profile handlers to a web server
func (srv *Server) ServeDebugProfiles(basePath string, middlewares ...HandlerFunc) {
// Prepare debug profile array if not done yet
if debugProfiles == nil {
for _, profile := range pprof.Profiles() {
debugProfiles = append(debugProfiles, debugProfile{
name: profile.Name(),
profile: profile,
handler: NewHandlerFromHttpHandler(httpprof.Handler(profile.Name())),
})
}
debugProfiles = append(debugProfiles, debugProfile{
name: "cmdline",
handler: NewHandlerFromHttpHandlerFunc(httpprof.Cmdline),
})
debugProfiles = append(debugProfiles, debugProfile{
name: "profile",
handler: onGenerateProfile, // NewHandlerFromHttpHandlerFunc(httpprof.Profile),
})
debugProfiles = append(debugProfiles, debugProfile{
name: "symbol",
handler: NewHandlerFromHttpHandlerFunc(httpprof.Symbol),
})
debugProfiles = append(debugProfiles, debugProfile{
name: "trace",
handler: NewHandlerFromHttpHandlerFunc(httpprof.Trace),
})
}
// Fix slashes
if !strings.HasPrefix(basePath, "/") {
basePath = "/" + basePath
}
if strings.HasSuffix(basePath, "/") {
basePath = basePath[:len(basePath)-1]
}
// Add index page
srv.GET(basePath, onDebugProfilesIndex, middlewares...)
// Add profile pages
for _, p := range debugProfiles {
srv.GET(basePath+"/"+p.name, p.handler, middlewares...)
}
}
func onDebugProfilesIndex(req *RequestContext) error {
var b bytes.Buffer
// Get base url
path := string(req.URI().Path())
if !strings.HasSuffix(path, "/") {
path += "/"
}
req.SetResponseHeader("X-Content-Type-Options", "nosniff")
req.SetResponseHeader("Content-Type", "text/html; charset=utf-8")
// Write index header
_, _ = b.WriteString(`<!doctype html>
<html>
<head>
<title>Debug profiles</title>
<style>
body {
font-family: monospace;
}
table {
border-collapse:collapse;
}
td {
padding: 2px;
}
td.header {
border-bottom: 1px solid #000;
}
td.vsep {
border-right: 1px solid #000;
}
td.ralign {
text-align: right;
}
</style>
</head>
<body>
<table>
<thead>
<td class='header vsep'>Profile</td>
<td class='header ralign'>Count</td>
</thead>
<tbody>
`)
// Write each profile on main table
for _, p := range debugProfiles {
link := &url.URL{
Path: path + p.name,
}
if p.profile != nil {
link.RawQuery = "debug=1"
} else if p.name == "profile" {
link.RawQuery = "seconds=15"
}
_, _ = fmt.Fprintf(&b, ` <tr>
<td class='vsep'><a href='%s'>%s</a>`, link, html.EscapeString(p.name))
switch p.name {
case "goroutine":
link.RawQuery = "debug=2"
_, _ = fmt.Fprintf(&b, ` (<a href='%s'>full</a>)`, link)
}
if p.profile != nil {
_, _ = fmt.Fprintf(&b, `</td>
<td class='ralign'>%d</td>
</tr>
`, p.profile.Count())
} else {
_, _ = b.WriteString(`</td>
<td></td>
</tr>
`)
}
}
// Close table and html pae
_, _ = b.WriteString(` </tbody>
</table>
</body>
</html>
`)
// Write response
_, _ = req.Write(b.Bytes())
req.Success()
// Done
return nil
}
func onGenerateProfile(req *RequestContext) error {
secs, err := req.QueryArgs().GetUint("seconds")
if secs <= 0 || err != nil {
secs = 30
}
format := req.QueryArgs().Peek("format")
if len(format) == 0 {
format = req.QueryArgs().Peek("fmt")
}
switch string(format) {
case "":
fallthrough
case "binary":
req.SetResponseHeader("Content-Type", "application/octet-stream")
req.SetResponseHeader("Content-Disposition", `attachment; filename="profile"`)
err = gatherCpuProfile(req, secs, req)
if err != nil {
req.InternalServerError(fmt.Sprintf("CPU profiling failed [err=%s]", err))
return nil
}
case "json":
var b bytes.Buffer
var pf *pprof_profile.Profile
var flameGraphRootNode *flameGraphNode
err = gatherCpuProfile(req, secs, &b)
if err != nil {
req.InternalServerError(fmt.Sprintf("CPU profiling failed [err=%s]", err))
return nil
}
// Use google/pprof to parse the profile
pf, err = pprof_profile.Parse(&b)
if err != nil {
req.InternalServerError(fmt.Sprintf("Unable to parse CPU profile [err=%s]", err))
return nil
}
flameGraphRootNode, err = createFlameGraph(pf)
if err != nil {
req.InternalServerError(fmt.Sprintf("Unable to create flame graph [err=%s]", err))
return nil
}
req.WriteJSON(flameGraphRootNode)
default:
req.BadRequest("Unsupported format")
}
// Done
return nil
}
func gatherCpuProfile(ctx context.Context, secs int, w io.Writer) error {
err := pprof.StartCPUProfile(w)
if err != nil {
return err
}
defer pprof.StopCPUProfile()
select {
case <-time.After(time.Duration(secs) * time.Second):
case <-ctx.Done():
return ctx.Err()
}
// Done
return nil
}
// Parse the pprof profile and collapse the stacks into a flame graph structure.
func createFlameGraph(profile *pprof_profile.Profile) (*flameGraphNode, error) {
root := &flameGraphNode{
FunctionName: "root",
}
cpuTimeIdx := -1
for idx, sampleType := range profile.SampleType {
if sampleType.Type == "cpu" && sampleType.Unit == "nanoseconds" {
cpuTimeIdx = idx
break
}
}
if cpuTimeIdx < 0 {
return nil, errors.New("unable to find cpu time information")
}
// Iterate through the profile's samples.
for _, sample := range profile.Sample {
root.Nanoseconds += sample.Value[cpuTimeIdx]
node := root
for i := len(sample.Location) - 1; i >= 0; i-- {
var childNode *flameGraphNode
// Locate child node
if len(sample.Location[i].Line) == 0 {
continue
}
functionName := sample.Location[i].Line[0].Function.Name
for _, child := range node.Children {
if child.FunctionName == functionName {
childNode = child
break
}
}
// Create a new child node if not found
if childNode == nil {
childNode = &flameGraphNode{
FunctionName: functionName,
FileName: sample.Location[i].Line[0].Function.Filename,
Line: sample.Location[i].Line[0].Line,
}
node.Children = append(node.Children, childNode)
}
childNode.Nanoseconds += sample.Value[cpuTimeIdx]
if i == 0 {
childNode.SelfNanoseconds += sample.Value[cpuTimeIdx]
}
node = childNode
}
}
// Done
return root, nil
}