From 5e02e28f9c8cca7c5d99c2da15a73eda63e2b7ea Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Sun, 11 Dec 2016 15:49:36 +0100 Subject: [PATCH] Add proper mmap calls --- db.go | 2 +- db_unix.go | 72 +++++++++++++++++------------------------------------- 2 files changed, 24 insertions(+), 50 deletions(-) diff --git a/db.go b/db.go index 6decbd6118..c546a4cd30 100644 --- a/db.go +++ b/db.go @@ -322,7 +322,7 @@ func (s *SeriesShard) persist() error { } } - sz := fmt.Sprintf("%fMiB", float64(sw.Size()+iw.Size())/1024/1024) + sz := fmt.Sprintf("%.2fMiB", float64(sw.Size()+iw.Size())/1024/1024) s.logger.With("size", sz). With("samples", head.samples). diff --git a/db_unix.go b/db_unix.go index 216da9af51..2e673f9516 100644 --- a/db_unix.go +++ b/db_unix.go @@ -2,52 +2,26 @@ package tsdb -// import ( -// "fmt" -// "syscall" -// "unsafe" -// ) - -// // 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) -// if err != nil { -// return err -// } - -// // Advise the kernel that the mmap is accessed randomly. -// if err := madvise(b, syscall.MADV_RANDOM); err != nil { -// return fmt.Errorf("madvise: %s", err) -// } - -// // Save the original byte slice and convert to a byte array pointer. -// db.dataref = b -// db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0])) -// db.datasz = sz -// return nil -// } - -// // munmap unmaps a DB's data file from memory. -// func munmap(db *DB) error { -// // Ignore the unmap if we have no mapped data. -// if db.dataref == nil { -// return nil -// } - -// // Unmap using the original byte slice. -// err := syscall.Munmap(db.dataref) -// db.dataref = nil -// db.data = nil -// db.datasz = 0 -// return err -// } - -// // NOTE: This function is copied from stdlib because it is not available on darwin. -// func madvise(b []byte, advice int) (err error) { -// _, _, e1 := syscall.Syscall(syscall.SYS_MADVISE, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)), uintptr(advice)) -// if e1 != 0 { -// err = e1 -// } -// return -// } +import ( + "os" + "unsafe" + + "golang.org/x/sys/unix" +) + +func mmap(f *os.File, length int) ([]byte, error) { + return unix.Mmap(int(f.Fd()), 0, length, unix.PROT_READ, unix.MAP_SHARED) +} + +func munmap(b []byte) (err error) { + return unix.Munmap(b) +} + +// unix.Madvise is not defined for darwin, so we define it ourselves. +func madvise(b []byte, advice int) (err error) { + _, _, e1 := unix.Syscall(unix.SYS_MADVISE, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)), uintptr(advice)) + if e1 != 0 { + err = e1 + } + return +}