Skip to content

Commit

Permalink
Consider large page size systems.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed Apr 9, 2024
1 parent 3854397 commit 9775518
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ require (

retract v0.4.0 // tagged from the wrong branch

replace github.com/tetratelabs/wazero => github.com/ncruces/wazero v0.0.0-20240409112346-f2be6da37a3d
replace github.com/tetratelabs/wazero => github.com/ncruces/wazero v0.0.0-20240409122832-a4dd11a09060
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/ncruces/julianday v1.0.0 h1:fH0OKwa7NWvniGQtxdJRxAgkBMolni2BjDHaWTxqt7M=
github.com/ncruces/julianday v1.0.0/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g=
github.com/ncruces/wazero v0.0.0-20240409112346-f2be6da37a3d h1:t3S5uhR50fDxrCgoMLcVDFo2jpYdKjq5VWbXFUMHv0w=
github.com/ncruces/wazero v0.0.0-20240409112346-f2be6da37a3d/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y=
github.com/ncruces/wazero v0.0.0-20240409122832-a4dd11a09060 h1:dkYSDM1fMCDttJ6sCdlUHP/BiBzG3hJxDnA+k6zUdyY=
github.com/ncruces/wazero v0.0.0-20240409122832-a4dd11a09060/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y=
github.com/psanford/httpreadat v0.1.0 h1:VleW1HS2zO7/4c7c7zNl33fO6oYACSagjJIyMIwZLUE=
github.com/psanford/httpreadat v0.1.0/go.mod h1:Zg7P+TlBm3bYbyHTKv/EdtSJZn3qwbPwpfZ/I9GKCRE=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
Expand Down
4 changes: 2 additions & 2 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/ncruces/wazero v0.0.0-20240409112346-f2be6da37a3d h1:t3S5uhR50fDxrCgoMLcVDFo2jpYdKjq5VWbXFUMHv0w=
github.com/ncruces/wazero v0.0.0-20240409112346-f2be6da37a3d/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y=
github.com/ncruces/wazero v0.0.0-20240409122832-a4dd11a09060 h1:dkYSDM1fMCDttJ6sCdlUHP/BiBzG3hJxDnA+k6zUdyY=
github.com/ncruces/wazero v0.0.0-20240409122832-a4dd11a09060/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
Expand Down
27 changes: 20 additions & 7 deletions internal/util/alloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ import (
)

func mmappedAllocator(min, cap, max uint64) experimental.MemoryBuffer {
// Round up to the page size.
rnd := uint64(unix.Getpagesize() - 1)
max = (max + rnd) &^ rnd
cap = (cap + rnd) &^ rnd

if max > math.MaxInt {
// This ensures int(max) overflows to a negative value,
// and unix.Mmap returns EINVAL.
max = math.MaxUint64
}
// Reserve the full max bytes of address space, to ensure we don't need to move it.
// Reserve max bytes of address space, to ensure we won't need to move it.
// A protected, private, anonymous mapping should not commit memory.
b, err := unix.Mmap(-1, 0, int(max), unix.PROT_NONE, unix.MAP_PRIVATE|unix.MAP_ANON)
if err != nil {
Expand Down Expand Up @@ -49,18 +54,26 @@ func (m *mmappedBuffer) Buffer() []byte {
}

func (m *mmappedBuffer) Grow(size uint64) []byte {
// Commit additional memory up to size bytes.
err := unix.Mprotect(m.buf[len(m.buf):size], unix.PROT_READ|unix.PROT_WRITE)
if err != nil {
panic(err)
if com := uint64(len(m.buf)); com < size {
// Round up to the page size.
rnd := uint64(unix.Getpagesize() - 1)
new := (size + rnd) &^ rnd

// Commit additional memory up to new bytes.
err := unix.Mprotect(m.buf[com:new], unix.PROT_READ|unix.PROT_WRITE)
if err != nil {
panic(err)
}

// Update commited memory.
m.buf = m.buf[:new]
}
m.buf = m.buf[:size]
m.cur = size
return m.Buffer()
}

func (m *mmappedBuffer) Free() {
err := unix.Munmap(m.buf)
err := unix.Munmap(m.buf[:cap(m.buf)])
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 9775518

Please sign in to comment.