From 850448b543880218dd125742305450346accd757 Mon Sep 17 00:00:00 2001 From: Daniel Gustafsson Date: Wed, 12 Jan 2022 15:26:51 +0100 Subject: [PATCH] feat(mmap+go): Add Falloc for mac in the go mmap experiment --- src/mmap-go-prototype/mac_falloc.go | 28 ++++++++++++++++++++++++++++ src/mmap-go-prototype/mmap.go | 5 +++-- 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/mmap-go-prototype/mac_falloc.go diff --git a/src/mmap-go-prototype/mac_falloc.go b/src/mmap-go-prototype/mac_falloc.go new file mode 100644 index 00000000..7681b71a --- /dev/null +++ b/src/mmap-go-prototype/mac_falloc.go @@ -0,0 +1,28 @@ +package main + +import ( + "os" + "syscall" + "unsafe" +) + +// based on https://github.com/coilhq/tigerbeetle/blob/7e12fccc4859f035cd26af29b8e9f9749a0899a3/src/storage.zig#L430 +func MacFallocate(file *os.File, offset int64, length int64) error { + store := syscall.Fstore_t{ + Flags: syscall.F_ALLOCATECONTIG | syscall.F_ALLOCATEALL, + Posmode: syscall.F_PEOFPOSMODE, + Offset: 0, + Length: offset + length, + Bytesalloc: 0, + } + _, _, err := syscall.Syscall(syscall.SYS_FCNTL, file.Fd(), syscall.F_PREALLOCATE, uintptr(unsafe.Pointer(&store))) + if err != 0 { + store.Flags = syscall.F_ALLOCATEALL + _, _, err = syscall.Syscall(syscall.SYS_FCNTL, file.Fd(), syscall.F_PREALLOCATE, uintptr(unsafe.Pointer(&store))) + if err != 0 { + return err + } + } + + return syscall.Ftruncate(int(file.Fd()), store.Length) +} diff --git a/src/mmap-go-prototype/mmap.go b/src/mmap-go-prototype/mmap.go index a1aa5f41..be87df51 100644 --- a/src/mmap-go-prototype/mmap.go +++ b/src/mmap-go-prototype/mmap.go @@ -25,9 +25,10 @@ func NewMmap(f string) *Mmap { // XXX: Passing `FALLOC_FL_ZERO_RANGE` as mode would zero the file, but // I couldn't find this constant in `syscall`... - mode := uint32(0) + // mode := uint32(0) // syscall.F_PREALLOCATE - if err := syscall.Fallocate(int(file.Fd()), mode, 0, int64(pageSize)); err != nil { + //if err := syscall.Fallocate(int(file.Fd()), mode, 0, int64(pageSize)); err != nil { + if err := MacFallocate(file, 0, int64(pageSize)); err != nil { panic(err) }