Skip to content

Commit b2281ea

Browse files
committed
Merge branch 'develop' into ny/support-otel
# Conflicts: # crates/base/src/runtime/mod.rs # crates/base_rt/src/lib.rs
2 parents 64741e0 + 0e71c5c commit b2281ea

File tree

17 files changed

+742
-295
lines changed

17 files changed

+742
-295
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/src/env.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use base::runtime;
22
use clap::builder::BoolishValueParser;
33
use clap::builder::TypedValueParser;
4+
use clap::value_parser;
45
use once_cell::sync::OnceCell;
56

67
pub(super) fn resolve_deno_runtime_env() {
78
let boolish_parser = BoolishValueParser::new();
9+
let u64_parser = value_parser!(u64);
810
let dumb_command = clap::Command::new(env!("CARGO_BIN_NAME"));
911
let resolve_boolish_env =
10-
move |key: &'static str, cell: &'static OnceCell<bool>| {
12+
|key: &'static str, cell: &'static OnceCell<bool>| {
1113
cell.get_or_init(|| {
1214
std::env::var_os(key)
1315
.map(|it| {
@@ -19,6 +21,18 @@ pub(super) fn resolve_deno_runtime_env() {
1921
})
2022
};
2123

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+
2236
runtime::MAYBE_DENO_VERSION.get_or_init(|| deno::version().to_string());
2337

2438
resolve_boolish_env(
@@ -35,4 +49,24 @@ pub(super) fn resolve_deno_runtime_env() {
3549
"EDGE_RUNTIME_INCLUDE_MALLOCED_MEMORY_ON_MEMCHECK",
3650
&runtime::SHOULD_INCLUDE_MALLOCED_MEMORY_ON_MEMCHECK,
3751
);
52+
53+
resolve_u64_env(
54+
"EDGE_RUNTIME_MAIN_WORKER_INITIAL_HEAP_SIZE_MIB",
55+
&runtime::MAIN_WORKER_INITIAL_HEAP_SIZE_MIB,
56+
);
57+
58+
resolve_u64_env(
59+
"EDGE_RUNTIME_MAIN_WORKER_MAX_HEAP_SIZE_MIB",
60+
&runtime::MAIN_WORKER_MAX_HEAP_SIZE_MIB,
61+
);
62+
63+
resolve_u64_env(
64+
"EDGE_RUNTIME_EVENT_WORKER_INITIAL_HEAP_SIZE_MIB",
65+
&runtime::EVENT_WORKER_INITIAL_HEAP_SIZE_MIB,
66+
);
67+
68+
resolve_u64_env(
69+
"EDGE_RUNTIME_EVENT_WORKER_MAX_HEAP_SIZE_MIB",
70+
&runtime::EVENT_WORKER_MAX_HEAP_SIZE_MIB,
71+
);
3872
}

crates/base/src/runtime/mod.rs

Lines changed: 88 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use base_rt::DenoRuntimeDropToken;
2525
use base_rt::DropToken;
2626
use base_rt::RuntimeOtelExtraAttributes;
2727
use base_rt::RuntimeState;
28+
use base_rt::RuntimeWaker;
2829
use cooked_waker::IntoWaker;
2930
use cooked_waker::WakeRef;
3031
use cpu_timer::CPUTimer;
@@ -87,6 +88,7 @@ use ext_runtime::MemCheckWaker;
8788
use ext_runtime::PromiseMetrics;
8889
use ext_workers::context::UserWorkerMsgs;
8990
use ext_workers::context::WorkerContextInitOpts;
91+
use ext_workers::context::WorkerKind;
9092
use ext_workers::context::WorkerRuntimeOpts;
9193
use fs::deno_compile_fs::DenoCompileFileSystem;
9294
use fs::prefix_fs::PrefixFs;
@@ -159,6 +161,11 @@ pub static SHOULD_INCLUDE_MALLOCED_MEMORY_ON_MEMCHECK: OnceCell<bool> =
159161
OnceCell::new();
160162
pub static MAYBE_DENO_VERSION: OnceCell<String> = OnceCell::new();
161163

164+
pub static MAIN_WORKER_INITIAL_HEAP_SIZE_MIB: OnceCell<u64> = OnceCell::new();
165+
pub static MAIN_WORKER_MAX_HEAP_SIZE_MIB: OnceCell<u64> = OnceCell::new();
166+
pub static EVENT_WORKER_INITIAL_HEAP_SIZE_MIB: OnceCell<u64> = OnceCell::new();
167+
pub static EVENT_WORKER_MAX_HEAP_SIZE_MIB: OnceCell<u64> = OnceCell::new();
168+
162169
#[ctor]
163170
fn init_v8_platform() {
164171
set_v8_flags();
@@ -387,6 +394,21 @@ fn cleanup_js_runtime(runtime: &mut JsRuntime) {
387394
}
388395
}
389396

397+
fn cleanup_js_runtime(runtime: &mut JsRuntime) {
398+
let isolate = runtime.v8_isolate();
399+
400+
assert_isolate_not_locked(isolate);
401+
let locker = unsafe {
402+
Locker::new(std::mem::transmute::<&mut Isolate, &mut Isolate>(isolate))
403+
};
404+
405+
isolate.set_slot(locker);
406+
407+
{
408+
let _scope = runtime.handle_scope();
409+
}
410+
}
411+
390412
pub struct DenoRuntime<RuntimeContext = DefaultRuntimeContext> {
391413
pub runtime_state: Arc<RuntimeState>,
392414
pub js_runtime: ManuallyDrop<JsRuntime>,
@@ -471,6 +493,7 @@ where
471493
..
472494
} = init_opts.unwrap();
473495

496+
let waker = Arc::<AtomicWaker>::default();
474497
let drop_token = CancellationToken::default();
475498
let is_user_worker = conf.is_user_worker();
476499
let is_some_entry_point = maybe_entrypoint.is_some();
@@ -486,6 +509,7 @@ where
486509
.unwrap_or_else(|| get_default_permissions(conf.to_worker_kind()));
487510

488511
struct Bootstrap {
512+
waker: Arc<AtomicWaker>,
489513
js_runtime: JsRuntime,
490514
mem_check: Arc<MemCheck>,
491515
has_inspector: bool,
@@ -662,15 +686,22 @@ where
662686
let tmp_fs =
663687
TmpFs::try_from(maybe_tmp_fs_config.unwrap_or_default())?;
664688
let tmp_fs_actual_path = tmp_fs.actual_path().to_path_buf();
665-
let fs = PrefixFs::new("/tmp", tmp_fs.clone(), Some(base_fs))
689+
let mut fs = PrefixFs::new("/tmp", tmp_fs.clone(), Some(base_fs))
666690
.tmp_dir("/tmp")
667691
.add_fs(tmp_fs_actual_path, tmp_fs);
668692

693+
fs
694+
.set_runtime_state(&runtime_state);
695+
669696
Ok(
670697
if let Some(s3_fs) =
671698
maybe_s3_fs_config.map(S3Fs::new).transpose()?
672699
{
673-
(Arc::new(fs.add_fs("/s3", s3_fs.clone())), Some(s3_fs))
700+
let mut s3_prefix_fs = fs.add_fs("/s3", s3_fs.clone());
701+
702+
s3_prefix_fs.set_check_sync_api(is_user_worker);
703+
704+
(Arc::new(s3_prefix_fs), Some(s3_fs))
674705
} else {
675706
(Arc::new(fs), None)
676707
},
@@ -779,39 +810,65 @@ where
779810
let beforeunload_mem_threshold =
780811
ArcSwapOption::<u64>::from_pointee(None);
781812

782-
if conf.is_user_worker() {
783-
let conf = maybe_user_conf.unwrap();
784-
let memory_limit_bytes = mib_to_bytes(conf.memory_limit_mb) as usize;
813+
match conf.to_worker_kind() {
814+
WorkerKind::UserWorker => {
815+
let conf = maybe_user_conf.unwrap();
816+
let memory_limit_bytes = mib_to_bytes(conf.memory_limit_mb) as usize;
785817

786-
beforeunload_mem_threshold.store(
787-
flags
788-
.beforeunload_memory_pct
789-
.and_then(|it| percentage_value(memory_limit_bytes as u64, it))
790-
.map(Arc::new),
791-
);
792-
793-
if conf.cpu_time_hard_limit_ms > 0 {
794-
beforeunload_cpu_threshold.store(
818+
beforeunload_mem_threshold.store(
795819
flags
796-
.beforeunload_cpu_pct
797-
.and_then(|it| {
798-
percentage_value(conf.cpu_time_hard_limit_ms, it)
799-
})
820+
.beforeunload_memory_pct
821+
.and_then(|it| percentage_value(memory_limit_bytes as u64, it))
800822
.map(Arc::new),
801823
);
824+
825+
if conf.cpu_time_hard_limit_ms > 0 {
826+
beforeunload_cpu_threshold.store(
827+
flags
828+
.beforeunload_cpu_pct
829+
.and_then(|it| {
830+
percentage_value(conf.cpu_time_hard_limit_ms, it)
831+
})
832+
.map(Arc::new),
833+
);
834+
}
835+
836+
let allocator = CustomAllocator::new(memory_limit_bytes);
837+
838+
allocator.set_waker(mem_check.waker.clone());
839+
840+
mem_check.limit = Some(memory_limit_bytes);
841+
create_params = Some(
842+
v8::CreateParams::default()
843+
.heap_limits(mib_to_bytes(0) as usize, memory_limit_bytes)
844+
.array_buffer_allocator(allocator.into_v8_allocator()),
845+
)
802846
}
803847

804-
let allocator = CustomAllocator::new(memory_limit_bytes);
848+
kind => {
849+
assert_ne!(kind, WorkerKind::UserWorker);
850+
let initial_heap_size = match kind {
851+
WorkerKind::MainWorker => &MAIN_WORKER_INITIAL_HEAP_SIZE_MIB,
852+
WorkerKind::EventsWorker => &EVENT_WORKER_INITIAL_HEAP_SIZE_MIB,
853+
_ => unreachable!(),
854+
};
855+
let max_heap_size = match kind {
856+
WorkerKind::MainWorker => &MAIN_WORKER_MAX_HEAP_SIZE_MIB,
857+
WorkerKind::EventsWorker => &EVENT_WORKER_MAX_HEAP_SIZE_MIB,
858+
_ => unreachable!(),
859+
};
805860

806-
allocator.set_waker(mem_check.waker.clone());
861+
let initial_heap_size = initial_heap_size.get().cloned().unwrap_or_default();
862+
let max_heap_size = max_heap_size.get().cloned().unwrap_or_default();
807863

808-
mem_check.limit = Some(memory_limit_bytes);
809-
create_params = Some(
810-
v8::CreateParams::default()
811-
.heap_limits(mib_to_bytes(0) as usize, memory_limit_bytes)
812-
.array_buffer_allocator(allocator.into_v8_allocator()),
813-
)
814-
};
864+
if max_heap_size > 0 {
865+
create_params = Some(v8::CreateParams::default().heap_limits(
866+
mib_to_bytes(initial_heap_size) as usize,
867+
mib_to_bytes(max_heap_size) as usize,
868+
));
869+
}
870+
}
871+
}
815872

816873
let mem_check = Arc::new(mem_check);
817874
let runtime_options = RuntimeOptions {
@@ -879,6 +936,7 @@ where
879936
op_state.put(promise_metrics.clone());
880937
op_state.put(runtime_state.clone());
881938
op_state.put(GlobalMainContext(main_context));
939+
op_state.put(RuntimeWaker(waker.clone()))
882940
}
883941

884942
{
@@ -913,6 +971,7 @@ where
913971
}
914972

915973
Ok(Bootstrap {
974+
waker,
916975
js_runtime,
917976
mem_check,
918977
has_inspector,
@@ -1029,6 +1088,7 @@ where
10291088
.await;
10301089

10311090
let Bootstrap {
1091+
waker,
10321092
mut js_runtime,
10331093
mem_check,
10341094
main_module_url,
@@ -1169,7 +1229,7 @@ where
11691229
promise_metrics,
11701230

11711231
mem_check,
1172-
waker: Arc::default(),
1232+
waker,
11731233

11741234
beforeunload_cpu_threshold: Arc::new(beforeunload_cpu_threshold),
11751235
beforeunload_mem_threshold: Arc::new(beforeunload_mem_threshold),

crates/base/test_cases/user-worker-san-check/.blocklisted

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ futime
2121
futimeSync
2222
link
2323
linkSync
24-
lstat
25-
lstatSync
2624
makeTempFile
2725
makeTempFileSync
2826
readLink

crates/base/test_cases/user-worker-san-check/.whitelisted

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mkdirSync
22
readDirSync
33
removeSync
4+
lstatSync
45
statSync
56
writeFileSync
67
writeTextFileSync

crates/base_rt/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::sync::Arc;
77
use cpu_timer::get_thread_time;
88
use deno_core::anyhow::Context;
99
use deno_core::error::AnyError;
10+
use deno_core::futures::task::AtomicWaker;
1011
use deno_core::OpState;
1112
use deno_core::Resource;
1213
use deno_core::V8CrossThreadTaskSpawner;
@@ -196,6 +197,9 @@ impl BlockingScopeCPUUsageMetricExt for &mut OpState {
196197
}
197198
}
198199

200+
#[derive(Debug, Clone)]
201+
pub struct RuntimeWaker(pub Arc<AtomicWaker>);
202+
199203
#[derive(Debug, Clone)]
200204
pub struct RuntimeOtelExtraAttributes(
201205
pub HashMap<opentelemetry::Key, opentelemetry::Value>,

crates/fs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ deno_semver.workspace = true
2020

2121
eszip_trait.workspace = true
2222

23+
base_rt.workspace = true
2324
ext_node.workspace = true
2425

2526
anyhow.workspace = true

0 commit comments

Comments
 (0)