Expand description
VM-to-MMTk interface: safe Rust APIs.
This module provides a safe Rust API for mmtk-core. We expect the VM binding to inherit and extend this API by:
- adding their VM-specific functions
- exposing the functions to native if necessary. And the VM binding needs to manage the unsafety for exposing this safe API to FFI.
For example, for mutators, this API provides a Box<Mutator>, and requires a &mut Mutator for allocation.
A VM binding can borrow a mutable reference directly from Box<Mutator>, and call alloc(). Alternatively,
it can turn the Box pointer to a native pointer (*mut Mutator), and forge a mut reference from the native
pointer. Either way, the VM binding code needs to guarantee the safety.
Functions§
- add_
finalizer - Register a finalizable object. MMTk will retain the liveness of the object even if it is not reachable from the program. Note that finalization upon exit is not supported.
- add_
phantom_ candidate - Add a reference to the list of phantom references. A binding may call this either when a weak reference is created, or when a weak reference is traced during GC.
- add_
soft_ candidate - Add a reference to the list of soft references. A binding may call this either when a weak reference is created, or when a weak reference is traced during GC.
- add_
weak_ candidate - Add a reference to the list of weak references. A binding may call this either when a weak reference is created, or when a weak reference is traced during GC.
- add_
work_ packet - Add a work packet to the given work bucket. Note that this simply adds the work packet to the given work bucket, and the scheduler will decide when to execute the work packet.
- add_
work_ packets - Bulk add a number of work packets to the given work bucket. Note that this simply adds the work packets to the given work bucket, and the scheduler will decide when to execute the work packets.
- alloc
- Allocate memory for an object.
- alloc_
slow - Invoke the allocation slow path of
alloc. Likealloc, this function may trigger GC and callcrate::vm::Collection::block_for_gcorcrate::vm::Collection::out_of_memory. The caller needs to be aware of that. - alloc_
slow_ with_ options - Invoke the allocation slow path of
alloc_with_options. - alloc_
with_ options - Allocate memory for an object.
- bind_
mutator - Request MMTk to create a mutator for the given thread. The ownership
of returned boxed mutator is transferred to the binding, and the binding needs to take care of its
lifetime. For performance reasons, A VM should store the returned mutator in a thread local storage
that can be accessed efficiently. A VM may also copy and embed the mutator stucture to a thread-local data
structure, and use that as a reference to the mutator (it is okay to drop the box once the content is copied –
Note that
Mutatormay contain pointers so a binding may drop the box only if they perform a deep copy). - calloc
- The standard calloc.
- counted_
calloc - The standard calloc except that with the feature
malloc_counted_size, MMTk will count the allocated memory into its heap size. Thus the method requires a reference to an MMTk instance. - counted_
malloc - The standard malloc except that with the feature
malloc_counted_size, MMTk will count the allocated memory into its heap size. Thus the method requires a reference to an MMTk instance. MMTk either uses its own allocator, or forward the call to a library malloc. - destroy_
mutator - Report to MMTk that a mutator is no longer needed. All mutator state is flushed before it is destroyed. A binding should not attempt to use the mutator after this call. MMTk will not attempt to reclaim the memory for the mutator, so a binding should properly reclaim the memory for the mutator after this call.
- find_
object_ from_ internal_ pointer - Find if there is an object with VO bit set for the given address range.
This should be used instead of
crate::memory_manager::is_mmtk_objectfor conservative stack scanning if the binding may have internal pointers on the stack. - flush_
mutator - Flush the mutator’s local states.
- free
- The standard free.
The
addrin the arguments must be an address that is earlier returned from MMTk’smalloc(),calloc()orrealloc(). - free_
bytes - Return free memory in bytes. MMTk accounts for memory in pages, thus this method always returns a value in page granularity.
- free_
with_ size - The standard free except that with the feature
malloc_counted_size, MMTk will count the allocated memory into its heap size. Thus the method requires a reference to an MMTk instance, and the size of the memory to free. Theaddrin the arguments must be an address that is earlier returned from MMTk’smalloc(),calloc()orrealloc(). - gc_poll
- Poll for GC. MMTk will decide if a GC is needed. If so, this call will block the current thread, and trigger a GC. Otherwise, it will simply return. Usually a binding does not need to call this function. MMTk will poll for GC during its allocation. However, if a binding uses counted malloc (which won’t poll for GC), they may want to poll for GC manually. This function should only be used by mutator threads.
- get_
all_ finalizers - Pop all the finalizers that were registered for finalization. The returned objects may or may not be ready for finalization. After this call, MMTk’s finalizer processor should have no registered finalizer any more.
- get_
allocator_ mapping - Return an AllocatorSelector for the given allocation semantic. This method is provided so that VM compilers may call it to help generate allocation fast-path.
- get_
finalized_ object - Get an object that is ready for finalization. After each GC, if any registered object is not alive, this call will return one of the objects. MMTk will retain the liveness of those objects until they are popped through this call. Once an object is popped, it is the responsibility of the VM to make sure they are properly finalized before reclaimed by the GC. This call is non-blocking, and will return None if no object is ready for finalization.
- get_
finalizers_ for - Pop finalizers that were registered and associated with a certain object. The returned objects may or may not be ready for finalization. This is useful for some VMs that may manually execute finalize method for an object.
- get_
malloc_ bytes - Get the current active malloc’d bytes. Here MMTk only accounts for bytes that are done through those ‘counted malloc’ functions.
- handle_
user_ collection_ request - The application code has requested a collection. This is just a GC hint, and we may ignore it.
- harness_
begin - Generic hook to allow benchmarks to be harnessed. We do a full heap GC, and then start recording statistics for MMTk.
- harness_
end - Generic hook to allow benchmarks to be harnessed. We stop collecting statistics, and print stats values.
- initialize_
collection - Wrapper for
crate::mmtk::MMTK::initialize_collection. - is_
in_ mmtk_ spaces - Return true if the
objectlies in a region of memory where - is_
live_ object - Is the object alive?
- is_
mapped_ address - Is the address in the mapped memory? The runtime can use this function to check if an address is mapped by MMTk. Note that this is different than is_in_mmtk_spaces(). For malloc spaces, MMTk does not map those addresses (malloc does the mmap), so this function will return false, but is_in_mmtk_spaces will return true if the address is actually a valid object in malloc spaces. To check if an object is in our heap, the runtime should always use is_in_mmtk_spaces(). This function is_mapped_address() may get removed at some point.
- is_
mmtk_ object - Check if
addris the raw address of an object reference to an MMTk object. - is_
pinned - Check whether an object is currently pinned
- last_
heap_ address - Return the ending address of the heap. Note that currently MMTk uses a fixed address range as heap.
- live_
bytes_ in_ last_ gc - Return a hash map for live bytes statistics in the last GC for each space.
- malloc
- The standard malloc. MMTk either uses its own allocator, or forward the call to a library malloc.
- memory_
region_ copy - The subsuming memory region copy barrier by MMTk. This is called when the VM tries to copy a piece of heap memory to another. The data within the slice does not necessarily to be all valid pointers, but the VM binding will be able to filter out non-reference values on slot iteration.
- memory_
region_ copy_ post - The generic memory region copy post barrier by MMTk, which we expect a binding to call after it performs memory copy. This is called when the VM tries to copy a piece of heap memory to another. The data within the slice does not necessarily to be all valid pointers, but the VM binding will be able to filter out non-reference values on slot iteration.
- memory_
region_ copy_ pre - The generic memory region copy pre barrier by MMTk, which we expect a binding to call before it performs memory copy. This is called when the VM tries to copy a piece of heap memory to another. The data within the slice does not necessarily to be all valid pointers, but the VM binding will be able to filter out non-reference values on slot iteration.
- mmtk_
init - Initialize an MMTk instance. A VM should call this method after creating an
crate::MMTKinstance but before using any of the methods provided in MMTk (exceptprocess()andprocess_bulk()). - num_
of_ workers - Get the number of workers. MMTk spawns worker threads for the ‘threads’ defined in the options. So the number of workers is derived from the threads option. Note the feature single_worker overwrites the threads option, and force one worker thread.
- object_
reference_ write Deprecated - The subsuming write barrier by MMTk. For performance reasons, a VM should implement the write barrier fast-path on their side rather than just calling this function.
- object_
reference_ write_ post - The write barrier by MMTk. This is a post write barrier, which we expect a binding to call after it modifies an object. For performance reasons, a VM should implement the write barrier fast-path on their side rather than just calling this function.
- object_
reference_ write_ pre - The write barrier by MMTk. This is a pre write barrier, which we expect a binding to call before it modifies an object. For performance reasons, a VM should implement the write barrier fast-path on their side rather than just calling this function.
- pin_
object - Pin an object. MMTk will make sure that the object does not move during GC. Note that action cannot happen in some plans, eg, semispace. It returns true if the pinning operation has been performed, i.e., the object status changed from non-pinned to pinned
- post_
alloc - Perform post-allocation actions, usually initializing object metadata. For many allocators none are required. For performance reasons, a VM should implement the post alloc fast-path on their side rather than just calling this function.
- process
- Process MMTk run-time options. Returns true if the option is processed successfully.
- process_
bulk - Process multiple MMTk run-time options. Returns true if all the options are processed successfully.
- realloc
- The standard realloc.
- realloc_
with_ old_ size - The standard realloc except that with the feature
malloc_counted_size, MMTk will count the allocated memory into its heap size. Thus the method requires a reference to an MMTk instance, and the size of the existing memory that will be reallocated. Theaddrin the arguments must be an address that is earlier returned from MMTk’smalloc(),calloc()orrealloc(). - set_
vm_ space - Add an externally mmapped region to the VM space. A VM space can be set through MMTk options (
vm_space_startandvm_space_size), and can also be set through this function call. A VM space can be discontiguous. This function can be called multiple times, and all the address ranges passed as arguments in the function will be considered as part of the VM space. Currently we do not allow removing regions from VM space. - start_
worker - Wrapper for
crate::scheduler::GCWorker::run. - starting_
heap_ address - Return the starting address of the heap. Note that currently MMTk uses a fixed address range as heap.
- total_
bytes - Return the total memory in bytes.
- unpin_
object - Unpin an object. Returns true if the unpinning operation has been performed, i.e., the object status changed from pinned to non-pinned
- used_
bytes - Return used memory in bytes. MMTk accounts for memory in pages, thus this method always returns a value in page granularity.