mini_racer_rs 1.2.0 → 1.3.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/COMPATIBILITY.md +18 -0
- data/Cargo.lock +5 -5
- data/Cargo.toml +1 -1
- data/README.md +13 -9
- data/crates/mini_racer_engine/src/context.rs +45 -4
- data/crates/mini_racer_engine/src/control.rs +48 -3
- data/crates/mini_racer_engine/src/counters.rs +208 -0
- data/crates/mini_racer_engine/src/lib.rs +2 -0
- data/crates/mini_racer_engine/src/runner.rs +88 -2
- data/crates/mini_racer_engine/src/sys.rs +47 -2
- data/ext/mini_racer_rs/src/context.rs +5 -1
- data/ext/mini_racer_rs/src/counters.rs +40 -0
- data/ext/mini_racer_rs/src/lib.rs +2 -0
- data/lib/mini_racer_rs/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 89724c314c6b6e76bcbf4766ee4bca491da165fb3a171a4810b31f67ef5c482b
|
|
4
|
+
data.tar.gz: 79363136b12b9284b9b7d7df8799d8228107d89fd3a60e708a153aef0cfd7efc
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a8ef0724b17c87ef891ff91e25673a8a98aba87b8e1f8ecad7cb9824b1e4e7ea39d60d5e5b30d6b435cc56eb06b3bd2a553f47b4f9182febf9cfe7b582a388a7
|
|
7
|
+
data.tar.gz: ef0ff9c5648543b82440b5fa12e4a90b3842df6bd3736a1da404ebf1aa1c0ce3e0a9d9a335447db14245e71467406a4fff10c4b0a8372602fcf28a4d79c3ad88
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.3.0](https://github.com/andymai/mini_racer_rs/compare/v1.2.0...v1.3.0) (2026-10-23)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* add MiniRacerRs.counters for per-context activity counts ([#126](https://github.com/andymai/mini_racer_rs/issues/126)) ([d482a3f](https://github.com/andymai/mini_racer_rs/commit/d482a3fb9fb4ac5ba3c077e6d2deffe25402ef3d))
|
|
9
|
+
|
|
3
10
|
## [1.2.0](https://github.com/andymai/mini_racer_rs/compare/v1.1.1...v1.2.0) (2026-10-22)
|
|
4
11
|
|
|
5
12
|
|
data/COMPATIBILITY.md
CHANGED
|
@@ -82,6 +82,24 @@
|
|
|
82
82
|
| `smethod:MiniRacer::Platform.set_flags!` | done | done |
|
|
83
83
|
| `smethod:MiniRacer::Snapshot.load` | done | n/a |
|
|
84
84
|
|
|
85
|
+
## Additions
|
|
86
|
+
|
|
87
|
+
What mini_racer_rs adds under `MiniRacerRs`. An internal id is not for callers.
|
|
88
|
+
|
|
89
|
+
| Id | CRuby | TruffleRuby |
|
|
90
|
+
|---|---|---|
|
|
91
|
+
| `class:MiniRacerRs::Pins < Object` | internal | n/a |
|
|
92
|
+
| `class:MiniRacerRs::Spill < Object` | internal | n/a |
|
|
93
|
+
| `const:MiniRacerRs::NATIVE_VERSION` | done | n/a |
|
|
94
|
+
| `const:MiniRacerRs::V8_VERSION` | done | n/a |
|
|
95
|
+
| `const:MiniRacerRs::VERSION` | done | done |
|
|
96
|
+
| `imethod:MiniRacerRs::ForkHook#_fork` | internal | n/a |
|
|
97
|
+
| `imethod:MiniRacerRs::ForkHook#daemon` | internal | n/a |
|
|
98
|
+
| `module:MiniRacerRs` | done | done |
|
|
99
|
+
| `module:MiniRacerRs::ForkHook` | internal | n/a |
|
|
100
|
+
| `smethod:MiniRacerRs.counters` | done | n/a |
|
|
101
|
+
| `smethod:MiniRacerRs.register_source_map` | done | n/a |
|
|
102
|
+
|
|
85
103
|
## Deviations
|
|
86
104
|
|
|
87
105
|
### `behavior:bigint-objects@ruby`
|
data/Cargo.lock
CHANGED
|
@@ -593,7 +593,7 @@ checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
|
|
593
593
|
|
|
594
594
|
[[package]]
|
|
595
595
|
name = "mini_racer_engine"
|
|
596
|
-
version = "1.
|
|
596
|
+
version = "1.3.0"
|
|
597
597
|
dependencies = [
|
|
598
598
|
"deno_core_icudata",
|
|
599
599
|
"libc",
|
|
@@ -606,7 +606,7 @@ dependencies = [
|
|
|
606
606
|
|
|
607
607
|
[[package]]
|
|
608
608
|
name = "mini_racer_rs"
|
|
609
|
-
version = "1.
|
|
609
|
+
version = "1.3.0"
|
|
610
610
|
dependencies = [
|
|
611
611
|
"magnus",
|
|
612
612
|
"mini_racer_engine",
|
|
@@ -617,7 +617,7 @@ dependencies = [
|
|
|
617
617
|
|
|
618
618
|
[[package]]
|
|
619
619
|
name = "mini_racer_sourcemap"
|
|
620
|
-
version = "1.
|
|
620
|
+
version = "1.3.0"
|
|
621
621
|
dependencies = [
|
|
622
622
|
"memchr",
|
|
623
623
|
"serde",
|
|
@@ -627,7 +627,7 @@ dependencies = [
|
|
|
627
627
|
|
|
628
628
|
[[package]]
|
|
629
629
|
name = "mini_racer_sync"
|
|
630
|
-
version = "1.
|
|
630
|
+
version = "1.3.0"
|
|
631
631
|
dependencies = [
|
|
632
632
|
"loom",
|
|
633
633
|
"thiserror",
|
|
@@ -635,7 +635,7 @@ dependencies = [
|
|
|
635
635
|
|
|
636
636
|
[[package]]
|
|
637
637
|
name = "mini_racer_wire"
|
|
638
|
-
version = "1.
|
|
638
|
+
version = "1.3.0"
|
|
639
639
|
dependencies = [
|
|
640
640
|
"criterion",
|
|
641
641
|
"proptest",
|
data/Cargo.toml
CHANGED
|
@@ -15,7 +15,7 @@ license = "MIT"
|
|
|
15
15
|
publish = false
|
|
16
16
|
repository = "https://github.com/andymai/mini_racer_rs"
|
|
17
17
|
rust-version = "1.91"
|
|
18
|
-
version = "1.
|
|
18
|
+
version = "1.3.0"
|
|
19
19
|
|
|
20
20
|
[workspace.dependencies]
|
|
21
21
|
criterion = {version = "0.8", default-features = false, features = ["cargo_bench_support"]}
|
data/README.md
CHANGED
|
@@ -101,7 +101,8 @@ ExecJS.eval("[1, 2].map(x => x * 2)") # => [2, 4]
|
|
|
101
101
|
| `Context#pump_message_loop`, `#perform_microtask_checkpoint` | Runs pending V8 tasks and microtasks. |
|
|
102
102
|
| `Snapshot.new`, `#warmup!`, `#dump`, `Snapshot.load` | Creates, warms, serializes, and loads startup snapshots. |
|
|
103
103
|
| `Platform.set_flags!(*flags)` | Sets V8 flags before the first context. |
|
|
104
|
-
| `MiniRacerRs.register_source_map(bundle, map)` | Registers a source map for a bundle, so `MiniRacer::RuntimeError` backtraces show its original files and positions. |
|
|
104
|
+
| `MiniRacerRs.register_source_map(bundle, map)` | Registers a source map for a bundle, so `MiniRacer::RuntimeError` backtraces show its original files and positions. CRuby only. |
|
|
105
|
+
| `MiniRacerRs.counters(context)` | Returns per-context activity counters without dispatching or taking the context lock, including while a script runs and after disposal. CRuby only. |
|
|
105
106
|
|
|
106
107
|
`Context.new` accepts these options:
|
|
107
108
|
|
|
@@ -112,7 +113,7 @@ ExecJS.eval("[1, 2].map(x => x * 2)") # => [2, 4]
|
|
|
112
113
|
| `ensure_gc_after_idle:` | Idle milliseconds before one full garbage collection. `0` disables it. |
|
|
113
114
|
| `snapshot:` | A `MiniRacer::Snapshot` used to initialize the context. |
|
|
114
115
|
|
|
115
|
-
|
|
116
|
+
`MiniRacer::EvalError` is the parent of `MiniRacer::ParseError`, `MiniRacer::ScriptTerminatedError`, `MiniRacer::V8OutOfMemoryError`, `MiniRacer::RuntimeError`, and `MiniRacer::ScriptError`. On CRuby, `MiniRacer::InternalError` also descends from `MiniRacer::EvalError` and reports a failure inside the gem rather than in JavaScript. `MiniRacer::ContextDisposedError`, `MiniRacer::SnapshotError`, and `MiniRacer::PlatformAlreadyInitialized` descend directly from `MiniRacer::Error`. Everything descends from `MiniRacer::Error`. `MiniRacer::RuntimeError` includes JavaScript frames in its backtrace.
|
|
116
117
|
|
|
117
118
|
**Source maps.** Bundles must be UTF-8 Strings of at least 1,024 bytes. Maps must be JSON Strings in source map v3 format. When a context evaluates UTF-8 source containing a registered bundle, mini_racer_rs rewrites `MiniRacer::RuntimeError` backtraces. V8 positions inside the bundle become mapped original file, line, and column positions. V8 function names remain.
|
|
118
119
|
|
|
@@ -120,6 +121,8 @@ V8 positions remain for async frames and frames after the tenth. They also remai
|
|
|
120
121
|
|
|
121
122
|
Detailed stack capture makes creating and throwing errors more expensive in affected contexts. Measured React and MUI server renders were unchanged. Registration is process wide and holds at most eight bundles. Maps cannot be removed. Registering a bundle again replaces its map. Trust bundles and maps like application code because source names appear in Ruby backtraces.
|
|
122
123
|
|
|
124
|
+
**Counters.** `MiniRacerRs.counters(context)` returns a Hash of symbol-keyed counts for activity since context creation. It reads without a dispatch or the context lock, so another thread can inspect it while a script runs. After `dispose`, it returns the last counts. A forked child that takes a context over continues from the parent's counts. Values are read individually, so an in-flight request can appear in `dispatches` before its result bytes. Keys are `dispatches` (runner requests, including nested callback requests), `callbacks` (Ruby callables run for JavaScript), `bytes_to_js` (sources, requests, callback returns and errors), `bytes_from_js` (results and callback arguments), `js_ns` (runner time excluding callback, await task and fork waits), `gc_count` and `gc_ns` (V8 collections and time), and `terminated` (counts by `stop`, `timeout`, `interrupt`, `max_memory` and `heap_exhausted`). A non-context argument raises `TypeError`. CRuby only. In release builds, timing `js_ns` added about 30 ns to a roughly 0.72 µs call round trip. Counting alone measured within noise.
|
|
125
|
+
|
|
123
126
|
## Switching from mini_racer
|
|
124
127
|
|
|
125
128
|
### What changes
|
|
@@ -148,7 +151,7 @@ See [COMPATIBILITY.md](COMPATIBILITY.md) for every difference.
|
|
|
148
151
|
|
|
149
152
|
### Stay on mini_racer if
|
|
150
153
|
|
|
151
|
-
- Download size matters more than compile time. The largest native gem is
|
|
154
|
+
- Download size matters more than compile time. The largest native gem is 66.2 MB because it contains three Ruby extensions. mini_racer downloads 47.1 MB on x86_64 Linux, then compiles.
|
|
152
155
|
|
|
153
156
|
### Gems that depend on mini_racer
|
|
154
157
|
|
|
@@ -159,11 +162,11 @@ gem "mini_racer_rs"
|
|
|
159
162
|
gem "mini_racer", github: "andymai/mini_racer_rs", tag: "vX.Y.Z", glob: "shim/*.gemspec"
|
|
160
163
|
```
|
|
161
164
|
|
|
162
|
-
The repository tests `path:` shim
|
|
165
|
+
The repository tests the `path:` form of the shim with rtlcss in `test/consumers/transitive/`. A dependency excluding `mini_racer` 0.22.1 cannot use the shim.
|
|
163
166
|
|
|
164
167
|
### Forking servers such as Puma
|
|
165
168
|
|
|
166
|
-
Before forking, evaluate each bundle of 1
|
|
169
|
+
Before forking, evaluate each bundle of 1 MiB or more twice in the master, then dispose those contexts. Use the renderer's exact source text and file name. The second evaluation builds the process-wide V8 code cache. Matching evaluations compile from it, and forked children inherit it. For Babel's 2.3 MB bundle at commit `5e13c1c`, a forked child's first load took 60.8 ms without a parent load. It took 65.5 ms after one load and 25.0 ms after two. These are medians across 5 trials. Prime a `react-rails` app by building its server renderer twice. Children do not use parent contexts. Only the cache carries over.
|
|
167
170
|
|
|
168
171
|
For Ruby's `fork`, `IO.popen("-")`, or `Process.daemon`, mini_racer_rs waits for context runners, pauses its worker pool and waits for running tasks. An async `WebAssembly.compile` in a new default-mode child completes on the child's own pool. On Ubuntu 24.04 with glibc 2.39, 20 of 20 mini_racer children aborted with signal 6 after React rendering; all mini_racer_rs children exited cleanly. On Fedora, mini_racer hung within 50 `test/stress/fork_busy.rb` forks; mini_racer_rs completed 300 per run on Fedora and Ubuntu.
|
|
169
172
|
|
|
@@ -185,7 +188,7 @@ With `mini_racer` 0.22.1, each worker compiles the bundle from source on its fir
|
|
|
185
188
|
|
|
186
189
|
Results are medians of 5 runs. Every workload produced identical output in both gems. The eight library workloads use the release build of commit `220bfe7`. The 44 MB production bundle load uses commit `2513a48`. Benchmarks ran on Ruby 3.4.2 and Linux with an AMD Ryzen 9 7950X3D. The governor was `performance`. Both gems were pinned to CPUs 0 through 7.
|
|
187
190
|
|
|
188
|
-
The per-call and memory figures below were measured on the same machine. Startup plus Babel, Babel context and MUI context figures come from commit `220bfe7`. One watchdog thread serves the whole process, making a call with `timeout:` fast. Scripts of 1
|
|
191
|
+
The per-call and memory figures below were measured on the same machine. Startup plus Babel, Babel context and MUI context figures come from commit `220bfe7`. One watchdog thread serves the whole process, making a call with `timeout:` fast. Scripts of 1 MiB or more use a process-wide V8 code cache. Contexts loading the same large script share one source copy.
|
|
189
192
|
|
|
190
193
|
| Measure | mini_racer 0.22.1 | mini_racer_rs |
|
|
191
194
|
|---|---:|---:|
|
|
@@ -237,12 +240,13 @@ mini_racer_rs uses the `v8` crate, rusty_v8 152.2.0, with magnus and rb-sys Ruby
|
|
|
237
240
|
|
|
238
241
|
A waiting Ruby thread spins with the GVL held, then without it. It then parks with the GVL released. After enough consecutive answers arrive beyond the spin budget, that context skips spinning and parks at once until an answer arrives within the budget. Under a fiber scheduler, only the waiting fiber parks. Values use V8's serialization format and decode directly into Ruby objects. There is no intermediate tree. Repeated objects keep their identity.
|
|
239
242
|
|
|
240
|
-
One watchdog thread per process enforces every context's `timeout:`. In
|
|
243
|
+
One watchdog thread per process enforces every context's `timeout:`. In both platform modes, a fork through Ruby waits for every runner to be idle or parked. Under `:single_threaded`, the child takes over contexts whose runners were idle at the fork, while contexts in use raise there. Default multi-threaded children refuse every inherited context. mini_racer_rs gives V8 a copied platform vtable in each process. Worker-thread slots post to its own pool. V8's platform has one worker thread, which receives no tasks. Forking pauses the pool. The parent resumes it afterward.
|
|
241
244
|
|
|
242
|
-
The implementation has
|
|
245
|
+
The implementation has five crates:
|
|
243
246
|
|
|
244
247
|
- `mini_racer_wire`: the wire codec, with no unsafe code.
|
|
245
248
|
- `mini_racer_sync`: the thread protocol, with no unsafe code and loom checking.
|
|
249
|
+
- `mini_racer_sourcemap`: source maps for JavaScript backtraces, with no unsafe code.
|
|
246
250
|
- `mini_racer_engine`: V8 process state and runners, and the only crate naming `v8`.
|
|
247
251
|
- `mini_racer_rs`: the Ruby extension, and the only crate naming magnus or rb-sys.
|
|
248
252
|
|
|
@@ -270,7 +274,7 @@ Supported implementations are CRuby 3.3, 3.4, and 4.0, plus TruffleRuby through
|
|
|
270
274
|
|
|
271
275
|
| Document | Covers |
|
|
272
276
|
|---|---|
|
|
273
|
-
| [ARCHITECTURE.md](ARCHITECTURE.md) | Crates, a call end to end, values, termination, callbacks, promises, snapshots, forks, fiber schedulers,
|
|
277
|
+
| [ARCHITECTURE.md](ARCHITECTURE.md) | Crates, a call end to end, values, termination and limits, callbacks, promises and V8's tasks, snapshots, code cache, shared sources, fast calls, source maps, forks, fiber schedulers, rules that keep it safe, and how it is checked. |
|
|
274
278
|
| [COMPATIBILITY.md](COMPATIBILITY.md) | Every public API item and deliberate difference from mini_racer 0.22.1, with its reason. |
|
|
275
279
|
| [BENCHMARKS.md](BENCHMARKS.md) | Benchmark method and full results. |
|
|
276
280
|
| [STABILITY.md](STABILITY.md) | Versioning, the public API, unstable interfaces, and support policy. |
|
|
@@ -11,6 +11,7 @@ use mini_racer_sync::{
|
|
|
11
11
|
use crate::control::{
|
|
12
12
|
CallRequest, CallbackReply, ContextConfig, ControlBlock, Depth, JsText, Request, Wire,
|
|
13
13
|
};
|
|
14
|
+
use crate::counters::{Counters, Counts};
|
|
14
15
|
use crate::notify::NotifyFd;
|
|
15
16
|
use crate::process::fork::{self, Generation};
|
|
16
17
|
use crate::process::platform::Mode;
|
|
@@ -498,6 +499,26 @@ impl ContextHandle {
|
|
|
498
499
|
}
|
|
499
500
|
}
|
|
500
501
|
|
|
502
|
+
/// Read from atomics, without a dispatch or taking an inherited context
|
|
503
|
+
/// over: in a forked child that has not, the counts it inherited, and
|
|
504
|
+
/// after a dispose, the last ones.
|
|
505
|
+
#[must_use]
|
|
506
|
+
pub fn counters(&self) -> Counts {
|
|
507
|
+
let epoch = fork::epoch();
|
|
508
|
+
let mut lineage = &self.lineage;
|
|
509
|
+
let mut counted = None;
|
|
510
|
+
loop {
|
|
511
|
+
if let Held::Block(block) = &lineage.held {
|
|
512
|
+
counted = Some(&block.counters);
|
|
513
|
+
}
|
|
514
|
+
match lineage.next.get() {
|
|
515
|
+
Some(next) if lineage.epoch != epoch => lineage = next,
|
|
516
|
+
_ => break,
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
counted.map_or_else(Counts::default, Counters::read)
|
|
520
|
+
}
|
|
521
|
+
|
|
501
522
|
/// Answers without taking an inherited context over.
|
|
502
523
|
#[must_use]
|
|
503
524
|
pub fn is_disposed(&self) -> bool {
|
|
@@ -588,8 +609,15 @@ impl ContextHandle {
|
|
|
588
609
|
self.session.owner.store(thread, Ordering::Release);
|
|
589
610
|
let owning = Owning(&self.session.owner);
|
|
590
611
|
let wait = WaitHandle::new(block);
|
|
591
|
-
let
|
|
592
|
-
|
|
612
|
+
let request = request(Depth::Outermost(dispatch));
|
|
613
|
+
let payload = request.payload();
|
|
614
|
+
let result = match block.channel.request(request) {
|
|
615
|
+
Ok(()) => {
|
|
616
|
+
if let Some(bytes) = payload {
|
|
617
|
+
block.counters.dispatched(bytes);
|
|
618
|
+
}
|
|
619
|
+
self.await_response(block, &wait, false, host)
|
|
620
|
+
}
|
|
593
621
|
Err(error) => Err(channel(error)),
|
|
594
622
|
};
|
|
595
623
|
drop(owning);
|
|
@@ -628,11 +656,15 @@ impl ContextHandle {
|
|
|
628
656
|
return Err(error);
|
|
629
657
|
}
|
|
630
658
|
let wait = WaitHandle::new(block);
|
|
659
|
+
let payload = request.payload();
|
|
631
660
|
let result = block
|
|
632
661
|
.channel
|
|
633
662
|
.request(request)
|
|
634
663
|
.map_err(channel)
|
|
635
664
|
.and_then(|()| {
|
|
665
|
+
if let Some(bytes) = payload {
|
|
666
|
+
block.counters.dispatched(bytes);
|
|
667
|
+
}
|
|
636
668
|
let _waiting = Counted::enter(&self.session.waits);
|
|
637
669
|
self.await_response(block, &wait, true, host)
|
|
638
670
|
});
|
|
@@ -687,17 +719,21 @@ impl ContextHandle {
|
|
|
687
719
|
if nested && cancelled {
|
|
688
720
|
block.term.release();
|
|
689
721
|
}
|
|
690
|
-
|
|
722
|
+
let response = if disposed {
|
|
691
723
|
Err(EngineError::Disposed)
|
|
692
724
|
} else {
|
|
693
725
|
response
|
|
694
726
|
};
|
|
727
|
+
block.counters.answered(&response, nested && cancelled);
|
|
728
|
+
return response;
|
|
695
729
|
}
|
|
696
730
|
Ok(Some(ToRuby::Callback(request))) => {
|
|
731
|
+
block.counters.asked(request.len());
|
|
697
732
|
let reply = if cancelled {
|
|
698
733
|
CallbackReply::Abort
|
|
699
734
|
} else {
|
|
700
735
|
let _running = Counted::enter(&self.session.callbacks);
|
|
736
|
+
block.counters.called_back();
|
|
701
737
|
let reply = host.callback(&request);
|
|
702
738
|
if wait.forked() {
|
|
703
739
|
return Err(EngineError::Orphaned);
|
|
@@ -712,6 +748,7 @@ impl ContextHandle {
|
|
|
712
748
|
if matches!(reply, CallbackReply::Abort) && !cancelled {
|
|
713
749
|
abandon(&mut cancelled);
|
|
714
750
|
}
|
|
751
|
+
block.counters.replied(&reply);
|
|
715
752
|
block.channel.reply(reply).map_err(channel)?;
|
|
716
753
|
}
|
|
717
754
|
Ok(None) => {}
|
|
@@ -781,7 +818,11 @@ fn adopt(inherited: &ControlBlock, carried: Inherited) -> Held {
|
|
|
781
818
|
else {
|
|
782
819
|
return orphaned(true, false);
|
|
783
820
|
};
|
|
784
|
-
let block = Arc::new(ControlBlock::
|
|
821
|
+
let block = Arc::new(ControlBlock::adopted(
|
|
822
|
+
inherited.config.clone(),
|
|
823
|
+
tasks,
|
|
824
|
+
inherited.counters.inherited(),
|
|
825
|
+
));
|
|
785
826
|
*lock(&block.custody) = Some(isolate);
|
|
786
827
|
block.term.adopt(carried);
|
|
787
828
|
// The isolate keeps whatever termination V8 still held for it.
|
|
@@ -4,6 +4,7 @@ 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 {
|
|
@@ -126,6 +135,39 @@ pub enum Request {
|
|
|
126
135
|
Dispose,
|
|
127
136
|
}
|
|
128
137
|
|
|
138
|
+
impl Request {
|
|
139
|
+
/// The bytes a counted dispatch hands JavaScript; `None` for the
|
|
140
|
+
/// context's own setup and dispose, which no caller asked for.
|
|
141
|
+
pub const fn payload(&self) -> Option<usize> {
|
|
142
|
+
match self {
|
|
143
|
+
Self::Init { .. } | Self::Dispose => None,
|
|
144
|
+
Self::Eval { source, .. } => Some(source.byte_len()),
|
|
145
|
+
Self::Call { request, .. } => Some(request.bytes.len()),
|
|
146
|
+
Self::Attach { request, .. } => Some(request.len()),
|
|
147
|
+
Self::Snapshot { code, .. } => Some(code.byte_len()),
|
|
148
|
+
Self::LowMemoryNotification { .. }
|
|
149
|
+
| Self::MicrotaskCheckpoint { .. }
|
|
150
|
+
| Self::HeapStats { .. }
|
|
151
|
+
| Self::HeapSnapshot { .. }
|
|
152
|
+
| Self::PumpMessageLoop { .. } => Some(0),
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/// Whether the runner's time on it counts as JavaScript's: the requests
|
|
157
|
+
/// that run scripts, not the heap's statistics, snapshot or full
|
|
158
|
+
/// collection, which can take far longer than the scripts.
|
|
159
|
+
pub const fn runs_script(&self) -> bool {
|
|
160
|
+
matches!(
|
|
161
|
+
self,
|
|
162
|
+
Self::Eval { .. }
|
|
163
|
+
| Self::Call { .. }
|
|
164
|
+
| Self::Attach { .. }
|
|
165
|
+
| Self::MicrotaskCheckpoint { .. }
|
|
166
|
+
| Self::PumpMessageLoop { .. }
|
|
167
|
+
)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
129
171
|
pub struct IsolateTerminator {
|
|
130
172
|
handle: v8::IsolateHandle,
|
|
131
173
|
tasks: Arc<Tasks>,
|
|
@@ -205,6 +247,7 @@ pub struct ControlBlock {
|
|
|
205
247
|
// An idle single-threaded runner leaves its isolate here, its only owner
|
|
206
248
|
// then, so a forked child can hand it to a runner of its own.
|
|
207
249
|
pub custody: Mutex<Option<v8::SharedIsolate>>,
|
|
250
|
+
pub counters: Counters,
|
|
208
251
|
}
|
|
209
252
|
|
|
210
253
|
impl Drop for ControlBlock {
|
|
@@ -238,11 +281,12 @@ impl ControlBlock {
|
|
|
238
281
|
}
|
|
239
282
|
|
|
240
283
|
pub fn new(config: ContextConfig) -> Self {
|
|
241
|
-
Self::
|
|
284
|
+
Self::adopted(config, Tasks::default(), Counters::default())
|
|
242
285
|
}
|
|
243
286
|
|
|
244
|
-
/// For a forked child taking over an idle context with its queued tasks
|
|
245
|
-
|
|
287
|
+
/// For a forked child taking over an idle context with its queued tasks
|
|
288
|
+
/// and its counts.
|
|
289
|
+
pub fn adopted(config: ContextConfig, tasks: Tasks, counters: Counters) -> Self {
|
|
246
290
|
Self {
|
|
247
291
|
channel: Channel::default(),
|
|
248
292
|
term: TermCell::default(),
|
|
@@ -255,6 +299,7 @@ impl ControlBlock {
|
|
|
255
299
|
isolate: OnceLock::new(),
|
|
256
300
|
building: Mutex::new(None),
|
|
257
301
|
custody: Mutex::new(None),
|
|
302
|
+
counters,
|
|
258
303
|
}
|
|
259
304
|
}
|
|
260
305
|
}
|
|
@@ -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
|
+
}
|
|
@@ -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;
|
|
@@ -18,6 +19,7 @@ mod text;
|
|
|
18
19
|
|
|
19
20
|
pub use context::{ContextHandle, Host, Unparker, WaitHandle, WaitOutcome};
|
|
20
21
|
pub use control::{CallRequest, CallbackReply, ContextConfig, JsText};
|
|
22
|
+
pub use counters::{Counts, Terminations};
|
|
21
23
|
pub use error::{
|
|
22
24
|
EngineError, FlagError, InvalidSnapshot, JsError, JsErrorKind, SourceMapError, TerminationCause,
|
|
23
25
|
};
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
use std::borrow::Cow;
|
|
2
|
+
use std::cell::Cell;
|
|
2
3
|
use std::collections::hash_map::RandomState;
|
|
3
4
|
use std::hash::BuildHasher;
|
|
4
5
|
use std::panic::{AssertUnwindSafe, catch_unwind};
|
|
5
|
-
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
|
|
6
|
+
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering};
|
|
6
7
|
use std::sync::{Arc, Mutex};
|
|
7
|
-
use std::time::Duration;
|
|
8
|
+
use std::time::{Duration, Instant};
|
|
8
9
|
|
|
9
10
|
use mini_racer_sourcemap::{Frame, Match, Placement, SourceMap};
|
|
10
11
|
use mini_racer_sync::{
|
|
@@ -74,6 +75,58 @@ struct Runtime {
|
|
|
74
75
|
frames: Mutex<Vec<v8::Global<v8::Array>>>,
|
|
75
76
|
// Scripts that hold a bundle with a source map, latest last.
|
|
76
77
|
matched: Mutex<Vec<Matched>>,
|
|
78
|
+
// When the runner's current stretch of JavaScript began, in nanoseconds
|
|
79
|
+
// after `clock`, or `STOPPED`; only the runner touches it.
|
|
80
|
+
clock: Instant,
|
|
81
|
+
since: AtomicU64,
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const STOPPED: u64 = u64::MAX;
|
|
85
|
+
|
|
86
|
+
// Times the runner's JavaScript: started for a request that runs scripts,
|
|
87
|
+
// and paused while a callback waits for Ruby, an await sleeps until a task is
|
|
88
|
+
// due or a script waits out a fork. Each restores what it found, so a request
|
|
89
|
+
// nested in a callback is timed inside the pause.
|
|
90
|
+
struct Timing<'a> {
|
|
91
|
+
runtime: &'a Runtime,
|
|
92
|
+
was_running: bool,
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
impl Drop for Timing<'_> {
|
|
96
|
+
fn drop(&mut self) {
|
|
97
|
+
self.runtime.switch(self.was_running);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// The collections V8 runs on a runner's isolate. Kept apart from the
|
|
102
|
+
// runtime, which needs the isolate's contexts, so a collection while they
|
|
103
|
+
// are made counts too.
|
|
104
|
+
struct Collections {
|
|
105
|
+
block: Arc<ControlBlock>,
|
|
106
|
+
began: Cell<Option<Instant>>,
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
impl Collections {
|
|
110
|
+
fn new(block: &Arc<ControlBlock>) -> Self {
|
|
111
|
+
Self {
|
|
112
|
+
block: Arc::clone(block),
|
|
113
|
+
began: Cell::new(None),
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
pub fn collection_began(isolate: &v8::Isolate) {
|
|
119
|
+
if let Some(collections) = isolate.get_slot::<Collections>() {
|
|
120
|
+
collections.began.set(Some(Instant::now()));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
pub fn collection_ended(isolate: &v8::Isolate) {
|
|
125
|
+
if let Some(collections) = isolate.get_slot::<Collections>()
|
|
126
|
+
&& let Some(began) = collections.began.take()
|
|
127
|
+
{
|
|
128
|
+
collections.block.counters.collected(began.elapsed());
|
|
129
|
+
}
|
|
77
130
|
}
|
|
78
131
|
|
|
79
132
|
#[derive(Clone)]
|
|
@@ -168,6 +221,30 @@ impl Runtime {
|
|
|
168
221
|
hoisting: Mutex::default(),
|
|
169
222
|
frames: Mutex::default(),
|
|
170
223
|
matched: Mutex::default(),
|
|
224
|
+
clock: Instant::now(),
|
|
225
|
+
since: AtomicU64::new(STOPPED),
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Charges the stretch running so far, if one is, and starts or stops the
|
|
230
|
+
// next; returns whether one was running. A load and a store, as only the
|
|
231
|
+
// runner writes either.
|
|
232
|
+
fn switch(&self, run: bool) -> bool {
|
|
233
|
+
let now = u64::try_from(self.clock.elapsed().as_nanos()).unwrap_or(STOPPED - 1);
|
|
234
|
+
let since = self.since.load(Ordering::Relaxed);
|
|
235
|
+
self.since
|
|
236
|
+
.store(if run { now } else { STOPPED }, Ordering::Relaxed);
|
|
237
|
+
if since == STOPPED {
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
self.block.counters.ran_js(now.saturating_sub(since));
|
|
241
|
+
true
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
fn timing(&self, run: bool) -> Timing<'_> {
|
|
245
|
+
Timing {
|
|
246
|
+
runtime: self,
|
|
247
|
+
was_running: self.switch(run),
|
|
171
248
|
}
|
|
172
249
|
}
|
|
173
250
|
}
|
|
@@ -236,6 +313,7 @@ pub fn check_heap(isolate: &mut v8::Isolate) {
|
|
|
236
313
|
|
|
237
314
|
pub fn park_for_fork(isolate: &v8::Isolate) {
|
|
238
315
|
if let Some(runtime) = isolate.get_slot::<Arc<Runtime>>() {
|
|
316
|
+
let _paused = runtime.timing(false);
|
|
239
317
|
runtime.generation.pause(&runtime.block);
|
|
240
318
|
}
|
|
241
319
|
}
|
|
@@ -264,6 +342,8 @@ fn serve(block: &Arc<ControlBlock>, generation: &'static Generation) -> bool {
|
|
|
264
342
|
None => params,
|
|
265
343
|
};
|
|
266
344
|
let mut isolate = v8::Isolate::new(params);
|
|
345
|
+
isolate.set_slot(Collections::new(block));
|
|
346
|
+
sys::count_collections(&mut isolate);
|
|
267
347
|
if block.config.max_memory.is_some() {
|
|
268
348
|
sys::watch_heap(&mut isolate);
|
|
269
349
|
}
|
|
@@ -329,6 +409,8 @@ fn adopt(
|
|
|
329
409
|
lock(&runtime.matched).clone_from(&matched);
|
|
330
410
|
}
|
|
331
411
|
locker.set_slot(Arc::clone(&runtime));
|
|
412
|
+
// The parent's callbacks stay registered and now count here.
|
|
413
|
+
locker.set_slot(Collections::new(block));
|
|
332
414
|
runtime
|
|
333
415
|
};
|
|
334
416
|
block
|
|
@@ -568,6 +650,7 @@ fn handle(
|
|
|
568
650
|
runtime: &Runtime,
|
|
569
651
|
request: Request,
|
|
570
652
|
) -> Result<Vec<u8>, EngineError> {
|
|
653
|
+
let _timing = request.runs_script().then(|| runtime.timing(true));
|
|
571
654
|
match request {
|
|
572
655
|
Request::Init { depth } => match depth {
|
|
573
656
|
Depth::Outermost(dispatch) => ended(runtime, dispatch),
|
|
@@ -1301,6 +1384,7 @@ fn await_promise<'s>(
|
|
|
1301
1384
|
// Waiting, the runner holds its isolate but runs nothing in it, so a fork need
|
|
1302
1385
|
// not wait for it.
|
|
1303
1386
|
fn wait_for_task(runtime: &Runtime) {
|
|
1387
|
+
let _paused = runtime.timing(false);
|
|
1304
1388
|
runtime.block.phase.set(Phase::Parked);
|
|
1305
1389
|
runtime.block.tasks.wait();
|
|
1306
1390
|
runtime.generation.resume(&runtime.block);
|
|
@@ -1570,6 +1654,7 @@ fn callback<'s>(
|
|
|
1570
1654
|
let Some(bytes) = serialize::write(scope, request.into()) else {
|
|
1571
1655
|
return;
|
|
1572
1656
|
};
|
|
1657
|
+
let waiting = runtime.timing(false);
|
|
1573
1658
|
if runtime.block.channel.callback(bytes).is_err() {
|
|
1574
1659
|
terminate(scope, runtime);
|
|
1575
1660
|
return;
|
|
@@ -1599,6 +1684,7 @@ fn callback<'s>(
|
|
|
1599
1684
|
return;
|
|
1600
1685
|
}
|
|
1601
1686
|
};
|
|
1687
|
+
drop(waiting);
|
|
1602
1688
|
// A nested dispatch that was halted leaves its termination to unwind
|
|
1603
1689
|
// this script too, as mini_racer restores it.
|
|
1604
1690
|
let terminating = scope.is_execution_terminating();
|
|
@@ -32,8 +32,8 @@ pub extern "C" fn __wrap_madvise(
|
|
|
32
32
|
/// runner to drop.
|
|
33
33
|
pub fn share(isolate: v8::OwnedIsolate) -> Result<v8::SharedIsolate, v8::OwnedIsolate> {
|
|
34
34
|
// SAFETY: the runner calls this on the thread that created `isolate`, with
|
|
35
|
-
// no reference from `Global::open` live, and the only
|
|
36
|
-
// `Arc<Runtime
|
|
35
|
+
// no reference from `Global::open` live, and the only slots it sets, an
|
|
36
|
+
// `Arc<Runtime>` and its `Collections`, are Send.
|
|
37
37
|
let shared = unsafe { isolate.try_into_shared() };
|
|
38
38
|
shared.map_err(v8::IntoSharedError::into_isolate)
|
|
39
39
|
}
|
|
@@ -208,6 +208,51 @@ unsafe extern "C" fn after_gc(
|
|
|
208
208
|
}
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
+
/// Has V8 report the start and end of each collection on `isolate` for its
|
|
212
|
+
/// runner's counters: scavenges, minor mark-sweeps and mark-compacts, not the
|
|
213
|
+
/// start of incremental marking or the processing of weak callbacks, which
|
|
214
|
+
/// collect nothing.
|
|
215
|
+
pub fn count_collections(isolate: &mut v8::Isolate) {
|
|
216
|
+
let collections = v8::GCType::kGCTypeScavenge
|
|
217
|
+
| v8::GCType::kGCTypeMinorMarkSweep
|
|
218
|
+
| v8::GCType::kGCTypeMarkSweepCompact;
|
|
219
|
+
isolate.add_gc_prologue_callback(collection_began, std::ptr::null_mut(), collections);
|
|
220
|
+
isolate.add_gc_epilogue_callback(collection_ended, std::ptr::null_mut(), collections);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
unsafe extern "C" fn collection_began(
|
|
224
|
+
isolate: v8::UnsafeRawIsolatePtr,
|
|
225
|
+
_type: v8::GCType,
|
|
226
|
+
_flags: v8::GCCallbackFlags,
|
|
227
|
+
_data: *mut c_void,
|
|
228
|
+
) {
|
|
229
|
+
let counted = std::panic::catch_unwind(|| {
|
|
230
|
+
// SAFETY: V8 calls a GC prologue callback on the thread that has the
|
|
231
|
+
// isolate entered, with its own non-null pointer to it.
|
|
232
|
+
let isolate = unsafe { v8::Isolate::ref_from_raw_isolate_ptr(&isolate) };
|
|
233
|
+
crate::runner::collection_began(isolate);
|
|
234
|
+
});
|
|
235
|
+
if counted.is_err() {
|
|
236
|
+
std::process::abort();
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
unsafe extern "C" fn collection_ended(
|
|
241
|
+
isolate: v8::UnsafeRawIsolatePtr,
|
|
242
|
+
_type: v8::GCType,
|
|
243
|
+
_flags: v8::GCCallbackFlags,
|
|
244
|
+
_data: *mut c_void,
|
|
245
|
+
) {
|
|
246
|
+
let counted = std::panic::catch_unwind(|| {
|
|
247
|
+
// SAFETY: as in `collection_began`, for the epilogue.
|
|
248
|
+
let isolate = unsafe { v8::Isolate::ref_from_raw_isolate_ptr(&isolate) };
|
|
249
|
+
crate::runner::collection_ended(isolate);
|
|
250
|
+
});
|
|
251
|
+
if counted.is_err() {
|
|
252
|
+
std::process::abort();
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
211
256
|
/// Asks the isolate's runner to park for a pending fork the next time V8
|
|
212
257
|
/// checks for interrupts, where JavaScript may be running for a long time.
|
|
213
258
|
pub fn interrupt_for_fork(handle: &v8::IsolateHandle) {
|
|
@@ -11,7 +11,7 @@ use magnus::{
|
|
|
11
11
|
Class, DataTypeFunctions, Error, Fixnum, Module, RArray, RHash, RModule, RString, Ruby, Symbol,
|
|
12
12
|
TypedData, Value,
|
|
13
13
|
};
|
|
14
|
-
use mini_racer_engine::{ContextConfig, ContextHandle, EngineError, JsText};
|
|
14
|
+
use mini_racer_engine::{ContextConfig, ContextHandle, Counts, EngineError, JsText};
|
|
15
15
|
|
|
16
16
|
use crate::encode;
|
|
17
17
|
use crate::gc_root::OwnedRoot;
|
|
@@ -197,6 +197,10 @@ fn config(ruby: &Ruby, keywords: RHash) -> Result<ContextConfig, Error> {
|
|
|
197
197
|
}
|
|
198
198
|
|
|
199
199
|
impl Context {
|
|
200
|
+
pub fn counters(&self, ruby: &Ruby) -> Result<Counts, Error> {
|
|
201
|
+
Ok(self.handle(ruby)?.counters())
|
|
202
|
+
}
|
|
203
|
+
|
|
200
204
|
fn handle(&self, ruby: &Ruby) -> Result<&ContextHandle, Error> {
|
|
201
205
|
self.handle
|
|
202
206
|
.get()
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
use magnus::{Error, Object, RHash, RModule, Ruby};
|
|
2
|
+
|
|
3
|
+
use crate::context::Context;
|
|
4
|
+
use crate::export;
|
|
5
|
+
|
|
6
|
+
pub fn init(rs: RModule) -> Result<(), Error> {
|
|
7
|
+
rs.define_singleton_method("counters", export!(function counters, 1))?;
|
|
8
|
+
Ok(())
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// Read without the context's lock, so a thread can read a context another
|
|
12
|
+
// thread is running.
|
|
13
|
+
fn counters(ruby: &Ruby, context: &Context) -> Result<RHash, Error> {
|
|
14
|
+
let counts = context.counters(ruby)?;
|
|
15
|
+
let terminated = counts.terminated;
|
|
16
|
+
let causes = ruby.hash_new();
|
|
17
|
+
for (name, count) in [
|
|
18
|
+
("stop", terminated.stop),
|
|
19
|
+
("timeout", terminated.timeout),
|
|
20
|
+
("interrupt", terminated.interrupt),
|
|
21
|
+
("max_memory", terminated.max_memory),
|
|
22
|
+
("heap_exhausted", terminated.heap_exhausted),
|
|
23
|
+
] {
|
|
24
|
+
causes.aset(ruby.to_symbol(name), count)?;
|
|
25
|
+
}
|
|
26
|
+
let hash = ruby.hash_new();
|
|
27
|
+
for (name, count) in [
|
|
28
|
+
("dispatches", counts.dispatches),
|
|
29
|
+
("callbacks", counts.callbacks),
|
|
30
|
+
("bytes_to_js", counts.bytes_to_js),
|
|
31
|
+
("bytes_from_js", counts.bytes_from_js),
|
|
32
|
+
("js_ns", counts.js_ns),
|
|
33
|
+
("gc_count", counts.gc_count),
|
|
34
|
+
("gc_ns", counts.gc_ns),
|
|
35
|
+
] {
|
|
36
|
+
hash.aset(ruby.to_symbol(name), count)?;
|
|
37
|
+
}
|
|
38
|
+
hash.aset(ruby.to_symbol("terminated"), causes)?;
|
|
39
|
+
Ok(hash)
|
|
40
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
mod context;
|
|
2
|
+
mod counters;
|
|
2
3
|
mod decode;
|
|
3
4
|
mod encode;
|
|
4
5
|
mod errors;
|
|
@@ -48,6 +49,7 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
|
48
49
|
context::init(ruby, racer)?;
|
|
49
50
|
snapshot::init(ruby, racer)?;
|
|
50
51
|
source_maps::init(rs)?;
|
|
52
|
+
counters::init(rs)?;
|
|
51
53
|
fork::init(ruby, rs)?;
|
|
52
54
|
Ok(())
|
|
53
55
|
}
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mini_racer_rs
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andy Aragon
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 2026-10-
|
|
10
|
+
date: 2026-10-23 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: rb_sys
|
|
@@ -46,6 +46,7 @@ files:
|
|
|
46
46
|
- crates/mini_racer_engine/build.rs
|
|
47
47
|
- crates/mini_racer_engine/src/context.rs
|
|
48
48
|
- crates/mini_racer_engine/src/control.rs
|
|
49
|
+
- crates/mini_racer_engine/src/counters.rs
|
|
49
50
|
- crates/mini_racer_engine/src/error.rs
|
|
50
51
|
- crates/mini_racer_engine/src/fast_calls.rs
|
|
51
52
|
- crates/mini_racer_engine/src/lib.rs
|
|
@@ -92,6 +93,7 @@ files:
|
|
|
92
93
|
- ext/mini_racer_rs/build.rs
|
|
93
94
|
- ext/mini_racer_rs/extconf.rb
|
|
94
95
|
- ext/mini_racer_rs/src/context.rs
|
|
96
|
+
- ext/mini_racer_rs/src/counters.rs
|
|
95
97
|
- ext/mini_racer_rs/src/decode.rs
|
|
96
98
|
- ext/mini_racer_rs/src/encode.rs
|
|
97
99
|
- ext/mini_racer_rs/src/errors.rs
|