Skip to content

Commit 5996229

Browse files
qdm12timwu20
authored andcommitted
feat(pprof): Pprof HTTP server service (ChainSafe#1991)
- Add a pprof HTTP server service to dot node - `internal/httpserver` package - `internal/pprof` package - Configurable server listening address from flags and toml config file - Configurable pprof server on/off from flags and toml config file - Configurable mutex profile fraction from flags and toml config file - Configurable block profile rate from flags and toml config file - Pprof documentation - Remove pprof to file operation and `--cpuprof` and `--memprof` flags
1 parent 88174ca commit 5996229

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1177
-139
lines changed

chain/dev/config.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,9 @@ port = 8545
3838
host = "localhost"
3939
modules = ["system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate", "syncstate", "payment"]
4040
ws-port = 8546
41+
42+
[pprof]
43+
enabled = false
44+
listening-address = "localhost:6060"
45+
block-rate = 0
46+
mutex-rate = 0

chain/dev/defaults.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,21 @@ var (
8686
// DefaultWSEnabled enables the WS server
8787
DefaultWSEnabled = true
8888
)
89+
90+
const (
91+
// PprofConfig
92+
93+
// DefaultPprofEnabled to indicate the pprof http server should be enabled or not.
94+
DefaultPprofEnabled = true
95+
96+
// DefaultPprofListeningAddress default pprof HTTP server listening address.
97+
DefaultPprofListeningAddress = "localhost:6060"
98+
99+
// DefaultPprofBlockRate default block profile rate.
100+
// Set to 0 to disable profiling.
101+
DefaultPprofBlockRate = 0
102+
103+
// DefaultPprofMutexRate default mutex profile rate.
104+
// Set to 0 to disable profiling.
105+
DefaultPprofMutexRate = 0
106+
)

chain/gssmr/config.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,9 @@ port = 8545
3939
host = "localhost"
4040
modules = ["system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate", "syncstate", "payment"]
4141
ws-port = 8546
42+
43+
[pprof]
44+
enabled = false
45+
listening-address = "localhost:6060"
46+
block-rate = 0
47+
mutex-rate = 0

chain/gssmr/defaults.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,21 @@ var (
9292
// DefaultRPCWSPort rpc websocket port
9393
DefaultRPCWSPort = uint32(8546)
9494
)
95+
96+
const (
97+
// PprofConfig
98+
99+
// DefaultPprofEnabled to indicate the pprof http server should be enabled or not.
100+
DefaultPprofEnabled = true
101+
102+
// DefaultPprofListeningAddress default pprof HTTP server listening address.
103+
DefaultPprofListeningAddress = "localhost:6060"
104+
105+
// DefaultPprofBlockRate default block profile rate.
106+
// Set to 0 to disable profiling.
107+
DefaultPprofBlockRate = 0
108+
109+
// DefaultPprofMutexRate default mutex profile rate.
110+
// Set to 0 to disable profiling.
111+
DefaultPprofMutexRate = 0
112+
)

chain/kusama/config.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,9 @@ modules = ["system", "author", "chain", "state", "rpc", "grandpa", "offchain", "
3939
ws-port = 8546
4040
ws = false
4141
ws-external = false
42+
43+
[pprof]
44+
enabled = false
45+
listening-address = "localhost:6060"
46+
block-rate = 0
47+
mutex-rate = 0

chain/kusama/defaults.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,21 @@ var (
7878
// DefaultRPCWSPort rpc websocket port
7979
DefaultRPCWSPort = uint32(8546)
8080
)
81+
82+
const (
83+
// PprofConfig
84+
85+
// DefaultPprofEnabled to indicate the pprof http server should be enabled or not.
86+
DefaultPprofEnabled = false
87+
88+
// DefaultPprofListeningAddress default pprof HTTP server listening address.
89+
DefaultPprofListeningAddress = "localhost:6060"
90+
91+
// DefaultPprofBlockRate default block profile rate.
92+
// Set to 0 to disable profiling.
93+
DefaultPprofBlockRate = 0
94+
95+
// DefaultPprofMutexRate default mutex profile rate.
96+
// Set to 0 to disable profiling.
97+
DefaultPprofMutexRate = 0
98+
)

chain/polkadot/config.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,10 @@ enabled = false
3535
port = 8545
3636
host = "localhost"
3737
modules = ["system", "author", "chain", "state", "rpc", "grandpa", "offchain", "childstate", "syncstate", "payment"]
38-
ws-port = 8546
38+
ws-port = 8546
39+
40+
[pprof]
41+
enabled = false
42+
listening-address = "localhost:6060"
43+
block-rate = 0
44+
mutex-rate = 0

chain/polkadot/defaults.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,21 @@ var (
7979
// DefaultRPCWSPort rpc websocket port
8080
DefaultRPCWSPort = uint32(8546)
8181
)
82+
83+
const (
84+
// PprofConfig
85+
86+
// DefaultPprofEnabled to indicate the pprof http server should be enabled or not.
87+
DefaultPprofEnabled = false
88+
89+
// DefaultPprofListeningAddress default pprof HTTP server listening address.
90+
DefaultPprofListeningAddress = "localhost:6060"
91+
92+
// DefaultPprofBlockRate default block profile rate.
93+
// Set to 0 to disable profiling.
94+
DefaultPprofBlockRate = 0
95+
96+
// DefaultPprofMutexRate default mutex profile rate.
97+
// Set to 0 to disable profiling.
98+
DefaultPprofMutexRate = 0
99+
)

cmd/gossamer/config.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ func createDotConfig(ctx *cli.Context) (*dot.Config, error) {
135135
setDotCoreConfig(ctx, tomlCfg.Core, &cfg.Core)
136136
setDotNetworkConfig(ctx, tomlCfg.Network, &cfg.Network)
137137
setDotRPCConfig(ctx, tomlCfg.RPC, &cfg.RPC)
138+
setDotPprofConfig(ctx, tomlCfg.Pprof, &cfg.Pprof)
138139

139140
if rewind := ctx.GlobalInt(RewindFlag.Name); rewind != 0 {
140141
cfg.State.Rewind = rewind
@@ -849,3 +850,49 @@ func updateDotConfigFromGenesisData(ctx *cli.Context, cfg *dot.Config) error {
849850

850851
return nil
851852
}
853+
854+
func setDotPprofConfig(ctx *cli.Context, tomlCfg ctoml.PprofConfig, cfg *dot.PprofConfig) {
855+
if !cfg.Enabled {
856+
// only allow to enable pprof from the TOML configuration.
857+
// If it is enabled by default, it cannot be disabled.
858+
cfg.Enabled = tomlCfg.Enabled
859+
}
860+
861+
if tomlCfg.ListeningAddress != "" {
862+
cfg.Settings.ListeningAddress = tomlCfg.ListeningAddress
863+
}
864+
865+
if tomlCfg.BlockRate > 0 {
866+
// block rate must be 0 (disabled) by default, since we
867+
// cannot disable it here.
868+
cfg.Settings.BlockProfileRate = tomlCfg.BlockRate
869+
}
870+
871+
if tomlCfg.MutexRate > 0 {
872+
// mutex rate must be 0 (disabled) by default, since we
873+
// cannot disable it here.
874+
cfg.Settings.MutexProfileRate = tomlCfg.MutexRate
875+
}
876+
877+
// check --pprofserver flag and update node configuration
878+
if enabled := ctx.GlobalBool(PprofServerFlag.Name); enabled || cfg.Enabled {
879+
cfg.Enabled = true
880+
} else if ctx.IsSet(PprofServerFlag.Name) && !enabled {
881+
cfg.Enabled = false
882+
}
883+
884+
// check --pprofaddress flag and update node configuration
885+
if address := ctx.GlobalString(PprofAddressFlag.Name); address != "" {
886+
cfg.Settings.ListeningAddress = address
887+
}
888+
889+
if rate := ctx.GlobalInt(PprofBlockRateFlag.Name); rate > 0 {
890+
cfg.Settings.BlockProfileRate = rate
891+
}
892+
893+
if rate := ctx.GlobalInt(PprofMutexRateFlag.Name); rate > 0 {
894+
cfg.Settings.MutexProfileRate = rate
895+
}
896+
897+
logger.Debug("pprof configuration: " + cfg.String())
898+
}

cmd/gossamer/config_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ func TestUpdateConfigFromGenesisJSON(t *testing.T) {
782782
Network: testCfg.Network,
783783
RPC: testCfg.RPC,
784784
System: testCfg.System,
785+
Pprof: testCfg.Pprof,
785786
}
786787

787788
cfg, err := createDotConfig(ctx)
@@ -836,6 +837,7 @@ func TestUpdateConfigFromGenesisJSON_Default(t *testing.T) {
836837
Network: testCfg.Network,
837838
RPC: testCfg.RPC,
838839
System: testCfg.System,
840+
Pprof: testCfg.Pprof,
839841
}
840842

841843
cfg, err := createDotConfig(ctx)
@@ -894,6 +896,7 @@ func TestUpdateConfigFromGenesisData(t *testing.T) {
894896
},
895897
RPC: testCfg.RPC,
896898
System: testCfg.System,
899+
Pprof: testCfg.Pprof,
897900
}
898901

899902
cfg, err := createDotConfig(ctx)

0 commit comments

Comments
 (0)