Skip to content

Commit

Permalink
pprof: create HTTP endpoint for setting MutexProfileFraction
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Kubuxu committed Sep 26, 2018
1 parent 1e0d53f commit ca014c0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error
corehttp.VersionOption(),
defaultMux("/debug/vars"),
defaultMux("/debug/pprof/"),
corehttp.MutexFractionOption("/debug/pprof-mutex/"),
corehttp.MetricsScrapingOption("/debug/metrics/prometheus"),
corehttp.LogOption(),
}
Expand Down
43 changes: 43 additions & 0 deletions core/corehttp/mutex_profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package corehttp

import (
"net"
"net/http"
"runtime"
"strconv"

core "github.com/ipfs/go-ipfs/core"
)

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 {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
if err := r.ParseForm(); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}

asfr := r.Form.Get("fraction")
if len(asfr) == 0 {
w.WriteHeader(http.StatusBadRequest)
return
}

fr, err := strconv.Atoi(asfr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
log.Infof("Setting MutexProfileFraction to %d", fr)
runtime.SetMutexProfileFraction(fr)
})

return mux, nil
}
}

0 comments on commit ca014c0

Please sign in to comment.