Skip to content

Commit

Permalink
Add Mlock flag.
Browse files Browse the repository at this point in the history
`Mlock` flag will cause mlock on db file which will prevent memory swapping of it. Motivation of this commit (etcd): etcd-io/etcd#12750
  • Loading branch information
wpedrak committed Apr 22, 2021
1 parent 8c17144 commit ed2436f
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 3 deletions.
4 changes: 2 additions & 2 deletions bolt_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func funlock(db *DB) error {
// mmap memory maps a DB's data file.
func mmap(db *DB, sz int) error {
// Map the data file to memory.
b, err := syscall.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags)
b, err := unix.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags)
if err != nil {
return err
}
Expand All @@ -78,7 +78,7 @@ func munmap(db *DB) error {
}

// Unmap using the original byte slice.
err := syscall.Munmap(db.dataref)
err := unix.Munmap(db.dataref)
db.dataref = nil
db.data = nil
db.datasz = 0
Expand Down
60 changes: 59 additions & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ type DB struct {
// of truncate() and fsync() when growing the data file.
AllocSize int

// Mlock locks database file in memory when set to true.
// It prevents major page faults, however used memory can't be reclaimed.
//
// Supported only on Unix via mlock/munlock syscalls.
Mlock bool

path string
openFile func(string, int, os.FileMode) (*os.File, error)
file *os.File
Expand Down Expand Up @@ -188,6 +194,7 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) {
db.MmapFlags = options.MmapFlags
db.NoFreelistSync = options.NoFreelistSync
db.FreelistType = options.FreelistType
db.Mlock = options.Mlock

// Set default values for later DB operations.
db.MaxBatchSize = DefaultMaxBatchSize
Expand Down Expand Up @@ -337,7 +344,8 @@ func (db *DB) mmap(minsz int) error {
}

// Ensure the size is at least the minimum size.
var size = int(info.Size())
fileSize := int(info.Size())
var size = fileSize
if size < minsz {
size = minsz
}
Expand All @@ -346,6 +354,13 @@ func (db *DB) mmap(minsz int) error {
return err
}

if db.Mlock {
// Unlock db memory
if err := db.munlock(fileSize); err != nil {
return err
}
}

// Dereference all mmap references before unmapping.
if db.rwtx != nil {
db.rwtx.root.dereference()
Expand All @@ -361,6 +376,13 @@ func (db *DB) mmap(minsz int) error {
return err
}

if db.Mlock {
// Don't allow swapping of data file
if err := db.mlock(fileSize); err != nil {
return err
}
}

// Save references to the meta pages.
db.meta0 = db.page(0).meta()
db.meta1 = db.page(1).meta()
Expand Down Expand Up @@ -422,6 +444,30 @@ func (db *DB) mmapSize(size int) (int, error) {
return int(sz), nil
}

func (db *DB) munlock(fileSize int) error {
if err := munlock(db, fileSize); err != nil {
return fmt.Errorf("munlock error: " + err.Error())
}
return nil
}

func (db *DB) mlock(fileSize int) error {
if err := mlock(db, fileSize); err != nil {
return fmt.Errorf("mlock error: " + err.Error())
}
return nil
}

func (db *DB) mrelock(fileSizeFrom, fileSizeTo int) error {
if err := db.munlock(fileSizeFrom); err != nil {
return err
}
if err := db.mlock(fileSizeTo); err != nil {
return err
}
return nil
}

// init creates a new database file and initializes its meta pages.
func (db *DB) init() error {
// Create two meta pages on a buffer.
Expand Down Expand Up @@ -462,6 +508,7 @@ func (db *DB) init() error {
if err := fdatasync(db); err != nil {
return err
}
db.filesz = len(buf)

return nil
}
Expand Down Expand Up @@ -973,6 +1020,12 @@ func (db *DB) grow(sz int) error {
if err := db.file.Sync(); err != nil {
return fmt.Errorf("file sync error: %s", err)
}
if db.Mlock {
// unlock old file and lock new one
if err := db.mrelock(db.filesz, sz); err != nil {
return fmt.Errorf("mlock/munlock error: %s", err)
}
}
}

db.filesz = sz
Expand Down Expand Up @@ -1064,6 +1117,11 @@ type Options struct {
// OpenFile is used to open files. It defaults to os.OpenFile. This option
// is useful for writing hermetic tests.
OpenFile func(string, int, os.FileMode) (*os.File, error)

// Mlock locks database file in memory when set to true.
// It prevents potential page faults, however
// used memory can't be reclaimed. (UNIX only)
Mlock bool
}

// DefaultOptions represent the options used if nil options are passed into Open().
Expand Down
36 changes: 36 additions & 0 deletions mlock_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// +build !windows

package bbolt

import "golang.org/x/sys/unix"

// mlock locks memory of db file
func mlock(db *DB, fileSize int) error {
sizeToLock := fileSize
if sizeToLock > db.datasz {
// Can't lock more than mmaped slice
sizeToLock = db.datasz
}
if err := unix.Mlock(db.dataref[:sizeToLock]); err != nil {
return err
}
return nil
}

//munlock unlocks memory of db file
func munlock(db *DB, fileSize int) error {
if db.dataref == nil {
return nil
}

sizeToUnlock := fileSize
if sizeToUnlock > db.datasz {
// Can't unlock more than mmaped slice
sizeToUnlock = db.datasz
}

if err := unix.Munlock(db.dataref[:sizeToUnlock]); err != nil {
return err
}
return nil
}
11 changes: 11 additions & 0 deletions mlock_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package bbolt

// mlock locks memory of db file
func mlock(_ *DB, _ int) error {
panic("mlock is supported only on UNIX systems")
}

//munlock unlocks memory of db file
func munlock(_ *DB, _ int) error {
panic("munlock is supported only on UNIX systems")
}

0 comments on commit ed2436f

Please sign in to comment.