-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack.go
42 lines (39 loc) · 904 Bytes
/
stack.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
package keeper
import (
"log"
"net/http"
"runtime"
"runtime/pprof"
)
// HandleStack ...
func HandleStack(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
debug := 1
pf := r.FormValue("pf")
switch pf {
case "goroutine", "heap", "threadcreate", "block":
err := pprof.Lookup(pf).WriteTo(w, debug)
if err != nil {
log.Print("profile.WriteTo error", err)
}
default:
w.Write(stacks(r.FormValue("all") == "yes")) //nolint
}
}
func stacks(all bool) []byte {
// 堆栈可能很大,只输出最近的几次,这些数字应该够了,如果不够,那说明出了大问题,而且肯定不在这儿
n := 10000
if all {
n = 100000
}
var trace []byte
for i := 0; i < 5; i++ {
trace = make([]byte, n)
nbytes := runtime.Stack(trace, all)
if nbytes < len(trace) {
return trace[:nbytes]
}
n *= 2
}
return trace
}