@cryptotaxi247 / kubo / commits / 50fffa297

pprof: create HTTP endpoint for setting MutexProfileFraction

Allows to dynamically change the MutexProfileFraction to enable and disable mutex profiling. It should be very useful for detecting deadlocks, lock contention and general concurrency problems. How to use: To enable run: curl -X POST -v 'localhost:5001/debug/pprof-mutex/?fraction=10 To disable: curl -X POST -v 'localhost:5001/debug/pprof-mutex/?fraction=0' Fraction defines which fraction of events will be profiled. Higher it is the lower performance impact but less reliable the result. To fetch the result use: go tool pprof $PATH_TO_IPFS_BIN http://localhost:5001/debug/pprof/mutex License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>

Jakub Sztandera committed Sep 26, 2018 at 21:10 UTC 50fffa2973452862ff1a805956f324da1b887f36
2 files changed +46
cmd/ipfs/daemon.go
+1
@@ -448,6 +448,7 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error
448 corehttp.VersionOption(),
449 defaultMux("/debug/vars"),
450 defaultMux("/debug/pprof/"),
451 + corehttp.MutexFractionOption("/debug/pprof-mutex/"),
452 corehttp.MetricsScrapingOption("/debug/metrics/prometheus"),
453 corehttp.LogOption(),
454 }
core/corehttp/mutex_profile.go new
+45
@@ -0,0 +1,45 @@
1 +package corehttp
2 +
3 +import (
4 + "net"
5 + "net/http"
6 + "runtime"
7 + "strconv"
8 +
9 + core "github.com/ipfs/go-ipfs/core"
10 +)
11 +
12 +// MutexFractionOption allows to set runtime.SetMutexProfileFraction via HTTP
13 +// using POST request with parameter 'fraction'.
14 +func MutexFractionOption(path string) ServeOption {
15 + return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
16 + mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
17 + if r.Method != http.MethodPost {
18 + w.WriteHeader(http.StatusMethodNotAllowed)
19 + return
20 + }
21 + if err := r.ParseForm(); err != nil {
22 + w.WriteHeader(http.StatusBadRequest)
23 + w.Write([]byte(err.Error()))
24 + return
25 + }
26 +
27 + asfr := r.Form.Get("fraction")
28 + if len(asfr) == 0 {
29 + w.WriteHeader(http.StatusBadRequest)
30 + return
31 + }
32 +
33 + fr, err := strconv.Atoi(asfr)
34 + if err != nil {
35 + w.WriteHeader(http.StatusBadRequest)
36 + w.Write([]byte(err.Error()))
37 + return
38 + }
39 + log.Infof("Setting MutexProfileFraction to %d", fr)
40 + runtime.SetMutexProfileFraction(fr)
41 + })
42 +
43 + return mux, nil
44 + }
45 +}