From ba7c2f9a78fd2a3b7ec42c3873b044dd6e971092 Mon Sep 17 00:00:00 2001 From: y21 <30553356+y21@users.noreply.github.com> Date: Tue, 29 Oct 2024 11:04:13 +0100 Subject: [PATCH] adjust GC threshold --- cli/src/main.rs | 2 +- crates/dash_rt/src/runtime.rs | 2 +- crates/dash_vm/src/dispatch.rs | 6 ++--- crates/dash_vm/src/js_std/date.rs | 2 +- crates/dash_vm/src/js_std/math.rs | 2 +- crates/dash_vm/src/lib.rs | 29 ++++++++++---------- crates/dash_vm/src/params.rs | 45 +++++++------------------------ 7 files changed, 32 insertions(+), 56 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 80279ca6..998ed8c1 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -21,7 +21,7 @@ fn main() -> anyhow::Result<()> { let nodejs = Arg::new("node").long("node").takes_value(false); let initial_gc_threshold = Arg::new("initial-gc-threshold") - .help("Sets the initial GC object threshold, i.e. the object count at which the first GC cycle triggers.") + .help("Sets the initial GC object threshold, i.e. the RSS at which the first GC cycle triggers.") .long("initial-gc-threshold") .takes_value(true) .required(false); diff --git a/crates/dash_rt/src/runtime.rs b/crates/dash_rt/src/runtime.rs index a4e420f6..c0a2667f 100644 --- a/crates/dash_rt/src/runtime.rs +++ b/crates/dash_rt/src/runtime.rs @@ -41,7 +41,7 @@ impl Runtime { .set_state(Box::new(state)); if let Some(threshold) = initial_gc_threshold { - params = params.set_initial_gc_object_threshold(threshold); + params = params.set_initial_gc_rss_threshold(threshold); } let vm = Vm::new(params); diff --git a/crates/dash_vm/src/dispatch.rs b/crates/dash_vm/src/dispatch.rs index 0e348076..a058421d 100755 --- a/crates/dash_vm/src/dispatch.rs +++ b/crates/dash_vm/src/dispatch.rs @@ -1900,7 +1900,7 @@ mod handlers { pub fn import_dyn(mut cx: DispatchContext<'_>) -> Result, Unrooted> { let value = cx.pop_stack_rooted(); - let _ret = match cx.params.dynamic_import_callback() { + let _ret = match cx.params.dynamic_import_callback { Some(cb) => cb(&mut cx, value)?, None => throw!(cx, Error, "Dynamic imports are disabled for this context"), }; @@ -1918,7 +1918,7 @@ mod handlers { let path = cx.constants().symbols[SymbolConstant(path_id)]; - let value = match cx.params.static_import_callback() { + let value = match cx.params.static_import_callback { Some(cb) => cb(&mut cx, ty, path.into())?, None => throw!(cx, Error, "Static imports are disabled for this context."), }; @@ -1957,7 +1957,7 @@ mod handlers { } pub fn debugger(mut cx: DispatchContext<'_>) -> Result, Unrooted> { - if let Some(cb) = cx.params().debugger_callback() { + if let Some(cb) = cx.params().debugger_callback { cb(&mut cx)?; } diff --git a/crates/dash_vm/src/js_std/date.rs b/crates/dash_vm/src/js_std/date.rs index 32793be5..0cd01731 100644 --- a/crates/dash_vm/src/js_std/date.rs +++ b/crates/dash_vm/src/js_std/date.rs @@ -4,7 +4,7 @@ use crate::value::root_ext::RootErrExt; use crate::value::Value; pub fn time_millis(cx: &mut CallContext) -> Result { - let callback = match cx.scope.params().time_millis_callback() { + let callback = match cx.scope.params().time_millis_callback { Some(c) => c, None => throw!(&mut cx.scope, Error, "Failed to get the current time"), }; diff --git a/crates/dash_vm/src/js_std/math.rs b/crates/dash_vm/src/js_std/math.rs index 5a6b817a..2dbf3069 100644 --- a/crates/dash_vm/src/js_std/math.rs +++ b/crates/dash_vm/src/js_std/math.rs @@ -210,7 +210,7 @@ pub fn floor(cx: CallContext) -> Result { } pub fn random(cx: CallContext) -> Result { - let num = match cx.scope.params().math_random_callback() { + let num = match cx.scope.params().math_random_callback { Some(cb) => cb(cx.scope).root_err(cx.scope)?, None => throw!(cx.scope, Error, "Math.random is disabled for this context"), }; diff --git a/crates/dash_vm/src/lib.rs b/crates/dash_vm/src/lib.rs index fc1e428a..5a8576fc 100644 --- a/crates/dash_vm/src/lib.rs +++ b/crates/dash_vm/src/lib.rs @@ -52,7 +52,7 @@ pub mod value; pub const MAX_FRAME_STACK_SIZE: usize = 1024; pub const MAX_STACK_SIZE: usize = 8192; -const DEFAULT_GC_OBJECT_COUNT_THRESHOLD: usize = 8192; +const DEFAULT_GC_RSS_THRESHOLD: usize = 1024 * 1024; #[derive(Clone, Default)] pub struct ExternalRefs(pub std::rc::Rc>>); @@ -81,7 +81,7 @@ pub struct Vm { try_blocks: Vec, #[cfg_attr(dash_lints, dash_lints::trusted_no_gc)] params: VmParams, - gc_object_threshold: usize, + gc_rss_threshold: usize, /// Keeps track of the "purity" of the builtins of this VM. /// Purity here refers to whether builtins have been (in one way or another) mutated. /// Removing a property from the global object (e.g. `Math`) or any other builtin, @@ -99,9 +99,9 @@ impl Vm { let statics = Statics::new(&mut alloc); // TODO: global __proto__ and constructor let global: ObjectId = alloc.alloc_object(PureBuiltin::new(NamedObject::null())); - let gc_object_threshold = params - .initial_gc_object_threshold() - .unwrap_or(DEFAULT_GC_OBJECT_COUNT_THRESHOLD); + let gc_rss_threshold = params + .initial_gc_rss_threshold + .unwrap_or(DEFAULT_GC_RSS_THRESHOLD); let mut vm = Self { frames: Vec::new(), @@ -115,7 +115,7 @@ impl Vm { statics: Box::new(statics), try_blocks: Vec::new(), params, - gc_object_threshold, + gc_rss_threshold, builtins_pure: true, #[cfg(feature = "jit")] @@ -1436,9 +1436,9 @@ impl Vm { } #[cfg(not(feature = "stress_gc"))] { - // if util::unlikely(self.gc.node_count() > self.gc_object_threshold) { - // self.perform_gc(); - // } + if util::unlikely(self.alloc.rss() > self.gc_rss_threshold) { + self.perform_gc(); + } } let instruction = Instruction::from_repr(self.fetch_and_inc_ip()).unwrap(); @@ -1475,18 +1475,19 @@ impl Vm { trace_roots.in_scope(|| self.trace_roots()); // All reachable roots are marked. - // debug!("object count before sweep: {}", self.gc.node_count()); + debug!("rss before sweep: {}", self.alloc.rss()); + let before = self.alloc.rss(); let sweep = span!(Level::TRACE, "gc sweep"); sweep.in_scope(|| unsafe { self.alloc.sweep() }); - // debug!("object count after sweep: {}", self.gc.node_count()); + debug!("rss after sweep: {}", self.alloc.rss()); + println!("[GC] {} -> {}", before, self.alloc.rss()); debug!("sweep interner"); self.interner.sweep(); // Adjust GC threshold - // let new_object_count = self.gc.node_count(); - // self.gc_object_threshold = new_object_count * 2; - debug!("new threshold: {}", self.gc_object_threshold); + self.gc_rss_threshold = self.alloc.rss() * 2; + debug!("new threshold: {}", self.gc_rss_threshold); } fn trace_roots(&mut self) { diff --git a/crates/dash_vm/src/params.rs b/crates/dash_vm/src/params.rs index 952ddb58..a70a35ed 100644 --- a/crates/dash_vm/src/params.rs +++ b/crates/dash_vm/src/params.rs @@ -33,14 +33,14 @@ impl State for T { #[derive(Default)] pub struct VmParams { - math_random_callback: Option, - time_millis_callback: Option, - static_import_callback: Option, - dynamic_import_callback: Option, - debugger_callback: Option, - unhandled_task_exception_callback: Option, - initial_gc_object_threshold: Option, - state: Option>, + pub math_random_callback: Option, + pub time_millis_callback: Option, + pub static_import_callback: Option, + pub dynamic_import_callback: Option, + pub debugger_callback: Option, + pub unhandled_task_exception_callback: Option, + pub initial_gc_rss_threshold: Option, + pub state: Option>, } impl VmParams { @@ -58,14 +58,6 @@ impl VmParams { self } - pub fn static_import_callback(&self) -> Option { - self.static_import_callback - } - - pub fn dynamic_import_callback(&self) -> Option { - self.dynamic_import_callback - } - pub fn set_state(mut self, state: Box) -> Self { self.state = Some(state); self @@ -91,29 +83,16 @@ impl VmParams { self.math_random_callback = Some(callback); self } - - pub fn math_random_callback(&self) -> Option { - self.math_random_callback - } - pub fn set_time_millis_callback(mut self, callback: TimeMillisCallback) -> Self { self.time_millis_callback = Some(callback); self } - pub fn time_millis_callback(&self) -> Option { - self.time_millis_callback - } - pub fn set_debugger_callback(mut self, callback: DebuggerCallback) -> Self { self.debugger_callback = Some(callback); self } - pub fn debugger_callback(&self) -> Option { - self.debugger_callback - } - pub fn set_unhandled_task_exception_callback(mut self, callback: UnhandledTaskException) -> Self { self.unhandled_task_exception_callback = Some(callback); self @@ -123,12 +102,8 @@ impl VmParams { self.unhandled_task_exception_callback } - pub fn set_initial_gc_object_threshold(mut self, threshold: usize) -> Self { - self.initial_gc_object_threshold = Some(threshold); + pub fn set_initial_gc_rss_threshold(mut self, threshold: usize) -> Self { + self.initial_gc_rss_threshold = Some(threshold); self } - - pub fn initial_gc_object_threshold(&self) -> Option { - self.initial_gc_object_threshold - } }