@@ -2,13 +2,18 @@ package main
22
33import (
44 "context"
5+ "errors"
56 "fmt"
7+ "net/http"
8+ _ "net/http/pprof"
69 "os"
710 "os/signal"
811 "runtime/pprof"
12+ "time"
913
1014 logging "github.com/ipfs/go-log/v2"
1115 "github.com/urfave/cli/v2"
16+ "golang.org/x/xerrors"
1217
1318 "github.com/filecoin-project/lotus/build"
1419)
@@ -120,6 +125,16 @@ func main() {
120125 Name : "pprof" ,
121126 Usage : "specify name of file for writing cpu profile to" ,
122127 },
128+ & cli.UintFlag {
129+ Name : "pprof-port" ,
130+ Usage : "specify port to run pprof server on" ,
131+ Action : func (_ * cli.Context , port uint ) error {
132+ if port > 65535 {
133+ return xerrors .New ("invalid port number" )
134+ }
135+ return nil
136+ },
137+ },
123138 },
124139 Before : func (cctx * cli.Context ) error {
125140 if prof := cctx .String ("pprof" ); prof != "" {
@@ -133,6 +148,19 @@ func main() {
133148 }
134149 }
135150
151+ if port := cctx .Int ("pprof-port" ); port != 0 {
152+ go func () {
153+ log .Infow ("Starting pprof server" , "port" , port )
154+ server := & http.Server {
155+ Addr : fmt .Sprintf ("localhost:%d" , port ),
156+ ReadHeaderTimeout : 5 * time .Second ,
157+ }
158+ if err := server .ListenAndServe (); err != nil && ! errors .Is (err , http .ErrServerClosed ) {
159+ log .Errorw ("pprof server stopped unexpectedly" , "err" , err )
160+ }
161+ }()
162+ }
163+
136164 return logging .SetLogLevel ("lotus-shed" , cctx .String ("log-level" ))
137165 },
138166 After : func (cctx * cli.Context ) error {
0 commit comments