From 6b5f8a6b1d9f86f82f7f49a3d9d043fd5be547ff Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Fri, 2 Oct 2020 18:42:44 +0200 Subject: [PATCH] Add options to set BlockProfileRate and MutexProfileFraction Signed-off-by: Jakub Sztandera --- cmd/lotus/pprof.go | 33 +++++++++++++++++++++++++++++++++ cmd/lotus/rpc.go | 5 +++++ 2 files changed, 38 insertions(+) create mode 100644 cmd/lotus/pprof.go diff --git a/cmd/lotus/pprof.go b/cmd/lotus/pprof.go new file mode 100644 index 0000000000..ea6823e48e --- /dev/null +++ b/cmd/lotus/pprof.go @@ -0,0 +1,33 @@ +package main + +import ( + "net/http" + "strconv" +) + +func handleFractionOpt(name string, setter func(int)) http.HandlerFunc { + return func(rw http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(rw, "only POST allowed", http.StatusMethodNotAllowed) + return + } + if err := r.ParseForm(); err != nil { + http.Error(rw, err.Error(), http.StatusBadRequest) + return + } + + asfr := r.Form.Get("x") + if len(asfr) == 0 { + http.Error(rw, "parameter 'x' must be set", http.StatusBadRequest) + return + } + + fr, err := strconv.Atoi(asfr) + if err != nil { + http.Error(rw, err.Error(), http.StatusBadRequest) + return + } + log.Infof("setting %s to %d", name, fr) + setter(fr) + } +} diff --git a/cmd/lotus/rpc.go b/cmd/lotus/rpc.go index 9718deb3ad..26d35493cc 100644 --- a/cmd/lotus/rpc.go +++ b/cmd/lotus/rpc.go @@ -7,6 +7,7 @@ import ( _ "net/http/pprof" "os" "os/signal" + "runtime" "syscall" "github.com/ipfs/go-cid" @@ -54,6 +55,10 @@ func serveRPC(a api.FullNode, stop node.StopFunc, addr multiaddr.Multiaddr, shut } http.Handle("/debug/metrics", exporter) + http.Handle("/debug/pprof-set/block", handleFractionOpt("BlockProfileRate", runtime.SetBlockProfileRate)) + http.Handle("/debug/pprof-set/mutex", handleFractionOpt("MutexProfileFraction", + func(x int) { runtime.SetMutexProfileFraction(x) }, + )) lst, err := manet.Listen(addr) if err != nil {