-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
mutex_profile.go
78 lines (69 loc) · 2.15 KB
/
mutex_profile.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
package corehttp
import (
"net"
"net/http"
"runtime"
"strconv"
core "github.com/ipfs/go-ipfs/core"
)
// MutexFractionOption allows to set runtime.SetMutexProfileFraction via HTTP
// using POST request with parameter 'fraction'.
func MutexFractionOption(path string) ServeOption {
return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "only POST allowed", http.StatusMethodNotAllowed)
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
asfr := r.Form.Get("fraction")
if len(asfr) == 0 {
http.Error(w, "parameter 'fraction' must be set", http.StatusBadRequest)
return
}
fr, err := strconv.Atoi(asfr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Infof("Setting MutexProfileFraction to %d", fr)
runtime.SetMutexProfileFraction(fr)
})
return mux, nil
}
}
// BlockProfileRateOption allows to set runtime.SetBlockProfileRate via HTTP
// using POST request with parameter 'rate'.
// The profiler tries to sample 1 event every <rate> nanoseconds.
// If rate == 1, then the profiler samples every blocking event.
// To disable, set rate = 0.
func BlockProfileRateOption(path string) ServeOption {
return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "only POST allowed", http.StatusMethodNotAllowed)
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
rateStr := r.Form.Get("rate")
if len(rateStr) == 0 {
http.Error(w, "parameter 'rate' must be set", http.StatusBadRequest)
return
}
rate, err := strconv.Atoi(rateStr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Infof("Setting BlockProfileRate to %d", rate)
runtime.SetBlockProfileRate(rate)
})
return mux, nil
}
}