forked from grafana/metrictank
/
pprof.go
62 lines (56 loc) · 1.69 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
package api
import (
"net/http"
"runtime"
rpprof "runtime/pprof"
"strconv"
"time"
)
// careful when setting rates.
// see https://github.com/golang/go/issues/23401
// blockhandler writes out a blocking profile
// similar to the standard library handler,
// except it allows to specify a rate.
// The profiler aims to sample an average of one blocking event
// per rate nanoseconds spent blocked.
//
// To include every blocking event in the profile, pass rate = 1.
// Defaults to 10k (10 microseconds)
func blockHandler(w http.ResponseWriter, r *http.Request) {
debug, _ := strconv.Atoi(r.FormValue("debug"))
sec, _ := strconv.ParseInt(r.FormValue("seconds"), 10, 64)
if sec == 0 {
sec = 30
}
rate, _ := strconv.Atoi(r.FormValue("rate"))
if rate == 0 {
rate = 10000
}
w.Header().Set("Content-Type", "application/octet-stream")
runtime.SetBlockProfileRate(rate)
time.Sleep(time.Duration(sec) * time.Second)
runtime.SetBlockProfileRate(0)
p := rpprof.Lookup("block")
p.WriteTo(w, debug)
}
// mutexHandler writes out a mutex profile similar to the
// standard library handler,
// except it allows to specify a mutex profiling rate.
// On average 1/rate events are reported. The default is 1000
func mutexHandler(w http.ResponseWriter, r *http.Request) {
debug, _ := strconv.Atoi(r.FormValue("debug"))
sec, _ := strconv.ParseInt(r.FormValue("seconds"), 10, 64)
if sec == 0 {
sec = 30
}
rate, _ := strconv.Atoi(r.FormValue("rate"))
if rate == 0 {
rate = 1000
}
w.Header().Set("Content-Type", "application/octet-stream")
runtime.SetMutexProfileFraction(rate)
time.Sleep(time.Duration(sec) * time.Second)
runtime.SetMutexProfileFraction(0)
p := rpprof.Lookup("mutex")
p.WriteTo(w, debug)
}