Skip to content

Commit

Permalink
runtime: notify OS more aggressively about freed pages
Browse files Browse the repository at this point in the history
This causes the number of dirty pages reported by
vmmap to decrease after a scavenge.

I don't know why this works. Apparently neither do the folks
on StackOverflow who suggested it:

https://stackoverflow.com/questions/7718964/

For more history and a discussion of this StackOverflow post,
see golang#29844.

The key observation is that MADV_FREE_REUSABLE allows the OS
to decide that pages may be considered not-dirty,
but the OS is under no immediate obligation to do so.
And apparently iOS does not do so promptly,
and thus the accounting is not updated promptly.

Change-Id: Iec09510c5cc57c24c1f190e5f658096b12b7d691
  • Loading branch information
josharian committed Aug 10, 2021
1 parent 6785c6a commit 38ab03e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/runtime/mem_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func sysUnused(v unsafe.Pointer, n uintptr) {
// MADV_FREE_REUSABLE is like MADV_FREE except it also propagates
// accounting information about the process to task_info.
madvise(v, n, _MADV_FREE_REUSABLE)
mprotect(v, n, _PROT_NONE)
mprotect(v, n, _PROT_READ|_PROT_WRITE)
}

func sysUsed(v unsafe.Pointer, n uintptr) {
Expand Down
7 changes: 7 additions & 0 deletions src/runtime/sys_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ func madvise(addr unsafe.Pointer, n uintptr, flags int32) {
}
func madvise_trampoline()

//go:nosplit
//go:cgo_unsafe_args
func mprotect(addr unsafe.Pointer, n uintptr, flags int32) {
libcCall(unsafe.Pointer(abi.FuncPCABI0(mprotect_trampoline)), unsafe.Pointer(&addr))
}
func mprotect_trampoline()

//go:nosplit
//go:cgo_unsafe_args
func mlock(addr unsafe.Pointer, n uintptr) {
Expand Down
7 changes: 7 additions & 0 deletions src/runtime/sys_darwin_arm64.s
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ TEXT runtime·madvise_trampoline(SB),NOSPLIT,$0
BL libc_madvise(SB)
RET

TEXT runtime·mprotect_trampoline(SB),NOSPLIT,$0
MOVD 8(R0), R1 // arg 2 len
MOVW 16(R0), R2 // arg 3 advice
MOVD 0(R0), R0 // arg 1 addr
BL libc_mprotect(SB)
RET

TEXT runtime·mlock_trampoline(SB),NOSPLIT,$0
MOVD 8(R0), R1 // arg 2 len
MOVD 0(R0), R0 // arg 1 addr
Expand Down

0 comments on commit 38ab03e

Please sign in to comment.