Documentation
¶
Overview ¶
Package purgeable wraps Darwin's Mach purgeable virtual memory API (mach_vm_allocate with VM_FLAGS_PURGABLE and mach_vm_purgable_control) without cgo.
Purgeable memory lets an application cache reconstructible data (such as KV cache blocks, prefix states, or decoded assets) in kernel-managed slabs. While a Slab is NonVolatile, its pages are pinned like ordinary anonymous memory. Marking it Volatile tells the XNU VM subsystem that it may discard the slab's physical pages synchronously under memory pressure instead of paging them out or killing the process via jetsam.
Before reading a volatile slab again, callers transition it back to NonVolatile via Slab.MarkNonVolatile or Slab.Pin. The kernel atomically reports whether the slab's contents survived or were purged (Empty / ErrPurged) while volatile.
Pool manages a bounded set of fixed-size Slab allocations with acquire/release tracking and purged-slab accounting.
Basic usage:
slab, err := purgeable.Alloc(4<<20, purgeable.Options{Behavior: purgeable.FIFO})
if err != nil {
log.Fatal(err)
}
defer slab.Close()
copy(slab.Bytes(), data)
if err := slab.MarkVolatile(purgeable.FIFO); err != nil {
log.Fatal(err)
}
purged, err := slab.MarkNonVolatile()
if err != nil {
log.Fatal(err)
}
if purged {
// Kernel discarded the pages under pressure; recompute data.
}
Example ¶
package main
import (
"fmt"
"log"
"github.com/tmc/apple/x/purgeable"
)
func main() {
slab, err := purgeable.Alloc(64<<10, purgeable.Options{Behavior: purgeable.FIFO})
if err != nil {
log.Fatal(err)
}
defer slab.Close()
copy(slab.Bytes(), "prefix-block-0")
if err := slab.MarkVolatile(purgeable.FIFO); err != nil {
log.Fatal(err)
}
purged, err := slab.MarkNonVolatile()
if err != nil {
log.Fatal(err)
}
if !purged {
fmt.Println(string(slab.Bytes()[:14]))
}
}
Output: prefix-block-0
Index ¶
- Variables
- type Behavior
- type Options
- type Pool
- type PoolConfig
- type PoolStats
- type Slab
- func (s *Slab) Addr() uintptr
- func (s *Slab) AllocSize() int
- func (s *Slab) Bytes() []byte
- func (s *Slab) Close() error
- func (s *Slab) Len() int
- func (s *Slab) MarkEmpty() error
- func (s *Slab) MarkNonVolatile() (purged bool, err error)
- func (s *Slab) MarkVolatile(behavior Behavior) error
- func (s *Slab) Pin() error
- func (s *Slab) State() (State, error)
- type State
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrPoolExhausted is returned by [Pool.Acquire] when all capacity slabs // are currently checked out. ErrPoolExhausted = errors.New("purgeable: pool exhausted") // ErrPoolClosed is returned when operating on a [Pool] after [Pool.Close]. ErrPoolClosed = errors.New("purgeable: pool is closed") )
var ( // ErrPurged is returned by [Slab.Pin] when the kernel discarded the slab's // physical pages while it was volatile. The slab has been transitioned back // to [NonVolatile] and its backing memory is zero-filled and ready to be // repopulated. ErrPurged = errors.New("purgeable: slab was purged by kernel") // ErrClosed is returned when operating on a [Slab] after [Slab.Close]. ErrClosed = errors.New("purgeable: slab is closed") )
Functions ¶
This section is empty.
Types ¶
type Behavior ¶
type Behavior uint8
Behavior controls eviction ordering within a volatile priority group.
const ( // FIFO evicts objects marked volatile earlier before objects marked later. // Use FIFO for backward-cached data that has already been used. FIFO Behavior = 0 // LIFO evicts objects marked volatile later before objects marked earlier. // Use LIFO for forward-cached data expected to be needed soon. LIFO Behavior = 1 )
type Options ¶
type Options struct {
// Behavior sets the default eviction order when marking the slab volatile.
Behavior Behavior
// Group selects the volatile priority group (0 through 7). Lower-numbered
// groups are purged before higher-numbered groups under memory pressure.
Group int
// Obsolete places the slab in the kernel's obsolete queue when marked
// volatile, causing it to be purged before all normal volatile groups.
Obsolete bool
// NoAging disables kernel volatile-queue aging when marked volatile.
NoAging bool
}
Options configures a purgeable Slab. The zero value selects FIFO eviction in volatile group 0 with normal ordering and aging enabled.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool manages a bounded collection of fixed-size purgeable Slab allocations. Slabs checked out via Pool.Acquire are NonVolatile; slabs returned via Pool.Release are marked Volatile so the kernel can reclaim their pages under memory pressure.
Example ¶
package main
import (
"fmt"
"log"
"github.com/tmc/apple/x/purgeable"
)
func main() {
pool, err := purgeable.NewPool(purgeable.PoolConfig{
SlabSize: 64 << 10,
Capacity: 2,
})
if err != nil {
log.Fatal(err)
}
defer pool.Close()
slab, purged, err := pool.Acquire()
if err != nil {
log.Fatal(err)
}
fmt.Println("initial purged:", purged)
if err := pool.Release(slab, purgeable.LIFO); err != nil {
log.Fatal(err)
}
if err := pool.MarkAllEmpty(); err != nil {
log.Fatal(err)
}
_, purged, err = pool.Acquire()
if err != nil {
log.Fatal(err)
}
fmt.Println("after empty purged:", purged)
}
Output: initial purged: false after empty purged: true
func NewPool ¶
func NewPool(cfg PoolConfig) (*Pool, error)
NewPool creates a Pool with the given configuration.
func (*Pool) Acquire ¶
Acquire returns a NonVolatile slab from the pool.
If an idle slab is available, Acquire transitions it from Volatile to NonVolatile via Slab.MarkNonVolatile. When purged is true, the kernel discarded the slab's pages while it was idle and the caller must repopulate its contents. If no idle slab is available and fewer than Capacity slabs have been allocated, Acquire allocates a new slab (with purged == false).
func (*Pool) Close ¶
Close deallocates all slabs managed by the pool. Calling Close more than once is a no-op and returns nil.
func (*Pool) MarkAllEmpty ¶
MarkAllEmpty transitions every currently idle slab in the pool to Empty, immediately discarding their physical pages while retaining their virtual address reservations. Subsequent Pool.Acquire calls on those slabs will report purged == true.
type PoolConfig ¶
type PoolConfig struct {
// SlabSize is the size of each slab in bytes. Must be positive.
SlabSize int
// Capacity is the maximum number of slabs managed by the pool. Must be positive.
Capacity int
// Preallocate allocates all Capacity slabs during [NewPool] and marks them
// volatile so they are immediately available for acquisition.
Preallocate bool
// Options configures each slab allocated by the pool.
Options Options
}
PoolConfig configures a Pool of fixed-size purgeable slabs.
type PoolStats ¶
type PoolStats struct {
Total int // slabs currently allocated and managed by the pool
InUse int // slabs currently checked out (NonVolatile)
Idle int // slabs currently idle in the pool (Volatile or Empty)
Acquires uint64 // total successful Acquire calls
Hits uint64 // Acquire calls that re-acquired an intact idle slab
Purged uint64 // Acquire calls that detected a kernel-purged idle slab
Allocations uint64 // total slabs allocated via Alloc
}
PoolStats reports current slab counts and cumulative activity for a Pool.
type Slab ¶
type Slab struct {
// contains filtered or unexported fields
}
Slab is a page-aligned Mach VM region allocated with VM_FLAGS_PURGABLE. A Slab starts in the NonVolatile state and must be released with Slab.Close when no longer needed.
func Alloc ¶
Alloc allocates a purgeable Mach VM region of at least size bytes. The underlying allocation is rounded up to the system page size. The returned Slab starts in the NonVolatile state.
func (*Slab) Addr ¶
Addr returns the page-aligned base address of the Mach VM mapping, or 0 if the slab is closed.
func (*Slab) Bytes ¶
Bytes returns a byte slice covering the requested length of the slab. It returns nil if the slab has been closed.
Callers must only read or write the returned slice while the slab is NonVolatile and before Slab.Close is called.
func (*Slab) Close ¶
Close deallocates the underlying Mach VM region. Calling Close more than once is a no-op and returns nil.
func (*Slab) MarkEmpty ¶
MarkEmpty transitions the slab to Empty, immediately discarding its physical pages. A subsequent Slab.MarkNonVolatile call will report purged == true and Slab.Pin will return ErrPurged.
func (*Slab) MarkNonVolatile ¶
MarkNonVolatile transitions the slab back to NonVolatile. If the kernel discarded the slab's pages while it was volatile (its previous state was Empty), purged is true and the slab's contents have been zeroed.
func (*Slab) MarkVolatile ¶
MarkVolatile transitions the slab to Volatile with the given eviction behavior and the slab's configured group, obsolete, and aging options. While volatile, the kernel may discard the slab's physical pages at any time under memory pressure.
func (*Slab) Pin ¶
Pin transitions the slab to NonVolatile and returns ErrPurged if the kernel had discarded its contents while volatile. Even when ErrPurged is returned, the slab is now NonVolatile and ready to be repopulated.
Example ¶
package main
import (
"errors"
"fmt"
"log"
"github.com/tmc/apple/x/purgeable"
)
func main() {
slab, err := purgeable.Alloc(64<<10, purgeable.Options{})
if err != nil {
log.Fatal(err)
}
defer slab.Close()
if err := slab.MarkEmpty(); err != nil {
log.Fatal(err)
}
err = slab.Pin()
fmt.Println("purged:", errors.Is(err, purgeable.ErrPurged))
state, err := slab.State()
if err != nil {
log.Fatal(err)
}
fmt.Println("state:", state)
}
Output: purged: true state: nonvolatile
type State ¶
type State uint8
State represents the kernel purgeability state of a Slab.
const ( // NonVolatile indicates the slab is pinned and cannot be purged by the kernel. NonVolatile State = 0 // Volatile indicates the slab may be discarded by the kernel under memory pressure. Volatile State = 1 // Empty indicates the slab is volatile and its backing pages have been discarded. Empty State = 2 // Deny indicates the underlying VM object is not purgeable. Deny State = 3 )