diff --git a/cmd/ipfs/daemon.go b/cmd/ipfs/daemon.go index 9d2ca22696e8..01c111935ced 100644 --- a/cmd/ipfs/daemon.go +++ b/cmd/ipfs/daemon.go @@ -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(), } diff --git a/core/corehttp/mutex_profile.go b/core/corehttp/mutex_profile.go new file mode 100644 index 000000000000..6f629746e95a --- /dev/null +++ b/core/corehttp/mutex_profile.go @@ -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 + } +}