Skip to content

Commit e00c2a7

Browse files
committed
Configurable pprof server
- Read from flags - Read from TOML config file - Update toml files with default values
1 parent 60ac9b4 commit e00c2a7

File tree

20 files changed

+255
-13
lines changed

20 files changed

+255
-13
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
@@ -100,3 +100,21 @@ var (
100100
// DefaultWSEnabled enables the WS server
101101
DefaultWSEnabled = true
102102
)
103+
104+
const (
105+
// PprofConfig
106+
107+
// DefaultPprofEnabled to indicate the pprof http server should be enabled or not.
108+
DefaultPprofEnabled = true
109+
110+
// DefaultPprofListeningAddress default pprof HTTP server listening address.
111+
DefaultPprofListeningAddress = "localhost:6060"
112+
113+
// DefaultPprofBlockRate default block profile rate.
114+
// Set to 0 to disable profiling.
115+
DefaultPprofBlockRate = 0
116+
117+
// DefaultPprofMutexRate default mutex profile rate.
118+
// Set to 0 to disable profiling.
119+
DefaultPprofMutexRate = 0
120+
)

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
@@ -106,3 +106,21 @@ var (
106106
// DefaultRPCWSPort rpc websocket port
107107
DefaultRPCWSPort = uint32(8546)
108108
)
109+
110+
const (
111+
// PprofConfig
112+
113+
// DefaultPprofEnabled to indicate the pprof http server should be enabled or not.
114+
DefaultPprofEnabled = true
115+
116+
// DefaultPprofListeningAddress default pprof HTTP server listening address.
117+
DefaultPprofListeningAddress = "localhost:6060"
118+
119+
// DefaultPprofBlockRate default block profile rate.
120+
// Set to 0 to disable profiling.
121+
DefaultPprofBlockRate = 0
122+
123+
// DefaultPprofMutexRate default mutex profile rate.
124+
// Set to 0 to disable profiling.
125+
DefaultPprofMutexRate = 0
126+
)

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
@@ -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 = false
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/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
@@ -93,3 +93,21 @@ var (
9393
// DefaultRPCWSPort rpc websocket port
9494
DefaultRPCWSPort = uint32(8546)
9595
)
96+
97+
const (
98+
// PprofConfig
99+
100+
// DefaultPprofEnabled to indicate the pprof http server should be enabled or not.
101+
DefaultPprofEnabled = false
102+
103+
// DefaultPprofListeningAddress default pprof HTTP server listening address.
104+
DefaultPprofListeningAddress = "localhost:6060"
105+
106+
// DefaultPprofBlockRate default block profile rate.
107+
// Set to 0 to disable profiling.
108+
DefaultPprofBlockRate = 0
109+
110+
// DefaultPprofMutexRate default mutex profile rate.
111+
// Set to 0 to disable profiling.
112+
DefaultPprofMutexRate = 0
113+
)

cmd/gossamer/config.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ func createDotConfig(ctx *cli.Context) (*dot.Config, error) {
150150
setDotCoreConfig(ctx, tomlCfg.Core, &cfg.Core)
151151
setDotNetworkConfig(ctx, tomlCfg.Network, &cfg.Network)
152152
setDotRPCConfig(ctx, tomlCfg.RPC, &cfg.RPC)
153+
setDotPprofConfig(ctx, tomlCfg.Pprof, &cfg.Pprof)
153154

154155
if rewind := ctx.GlobalInt(RewindFlag.Name); rewind != 0 {
155156
cfg.State.Rewind = rewind
@@ -872,3 +873,49 @@ func updateDotConfigFromGenesisData(ctx *cli.Context, cfg *dot.Config) error {
872873

873874
return nil
874875
}
876+
877+
func setDotPprofConfig(ctx *cli.Context, tomlCfg ctoml.PprofConfig, cfg *dot.PprofConfig) {
878+
if !cfg.Enabled {
879+
// only allow to enable pprof from the TOML configuration.
880+
// If it is enabled by default, it cannot be disabled.
881+
cfg.Enabled = tomlCfg.Enabled
882+
}
883+
884+
if tomlCfg.ListeningAddress != "" {
885+
cfg.Settings.ListeningAddress = tomlCfg.ListeningAddress
886+
}
887+
888+
if tomlCfg.BlockRate > 0 {
889+
// block rate must be 0 (disabled) by default, since we
890+
// cannot disable it here.
891+
cfg.Settings.BlockProfileRate = tomlCfg.BlockRate
892+
}
893+
894+
if tomlCfg.MutexRate > 0 {
895+
// mutex rate must be 0 (disabled) by default, since we
896+
// cannot disable it here.
897+
cfg.Settings.MutexProfileRate = tomlCfg.MutexRate
898+
}
899+
900+
// check --pprofserver flag and update node configuration
901+
if enabled := ctx.GlobalBool(PprofServerFlag.Name); enabled || cfg.Enabled {
902+
cfg.Enabled = true
903+
} else if ctx.IsSet(PprofServerFlag.Name) && !enabled {
904+
cfg.Enabled = false
905+
}
906+
907+
// check --pprofaddress flag and update node configuration
908+
if address := ctx.GlobalString(PprofAddressFlag.Name); address != "" {
909+
cfg.Settings.ListeningAddress = address
910+
}
911+
912+
if rate := ctx.GlobalInt(PprofBlockRateFlag.Name); rate > 0 {
913+
cfg.Settings.BlockProfileRate = rate
914+
}
915+
916+
if rate := ctx.GlobalInt(PprofMutexRateFlag.Name); rate > 0 {
917+
cfg.Settings.MutexProfileRate = rate
918+
}
919+
920+
logger.Debug("pprof configuration: " + cfg.String())
921+
}

cmd/gossamer/config_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,7 @@ func TestUpdateConfigFromGenesisJSON(t *testing.T) {
792792
Network: testCfg.Network,
793793
RPC: testCfg.RPC,
794794
System: testCfg.System,
795+
Pprof: testCfg.Pprof,
795796
}
796797

797798
cfg, err := createDotConfig(ctx)
@@ -846,6 +847,7 @@ func TestUpdateConfigFromGenesisJSON_Default(t *testing.T) {
846847
Network: testCfg.Network,
847848
RPC: testCfg.RPC,
848849
System: testCfg.System,
850+
Pprof: testCfg.Pprof,
849851
}
850852

851853
cfg, err := createDotConfig(ctx)
@@ -904,6 +906,7 @@ func TestUpdateConfigFromGenesisData(t *testing.T) {
904906
},
905907
RPC: testCfg.RPC,
906908
System: testCfg.System,
909+
Pprof: testCfg.Pprof,
907910
}
908911

909912
cfg, err := createDotConfig(ctx)

0 commit comments

Comments
 (0)