Skip to content

Commit 8f05cfa

Browse files
authored
cmd, consensus: add option to disable mmap for DAG caches/datasets (#20484)
* cmd, consensus: add option to disable mmap for DAG caches/datasets * consensus: add benchmarks for mmap with/with lock
1 parent 76eed9e commit 8f05cfa

8 files changed

Lines changed: 108 additions & 48 deletions

File tree

‎cmd/geth/main.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ var (
7474
utils.EthashCacheDirFlag,
7575
utils.EthashCachesInMemoryFlag,
7676
utils.EthashCachesOnDiskFlag,
77+
utils.EthashCachesLockMmapFlag,
7778
utils.EthashDatasetDirFlag,
7879
utils.EthashDatasetsInMemoryFlag,
7980
utils.EthashDatasetsOnDiskFlag,
81+
utils.EthashDatasetsLockMmapFlag,
8082
utils.TxPoolLocalsFlag,
8183
utils.TxPoolNoLocalsFlag,
8284
utils.TxPoolJournalFlag,

‎cmd/geth/retesteth.go‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,13 @@ func (api *RetestethAPI) SetChainParams(ctx context.Context, chainParams ChainPa
387387
inner = ethash.NewFaker()
388388
case "Ethash":
389389
inner = ethash.New(ethash.Config{
390-
CacheDir: "ethash",
391-
CachesInMem: 2,
392-
CachesOnDisk: 3,
393-
DatasetsInMem: 1,
394-
DatasetsOnDisk: 2,
390+
CacheDir: "ethash",
391+
CachesInMem: 2,
392+
CachesOnDisk: 3,
393+
CachesLockMmap: false,
394+
DatasetsInMem: 1,
395+
DatasetsOnDisk: 2,
396+
DatasetsLockMmap: false,
395397
}, nil, false)
396398
default:
397399
return false, fmt.Errorf("unrecognised seal engine: %s", chainParams.SealEngine)

‎cmd/geth/usage.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ var AppHelpFlagGroups = []flagGroup{
109109
utils.EthashCacheDirFlag,
110110
utils.EthashCachesInMemoryFlag,
111111
utils.EthashCachesOnDiskFlag,
112+
utils.EthashCachesLockMmapFlag,
112113
utils.EthashDatasetDirFlag,
113114
utils.EthashDatasetsInMemoryFlag,
114115
utils.EthashDatasetsOnDiskFlag,
116+
utils.EthashDatasetsLockMmapFlag,
115117
},
116118
},
117119
{

‎cmd/utils/flags.go‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ var (
305305
Usage: "Number of recent ethash caches to keep on disk (16MB each)",
306306
Value: eth.DefaultConfig.Ethash.CachesOnDisk,
307307
}
308+
EthashCachesLockMmapFlag = cli.BoolFlag{
309+
Name: "ethash.cacheslockmmap",
310+
Usage: "Lock memory maps of recent ethash caches",
311+
}
308312
EthashDatasetDirFlag = DirectoryFlag{
309313
Name: "ethash.dagdir",
310314
Usage: "Directory to store the ethash mining DAGs",
@@ -320,6 +324,10 @@ var (
320324
Usage: "Number of recent ethash mining DAGs to keep on disk (1+GB each)",
321325
Value: eth.DefaultConfig.Ethash.DatasetsOnDisk,
322326
}
327+
EthashDatasetsLockMmapFlag = cli.BoolFlag{
328+
Name: "ethash.dagslockmmap",
329+
Usage: "Lock memory maps for recent ethash mining DAGs",
330+
}
323331
// Transaction pool settings
324332
TxPoolLocalsFlag = cli.StringFlag{
325333
Name: "txpool.locals",
@@ -1306,12 +1314,18 @@ func setEthash(ctx *cli.Context, cfg *eth.Config) {
13061314
if ctx.GlobalIsSet(EthashCachesOnDiskFlag.Name) {
13071315
cfg.Ethash.CachesOnDisk = ctx.GlobalInt(EthashCachesOnDiskFlag.Name)
13081316
}
1317+
if ctx.GlobalIsSet(EthashCachesLockMmapFlag.Name) {
1318+
cfg.Ethash.CachesLockMmap = ctx.GlobalBool(EthashCachesLockMmapFlag.Name)
1319+
}
13091320
if ctx.GlobalIsSet(EthashDatasetsInMemoryFlag.Name) {
13101321
cfg.Ethash.DatasetsInMem = ctx.GlobalInt(EthashDatasetsInMemoryFlag.Name)
13111322
}
13121323
if ctx.GlobalIsSet(EthashDatasetsOnDiskFlag.Name) {
13131324
cfg.Ethash.DatasetsOnDisk = ctx.GlobalInt(EthashDatasetsOnDiskFlag.Name)
13141325
}
1326+
if ctx.GlobalIsSet(EthashDatasetsLockMmapFlag.Name) {
1327+
cfg.Ethash.DatasetsLockMmap = ctx.GlobalBool(EthashDatasetsLockMmapFlag.Name)
1328+
}
13151329
}
13161330

13171331
func setMiner(ctx *cli.Context, cfg *miner.Config) {
@@ -1721,12 +1735,14 @@ func MakeChain(ctx *cli.Context, stack *node.Node) (chain *core.BlockChain, chai
17211735
engine = ethash.NewFaker()
17221736
if !ctx.GlobalBool(FakePoWFlag.Name) {
17231737
engine = ethash.New(ethash.Config{
1724-
CacheDir: stack.ResolvePath(eth.DefaultConfig.Ethash.CacheDir),
1725-
CachesInMem: eth.DefaultConfig.Ethash.CachesInMem,
1726-
CachesOnDisk: eth.DefaultConfig.Ethash.CachesOnDisk,
1727-
DatasetDir: stack.ResolvePath(eth.DefaultConfig.Ethash.DatasetDir),
1728-
DatasetsInMem: eth.DefaultConfig.Ethash.DatasetsInMem,
1729-
DatasetsOnDisk: eth.DefaultConfig.Ethash.DatasetsOnDisk,
1738+
CacheDir: stack.ResolvePath(eth.DefaultConfig.Ethash.CacheDir),
1739+
CachesInMem: eth.DefaultConfig.Ethash.CachesInMem,
1740+
CachesOnDisk: eth.DefaultConfig.Ethash.CachesOnDisk,
1741+
CachesLockMmap: eth.DefaultConfig.Ethash.CachesLockMmap,
1742+
DatasetDir: stack.ResolvePath(eth.DefaultConfig.Ethash.DatasetDir),
1743+
DatasetsInMem: eth.DefaultConfig.Ethash.DatasetsInMem,
1744+
DatasetsOnDisk: eth.DefaultConfig.Ethash.DatasetsOnDisk,
1745+
DatasetsLockMmap: eth.DefaultConfig.Ethash.DatasetsLockMmap,
17301746
}, nil, false)
17311747
}
17321748
}

‎consensus/ethash/algorithm_test.go‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ func TestConcurrentDiskCacheGeneration(t *testing.T) {
729729

730730
go func(idx int) {
731731
defer pend.Done()
732-
ethash := New(Config{cachedir, 0, 1, "", 0, 0, ModeNormal, nil}, nil, false)
732+
ethash := New(Config{cachedir, 0, 1, false, "", 0, 0, false, ModeNormal, nil}, nil, false)
733733
defer ethash.Close()
734734
if err := ethash.VerifySeal(nil, block.Header()); err != nil {
735735
t.Errorf("proc %d: block verification failed: %v", idx, err)
@@ -787,3 +787,28 @@ func BenchmarkHashimotoFullSmall(b *testing.B) {
787787
hashimotoFull(dataset, hash, 0)
788788
}
789789
}
790+
791+
func benchmarkHashimotoFullMmap(b *testing.B, name string, lock bool) {
792+
b.Run(name, func(b *testing.B) {
793+
tmpdir, err := ioutil.TempDir("", "ethash-test")
794+
if err != nil {
795+
b.Fatal(err)
796+
}
797+
defer os.RemoveAll(tmpdir)
798+
799+
d := &dataset{epoch: 0}
800+
d.generate(tmpdir, 1, lock, false)
801+
var hash [common.HashLength]byte
802+
b.ResetTimer()
803+
for i := 0; i < b.N; i++ {
804+
binary.PutVarint(hash[:], int64(i))
805+
hashimotoFull(d.dataset, hash[:], 0)
806+
}
807+
})
808+
}
809+
810+
// Benchmarks the full verification performance for mmap
811+
func BenchmarkHashimotoFullMmap(b *testing.B) {
812+
benchmarkHashimotoFullMmap(b, "WithLock", true)
813+
benchmarkHashimotoFullMmap(b, "WithoutLock", false)
814+
}

‎consensus/ethash/ethash.go‎

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var (
4848
two256 = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0))
4949

5050
// sharedEthash is a full instance that can be shared between multiple users.
51-
sharedEthash = New(Config{"", 3, 0, "", 1, 0, ModeNormal, nil}, nil, false)
51+
sharedEthash = New(Config{"", 3, 0, false, "", 1, 0, false, ModeNormal, nil}, nil, false)
5252

5353
// algorithmRevision is the data structure version used for file naming.
5454
algorithmRevision = 23
@@ -65,7 +65,7 @@ func isLittleEndian() bool {
6565
}
6666

6767
// memoryMap tries to memory map a file of uint32s for read only access.
68-
func memoryMap(path string) (*os.File, mmap.MMap, []uint32, error) {
68+
func memoryMap(path string, lock bool) (*os.File, mmap.MMap, []uint32, error) {
6969
file, err := os.OpenFile(path, os.O_RDONLY, 0644)
7070
if err != nil {
7171
return nil, nil, nil, err
@@ -82,6 +82,13 @@ func memoryMap(path string) (*os.File, mmap.MMap, []uint32, error) {
8282
return nil, nil, nil, ErrInvalidDumpMagic
8383
}
8484
}
85+
if lock {
86+
if err := mem.Lock(); err != nil {
87+
mem.Unmap()
88+
file.Close()
89+
return nil, nil, nil, err
90+
}
91+
}
8592
return file, mem, buffer[len(dumpMagic):], err
8693
}
8794

@@ -107,7 +114,7 @@ func memoryMapFile(file *os.File, write bool) (mmap.MMap, []uint32, error) {
107114
// memoryMapAndGenerate tries to memory map a temporary file of uint32s for write
108115
// access, fill it with the data from a generator and then move it into the final
109116
// path requested.
110-
func memoryMapAndGenerate(path string, size uint64, generator func(buffer []uint32)) (*os.File, mmap.MMap, []uint32, error) {
117+
func memoryMapAndGenerate(path string, size uint64, lock bool, generator func(buffer []uint32)) (*os.File, mmap.MMap, []uint32, error) {
111118
// Ensure the data folder exists
112119
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
113120
return nil, nil, nil, err
@@ -142,7 +149,7 @@ func memoryMapAndGenerate(path string, size uint64, generator func(buffer []uint
142149
if err := os.Rename(temp, path); err != nil {
143150
return nil, nil, nil, err
144151
}
145-
return memoryMap(path)
152+
return memoryMap(path, lock)
146153
}
147154

148155
// lru tracks caches or datasets by their last use time, keeping at most N of them.
@@ -213,7 +220,7 @@ func newCache(epoch uint64) interface{} {
213220
}
214221

215222
// generate ensures that the cache content is generated before use.
216-
func (c *cache) generate(dir string, limit int, test bool) {
223+
func (c *cache) generate(dir string, limit int, lock bool, test bool) {
217224
c.once.Do(func() {
218225
size := cacheSize(c.epoch*epochLength + 1)
219226
seed := seedHash(c.epoch*epochLength + 1)
@@ -240,15 +247,15 @@ func (c *cache) generate(dir string, limit int, test bool) {
240247

241248
// Try to load the file from disk and memory map it
242249
var err error
243-
c.dump, c.mmap, c.cache, err = memoryMap(path)
250+
c.dump, c.mmap, c.cache, err = memoryMap(path, lock)
244251
if err == nil {
245252
logger.Debug("Loaded old ethash cache from disk")
246253
return
247254
}
248255
logger.Debug("Failed to load old ethash cache", "err", err)
249256

250257
// No previous cache available, create a new cache file to fill
251-
c.dump, c.mmap, c.cache, err = memoryMapAndGenerate(path, size, func(buffer []uint32) { generateCache(buffer, c.epoch, seed) })
258+
c.dump, c.mmap, c.cache, err = memoryMapAndGenerate(path, size, lock, func(buffer []uint32) { generateCache(buffer, c.epoch, seed) })
252259
if err != nil {
253260
logger.Error("Failed to generate mapped ethash cache", "err", err)
254261

@@ -290,7 +297,7 @@ func newDataset(epoch uint64) interface{} {
290297
}
291298

292299
// generate ensures that the dataset content is generated before use.
293-
func (d *dataset) generate(dir string, limit int, test bool) {
300+
func (d *dataset) generate(dir string, limit int, lock bool, test bool) {
294301
d.once.Do(func() {
295302
// Mark the dataset generated after we're done. This is needed for remote
296303
defer atomic.StoreUint32(&d.done, 1)
@@ -326,7 +333,7 @@ func (d *dataset) generate(dir string, limit int, test bool) {
326333

327334
// Try to load the file from disk and memory map it
328335
var err error
329-
d.dump, d.mmap, d.dataset, err = memoryMap(path)
336+
d.dump, d.mmap, d.dataset, err = memoryMap(path, lock)
330337
if err == nil {
331338
logger.Debug("Loaded old ethash dataset from disk")
332339
return
@@ -337,7 +344,7 @@ func (d *dataset) generate(dir string, limit int, test bool) {
337344
cache := make([]uint32, csize/4)
338345
generateCache(cache, d.epoch, seed)
339346

340-
d.dump, d.mmap, d.dataset, err = memoryMapAndGenerate(path, dsize, func(buffer []uint32) { generateDataset(buffer, d.epoch, cache) })
347+
d.dump, d.mmap, d.dataset, err = memoryMapAndGenerate(path, dsize, lock, func(buffer []uint32) { generateDataset(buffer, d.epoch, cache) })
341348
if err != nil {
342349
logger.Error("Failed to generate mapped ethash dataset", "err", err)
343350

@@ -372,13 +379,13 @@ func (d *dataset) finalizer() {
372379
// MakeCache generates a new ethash cache and optionally stores it to disk.
373380
func MakeCache(block uint64, dir string) {
374381
c := cache{epoch: block / epochLength}
375-
c.generate(dir, math.MaxInt32, false)
382+
c.generate(dir, math.MaxInt32, false, false)
376383
}
377384

378385
// MakeDataset generates a new ethash dataset and optionally stores it to disk.
379386
func MakeDataset(block uint64, dir string) {
380387
d := dataset{epoch: block / epochLength}
381-
d.generate(dir, math.MaxInt32, false)
388+
d.generate(dir, math.MaxInt32, false, false)
382389
}
383390

384391
// Mode defines the type and amount of PoW verification an ethash engine makes.
@@ -394,13 +401,15 @@ const (
394401

395402
// Config are the configuration parameters of the ethash.
396403
type Config struct {
397-
CacheDir string
398-
CachesInMem int
399-
CachesOnDisk int
400-
DatasetDir string
401-
DatasetsInMem int
402-
DatasetsOnDisk int
403-
PowMode Mode
404+
CacheDir string
405+
CachesInMem int
406+
CachesOnDisk int
407+
CachesLockMmap bool
408+
DatasetDir string
409+
DatasetsInMem int
410+
DatasetsOnDisk int
411+
DatasetsLockMmap bool
412+
PowMode Mode
404413

405414
Log log.Logger `toml:"-"`
406415
}
@@ -549,12 +558,12 @@ func (ethash *Ethash) cache(block uint64) *cache {
549558
current := currentI.(*cache)
550559

551560
// Wait for generation finish.
552-
current.generate(ethash.config.CacheDir, ethash.config.CachesOnDisk, ethash.config.PowMode == ModeTest)
561+
current.generate(ethash.config.CacheDir, ethash.config.CachesOnDisk, ethash.config.CachesLockMmap, ethash.config.PowMode == ModeTest)
553562

554563
// If we need a new future cache, now's a good time to regenerate it.
555564
if futureI != nil {
556565
future := futureI.(*cache)
557-
go future.generate(ethash.config.CacheDir, ethash.config.CachesOnDisk, ethash.config.PowMode == ModeTest)
566+
go future.generate(ethash.config.CacheDir, ethash.config.CachesOnDisk, ethash.config.CachesLockMmap, ethash.config.PowMode == ModeTest)
558567
}
559568
return current
560569
}
@@ -574,20 +583,20 @@ func (ethash *Ethash) dataset(block uint64, async bool) *dataset {
574583
// If async is specified, generate everything in a background thread
575584
if async && !current.generated() {
576585
go func() {
577-
current.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.PowMode == ModeTest)
586+
current.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.DatasetsLockMmap, ethash.config.PowMode == ModeTest)
578587

579588
if futureI != nil {
580589
future := futureI.(*dataset)
581-
future.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.PowMode == ModeTest)
590+
future.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.DatasetsLockMmap, ethash.config.PowMode == ModeTest)
582591
}
583592
}()
584593
} else {
585594
// Either blocking generation was requested, or already done
586-
current.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.PowMode == ModeTest)
595+
current.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.DatasetsLockMmap, ethash.config.PowMode == ModeTest)
587596

588597
if futureI != nil {
589598
future := futureI.(*dataset)
590-
go future.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.PowMode == ModeTest)
599+
go future.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.DatasetsLockMmap, ethash.config.PowMode == ModeTest)
591600
}
592601
}
593602
return current

‎eth/backend.go‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,14 @@ func CreateConsensusEngine(ctx *node.ServiceContext, chainConfig *params.ChainCo
266266
return ethash.NewShared()
267267
default:
268268
engine := ethash.New(ethash.Config{
269-
CacheDir: ctx.ResolvePath(config.CacheDir),
270-
CachesInMem: config.CachesInMem,
271-
CachesOnDisk: config.CachesOnDisk,
272-
DatasetDir: config.DatasetDir,
273-
DatasetsInMem: config.DatasetsInMem,
274-
DatasetsOnDisk: config.DatasetsOnDisk,
269+
CacheDir: ctx.ResolvePath(config.CacheDir),
270+
CachesInMem: config.CachesInMem,
271+
CachesOnDisk: config.CachesOnDisk,
272+
CachesLockMmap: config.CachesLockMmap,
273+
DatasetDir: config.DatasetDir,
274+
DatasetsInMem: config.DatasetsInMem,
275+
DatasetsOnDisk: config.DatasetsOnDisk,
276+
DatasetsLockMmap: config.DatasetsLockMmap,
275277
}, notify, noverify)
276278
engine.SetThreads(-1) // Disable CPU mining
277279
return engine

‎eth/config.go‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ import (
3737
var DefaultConfig = Config{
3838
SyncMode: downloader.FastSync,
3939
Ethash: ethash.Config{
40-
CacheDir: "ethash",
41-
CachesInMem: 2,
42-
CachesOnDisk: 3,
43-
DatasetsInMem: 1,
44-
DatasetsOnDisk: 2,
40+
CacheDir: "ethash",
41+
CachesInMem: 2,
42+
CachesOnDisk: 3,
43+
CachesLockMmap: false,
44+
DatasetsInMem: 1,
45+
DatasetsOnDisk: 2,
46+
DatasetsLockMmap: false,
4547
},
4648
NetworkId: 1,
4749
LightPeers: 100,

0 commit comments

Comments
 (0)