Skip to content

consensus/ethash: improve cache/dataset handling#15864

Merged
karalabe merged 10 commits into
ethereum:masterfrom
fjl:ethash-release-fix
Jan 23, 2018
Merged

consensus/ethash: improve cache/dataset handling#15864
karalabe merged 10 commits into
ethereum:masterfrom
fjl:ethash-release-fix

Conversation

@fjl
Copy link
Copy Markdown
Contributor

@fjl fjl commented Jan 12, 2018

There are two fixes in this PR:

Unmap the memory through a finalizer like the libethash wrapper did. The
release logic was incorrect and freed the memory while it was being
used, leading to crashes like in #14495 or #14943.

Track caches and datasets using simplelru instead of reinventing LRU
logic. This should make it easier to see whether it's correct.

Fixes #14495, #14943

@fjl fjl requested a review from karalabe as a code owner January 12, 2018 10:50
There are two fixes in this commit:

Unmap the memory through a finalizer like the libethash wrapper did. The
release logic was incorrect and freed the memory while it was being
used, leading to crashes like in ethereum#14495 or ethereum#14943.

Track caches and datasets using simplelru instead of reinventing LRU
logic. This should make it easier to see whether it's correct.
@fjl fjl force-pushed the ethash-release-fix branch from 7216825 to 253747a Compare January 12, 2018 11:00
Copy link
Copy Markdown
Member

@karalabe karalabe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick, otherwise seems good to me.

return calcCacheSize(epoch)
}

func calcCacheSize(epoch int) uint64 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document this method. I know it's private, but all methods in ethash are fully documented and I'd like to keep it so.

return calcDatasetSize(epoch)
}

func calcDatasetSize(epoch int) uint64 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document this method. I know it's private, but all methods in ethash are fully documented and I'd like to keep it so.

}
}

func TestCacheFileEvict(t *testing.T) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a small doc describing what this tests.

@fjl
Copy link
Copy Markdown
Contributor Author

fjl commented Jan 16, 2018

@karalabe PTAL

Copy link
Copy Markdown
Member

@karalabe karalabe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed some tiny nitpicks, otherwise LGTM.

Comment thread consensus/ethash/algorithm_go1.8.go Outdated
func datasetSize(block uint64) uint64 {
// If we have a pre-generated value, use that
epoch := int(block / epochLength)
if epoch < len(datasetSizes) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len(datasetSized) -> maxEpoch (you changed this everywhere else).

futureItem interface{}
}

func newlru(what string, maxItems int, new func(epoch uint64) interface{}) *lru {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// newlru create a new least-recently-used cache for ither the verification caches
// or the mining datasets.

used time.Time // Timestamp of the last use for smarter eviction
once sync.Once // Ensures the cache is generated only once
lock sync.Mutex // Ensures thread safety for updating the usage time
func newCache(epoch uint64) interface{} {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// newCache creates a new ethash verification cache and returns it as a plain Go
// interface to be usable in an LRU cache.

used time.Time // Timestamp of the last use for smarter eviction
once sync.Once // Ensures the cache is generated only once
lock sync.Mutex // Ensures thread safety for updating the usage time
func newDataset(epoch uint64) interface{} {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// newDataset creates a new ethash mining dataset and returns it as a plain Go
// interface to be usable in an LRU cache.

@karalabe karalabe added this to the 1.8.0 milestone Jan 23, 2018
@karalabe karalabe merged commit 924065e into ethereum:master Jan 23, 2018
prestonvanloon pushed a commit to prestonvanloon/go-ethereum that referenced this pull request Apr 2, 2018
* consensus/ethash: add maxEpoch constant

* consensus/ethash: improve cache/dataset handling

There are two fixes in this commit:

Unmap the memory through a finalizer like the libethash wrapper did. The
release logic was incorrect and freed the memory while it was being
used, leading to crashes like in ethereum#14495 or ethereum#14943.

Track caches and datasets using simplelru instead of reinventing LRU
logic. This should make it easier to see whether it's correct.

* consensus/ethash: restore 'future item' logic in lru

* consensus/ethash: use mmap even in test mode

This makes it possible to shorten the time taken for TestCacheFileEvict.

* consensus/ethash: shuffle func calc*Size comments around

* consensus/ethash: ensure future cache/dataset is in the lru cache

* consensus/ethash: add issue link to the new test

* consensus/ethash: fix vet

* consensus/ethash: fix test

* consensus: tiny issue + nitpick fixes
mariameda pushed a commit to NiluPlatform/go-nilu that referenced this pull request Aug 23, 2018
* consensus/ethash: add maxEpoch constant

* consensus/ethash: improve cache/dataset handling

There are two fixes in this commit:

Unmap the memory through a finalizer like the libethash wrapper did. The
release logic was incorrect and freed the memory while it was being
used, leading to crashes like in ethereum#14495 or ethereum#14943.

Track caches and datasets using simplelru instead of reinventing LRU
logic. This should make it easier to see whether it's correct.

* consensus/ethash: restore 'future item' logic in lru

* consensus/ethash: use mmap even in test mode

This makes it possible to shorten the time taken for TestCacheFileEvict.

* consensus/ethash: shuffle func calc*Size comments around

* consensus/ethash: ensure future cache/dataset is in the lru cache

* consensus/ethash: add issue link to the new test

* consensus/ethash: fix vet

* consensus/ethash: fix test

* consensus: tiny issue + nitpick fixes
@fjl fjl mentioned this pull request Nov 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segfault with CPU mining

2 participants