From 7bfaeb8d073cc7e25f44949abac8b8417cead98d Mon Sep 17 00:00:00 2001 From: kanandev2024 Date: Sun, 8 Nov 2015 17:30:52 -0800 Subject: [PATCH] Add MmapFlags option for MAP_POPULATE (unix) This adds MmapFlags to DB.Options in case we need syscall.MAP_POPULATE flag in Linux 2.6.23+ to do the sequential read-ahead, as discussed in [1]. --- [1]: https://github.com/coreos/etcd/issues/3786 --- bolt_unix.go | 2 +- bolt_unix_solaris.go | 4 ++-- db.go | 8 ++++++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/bolt_unix.go b/bolt_unix.go index 6eef6b2..251680b 100644 --- a/bolt_unix.go +++ b/bolt_unix.go @@ -58,7 +58,7 @@ 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) + b, err := syscall.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags) if err != nil { return err } diff --git a/bolt_unix_solaris.go b/bolt_unix_solaris.go index f480ee7..214009b 100644 --- a/bolt_unix_solaris.go +++ b/bolt_unix_solaris.go @@ -1,4 +1,3 @@ - package bolt import ( @@ -7,6 +6,7 @@ import ( "syscall" "time" "unsafe" + "golang.org/x/sys/unix" ) @@ -68,7 +68,7 @@ func mmap(db *DB, sz int) error { } // Map the data file to memory. - b, err := unix.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED) + b, err := unix.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags) if err != nil { return err } diff --git a/db.go b/db.go index d39c4aa..4ff07ae 100644 --- a/db.go +++ b/db.go @@ -63,6 +63,10 @@ type DB struct { // https://github.com/boltdb/bolt/issues/284 NoGrowSync bool + // If you want to read the entire database fast, you can set MmapFlag to + // syscall.MAP_POPULATE on Linux 2.6.23+ for sequential read-ahead. + MmapFlags int + // MaxBatchSize is the maximum size of a batch. Default value is // copied from DefaultMaxBatchSize in Open. // @@ -136,6 +140,7 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) { options = DefaultOptions } db.NoGrowSync = options.NoGrowSync + db.MmapFlags = options.MmapFlags // Set default values for later DB operations. db.MaxBatchSize = DefaultMaxBatchSize @@ -672,6 +677,9 @@ type Options struct { // Open database in read-only mode. Uses flock(..., LOCK_SH |LOCK_NB) to // grab a shared lock (UNIX). ReadOnly bool + + // Sets the DB.MmapFlags flag before memory mapping the file. + MmapFlags int } // DefaultOptions represent the options used if nil options are passed into Open().