-
Notifications
You must be signed in to change notification settings - Fork 600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
walletdb/bdb: attempt to set initial memory map size when opening #697
Open
Roasbeef
wants to merge
2
commits into
btcsuite:master
Choose a base branch
from
Roasbeef:bbolt-mmap-options
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ import ( | |
"os" | ||
|
||
"github.com/btcsuite/btcwallet/walletdb" | ||
"github.com/coreos/bbolt" | ||
"go.etcd.io/bbolt" | ||
) | ||
|
||
// convertErr converts some bolt errors to the equivalent walletdb error. | ||
|
@@ -365,18 +365,52 @@ func fileExists(name string) bool { | |
return true | ||
} | ||
|
||
const ( | ||
// is64Bit tells us if we're running on a 64-bit system or not. If uintptr is | ||
// 32 bits, then its complement will be 0xffffffff rather than | ||
// 0xffffffffffffffff. The complement of uint64(0) will always be | ||
// 0xffffffffffffffff. | ||
is64Bit = uint64(^uintptr(0)) == ^uint64(0) | ||
|
||
// max32BitMapSize is the largest mmap size we can support on 32-bit | ||
// systems. (2^31)-1 == 0x7FFFFFFF. | ||
max32BitMapSize = 0x7FFFFFFF | ||
) | ||
|
||
// openDB opens the database at the provided path. walletdb.ErrDbDoesNotExist | ||
// is returned if the database doesn't exist and the create flag is not set. | ||
func openDB(dbPath string, noFreelistSync bool, create bool) (walletdb.DB, error) { | ||
func openDB(dbPath string, create bool, options *bbolt.Options) (walletdb.DB, error) { | ||
if !create && !fileExists(dbPath) { | ||
return nil, walletdb.ErrDbDoesNotExist | ||
} | ||
|
||
// Specify bbolt freelist options to reduce heap pressure in case the | ||
// freelist grows to be very large. | ||
options := &bbolt.Options{ | ||
NoFreelistSync: noFreelistSync, | ||
FreelistType: bbolt.FreelistMapType, | ||
options.FreelistType = bbolt.FreelistMapType | ||
|
||
if !create { | ||
// The other value that we want to set is the initial memory | ||
// map size. When bolt expands the mmap, it actually copies | ||
// over the entire database. By setting this to a large value | ||
// than zero, then we can avoid some of that heap pressure due | ||
// to the copies. | ||
dbFileInfo, err := os.Stat(dbPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// We'll aim to set the initial memory map to 2x the size of | ||
// the database as it exists on disk, meaning the DB size can | ||
// double without us having to remap everything. | ||
mmapSize := dbFileInfo.Size() * 2 | ||
|
||
// If we're on a 32-bit system, then we'll need to limit this | ||
// value to ensure we don't run into errors down the line. | ||
if !is64Bit { | ||
mmapSize = max32BitMapSize | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we always set this for 32-bit systems, this will fail bc the process may be using other virtual memory. Also the mmap is a contiguous block of vmem, so even if this value is lower, this problem can still occur. Part of the reason why I think 32-bit support should be dropped |
||
} | ||
|
||
options.InitialMmapSize = int(mmapSize) | ||
} | ||
|
||
boltDB, err := bbolt.Open(dbPath, 0600, options) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
value larger than zero