From 5605237db8f63f6ada93d77dfde603f3073f6cb1 Mon Sep 17 00:00:00 2001 From: Kunal Sareen Date: Fri, 20 Sep 2024 16:37:13 +1000 Subject: [PATCH] Return if a GC ran or not for `handle_user_collection_request` (#1205) Closes #1204 --------- Co-authored-by: Yi Lin --- src/memory_manager.rs | 14 +++++++++++--- src/mmtk.rs | 11 +++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/memory_manager.rs b/src/memory_manager.rs index a0ae84a06c..9c6b30034a 100644 --- a/src/memory_manager.rs +++ b/src/memory_manager.rs @@ -566,13 +566,21 @@ pub fn total_bytes(mmtk: &MMTK) -> usize { mmtk.get_plan().get_total_pages() << LOG_BYTES_IN_PAGE } -/// Trigger a garbage collection as requested by the user. +/// The application code has requested a collection. This is just a GC hint, and +/// we may ignore it. +/// +/// Returns whether a GC was ran or not. If MMTk triggers a GC, this method will block the +/// calling thread and return true when the GC finishes. Otherwise, this method returns +/// false immediately. /// /// Arguments: /// * `mmtk`: A reference to an MMTk instance. /// * `tls`: The thread that triggers this collection request. -pub fn handle_user_collection_request(mmtk: &MMTK, tls: VMMutatorThread) { - mmtk.handle_user_collection_request(tls, false, false); +pub fn handle_user_collection_request( + mmtk: &MMTK, + tls: VMMutatorThread, +) -> bool { + mmtk.handle_user_collection_request(tls, false, false) } /// Is the object alive? diff --git a/src/mmtk.rs b/src/mmtk.rs index f5f7622130..d636e2472a 100644 --- a/src/mmtk.rs +++ b/src/mmtk.rs @@ -403,6 +403,10 @@ impl MMTK { /// The application code has requested a collection. This is just a GC hint, and /// we may ignore it. /// + /// Returns whether a GC was ran or not. If MMTk triggers a GC, this method will block the + /// calling thread and return true when the GC finishes. Otherwise, this method returns + /// false immediately. + /// /// # Arguments /// * `tls`: The mutator thread that requests the GC /// * `force`: The request cannot be ignored (except for NoGC) @@ -412,11 +416,11 @@ impl MMTK { tls: VMMutatorThread, force: bool, exhaustive: bool, - ) { + ) -> bool { use crate::vm::Collection; if !self.get_plan().constraints().collects_garbage { warn!("User attempted a collection request, but the plan can not do GC. The request is ignored."); - return; + return false; } if force || !*self.options.ignore_system_gc && VM::VMCollection::is_collection_enabled() { @@ -432,7 +436,10 @@ impl MMTK { .store(true, Ordering::Relaxed); self.gc_requester.request(); VM::VMCollection::block_for_gc(tls); + return true; } + + false } /// MMTK has requested stop-the-world activity (e.g., stw within a concurrent gc).