@@ -2,13 +2,18 @@ package main
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
7
+ "net/http"
8
+ _ "net/http/pprof"
6
9
"os"
7
10
"os/signal"
8
11
"runtime/pprof"
12
+ "time"
9
13
10
14
logging "github.com/ipfs/go-log/v2"
11
15
"github.com/urfave/cli/v2"
16
+ "golang.org/x/xerrors"
12
17
13
18
"github.com/filecoin-project/lotus/build"
14
19
)
@@ -120,6 +125,16 @@ func main() {
120
125
Name : "pprof" ,
121
126
Usage : "specify name of file for writing cpu profile to" ,
122
127
},
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
+ },
123
138
},
124
139
Before : func (cctx * cli.Context ) error {
125
140
if prof := cctx .String ("pprof" ); prof != "" {
@@ -133,6 +148,19 @@ func main() {
133
148
}
134
149
}
135
150
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
+
136
164
return logging .SetLogLevel ("lotus-shed" , cctx .String ("log-level" ))
137
165
},
138
166
After : func (cctx * cli.Context ) error {
0 commit comments