Skip to content

Commit 098f785

Browse files
gzliudanwanwiset25
authored andcommitted
cmd/utils: allow file descriptor limit to be set via CLI (ethereum#24477)
1 parent 38a79e1 commit 098f785

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

cmd/XDC/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ var (
9393
//utils.CacheDatabaseFlag,
9494
//utils.CacheGCFlag,
9595
//utils.TrieCacheGenFlag,
96+
utils.FDLimitFlag,
9697
utils.ListenPortFlag,
9798
utils.MaxPeersFlag,
9899
utils.MaxPendingPeersFlag,

cmd/XDC/usage.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,16 @@ var AppHelpFlagGroups = []flagGroup{
123123
// utils.TxPoolLifetimeFlag,
124124
// },
125125
//},
126-
//{
127-
// Name: "PERFORMANCE TUNING",
128-
// Flags: []cli.Flag{
129-
// utils.CacheFlag,
130-
// utils.CacheDatabaseFlag,
131-
// utils.CacheGCFlag,
132-
// utils.TrieCacheGenFlag,
133-
// },
134-
//},
126+
{
127+
Name: "PERFORMANCE TUNING",
128+
Flags: []cli.Flag{
129+
// utils.CacheFlag,
130+
// utils.CacheDatabaseFlag,
131+
// utils.CacheGCFlag,
132+
// utils.TrieCacheGenFlag,
133+
utils.FDLimitFlag,
134+
},
135+
},
135136
{
136137
Name: "ACCOUNT",
137138
Flags: []cli.Flag{

cmd/gc/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type ResultProcessNode struct {
5252

5353
func main() {
5454
flag.Parse()
55-
db, _ := leveldb.New(*dir, ethconfig.Defaults.DatabaseCache, utils.MakeDatabaseHandles(), "")
55+
db, _ := leveldb.New(*dir, ethconfig.Defaults.DatabaseCache, utils.MakeDatabaseHandles(0), "")
5656
lddb := rawdb.NewDatabase(db)
5757
head := core.GetHeadBlockHash(lddb)
5858
currentHeader := core.GetHeader(lddb, head, core.GetBlockNumber(lddb, head))

cmd/utils/flags.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ var (
313313
Usage: "Percentage of cache memory allowance to use for trie pruning",
314314
Value: 25,
315315
}
316+
FDLimitFlag = cli.IntFlag{
317+
Name: "fdlimit",
318+
Usage: "Raise the open file descriptor resource limit (default = system fd limit)",
319+
}
316320
// Miner settings
317321
StakingEnabledFlag = cli.BoolFlag{
318322
Name: "mine",
@@ -816,11 +820,24 @@ func setPrefix(ctx *cli.Context, cfg *node.Config) {
816820

817821
// MakeDatabaseHandles raises out the number of allowed file handles per process
818822
// for XDC and returns half of the allowance to assign to the database.
819-
func MakeDatabaseHandles() int {
823+
func MakeDatabaseHandles(max int) int {
820824
limit, err := fdlimit.Maximum()
821825
if err != nil {
822826
Fatalf("Failed to retrieve file descriptor allowance: %v", err)
823827
}
828+
switch {
829+
case max == 0:
830+
// User didn't specify a meaningful value, use system limits
831+
case max < 128:
832+
// User specified something unhealthy, just use system defaults
833+
log.Error("File descriptor limit invalid (<128)", "had", max, "updated", limit)
834+
case max > limit:
835+
// User requested more than the OS allows, notify that we can't allocate it
836+
log.Warn("Requested file descriptors denied by OS", "req", max, "limit", limit)
837+
default:
838+
// User limit is meaningful and within allowed range, use that
839+
limit = max
840+
}
824841
raised, err := fdlimit.Raise(uint64(limit))
825842
if err != nil {
826843
Fatalf("Failed to raise file descriptor allowance: %v", err)
@@ -1178,7 +1195,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
11781195
if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheDatabaseFlag.Name) {
11791196
cfg.DatabaseCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheDatabaseFlag.Name) / 100
11801197
}
1181-
cfg.DatabaseHandles = MakeDatabaseHandles()
1198+
cfg.DatabaseHandles = MakeDatabaseHandles(ctx.GlobalInt(FDLimitFlag.Name))
11821199

11831200
if gcmode := ctx.GlobalString(GCModeFlag.Name); gcmode != "full" && gcmode != "archive" {
11841201
Fatalf("--%s must be either 'full' or 'archive'", GCModeFlag.Name)
@@ -1267,7 +1284,7 @@ func SetupNetwork(ctx *cli.Context) {
12671284
func MakeChainDatabase(ctx *cli.Context, stack *node.Node) ethdb.Database {
12681285
var (
12691286
cache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheDatabaseFlag.Name) / 100
1270-
handles = MakeDatabaseHandles()
1287+
handles = MakeDatabaseHandles(ctx.GlobalInt(FDLimitFlag.Name))
12711288
)
12721289
name := "chaindata"
12731290
if ctx.GlobalBool(LightModeFlag.Name) {

0 commit comments

Comments
 (0)