Skip to content

Commit bd73b45

Browse files
authored
fix: make initial/max heap size configurable for main/event worker (#568)
* fix: make initial/max heap size configurable for main/event worker * stamp: polishing
1 parent 5859f07 commit bd73b45

File tree

2 files changed

+93
-27
lines changed

2 files changed

+93
-27
lines changed

cli/src/env.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
use base::deno_runtime;
2-
use clap::builder::{BoolishValueParser, TypedValueParser};
2+
use clap::{
3+
builder::{BoolishValueParser, TypedValueParser},
4+
value_parser,
5+
};
36
use once_cell::sync::OnceCell;
47

58
pub(super) fn resolve_deno_runtime_env() {
69
let boolish_parser = BoolishValueParser::new();
10+
let u64_parser = value_parser!(u64);
711
let dumb_command = clap::Command::new(env!("CARGO_BIN_NAME"));
8-
let resolve_boolish_env = move |key: &'static str, cell: &'static OnceCell<bool>| {
12+
let resolve_boolish_env = |key: &'static str, cell: &'static OnceCell<bool>| {
913
cell.get_or_init(|| {
1014
std::env::var_os(key)
1115
.map(|it| {
@@ -17,6 +21,18 @@ pub(super) fn resolve_deno_runtime_env() {
1721
})
1822
};
1923

24+
let resolve_u64_env = |key: &'static str, cell: &'static OnceCell<u64>| {
25+
cell.get_or_init(|| {
26+
std::env::var_os(key)
27+
.map(|it| {
28+
u64_parser
29+
.parse_ref(&dumb_command, None, &it)
30+
.unwrap_or_default()
31+
})
32+
.unwrap_or_default()
33+
})
34+
};
35+
2036
deno_runtime::MAYBE_DENO_VERSION.get_or_init(|| deno_manifest::version().to_string());
2137

2238
resolve_boolish_env(
@@ -33,4 +49,24 @@ pub(super) fn resolve_deno_runtime_env() {
3349
"EDGE_RUNTIME_INCLUDE_MALLOCED_MEMORY_ON_MEMCHECK",
3450
&deno_runtime::SHOULD_INCLUDE_MALLOCED_MEMORY_ON_MEMCHECK,
3551
);
52+
53+
resolve_u64_env(
54+
"EDGE_RUNTIME_MAIN_WORKER_INITIAL_HEAP_SIZE_MIB",
55+
&deno_runtime::MAIN_WORKER_INITIAL_HEAP_SIZE_MIB,
56+
);
57+
58+
resolve_u64_env(
59+
"EDGE_RUNTIME_MAIN_WORKER_MAX_HEAP_SIZE_MIB",
60+
&deno_runtime::MAIN_WORKER_MAX_HEAP_SIZE_MIB,
61+
);
62+
63+
resolve_u64_env(
64+
"EDGE_RUNTIME_EVENT_WORKER_INITIAL_HEAP_SIZE_MIB",
65+
&deno_runtime::EVENT_WORKER_INITIAL_HEAP_SIZE_MIB,
66+
);
67+
68+
resolve_u64_env(
69+
"EDGE_RUNTIME_EVENT_WORKER_MAX_HEAP_SIZE_MIB",
70+
&deno_runtime::EVENT_WORKER_MAX_HEAP_SIZE_MIB,
71+
);
3672
}

crates/base/src/deno_runtime.rs

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ use sb_event_worker::events::{EventMetadata, WorkerEventWithMetadata};
7878
use sb_event_worker::js_interceptors::sb_events_js_interceptors;
7979
use sb_event_worker::sb_user_event_worker;
8080
use sb_node::deno_node;
81-
use sb_workers::context::{UserWorkerMsgs, WorkerContextInitOpts, WorkerRuntimeOpts};
81+
use sb_workers::context::{UserWorkerMsgs, WorkerContextInitOpts, WorkerKind, WorkerRuntimeOpts};
8282
use sb_workers::sb_user_workers;
8383

8484
const DEFAULT_ALLOC_CHECK_INT_MSEC: u64 = 1000;
@@ -107,6 +107,11 @@ pub static SHOULD_USE_VERBOSE_DEPRECATED_API_WARNING: OnceCell<bool> = OnceCell:
107107
pub static SHOULD_INCLUDE_MALLOCED_MEMORY_ON_MEMCHECK: OnceCell<bool> = OnceCell::new();
108108
pub static MAYBE_DENO_VERSION: OnceCell<String> = OnceCell::new();
109109

110+
pub static MAIN_WORKER_INITIAL_HEAP_SIZE_MIB: OnceCell<u64> = OnceCell::new();
111+
pub static MAIN_WORKER_MAX_HEAP_SIZE_MIB: OnceCell<u64> = OnceCell::new();
112+
pub static EVENT_WORKER_INITIAL_HEAP_SIZE_MIB: OnceCell<u64> = OnceCell::new();
113+
pub static EVENT_WORKER_MAX_HEAP_SIZE_MIB: OnceCell<u64> = OnceCell::new();
114+
110115
thread_local! {
111116
// NOTE: Suppose we have met `.await` points while initializing a
112117
// DenoRuntime. In that case, the current v8 isolate's thread-local state can be
@@ -761,37 +766,62 @@ where
761766
let beforeunload_cpu_threshold = ArcSwapOption::<u64>::from_pointee(None);
762767
let beforeunload_mem_threshold = ArcSwapOption::<u64>::from_pointee(None);
763768

764-
if conf.is_user_worker() {
765-
let conf = maybe_user_conf.unwrap();
766-
let memory_limit_bytes = mib_to_bytes(conf.memory_limit_mb) as usize;
767-
768-
beforeunload_mem_threshold.store(
769-
flags
770-
.beforeunload_memory_pct
771-
.and_then(|it| percentage_value(memory_limit_bytes as u64, it))
772-
.map(Arc::new),
773-
);
769+
match conf.to_worker_kind() {
770+
WorkerKind::UserWorker => {
771+
let conf = maybe_user_conf.unwrap();
772+
let memory_limit_bytes = mib_to_bytes(conf.memory_limit_mb) as usize;
774773

775-
if conf.cpu_time_hard_limit_ms > 0 {
776-
beforeunload_cpu_threshold.store(
774+
beforeunload_mem_threshold.store(
777775
flags
778-
.beforeunload_cpu_pct
779-
.and_then(|it| percentage_value(conf.cpu_time_hard_limit_ms, it))
776+
.beforeunload_memory_pct
777+
.and_then(|it| percentage_value(memory_limit_bytes as u64, it))
780778
.map(Arc::new),
781779
);
782-
}
783780

784-
let allocator = CustomAllocator::new(memory_limit_bytes);
781+
if conf.cpu_time_hard_limit_ms > 0 {
782+
beforeunload_cpu_threshold.store(
783+
flags
784+
.beforeunload_cpu_pct
785+
.and_then(|it| percentage_value(conf.cpu_time_hard_limit_ms, it))
786+
.map(Arc::new),
787+
);
788+
}
785789

786-
allocator.set_waker(mem_check.waker.clone());
790+
let allocator = CustomAllocator::new(memory_limit_bytes);
787791

788-
mem_check.limit = Some(memory_limit_bytes);
789-
create_params = Some(
790-
v8::CreateParams::default()
791-
.heap_limits(mib_to_bytes(0) as usize, memory_limit_bytes)
792-
.array_buffer_allocator(allocator.into_v8_allocator()),
793-
)
794-
};
792+
allocator.set_waker(mem_check.waker.clone());
793+
794+
mem_check.limit = Some(memory_limit_bytes);
795+
create_params = Some(
796+
v8::CreateParams::default()
797+
.heap_limits(mib_to_bytes(0) as usize, memory_limit_bytes)
798+
.array_buffer_allocator(allocator.into_v8_allocator()),
799+
)
800+
}
801+
kind => {
802+
assert_ne!(kind, WorkerKind::UserWorker);
803+
let initial_heap_size = match kind {
804+
WorkerKind::MainWorker => &MAIN_WORKER_INITIAL_HEAP_SIZE_MIB,
805+
WorkerKind::EventsWorker => &EVENT_WORKER_INITIAL_HEAP_SIZE_MIB,
806+
_ => unreachable!(),
807+
};
808+
let max_heap_size = match kind {
809+
WorkerKind::MainWorker => &MAIN_WORKER_MAX_HEAP_SIZE_MIB,
810+
WorkerKind::EventsWorker => &EVENT_WORKER_MAX_HEAP_SIZE_MIB,
811+
_ => unreachable!(),
812+
};
813+
814+
let initial_heap_size = initial_heap_size.get().cloned().unwrap_or_default();
815+
let max_heap_size = max_heap_size.get().cloned().unwrap_or_default();
816+
817+
if max_heap_size > 0 {
818+
create_params = Some(v8::CreateParams::default().heap_limits(
819+
mib_to_bytes(initial_heap_size) as usize,
820+
mib_to_bytes(max_heap_size) as usize,
821+
));
822+
}
823+
}
824+
}
795825

796826
let mem_check = Arc::new(mem_check);
797827
let runtime_options = RuntimeOptions {

0 commit comments

Comments
 (0)