Skip to content

Commit

Permalink
adjust GC threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Oct 29, 2024
1 parent 492379e commit ba7c2f9
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 56 deletions.
2 changes: 1 addition & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion crates/dash_rt/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions crates/dash_vm/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1900,7 +1900,7 @@ mod handlers {
pub fn import_dyn(mut cx: DispatchContext<'_>) -> Result<Option<HandleResult>, 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"),
};
Expand All @@ -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."),
};
Expand Down Expand Up @@ -1957,7 +1957,7 @@ mod handlers {
}

pub fn debugger(mut cx: DispatchContext<'_>) -> Result<Option<HandleResult>, Unrooted> {
if let Some(cb) = cx.params().debugger_callback() {
if let Some(cb) = cx.params().debugger_callback {
cb(&mut cx)?;
}

Expand Down
2 changes: 1 addition & 1 deletion crates/dash_vm/src/js_std/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::value::root_ext::RootErrExt;
use crate::value::Value;

pub fn time_millis(cx: &mut CallContext) -> Result<u64, Value> {
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"),
};
Expand Down
2 changes: 1 addition & 1 deletion crates/dash_vm/src/js_std/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub fn floor(cx: CallContext) -> Result<Value, Value> {
}

pub fn random(cx: CallContext) -> Result<Value, Value> {
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"),
};
Expand Down
29 changes: 15 additions & 14 deletions crates/dash_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::cell::RefCell<FxHashMap<ObjectId, u32>>>);
Expand Down Expand Up @@ -81,7 +81,7 @@ pub struct Vm {
try_blocks: Vec<TryBlock>,
#[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,
Expand All @@ -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(),
Expand All @@ -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")]
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
45 changes: 10 additions & 35 deletions crates/dash_vm/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ impl<T: Any + Trace> State for T {

#[derive(Default)]
pub struct VmParams {
math_random_callback: Option<MathRandomCallback>,
time_millis_callback: Option<TimeMillisCallback>,
static_import_callback: Option<StaticImportCallback>,
dynamic_import_callback: Option<DynamicImportCallback>,
debugger_callback: Option<DebuggerCallback>,
unhandled_task_exception_callback: Option<UnhandledTaskException>,
initial_gc_object_threshold: Option<usize>,
state: Option<Box<dyn State>>,
pub math_random_callback: Option<MathRandomCallback>,
pub time_millis_callback: Option<TimeMillisCallback>,
pub static_import_callback: Option<StaticImportCallback>,
pub dynamic_import_callback: Option<DynamicImportCallback>,
pub debugger_callback: Option<DebuggerCallback>,
pub unhandled_task_exception_callback: Option<UnhandledTaskException>,
pub initial_gc_rss_threshold: Option<usize>,
pub state: Option<Box<dyn State>>,
}

impl VmParams {
Expand All @@ -58,14 +58,6 @@ impl VmParams {
self
}

pub fn static_import_callback(&self) -> Option<StaticImportCallback> {
self.static_import_callback
}

pub fn dynamic_import_callback(&self) -> Option<DynamicImportCallback> {
self.dynamic_import_callback
}

pub fn set_state(mut self, state: Box<dyn State>) -> Self {
self.state = Some(state);
self
Expand All @@ -91,29 +83,16 @@ impl VmParams {
self.math_random_callback = Some(callback);
self
}

pub fn math_random_callback(&self) -> Option<MathRandomCallback> {
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<TimeMillisCallback> {
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<DebuggerCallback> {
self.debugger_callback
}

pub fn set_unhandled_task_exception_callback(mut self, callback: UnhandledTaskException) -> Self {
self.unhandled_task_exception_callback = Some(callback);
self
Expand All @@ -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<usize> {
self.initial_gc_object_threshold
}
}

0 comments on commit ba7c2f9

Please sign in to comment.