Skip to content
This repository has been archived by the owner on Mar 9, 2019. It is now read-only.

Add madvise setting for sequential dbs #737

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bolt_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func mmap(db *DB, sz int) error {
return err
}

// Advise the kernel that the mmap is accessed randomly.
if err := madvise(b, syscall.MADV_RANDOM); err != nil {
// Advise the kernel how the mmap should be accessed.
if err := madvise(b, db.Madvise); err != nil {
return fmt.Errorf("madvise: %s", err)
}

Expand Down
4 changes: 2 additions & 2 deletions bolt_unix_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func mmap(db *DB, sz int) error {
return err
}

// Advise the kernel that the mmap is accessed randomly.
if err := unix.Madvise(b, syscall.MADV_RANDOM); err != nil {
// Advise the kernel how the mmap should be accessed.
if err := unix.Madvise(b, db.Madvise); err != nil {
return fmt.Errorf("madvise: %s", err)
}

Expand Down
29 changes: 29 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,18 @@ const magic uint32 = 0xED0CDAED
// must be synchronized using the msync(2) syscall.
const IgnoreNoSync = runtime.GOOS == "openbsd"

// Madvise option values (from unix 'syscall.MADV_*'). No effect on non-unix.
const (
MADV_NORMAL = 0x0
MADV_RANDOM = 0x1
)

// Default values if not set in a DB instance.
const (
DefaultMaxBatchSize int = 1000
DefaultMaxBatchDelay = 10 * time.Millisecond
DefaultAllocSize = 16 * 1024 * 1024
DefaultMadvise = MADV_RANDOM
)

// default page size for db is set to the OS page size.
Expand Down Expand Up @@ -94,6 +101,14 @@ type DB struct {
// of truncate() and fsync() when growing the data file.
AllocSize int

// Madvise allows advising the kernel how the mmap should be accessed. The
// default is "random", but if the db is being used sequentially, setting
// to "normal" has been shown to improve performance on high latency
// filesystem storage.
//
// https://github.com/boltdb/bolt/issues/691.
Madvise int

path string
file *os.File
lockfile *os.File // windows only
Expand Down Expand Up @@ -161,6 +176,12 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) {
db.MaxBatchSize = DefaultMaxBatchSize
db.MaxBatchDelay = DefaultMaxBatchDelay
db.AllocSize = DefaultAllocSize
db.Madvise = DefaultMadvise

// because an empty value would be 'normal', use options only if set
if options.Madvise != nil {
db.Madvise = *options.Madvise
}

flag := os.O_RDWR
if options.ReadOnly {
Expand Down Expand Up @@ -917,6 +938,14 @@ type Options struct {
// If initialMmapSize is smaller than the previous database size,
// it takes no effect.
InitialMmapSize int

// Madvise allows advising the kernel how the mmap should be accessed. The
// default is "random", but if the db is being used sequentially, setting
// to "normal" has been shown to improve performance on high latency
// filesystem storage.
//
// https://github.com/boltdb/bolt/issues/691.
Madvise *int
}

// DefaultOptions represent the options used if nil options are passed into Open().
Expand Down