mini_racer_rs 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,10 @@
1
- use std::sync::atomic::{AtomicBool, Ordering};
1
+ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
2
2
  use std::sync::{Arc, Mutex, OnceLock};
3
3
  use std::time::{Duration, Instant};
4
4
 
5
5
  use mini_racer_sync::{Channel, DispatchId, Protocol, RunnerPhase, TermCell, Terminator, lock};
6
6
 
7
+ use crate::counters::Counters;
7
8
  use crate::notify::NotifyFd;
8
9
  use crate::process::tasks::Tasks;
9
10
  use crate::{EngineError, SnapshotBlob};
@@ -53,6 +54,14 @@ impl JsText {
53
54
  Self::Utf16(units) => units.len(),
54
55
  }
55
56
  }
57
+
58
+ #[must_use]
59
+ pub const fn byte_len(&self) -> usize {
60
+ match self {
61
+ Self::Utf8(bytes) | Self::Latin1(bytes) => bytes.len(),
62
+ Self::Utf16(units) => units.len().saturating_mul(2),
63
+ }
64
+ }
56
65
  }
57
66
 
58
67
  impl From<String> for JsText {
@@ -83,16 +92,19 @@ pub enum Request {
83
92
  depth: Depth,
84
93
  },
85
94
  /// `awaited`: a promise result is awaited, as `eval_await` does.
95
+ /// `timeout`: the dispatch's own, which only tightens the context's.
86
96
  Eval {
87
97
  filename: JsText,
88
98
  source: JsText,
89
99
  depth: Depth,
90
100
  awaited: bool,
101
+ timeout: Option<Duration>,
91
102
  },
92
103
  Call {
93
104
  request: CallRequest,
94
105
  depth: Depth,
95
106
  awaited: bool,
107
+ timeout: Option<Duration>,
96
108
  },
97
109
  Attach {
98
110
  request: Vec<u8>,
@@ -126,6 +138,47 @@ pub enum Request {
126
138
  Dispose,
127
139
  }
128
140
 
141
+ impl Request {
142
+ /// The timeout a script request was given for itself.
143
+ pub const fn timeout(&self) -> Option<Duration> {
144
+ match self {
145
+ Self::Eval { timeout, .. } | Self::Call { timeout, .. } => *timeout,
146
+ _ => None,
147
+ }
148
+ }
149
+
150
+ /// The bytes a counted dispatch hands JavaScript; `None` for the
151
+ /// context's own setup and dispose, which no caller asked for.
152
+ pub const fn payload(&self) -> Option<usize> {
153
+ match self {
154
+ Self::Init { .. } | Self::Dispose => None,
155
+ Self::Eval { source, .. } => Some(source.byte_len()),
156
+ Self::Call { request, .. } => Some(request.bytes.len()),
157
+ Self::Attach { request, .. } => Some(request.len()),
158
+ Self::Snapshot { code, .. } => Some(code.byte_len()),
159
+ Self::LowMemoryNotification { .. }
160
+ | Self::MicrotaskCheckpoint { .. }
161
+ | Self::HeapStats { .. }
162
+ | Self::HeapSnapshot { .. }
163
+ | Self::PumpMessageLoop { .. } => Some(0),
164
+ }
165
+ }
166
+
167
+ /// Whether the runner's time on it counts as JavaScript's: the requests
168
+ /// that run scripts, not the heap's statistics, snapshot or full
169
+ /// collection, which can take far longer than the scripts.
170
+ pub const fn runs_script(&self) -> bool {
171
+ matches!(
172
+ self,
173
+ Self::Eval { .. }
174
+ | Self::Call { .. }
175
+ | Self::Attach { .. }
176
+ | Self::MicrotaskCheckpoint { .. }
177
+ | Self::PumpMessageLoop { .. }
178
+ )
179
+ }
180
+ }
181
+
129
182
  pub struct IsolateTerminator {
130
183
  handle: v8::IsolateHandle,
131
184
  tasks: Arc<Tasks>,
@@ -174,13 +227,57 @@ pub struct ContextConfig {
174
227
  /// Has V8 report each exception that escapes a script, as `mini_racer`'s
175
228
  /// verbose `TryCatch` does: it prints where it happened to stdout.
176
229
  pub verbose_exceptions: bool,
230
+ /// The heap size, in bytes, V8 is given for the isolate. A script that
231
+ /// reaches it ends with `TerminationCause::HeapExhausted` where V8 would
232
+ /// abort the process, as long as what it allocates past the limit fits
233
+ /// in the room the runner lends V8 to end it.
234
+ pub heap_limit: Option<usize>,
235
+ }
236
+
237
+ /// What V8's near-heap-limit callback and the runner share for a context
238
+ /// with a heap limit. The callback touches nothing else but the termination
239
+ /// cell, so V8 may call it from any thread.
240
+ #[derive(Default)]
241
+ pub struct HeapWatch {
242
+ /// V8's limit for the old generation under the heap limit, which a hit
243
+ /// raises and the runner puts back.
244
+ pub old_generation: AtomicUsize,
245
+ /// V8's whole heap size limit under the heap limit, which the runner
246
+ /// checks once it has put the old generation's back.
247
+ pub reserved: AtomicUsize,
248
+ /// V8 called the callback since the runner last recovered.
249
+ pub hit: AtomicBool,
250
+ /// V8 terminated a script since the runner last recovered.
251
+ pub ended: AtomicBool,
252
+ /// The runner is collecting on its own, with no script to terminate.
253
+ pub collecting: AtomicBool,
254
+ /// A collection after a hit left the heap over its limit, so the context
255
+ /// runs no more JavaScript.
256
+ pub poisoned: AtomicBool,
257
+ }
258
+
259
+ impl HeapWatch {
260
+ /// For a forked child taking over the isolate this watched in its parent.
261
+ pub fn inherit(&self, parent: &Self) {
262
+ for (mine, theirs) in [
263
+ (&self.old_generation, &parent.old_generation),
264
+ (&self.reserved, &parent.reserved),
265
+ ] {
266
+ mine.store(theirs.load(Ordering::Relaxed), Ordering::Relaxed);
267
+ }
268
+ self.poisoned
269
+ .store(parent.poisoned.load(Ordering::Relaxed), Ordering::Relaxed);
270
+ }
177
271
  }
178
272
 
179
273
  /// The context's pending timeout and the earliest deadline the watchdog holds
180
274
  /// for it, so the watchdog keeps about one entry per context, not per dispatch.
275
+ /// Nested scripts timed on their own have slots apart, innermost last, so
276
+ /// the deadlines around each outlive it.
181
277
  #[derive(Default)]
182
278
  pub struct Timer {
183
279
  pub due: Option<(Instant, DispatchId)>,
280
+ pub nested: Vec<(Instant, DispatchId)>,
184
281
  pub queued: Option<Instant>,
185
282
  }
186
283
 
@@ -205,6 +302,8 @@ pub struct ControlBlock {
205
302
  // An idle single-threaded runner leaves its isolate here, its only owner
206
303
  // then, so a forked child can hand it to a runner of its own.
207
304
  pub custody: Mutex<Option<v8::SharedIsolate>>,
305
+ pub heap: HeapWatch,
306
+ pub counters: Counters,
208
307
  }
209
308
 
210
309
  impl Drop for ControlBlock {
@@ -224,6 +323,18 @@ impl Drop for ControlBlock {
224
323
  }
225
324
  }
226
325
 
326
+ impl ContextConfig {
327
+ /// The timeout a script runs under: the shorter of the context's and the
328
+ /// one the script was given, so a script's own only tightens it.
329
+ #[must_use]
330
+ pub fn timeout_within(&self, own: Option<Duration>) -> Option<Duration> {
331
+ match (self.timeout, own) {
332
+ (Some(context), Some(own)) => Some(context.min(own)),
333
+ (context, own) => context.or(own),
334
+ }
335
+ }
336
+ }
337
+
227
338
  impl ControlBlock {
228
339
  /// Asks the runner to park for a pending fork, in JavaScript or in a
229
340
  /// snapshot build, wherever V8 next checks for interrupts.
@@ -238,11 +349,12 @@ impl ControlBlock {
238
349
  }
239
350
 
240
351
  pub fn new(config: ContextConfig) -> Self {
241
- Self::with_tasks(config, Tasks::default())
352
+ Self::adopted(config, Tasks::default(), Counters::default())
242
353
  }
243
354
 
244
- /// For a forked child taking over an idle context with its queued tasks.
245
- pub fn with_tasks(config: ContextConfig, tasks: Tasks) -> Self {
355
+ /// For a forked child taking over an idle context with its queued tasks
356
+ /// and its counts.
357
+ pub fn adopted(config: ContextConfig, tasks: Tasks, counters: Counters) -> Self {
246
358
  Self {
247
359
  channel: Channel::default(),
248
360
  term: TermCell::default(),
@@ -255,6 +367,8 @@ impl ControlBlock {
255
367
  isolate: OnceLock::new(),
256
368
  building: Mutex::new(None),
257
369
  custody: Mutex::new(None),
370
+ heap: HeapWatch::default(),
371
+ counters,
258
372
  }
259
373
  }
260
374
  }
@@ -0,0 +1,208 @@
1
+ use std::sync::atomic::{AtomicU64, Ordering};
2
+ use std::time::Duration;
3
+
4
+ use crate::control::CallbackReply;
5
+ use crate::{EngineError, JsError, JsErrorKind, TerminationCause};
6
+
7
+ /// What a context has done since it was made; a forked child that takes it
8
+ /// over counts on from its parent's counts.
9
+ #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
10
+ pub struct Counts {
11
+ /// Requests sent to the runner, nested ones included: evals, calls and
12
+ /// their await forms, attaches, heap statistics and snapshots, low memory
13
+ /// notifications, microtask checkpoints and pumps; not the context's own
14
+ /// setup or dispose.
15
+ pub dispatches: u64,
16
+ /// Attached Ruby callables run for JavaScript.
17
+ pub callbacks: u64,
18
+ /// An eval's source in the encoding it was sent in, a call's or attach's
19
+ /// request and a callback's value in V8's wire format, and the message of
20
+ /// an error a callback raised.
21
+ pub bytes_to_js: u64,
22
+ /// Each result the runner answered with and each callback's arguments.
23
+ pub bytes_from_js: u64,
24
+ /// The runner's time on evals, calls, attaches, microtask checkpoints and
25
+ /// pumps, short of its waits for Ruby callbacks, for a task an await
26
+ /// needs and for a fork.
27
+ pub js_ns: u64,
28
+ /// V8's scavenges, minor mark-sweeps and mark-compacts on the context's
29
+ /// isolate, in a dispatch or not.
30
+ pub gc_count: u64,
31
+ /// The runner's time inside them, from V8's prologue to its epilogue.
32
+ pub gc_ns: u64,
33
+ pub terminated: Terminations,
34
+ }
35
+
36
+ /// Dispatches a termination ended, by the cause the engine reported for each.
37
+ #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
38
+ pub struct Terminations {
39
+ pub stop: u64,
40
+ pub timeout: u64,
41
+ /// The caller abandoned the wait: an exception raised into it, or a throw
42
+ /// or kill out of a callback.
43
+ pub interrupt: u64,
44
+ pub max_memory: u64,
45
+ pub heap_exhausted: u64,
46
+ }
47
+
48
+ /// A context's counts, in the control block. Each half has one writer at a
49
+ /// time, so an increment is a load and a store rather than a locked add: the
50
+ /// thread dispatching, which holds the context, or the runner. Readers on any
51
+ /// thread see each value on its own, never torn.
52
+ #[derive(Default)]
53
+ pub struct Counters {
54
+ caller: Caller,
55
+ runner: Runner,
56
+ }
57
+
58
+ // 128 bytes apart, as x86's prefetcher fetches 64-byte lines in pairs and
59
+ // Apple's cores have 128-byte lines, so the two writers never share one.
60
+ #[derive(Default)]
61
+ #[repr(align(128))]
62
+ struct Caller {
63
+ dispatches: AtomicU64,
64
+ callbacks: AtomicU64,
65
+ bytes_to_js: AtomicU64,
66
+ bytes_from_js: AtomicU64,
67
+ terminated: [AtomicU64; 5],
68
+ }
69
+
70
+ #[derive(Default)]
71
+ #[repr(align(128))]
72
+ struct Runner {
73
+ js_ns: AtomicU64,
74
+ gc_count: AtomicU64,
75
+ gc_ns: AtomicU64,
76
+ }
77
+
78
+ fn add(counter: &AtomicU64, amount: u64) {
79
+ counter.store(
80
+ counter.load(Ordering::Relaxed).wrapping_add(amount),
81
+ Ordering::Relaxed,
82
+ );
83
+ }
84
+
85
+ fn read(counter: &AtomicU64) -> u64 {
86
+ counter.load(Ordering::Relaxed)
87
+ }
88
+
89
+ fn copied(counter: &AtomicU64) -> AtomicU64 {
90
+ AtomicU64::new(read(counter))
91
+ }
92
+
93
+ fn length(bytes: usize) -> u64 {
94
+ u64::try_from(bytes).unwrap_or(u64::MAX)
95
+ }
96
+
97
+ const fn slot(cause: TerminationCause) -> usize {
98
+ match cause {
99
+ TerminationCause::Stop => 0,
100
+ TerminationCause::Timeout => 1,
101
+ TerminationCause::Cancelled => 2,
102
+ TerminationCause::MaxMemory => 3,
103
+ TerminationCause::HeapExhausted => 4,
104
+ }
105
+ }
106
+
107
+ impl Counters {
108
+ #[must_use]
109
+ pub fn read(&self) -> Counts {
110
+ let caller = &self.caller;
111
+ let terminated = |cause| read(&caller.terminated[slot(cause)]);
112
+ Counts {
113
+ dispatches: read(&caller.dispatches),
114
+ callbacks: read(&caller.callbacks),
115
+ bytes_to_js: read(&caller.bytes_to_js),
116
+ bytes_from_js: read(&caller.bytes_from_js),
117
+ js_ns: read(&self.runner.js_ns),
118
+ gc_count: read(&self.runner.gc_count),
119
+ gc_ns: read(&self.runner.gc_ns),
120
+ terminated: Terminations {
121
+ stop: terminated(TerminationCause::Stop),
122
+ timeout: terminated(TerminationCause::Timeout),
123
+ interrupt: terminated(TerminationCause::Cancelled),
124
+ max_memory: terminated(TerminationCause::MaxMemory),
125
+ heap_exhausted: terminated(TerminationCause::HeapExhausted),
126
+ },
127
+ }
128
+ }
129
+
130
+ /// The counts so far, for a forked child taking the context over, read
131
+ /// from atomics alone.
132
+ #[must_use]
133
+ pub fn inherited(&self) -> Self {
134
+ let caller = &self.caller;
135
+ Self {
136
+ caller: Caller {
137
+ dispatches: copied(&caller.dispatches),
138
+ callbacks: copied(&caller.callbacks),
139
+ bytes_to_js: copied(&caller.bytes_to_js),
140
+ bytes_from_js: copied(&caller.bytes_from_js),
141
+ terminated: std::array::from_fn(|at| copied(&caller.terminated[at])),
142
+ },
143
+ runner: Runner {
144
+ js_ns: copied(&self.runner.js_ns),
145
+ gc_count: copied(&self.runner.gc_count),
146
+ gc_ns: copied(&self.runner.gc_ns),
147
+ },
148
+ }
149
+ }
150
+
151
+ pub(crate) fn dispatched(&self, bytes: usize) {
152
+ add(&self.caller.dispatches, 1);
153
+ add(&self.caller.bytes_to_js, length(bytes));
154
+ }
155
+
156
+ pub(crate) fn asked(&self, bytes: usize) {
157
+ add(&self.caller.bytes_from_js, length(bytes));
158
+ }
159
+
160
+ pub(crate) fn called_back(&self) {
161
+ add(&self.caller.callbacks, 1);
162
+ }
163
+
164
+ pub(crate) fn replied(&self, reply: &CallbackReply) {
165
+ let bytes = match reply {
166
+ CallbackReply::Value(bytes) => bytes.len(),
167
+ CallbackReply::Raised(message) => message.byte_len(),
168
+ CallbackReply::Abort => return,
169
+ };
170
+ add(&self.caller.bytes_to_js, length(bytes));
171
+ }
172
+
173
+ /// A dispatch's response as its caller gets it. A nested dispatch its
174
+ /// caller abandoned was ended without a cause of its own
175
+ /// (`TermCell::interrupt`), so its termination is the interrupt's, which
176
+ /// outranks every cause but the memory ones, as the engine ranks them.
177
+ pub(crate) fn answered(&self, response: &Result<Vec<u8>, EngineError>, abandoned: bool) {
178
+ match response {
179
+ Ok(bytes) => add(&self.caller.bytes_from_js, length(bytes.len())),
180
+ Err(EngineError::Js(JsError {
181
+ kind: JsErrorKind::Terminated(cause),
182
+ ..
183
+ })) => {
184
+ let cause = match *cause {
185
+ memory @ (TerminationCause::MaxMemory | TerminationCause::HeapExhausted) => {
186
+ memory
187
+ }
188
+ _ if abandoned => TerminationCause::Cancelled,
189
+ other => other,
190
+ };
191
+ add(&self.caller.terminated[slot(cause)], 1);
192
+ }
193
+ Err(_) => {}
194
+ }
195
+ }
196
+
197
+ pub(crate) fn ran_js(&self, nanoseconds: u64) {
198
+ add(&self.runner.js_ns, nanoseconds);
199
+ }
200
+
201
+ pub(crate) fn collected(&self, took: Duration) {
202
+ add(&self.runner.gc_count, 1);
203
+ add(
204
+ &self.runner.gc_ns,
205
+ u64::try_from(took.as_nanos()).unwrap_or(u64::MAX),
206
+ );
207
+ }
208
+ }
@@ -8,6 +8,23 @@ pub enum FlagError {
8
8
  TooLong,
9
9
  #[error("platform already initialized")]
10
10
  AlreadyInitialized,
11
+ /// The flag sets a heap size V8 would use in place of the heap limit.
12
+ #[error("--{0} would override the heap limit")]
13
+ OverridesHeapLimit(&'static str),
14
+ }
15
+
16
+ /// Why `platform::set_heap_limit` refused a limit.
17
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
18
+ pub enum HeapLimitError {
19
+ #[error(
20
+ "heap limit must be from {} to {} bytes",
21
+ crate::process::platform::MIN_HEAP_LIMIT,
22
+ crate::process::platform::MAX_HEAP_LIMIT
23
+ )]
24
+ Range,
25
+ /// A flag already set sizes the heap in its place.
26
+ #[error("the heap limit would be overridden by --{0}")]
27
+ Overridden(&'static str),
11
28
  }
12
29
 
13
30
  /// Why a startup snapshot's blob is refused before V8 reads it, where V8
@@ -67,6 +84,26 @@ impl JsError {
67
84
  message: message.to_owned(),
68
85
  }
69
86
  }
87
+
88
+ /// A script that reached the context's heap limit of `limit` bytes.
89
+ pub(crate) fn heap_limit_reached(limit: usize) -> Self {
90
+ Self {
91
+ kind: JsErrorKind::Terminated(TerminationCause::HeapExhausted),
92
+ message: format!("heap limit of {limit} bytes reached"),
93
+ }
94
+ }
95
+
96
+ /// Any later script in a context whose heap a collection after a hit left
97
+ /// over its limit of `limit` bytes.
98
+ pub(crate) fn heap_limit_exceeded(limit: usize) -> Self {
99
+ Self {
100
+ kind: JsErrorKind::Terminated(TerminationCause::HeapExhausted),
101
+ message: format!(
102
+ "heap still over its limit of {limit} bytes after a collection: \
103
+ this context runs no more JavaScript"
104
+ ),
105
+ }
106
+ }
70
107
  }
71
108
 
72
109
  #[derive(Debug, thiserror::Error)]
@@ -88,6 +125,9 @@ pub enum EngineError {
88
125
  InvalidSnapshot(#[from] InvalidSnapshot),
89
126
  #[error("internal error: {0}")]
90
127
  Internal(String),
128
+ /// V8's flags sized a context's heap past its heap limit.
129
+ #[error("the heap limit is overridden by a V8 flag")]
130
+ HeapLimitOverridden,
91
131
  }
92
132
 
93
133
  fn ebusy() -> String {
@@ -1,5 +1,6 @@
1
1
  mod context;
2
2
  mod control;
3
+ mod counters;
3
4
  mod error;
4
5
  mod fast_calls;
5
6
  mod notify;
@@ -16,10 +17,12 @@ mod spin;
16
17
  mod sys;
17
18
  mod text;
18
19
 
19
- pub use context::{ContextHandle, Host, Unparker, WaitHandle, WaitOutcome};
20
+ pub use context::{ContextHandle, Host, ScriptOptions, Unparker, WaitHandle, WaitOutcome};
20
21
  pub use control::{CallRequest, CallbackReply, ContextConfig, JsText};
22
+ pub use counters::{Counts, Terminations};
21
23
  pub use error::{
22
- EngineError, FlagError, InvalidSnapshot, JsError, JsErrorKind, SourceMapError, TerminationCause,
24
+ EngineError, FlagError, HeapLimitError, InvalidSnapshot, JsError, JsErrorKind, SourceMapError,
25
+ TerminationCause,
23
26
  };
24
27
  pub use snapshot::{SnapshotBlob, WeakSnapshotBlob};
25
28
  pub use text::{Decoded, Transcoder};