mirror of https://github.com/etcd-io/bbolt.git
Add `Mlock` flag.
`Mlock` flag will cause mlock on db file which will prevent memory swapping of it. Motivation of this commit (etcd): https://github.com/etcd-io/etcd/pull/12750pull/273/head
parent
8c171443bc
commit
ed2436f1f8
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
|
60
db.go
60
db.go
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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()
|
||||
|
@ -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()
|
||||
|
@ -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.
|
||||
|
@ -462,6 +508,7 @@ func (db *DB) init() error {
|
|||
if err := fdatasync(db); err != nil {
|
||||
return err
|
||||
}
|
||||
db.filesz = len(buf)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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().
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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")
|
||||
}
|
Loading…
Reference in New Issue