feat: add endpoint for enabling block profiling (#8469)
Gus Eggert committed
Mar 11, 2022 at 14:32 UTC
0487f03eaea8f0207e2dec65809bc678813a1ec3
3 files changed
+49
cmd/ipfs/daemon.go
+1
@@ -661,6 +661,7 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error
661
defaultMux("/debug/vars"),
662
defaultMux("/debug/pprof/"),
663
corehttp.MutexFractionOption("/debug/pprof-mutex/"),
664
+ corehttp.BlockProfileRateOption("/debug/pprof-block/"),
665
corehttp.MetricsScrapingOption("/debug/metrics/prometheus"),
666
corehttp.LogOption(),
667
}
core/corehttp/mutex_profile.go
+35
@@ -41,3 +41,38 @@ func MutexFractionOption(path string) ServeOption {
41
return mux, nil
42
}
43
}
44
+
45
+// BlockProfileRateOption allows to set runtime.SetBlockProfileRate via HTTP
46
+// using POST request with parameter 'rate'.
47
+// The profiler tries to sample 1 event every <rate> nanoseconds.
48
+// If rate == 1, then the profiler samples every blocking event.
49
+// To disable, set rate = 0.
50
+func BlockProfileRateOption(path string) ServeOption {
51
+ return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
52
+ mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
53
+ if r.Method != http.MethodPost {
54
+ http.Error(w, "only POST allowed", http.StatusMethodNotAllowed)
55
+ return
56
+ }
57
+ if err := r.ParseForm(); err != nil {
58
+ http.Error(w, err.Error(), http.StatusBadRequest)
59
+ return
60
+ }
61
+
62
+ rateStr := r.Form.Get("rate")
63
+ if len(rateStr) == 0 {
64
+ http.Error(w, "parameter 'rate' must be set", http.StatusBadRequest)
65
+ return
66
+ }
67
+
68
+ rate, err := strconv.Atoi(rateStr)
69
+ if err != nil {
70
+ http.Error(w, err.Error(), http.StatusBadRequest)
71
+ return
72
+ }
73
+ log.Infof("Setting BlockProfileRate to %d", rate)
74
+ runtime.SetBlockProfileRate(rate)
75
+ })
76
+ return mux, nil
77
+ }
78
+}
test/sharness/t0110-gateway.sh
+13
@@ -163,6 +163,19 @@ test_expect_success "test failure conditions of mutex pprof endpoint" '
163
test_must_fail curl -f -X GET "http://127.0.0.1:$apiport/debug/pprof-mutex/?fraction=-1"
164
'
165
166
+curl_pprofblock() {
167
+ curl -f -X POST "http://127.0.0.1:$apiport/debug/pprof-block/?rate=$1"
168
+}
169
+
170
+test_expect_success "set blocking profiler rate for pprof (0 so it doesn't enable)" '
171
+ curl_pprofblock 0
172
+'
173
+
174
+test_expect_success "test failure conditions of mutex block endpoint" '
175
+ test_must_fail curl_pprofblock &&
176
+ test_must_fail curl_pprofblock that_is_string &&
177
+ test_must_fail curl -f -X GET "http://127.0.0.1:$apiport/debug/pprof-block/?rate=0"
178
+'
179
180
test_expect_success "setup index hash" '
181
mkdir index &&