@threadedstack/wmd-web 0.0.0-stage → 0.1.13
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.
- package/README.md +2601 -2
- package/RELEASE.json +123 -0
- package/export-page/coi-sw.js +20 -0
- package/export-page/index.html +17 -0
- package/export-page/run.css +10 -0
- package/export-page/run.js +236 -0
- package/guest/arm64/Image +4 -0
- package/guest/arm64/Image-4k +4 -0
- package/guest/arm64/initramfs.cpio.gz +0 -0
- package/guest/arm64/vm.wasm +0 -0
- package/package.json +36 -4
- package/page/sw.js +381 -0
- package/relay/control.ts +139 -0
- package/relay/engine.ts +787 -0
- package/relay/hosted.ts +660 -0
- package/relay/keys.ts +53 -0
- package/relay/local-tls.ts +121 -0
- package/relay/lock.ts +81 -0
- package/relay/logfile.ts +138 -0
- package/relay/main.ts +642 -0
- package/relay/policy.ts +241 -0
- package/relay/relay.ts +266 -0
- package/relay/token.ts +116 -0
- package/relay/v1.ts +107 -0
- package/relay/wisp.ts +615 -0
- package/src/base.js +325 -0
- package/src/boot.js +2037 -0
- package/src/calib-worker.js +40 -0
- package/src/fs/9p/codec.js +303 -0
- package/src/fs/9p/server.js +521 -0
- package/src/fs/errno.js +43 -0
- package/src/fs/graph.js +380 -0
- package/src/fs/overlay.js +1018 -0
- package/src/fs/pack-write.js +165 -0
- package/src/fs/pack.js +427 -0
- package/src/fs/seed.js +22 -0
- package/src/fs/svc-snapshot.js +65 -0
- package/src/fs/upper-pack.js +112 -0
- package/src/host/base-cache.js +195 -0
- package/src/host/base-file.js +328 -0
- package/src/host/base-io.js +93 -0
- package/src/host/boot-events.js +209 -0
- package/src/host/chan-service.js +300 -0
- package/src/host/chan-services.js +689 -0
- package/src/host/chan.js +655 -0
- package/src/host/ckpt-file.js +395 -0
- package/src/host/ckpt-io.js +207 -0
- package/src/host/ckpt-point.js +160 -0
- package/src/host/ckpt-save.js +64 -0
- package/src/host/ckpt.js +434 -0
- package/src/host/clock.js +111 -0
- package/src/host/ctl.js +778 -0
- package/src/host/errno.js +18 -0
- package/src/host/fs.js +167 -0
- package/src/host/gcnudge.js +83 -0
- package/src/host/i64shim.js +81 -0
- package/src/host/jit-cache.js +341 -0
- package/src/host/jit-hub-core.js +208 -0
- package/src/host/jit-hub.js +93 -0
- package/src/host/jit-snap.js +226 -0
- package/src/host/jit.js +1903 -0
- package/src/host/layout.js +100 -0
- package/src/host/monitor.js +110 -0
- package/src/host/p9mount.js +27 -0
- package/src/host/ram-mem.js +125 -0
- package/src/host/stdio.js +143 -0
- package/src/host/threads.js +109 -0
- package/src/host/wasi.js +335 -0
- package/src/host/wmd-imports.js +71 -0
- package/src/host/yamux.js +387 -0
- package/src/image-files.js +154 -0
- package/src/net/dhcp-dns.js +126 -0
- package/src/net/net-worker.js +172 -0
- package/src/net/packet.js +227 -0
- package/src/net/peer.js +230 -0
- package/src/net/proto.js +189 -0
- package/src/net/relay-client.js +226 -0
- package/src/net/stack.js +867 -0
- package/src/net/switch.js +160 -0
- package/src/net/wisp-client.js +570 -0
- package/src/net/wisp-transport.js +196 -0
- package/src/net/wisp.js +511 -0
- package/src/registry/client.js +176 -0
- package/src/registry/converter.js +68 -0
- package/src/registry/hub.js +221 -0
- package/src/registry/oci.js +76 -0
- package/src/registry/pack-worker.js +70 -0
- package/src/registry/pull.js +258 -0
- package/src/registry/ref.js +110 -0
- package/src/registry/wasi-mem.js +355 -0
- package/src/worker.js +315 -0
- package/wmdpack.wasm +0 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Clocks shared by every Worker of one machine.
|
|
2
|
+
//
|
|
3
|
+
// performance.now() is relative to each Worker's own timeOrigin, so two Workers
|
|
4
|
+
// disagree by however far apart they started. The guest compares deadlines one
|
|
5
|
+
// thread computed against "now" read on another (the timer thread vs a vCPU), so
|
|
6
|
+
// all Workers use the absolute time performance.timeOrigin + performance.now() and
|
|
7
|
+
// subtract one start time kept in the control buffer. A shared high-water mark keeps
|
|
8
|
+
// CLOCK_MONOTONIC from ever going backwards across Workers (timeOrigin values can
|
|
9
|
+
// differ by a fraction of a millisecond between agents). CLOCK_REALTIME is the start
|
|
10
|
+
// time plus CLOCK_MONOTONIC, so it never goes backwards either.
|
|
11
|
+
|
|
12
|
+
import { CTL_MONO_LOCK, F64_LAST_MONO, F64_START_MS } from './layout.js'
|
|
13
|
+
|
|
14
|
+
export const absMs = () => performance.timeOrigin + performance.now()
|
|
15
|
+
|
|
16
|
+
/** One tick of the arm64 guest's 62.5 MHz counter (arm_timer.c: ticks = ns / 16). */
|
|
17
|
+
export const TICK_NS = 16n
|
|
18
|
+
const TICK = 16
|
|
19
|
+
const TWO32 = 4294967296
|
|
20
|
+
|
|
21
|
+
export function createClock(ctlSab) {
|
|
22
|
+
const f64 = new Float64Array(ctlSab, F64_START_MS, 1)
|
|
23
|
+
const last = new Float64Array(ctlSab, F64_LAST_MONO, 1)
|
|
24
|
+
const ctl = new Int32Array(ctlSab, 0, CTL_MONO_LOCK + 1)
|
|
25
|
+
|
|
26
|
+
const startMs = () => f64[0]
|
|
27
|
+
|
|
28
|
+
// W40: the clock allocates nothing. vm.wasm reads it about 9,000 times a second on an idle
|
|
29
|
+
// guest, and WebKit collects garbage late in a thread holding the guest's memory (D-419,
|
|
30
|
+
// gcnudge.js), so the garbage this made (BigInts, argument arrays) piled up: the content process grew
|
|
31
|
+
// about 0.7 MiB/s from this clock alone. So the values are Numbers (exact up to 2^53 ns,
|
|
32
|
+
// 104 days of uptime), the shared high-water mark is a Float64 under a spin lock instead
|
|
33
|
+
// of a BigInt64 compare-exchange, and writeNs stores the 64-bit result as two words.
|
|
34
|
+
// Workers are terminated only when the machine ends (or before they ever ran), so no
|
|
35
|
+
// Worker dies holding the lock.
|
|
36
|
+
|
|
37
|
+
// ns since machine start, strictly increasing: every read returns at least TICK_NS more
|
|
38
|
+
// than the read before it, on any Worker. Browsers coarsen performance.now() (5 us in
|
|
39
|
+
// Chromium, more elsewhere), so two reads of the guest's counter (CNTVCT = ns / 16)
|
|
40
|
+
// were usually equal. Linux's jitter entropy (try_to_generate_entropy, which a blocked
|
|
41
|
+
// getrandom() runs) then counts too few distinct samples in 8,192 reads and gives up, the
|
|
42
|
+
// crng never initialises on an idle guest, and getrandom() blocks for good: crun hung
|
|
43
|
+
// in the container boot (P-B4). The step cannot run ahead of real time by more than
|
|
44
|
+
// TICK_NS per read made within one coarsened step of the browser's clock.
|
|
45
|
+
function mono() {
|
|
46
|
+
const ns = Math.max(0, Math.round((absMs() - f64[0]) * 1e6))
|
|
47
|
+
while (Atomics.compareExchange(ctl, CTL_MONO_LOCK, 0, 1) !== 0);
|
|
48
|
+
const prev = last[0]
|
|
49
|
+
const next = ns > prev ? ns : prev + TICK
|
|
50
|
+
last[0] = next
|
|
51
|
+
Atomics.store(ctl, CTL_MONO_LOCK, 0)
|
|
52
|
+
return next
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ns since the Unix epoch: the machine's start time (one value, in the shared control
|
|
56
|
+
// block) plus CLOCK_MONOTONIC. Reading each Worker's own timeOrigin + now() instead
|
|
57
|
+
// stepped back by up to ~20 us across Workers (adversarial review, clock.wat). Split
|
|
58
|
+
// into whole ms and the fraction so the result keeps sub-microsecond digits. The start
|
|
59
|
+
// in ns does not fit a Number exactly, so it is kept as two words, worked out again
|
|
60
|
+
// only when the start changes.
|
|
61
|
+
let baseMs = NaN
|
|
62
|
+
let baseHi = 0
|
|
63
|
+
let baseLo = 0
|
|
64
|
+
function base() {
|
|
65
|
+
const ms = f64[0]
|
|
66
|
+
if (ms === baseMs) return
|
|
67
|
+
const whole = Math.floor(ms)
|
|
68
|
+
const b = BigInt(whole) * 1000000n + BigInt(Math.round((ms - whole) * 1e6))
|
|
69
|
+
baseHi = Number(b >> 32n)
|
|
70
|
+
baseLo = Number(b & 0xffffffffn)
|
|
71
|
+
baseMs = ms
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Store clock id's time (0 realtime, 1-3 monotonic) at dv[off] as a little-endian u64. */
|
|
75
|
+
function writeNs(dv, off, id) {
|
|
76
|
+
const m = mono()
|
|
77
|
+
let lo = m % TWO32
|
|
78
|
+
let hi = (m - lo) / TWO32
|
|
79
|
+
if (id === 0) {
|
|
80
|
+
base()
|
|
81
|
+
lo += baseLo
|
|
82
|
+
hi += baseHi
|
|
83
|
+
if (lo >= TWO32) {
|
|
84
|
+
lo -= TWO32
|
|
85
|
+
hi++
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
dv.setUint32(off, lo, true)
|
|
89
|
+
dv.setUint32(off + 4, hi, true)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Clock id's time in ns as a Number (realtime to within a few hundred ns): for timeouts. */
|
|
93
|
+
function nowNum(id) {
|
|
94
|
+
const m = mono()
|
|
95
|
+
if (id !== 0) return m
|
|
96
|
+
base()
|
|
97
|
+
return baseHi * TWO32 + baseLo + m
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const monoNs = () => BigInt(mono())
|
|
101
|
+
function realNs() {
|
|
102
|
+
base()
|
|
103
|
+
return (BigInt(baseHi) << 32n) + BigInt(baseLo) + BigInt(mono())
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return { monoNs, realNs, writeNs, nowNum, startMs }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function markStart(ctlSab, ms = absMs()) {
|
|
110
|
+
new Float64Array(ctlSab, F64_START_MS, 1)[0] = ms
|
|
111
|
+
}
|