$ show mmtk/mmtk-core
MMTk (Memory Management Toolkit) is a high-performance, portable garbage collection framework written in Rust. It provides a library of reusable GC algorithms (plans) including generational, concurrent, mark-sweep, immix, reference-counting (LXR), and mark-compact variants. The system exposes a trait-based VM binding interface (VMBinding) that language runtimes implement to integrate MMTk as their garbage collector. The architecture is modular: a parallel work-stealing scheduler dispatches GC work packets, policy spaces encapsulate memory allocation strategies (bump, free-list, immix, large object, etc.), and a flexible metadata system supports both header-resident and side metadata. The crate is not a standalone application; it is consumed as an rlib by VM bindings such as OpenJDK, JikesRVM, V8, Julia, and Ruby. Extensive use of procedural macros (HasSpaces, PlanTraceObject) reduces boilerplate for adding new GC plans. The codebase includes performance tracing tools (eBPF, Perfetto) and a comprehensive test suite with mock VM support.
The LLM identified 5 files worth highlighting for reuse:
Provides a generic, type-safe API for per-object and per-block metadata stored either in side tables (discontiguous or contiguous) or in object headers. Supports atomic and non-atomic reads, writes, fetch-and operations, bulk clear/set, and VO-bit scanning. The abstraction decouples metadata layout from GC logic, making it reusable across different GC plans and policies.
Implements a parallel work distribution system using buckets of work packets that can be opened, closed, drained, and have sentinel packets for coordination. Supports work stealing and conditional opening (e.g., open only after all predecessors close). The bucket stage enum defines the GC pipeline order. This pattern is reusable for any parallel phased computation.
Provides a MockVM and MockMethod system that allows unit testing GC components without a real VM binding. Uses thread-local state and unsafe scope functions (with_mockvm) to set up and tear down mock implementations of VM traits (ActivePlan, Collection, ObjectModel, Scanning, ReferenceGlue). This enables isolated testing of allocators, barriers, work packets, and the scheduler, which is critical for a library that must integrate with multiple runtimes.
Defines the Allocator trait with alloc/alloc_slow/post_alloc methods, allocation options (alignment, offset, stress trigger), and a thread-local allocation context. The trait design allows VM bindings to implement a fast path in native code while relying on MMTk's slow path for buffer refill, GC triggering, and OOM handling. This separation of concerns is reusable for any allocator integrated into a managed runtime.
Implements WorkerMonitor, a synchronization primitive for parking/unparking GC workers with goal-based wakeups (GC start, shutdown, fork). Uses condvars and a last-parked-worker callback to coordinate when to wake all workers, wake only the requesting worker, or park itself. This pattern is reusable for any multi-threaded system where workers need to sleep and be selectively awakened based on dynamic conditions.