@@ -2,7 +2,6 @@ use crate::deno_runtime::DenoRuntime;
2
2
use crate :: inspector_server:: Inspector ;
3
3
use crate :: timeout:: { self , CancelOnWriteTimeout , ReadTimeoutStream } ;
4
4
use crate :: utils:: send_event_if_event_worker_available;
5
- use crate :: utils:: units:: bytes_to_display;
6
5
7
6
use crate :: rt_worker:: worker:: { Worker , WorkerHandler } ;
8
7
use crate :: rt_worker:: worker_pool:: WorkerPool ;
@@ -11,7 +10,8 @@ use cpu_timer::CPUTimer;
11
10
use deno_config:: JsxImportSourceConfig ;
12
11
use deno_core:: { InspectorSessionProxy , LocalInspectorSession } ;
13
12
use event_worker:: events:: {
14
- BootEvent , ShutdownEvent , WorkerEventWithMetadata , WorkerEvents , WorkerMemoryUsed ,
13
+ BootEvent , MemoryLimitDetail , MemoryLimitDetailMemCheck , MemoryLimitDetailV8 , ShutdownEvent ,
14
+ WorkerEventWithMetadata , WorkerEvents , WorkerMemoryUsed ,
15
15
} ;
16
16
use futures_util:: pin_mut;
17
17
use http:: StatusCode ;
@@ -31,6 +31,7 @@ use sb_workers::errors::WorkerError;
31
31
use std:: future:: pending;
32
32
use std:: io:: ErrorKind ;
33
33
use std:: path:: PathBuf ;
34
+ use std:: sync:: atomic:: Ordering ;
34
35
use std:: sync:: Arc ;
35
36
use std:: time:: Duration ;
36
37
use tokio:: io:: { self , copy_bidirectional} ;
@@ -260,7 +261,7 @@ pub fn create_supervisor(
260
261
timing : Option < Timing > ,
261
262
termination_token : Option < TerminationToken > ,
262
263
) -> Result < ( Option < CPUTimer > , CancellationToken ) , Error > {
263
- let ( memory_limit_tx, memory_limit_rx) = mpsc:: unbounded_channel :: < ( ) > ( ) ;
264
+ let ( memory_limit_tx, memory_limit_rx) = mpsc:: unbounded_channel :: < MemoryLimitDetail > ( ) ;
264
265
let ( waker, thread_safe_handle) = {
265
266
let js_runtime = & mut worker_runtime. js_runtime ;
266
267
(
@@ -271,6 +272,7 @@ pub fn create_supervisor(
271
272
272
273
// we assert supervisor is only run for user workers
273
274
let conf = worker_runtime. conf . as_user_worker ( ) . unwrap ( ) . clone ( ) ;
275
+ let mem_check_captured_bytes = worker_runtime. mem_check_captured_bytes ( ) ;
274
276
let is_termination_requested = worker_runtime. is_termination_requested . clone ( ) ;
275
277
276
278
let giveup_process_requests_token = cancel. clone ( ) ;
@@ -292,32 +294,43 @@ pub fn create_supervisor(
292
294
)
293
295
} ) ;
294
296
295
- worker_runtime. add_memory_limit_callback ( {
296
- let memory_limit_tx = memory_limit_tx. clone ( ) ;
297
- move || {
298
- debug ! ( "Hard memory limit triggered" ) ;
297
+ let send_memory_limit_fn = move |detail : MemoryLimitDetail | {
298
+ debug ! (
299
+ "memory limit triggered: isolate: {:?}, detail: {}" ,
300
+ key, detail
301
+ ) ;
299
302
300
- if memory_limit_tx. send ( ( ) ) . is_err ( ) {
301
- error ! ( "failed to send memory limit reached notification - isolate may already be terminating" ) ;
302
- }
303
+ if memory_limit_tx. send ( detail) . is_err ( ) {
304
+ error ! (
305
+ "failed to send memory limit reached notification - isolate may already be terminating: kind: {}" ,
306
+ <& ' static str >:: from( & detail)
307
+ ) ;
308
+ }
309
+ } ;
310
+
311
+ worker_runtime. add_memory_limit_callback ( {
312
+ let send_fn = send_memory_limit_fn. clone ( ) ;
313
+ move |captured| {
314
+ send_fn ( MemoryLimitDetail :: MemCheck ( MemoryLimitDetailMemCheck {
315
+ captured,
316
+ } ) ) ;
303
317
304
318
true
305
319
}
306
320
} ) ;
307
321
308
322
worker_runtime. js_runtime . add_near_heap_limit_callback ( {
309
- let memory_limit_tx = memory_limit_tx. clone ( ) ;
310
- move |cur, _| {
311
- debug ! ( "Low memory alert triggered: {}" , bytes_to_display( cur as u64 ) , ) ;
312
-
313
- if memory_limit_tx. send ( ( ) ) . is_err ( ) {
314
- error ! ( "failed to send memory limit reached notification - isolate may already be terminating" ) ;
315
- }
323
+ let send_fn = send_memory_limit_fn;
324
+ move |current, initial| {
325
+ send_fn ( MemoryLimitDetail :: V8 ( MemoryLimitDetailV8 {
326
+ current,
327
+ initial,
328
+ } ) ) ;
316
329
317
330
// give an allowance on current limit (until the isolate is
318
331
// terminated) we do this so that oom won't end up killing the
319
332
// edge-runtime process
320
- cur * ( conf. low_memory_multiplier as usize )
333
+ current * ( conf. low_memory_multiplier as usize )
321
334
}
322
335
} ) ;
323
336
@@ -471,7 +484,9 @@ pub fn create_supervisor(
471
484
total : v. used_heap_size + v. external_memory ,
472
485
heap : v. used_heap_size ,
473
486
external : v. external_memory ,
487
+ mem_check_captured : mem_check_captured_bytes. load ( Ordering :: Acquire ) ,
474
488
} ,
489
+
475
490
Err ( _) => {
476
491
if !supervise_cancel_token_inner. is_cancelled ( ) {
477
492
error ! ( "isolate memory usage sender dropped" ) ;
@@ -481,6 +496,7 @@ pub fn create_supervisor(
481
496
total : 0 ,
482
497
heap : 0 ,
483
498
external : 0 ,
499
+ mem_check_captured : 0 ,
484
500
}
485
501
}
486
502
} ;
0 commit comments