lotus/cli/pprof.go

58 lines
1.1 KiB
Go
Raw Permalink Normal View History

2020-08-17 09:48:12 +00:00
package cli
import (
"io"
"net/http"
"os"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
"github.com/filecoin-project/lotus/node/repo"
)
2021-03-23 23:26:59 +00:00
var PprofCmd = &cli.Command{
2020-08-18 16:27:28 +00:00
Name: "pprof",
2020-08-17 09:48:12 +00:00
Hidden: true,
Subcommands: []*cli.Command{
PprofGoroutines,
},
}
var PprofGoroutines = &cli.Command{
Name: "goroutines",
Usage: "Get goroutine stacks",
Action: func(cctx *cli.Context) error {
ti, ok := cctx.App.Metadata["repoType"]
if !ok {
log.Errorf("unknown repo type, are you sure you want to use GetAPI?")
ti = repo.FullNode
}
t, ok := ti.(repo.RepoType)
if !ok {
log.Errorf("repoType type does not match the type of repo.RepoType")
}
ainfo, err := GetAPIInfo(cctx, t)
if err != nil {
2021-08-16 11:33:08 +00:00
return xerrors.Errorf("could not get API info for %s: %w", t, err)
2020-08-17 09:48:12 +00:00
}
addr, err := ainfo.Host()
2020-08-17 09:48:12 +00:00
if err != nil {
return err
}
2020-08-18 16:27:28 +00:00
addr = "http://" + addr + "/debug/pprof/goroutine?debug=2"
2020-08-17 09:48:12 +00:00
r, err := http.Get(addr) //nolint:gosec
2020-08-17 09:48:12 +00:00
if err != nil {
return err
}
if _, err := io.Copy(os.Stdout, r.Body); err != nil {
return err
}
return r.Body.Close()
},
}