Skip to content

Commit

Permalink
feat(mmap+go): Add Falloc for mac in the go mmap experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-daniel-gustafsson committed Jan 12, 2022
1 parent 0d2982c commit 850448b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/mmap-go-prototype/mac_falloc.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 3 additions & 2 deletions src/mmap-go-prototype/mmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 850448b

Please sign in to comment.