Skip to content

Commit e90958c

Browse files
committed
cmd, eth, ethdb, node: prioritise chaindata for resources, bump cache
1 parent 05c86c2 commit e90958c

13 files changed

Lines changed: 176 additions & 24 deletions

‎cmd/utils/fdlimit_test.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2016 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// go-ethereum is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// go-ethereum is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package utils
18+
19+
import "testing"
20+
21+
// TestFileDescriptorLimits simply tests whether the file descriptor allowance
22+
// per this process can be retrieved.
23+
func TestFileDescriptorLimits(t *testing.T) {
24+
target := 4096
25+
26+
if limit, err := getFdLimit(); err != nil || limit <= 0 {
27+
t.Fatalf("failed to retrieve file descriptor limit (%d): %v", limit, err)
28+
}
29+
if err := raiseFdLimit(uint64(target)); err != nil {
30+
t.Fatalf("failed to raise file allowance")
31+
}
32+
if limit, err := getFdLimit(); err != nil || limit < target {
33+
t.Fatalf("failed to retrieve raised descriptor limit (have %v, want %v): %v", limit, target, err)
34+
}
35+
}

‎cmd/utils/fdlimit_unix.go‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2016 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// go-ethereum is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// go-ethereum is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// +build linux darwin
18+
19+
package utils
20+
21+
import "syscall"
22+
23+
// raiseFdLimit tries to maximize the file descriptor allowance of this process
24+
// to the maximum hard-limit allowed by the OS.
25+
func raiseFdLimit(max uint64) error {
26+
// Get the current limit
27+
var limit syscall.Rlimit
28+
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
29+
return err
30+
}
31+
// Try to update the limit to the max allowance
32+
limit.Cur = limit.Max
33+
if limit.Cur > max {
34+
limit.Cur = max
35+
}
36+
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
37+
return err
38+
}
39+
return nil
40+
}
41+
42+
// getFdLimit retrieves the number of file descriptors allowed to be opened by this
43+
// process.
44+
func getFdLimit() (int, error) {
45+
var limit syscall.Rlimit
46+
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
47+
return 0, err
48+
}
49+
return int(limit.Cur), nil
50+
}

‎cmd/utils/fdlimit_windows.go‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2016 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// go-ethereum is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// go-ethereum is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package utils
18+
19+
import "errors"
20+
21+
// raiseFdLimit tries to maximize the file descriptor allowance of this process
22+
// to the maximum hard-limit allowed by the OS.
23+
func raiseFdLimit(max uint64) error {
24+
// This method is NOP by design:
25+
// * Linux/Darwin counterparts need to manually increase per process limits
26+
// * On Windows Go uses the CreateFile API, which is limited to 16K files, non
27+
// changeable from within a running process
28+
// This way we can always "request" raising the limits, which will either have
29+
// or not have effect based on the platform we're running on.
30+
if max > 16384 {
31+
return errors.New("file descriptor limit (16384) reached")
32+
}
33+
return nil
34+
}
35+
36+
// getFdLimit retrieves the number of file descriptors allowed to be opened by this
37+
// process.
38+
func getFdLimit() (int, error) {
39+
// Please see raiseFdLimit for the reason why we use hard coded 16K as the limit
40+
return 16384, nil
41+
}

‎cmd/utils/flags.go‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ var (
143143
CacheFlag = cli.IntFlag{
144144
Name: "cache",
145145
Usage: "Megabytes of memory allocated to internal caching (min 16MB / database forced)",
146-
Value: 0,
146+
Value: 128,
147147
}
148148
BlockchainVersionFlag = cli.IntFlag{
149149
Name: "blockchainversion",
@@ -527,6 +527,22 @@ func MakeGenesisBlock(ctx *cli.Context) string {
527527
return string(data)
528528
}
529529

530+
// MakeDatabaseHandles raises out the number of allowed file handles per process
531+
// for Geth and returns half of the allowance to assign to the database.
532+
func MakeDatabaseHandles() int {
533+
if err := raiseFdLimit(2048); err != nil {
534+
Fatalf("Failed to raise file descriptor allowance: %v", err)
535+
}
536+
limit, err := getFdLimit()
537+
if err != nil {
538+
Fatalf("Failed to retrieve file descriptor allowance: %v", err)
539+
}
540+
if limit > 2048 { // cap database file descriptors even if more is available
541+
limit = 2048
542+
}
543+
return limit / 2 // Leave half for networking and other stuff
544+
}
545+
530546
// MakeAccountManager creates an account manager from set command line flags.
531547
func MakeAccountManager(ctx *cli.Context) *accounts.Manager {
532548
// Create the keystore crypto primitive, light if requested
@@ -649,6 +665,7 @@ func MakeSystemNode(name, version string, extra []byte, ctx *cli.Context) *node.
649665
FastSync: ctx.GlobalBool(FastSyncFlag.Name),
650666
BlockChainVersion: ctx.GlobalInt(BlockchainVersionFlag.Name),
651667
DatabaseCache: ctx.GlobalInt(CacheFlag.Name),
668+
DatabaseHandles: MakeDatabaseHandles(),
652669
NetworkId: ctx.GlobalInt(NetworkIdFlag.Name),
653670
AccountManager: accman,
654671
Etherbase: MakeEtherbase(accman, ctx),
@@ -763,9 +780,10 @@ func SetupVM(ctx *cli.Context) {
763780
func MakeChain(ctx *cli.Context) (chain *core.BlockChain, chainDb ethdb.Database) {
764781
datadir := MustMakeDataDir(ctx)
765782
cache := ctx.GlobalInt(CacheFlag.Name)
783+
handles := MakeDatabaseHandles()
766784

767785
var err error
768-
if chainDb, err = ethdb.NewLDBDatabase(filepath.Join(datadir, "chaindata"), cache); err != nil {
786+
if chainDb, err = ethdb.NewLDBDatabase(filepath.Join(datadir, "chaindata"), cache, handles); err != nil {
769787
Fatalf("Could not open database: %v", err)
770788
}
771789
if ctx.GlobalBool(OlympicFlag.Name) {

‎core/bench_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) {
153153
b.Fatalf("cannot create temporary directory: %v", err)
154154
}
155155
defer os.RemoveAll(dir)
156-
db, err = ethdb.NewLDBDatabase(dir, 0)
156+
db, err = ethdb.NewLDBDatabase(dir, 128, 128)
157157
if err != nil {
158158
b.Fatalf("cannot create temporary database: %v", err)
159159
}

‎core/database_util_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ func TestMipmapChain(t *testing.T) {
551551
defer os.RemoveAll(dir)
552552

553553
var (
554-
db, _ = ethdb.NewLDBDatabase(dir, 16)
554+
db, _ = ethdb.NewLDBDatabase(dir, 0, 0)
555555
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
556556
addr = crypto.PubkeyToAddress(key1.PublicKey)
557557
addr2 = common.BytesToAddress([]byte("jeff"))

‎eth/backend.go‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type Config struct {
6969
BlockChainVersion int
7070
SkipBcVersionCheck bool // e.g. blockchain export
7171
DatabaseCache int
72+
DatabaseHandles int
7273

7374
NatSpec bool
7475
DocRoot string
@@ -135,12 +136,8 @@ type Ethereum struct {
135136
}
136137

137138
func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
138-
// Let the database take 3/4 of the max open files (TODO figure out a way to get the actual limit of the open files)
139-
const dbCount = 3
140-
ethdb.OpenFileLimit = 128 / (dbCount + 1)
141-
142139
// Open the chain database and perform any upgrades needed
143-
chainDb, err := ctx.OpenDatabase("chaindata", config.DatabaseCache)
140+
chainDb, err := ctx.OpenDatabase("chaindata", config.DatabaseCache, config.DatabaseHandles)
144141
if err != nil {
145142
return nil, err
146143
}
@@ -154,7 +151,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
154151
return nil, err
155152
}
156153

157-
dappDb, err := ctx.OpenDatabase("dapp", config.DatabaseCache)
154+
dappDb, err := ctx.OpenDatabase("dapp", config.DatabaseCache, config.DatabaseHandles)
158155
if err != nil {
159156
return nil, err
160157
}

‎eth/filters/filter_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func BenchmarkMipmaps(b *testing.B) {
3131
defer os.RemoveAll(dir)
3232

3333
var (
34-
db, _ = ethdb.NewLDBDatabase(dir, 16)
34+
db, _ = ethdb.NewLDBDatabase(dir, 0, 0)
3535
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
3636
addr1 = crypto.PubkeyToAddress(key1.PublicKey)
3737
addr2 = common.BytesToAddress([]byte("jeff"))
@@ -105,7 +105,7 @@ func TestFilters(t *testing.T) {
105105
defer os.RemoveAll(dir)
106106

107107
var (
108-
db, _ = ethdb.NewLDBDatabase(dir, 16)
108+
db, _ = ethdb.NewLDBDatabase(dir, 0, 0)
109109
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
110110
addr = crypto.PubkeyToAddress(key1.PublicKey)
111111

‎ethdb/database.go‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,15 @@ var OpenFileLimit = 64
3939
// cacheRatio specifies how the total alloted cache is distributed between the
4040
// various system databases.
4141
var cacheRatio = map[string]float64{
42-
"dapp": 2.0 / 13.0,
43-
"chaindata": 11.0 / 13.0,
42+
"dapp": 0.0,
43+
"chaindata": 1.0,
44+
}
45+
46+
// handleRatio specifies how the total alloted file descriptors is distributed
47+
// between the various system databases.
48+
var handleRatio = map[string]float64{
49+
"dapp": 0.0,
50+
"chaindata": 1.0,
4451
}
4552

4653
type LDBDatabase struct {
@@ -62,17 +69,21 @@ type LDBDatabase struct {
6269
}
6370

6471
// NewLDBDatabase returns a LevelDB wrapped object.
65-
func NewLDBDatabase(file string, cache int) (*LDBDatabase, error) {
66-
// Calculate the cache allowance for this particular database
72+
func NewLDBDatabase(file string, cache int, handles int) (*LDBDatabase, error) {
73+
// Calculate the cache and file descriptor allowance for this particular database
6774
cache = int(float64(cache) * cacheRatio[filepath.Base(file)])
6875
if cache < 16 {
6976
cache = 16
7077
}
71-
glog.V(logger.Info).Infof("Alloted %dMB cache to %s", cache, file)
78+
handles = int(float64(handles) * handleRatio[filepath.Base(file)])
79+
if handles < 16 {
80+
handles = 16
81+
}
82+
glog.V(logger.Info).Infof("Alloted %dMB cache and %d file handles to %s", cache, handles, file)
7283

7384
// Open the db and recover any potential corruptions
7485
db, err := leveldb.OpenFile(file, &opt.Options{
75-
OpenFilesCacheCapacity: OpenFileLimit,
86+
OpenFilesCacheCapacity: handles,
7687
BlockCacheCapacity: cache / 2 * opt.MiB,
7788
WriteBuffer: cache / 4 * opt.MiB, // Two of these are used internally
7889
})

‎ethdb/database_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func newDb() *LDBDatabase {
2828
if common.FileExist(file) {
2929
os.RemoveAll(file)
3030
}
31-
db, _ := NewLDBDatabase(file, 0)
31+
db, _ := NewLDBDatabase(file, 0, 0)
3232

3333
return db
3434
}

0 commit comments

Comments
 (0)