From e90958cd29a228b051faeaa25d66e053cf9d2228 Mon Sep 17 00:00:00 2001 From: Péter Szilágyi Date: Fri, 19 Feb 2016 14:29:19 +0200 Subject: cmd, eth, ethdb, node: prioritise chaindata for resources, bump cache --- cmd/utils/fdlimit_test.go | 35 +++++++++++++++++++++++++++++++ cmd/utils/fdlimit_unix.go | 50 ++++++++++++++++++++++++++++++++++++++++++++ cmd/utils/fdlimit_windows.go | 41 ++++++++++++++++++++++++++++++++++++ cmd/utils/flags.go | 22 +++++++++++++++++-- 4 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 cmd/utils/fdlimit_test.go create mode 100644 cmd/utils/fdlimit_unix.go create mode 100644 cmd/utils/fdlimit_windows.go (limited to 'cmd/utils') diff --git a/cmd/utils/fdlimit_test.go b/cmd/utils/fdlimit_test.go new file mode 100644 index 000000000..0a950a6c9 --- /dev/null +++ b/cmd/utils/fdlimit_test.go @@ -0,0 +1,35 @@ +// Copyright 2016 The go-ethereum Authors +// This file is part of go-ethereum. +// +// go-ethereum is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// go-ethereum is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with go-ethereum. If not, see . + +package utils + +import "testing" + +// TestFileDescriptorLimits simply tests whether the file descriptor allowance +// per this process can be retrieved. +func TestFileDescriptorLimits(t *testing.T) { + target := 4096 + + if limit, err := getFdLimit(); err != nil || limit <= 0 { + t.Fatalf("failed to retrieve file descriptor limit (%d): %v", limit, err) + } + if err := raiseFdLimit(uint64(target)); err != nil { + t.Fatalf("failed to raise file allowance") + } + if limit, err := getFdLimit(); err != nil || limit < target { + t.Fatalf("failed to retrieve raised descriptor limit (have %v, want %v): %v", limit, target, err) + } +} diff --git a/cmd/utils/fdlimit_unix.go b/cmd/utils/fdlimit_unix.go new file mode 100644 index 000000000..2a6dffc8f --- /dev/null +++ b/cmd/utils/fdlimit_unix.go @@ -0,0 +1,50 @@ +// Copyright 2016 The go-ethereum Authors +// This file is part of go-ethereum. +// +// go-ethereum is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// go-ethereum is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with go-ethereum. If not, see . + +// +build linux darwin + +package utils + +import "syscall" + +// raiseFdLimit tries to maximize the file descriptor allowance of this process +// to the maximum hard-limit allowed by the OS. +func raiseFdLimit(max uint64) error { + // Get the current limit + var limit syscall.Rlimit + if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { + return err + } + // Try to update the limit to the max allowance + limit.Cur = limit.Max + if limit.Cur > max { + limit.Cur = max + } + if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { + return err + } + return nil +} + +// getFdLimit retrieves the number of file descriptors allowed to be opened by this +// process. +func getFdLimit() (int, error) { + var limit syscall.Rlimit + if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { + return 0, err + } + return int(limit.Cur), nil +} diff --git a/cmd/utils/fdlimit_windows.go b/cmd/utils/fdlimit_windows.go new file mode 100644 index 000000000..53aad3d7a --- /dev/null +++ b/cmd/utils/fdlimit_windows.go @@ -0,0 +1,41 @@ +// Copyright 2016 The go-ethereum Authors +// This file is part of go-ethereum. +// +// go-ethereum is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// go-ethereum is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with go-ethereum. If not, see . + +package utils + +import "errors" + +// raiseFdLimit tries to maximize the file descriptor allowance of this process +// to the maximum hard-limit allowed by the OS. +func raiseFdLimit(max uint64) error { + // This method is NOP by design: + // * Linux/Darwin counterparts need to manually increase per process limits + // * On Windows Go uses the CreateFile API, which is limited to 16K files, non + // changeable from within a running process + // This way we can always "request" raising the limits, which will either have + // or not have effect based on the platform we're running on. + if max > 16384 { + return errors.New("file descriptor limit (16384) reached") + } + return nil +} + +// getFdLimit retrieves the number of file descriptors allowed to be opened by this +// process. +func getFdLimit() (int, error) { + // Please see raiseFdLimit for the reason why we use hard coded 16K as the limit + return 16384, nil +} diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 3efb65e42..69fb0b9db 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -143,7 +143,7 @@ var ( CacheFlag = cli.IntFlag{ Name: "cache", Usage: "Megabytes of memory allocated to internal caching (min 16MB / database forced)", - Value: 0, + Value: 128, } BlockchainVersionFlag = cli.IntFlag{ Name: "blockchainversion", @@ -527,6 +527,22 @@ func MakeGenesisBlock(ctx *cli.Context) string { return string(data) } +// MakeDatabaseHandles raises out the number of allowed file handles per process +// for Geth and returns half of the allowance to assign to the database. +func MakeDatabaseHandles() int { + if err := raiseFdLimit(2048); err != nil { + Fatalf("Failed to raise file descriptor allowance: %v", err) + } + limit, err := getFdLimit() + if err != nil { + Fatalf("Failed to retrieve file descriptor allowance: %v", err) + } + if limit > 2048 { // cap database file descriptors even if more is available + limit = 2048 + } + return limit / 2 // Leave half for networking and other stuff +} + // MakeAccountManager creates an account manager from set command line flags. func MakeAccountManager(ctx *cli.Context) *accounts.Manager { // Create the keystore crypto primitive, light if requested @@ -649,6 +665,7 @@ func MakeSystemNode(name, version string, extra []byte, ctx *cli.Context) *node. FastSync: ctx.GlobalBool(FastSyncFlag.Name), BlockChainVersion: ctx.GlobalInt(BlockchainVersionFlag.Name), DatabaseCache: ctx.GlobalInt(CacheFlag.Name), + DatabaseHandles: MakeDatabaseHandles(), NetworkId: ctx.GlobalInt(NetworkIdFlag.Name), AccountManager: accman, Etherbase: MakeEtherbase(accman, ctx), @@ -763,9 +780,10 @@ func SetupVM(ctx *cli.Context) { func MakeChain(ctx *cli.Context) (chain *core.BlockChain, chainDb ethdb.Database) { datadir := MustMakeDataDir(ctx) cache := ctx.GlobalInt(CacheFlag.Name) + handles := MakeDatabaseHandles() var err error - if chainDb, err = ethdb.NewLDBDatabase(filepath.Join(datadir, "chaindata"), cache); err != nil { + if chainDb, err = ethdb.NewLDBDatabase(filepath.Join(datadir, "chaindata"), cache, handles); err != nil { Fatalf("Could not open database: %v", err) } if ctx.GlobalBool(OlympicFlag.Name) { -- cgit