@threadedstack/wmd-web 0.0.0-stage → 0.1.12

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.
Files changed (92) hide show
  1. package/README.md +2601 -2
  2. package/RELEASE.json +123 -0
  3. package/export-page/coi-sw.js +20 -0
  4. package/export-page/index.html +17 -0
  5. package/export-page/run.css +10 -0
  6. package/export-page/run.js +236 -0
  7. package/guest/arm64/Image +4 -0
  8. package/guest/arm64/Image-4k +4 -0
  9. package/guest/arm64/initramfs.cpio.gz +0 -0
  10. package/guest/arm64/vm.wasm +0 -0
  11. package/package.json +36 -4
  12. package/page/sw.js +381 -0
  13. package/relay/control.ts +139 -0
  14. package/relay/engine.ts +787 -0
  15. package/relay/hosted.ts +660 -0
  16. package/relay/keys.ts +53 -0
  17. package/relay/local-tls.ts +121 -0
  18. package/relay/lock.ts +81 -0
  19. package/relay/logfile.ts +138 -0
  20. package/relay/main.ts +642 -0
  21. package/relay/policy.ts +241 -0
  22. package/relay/relay.ts +266 -0
  23. package/relay/token.ts +116 -0
  24. package/relay/v1.ts +107 -0
  25. package/relay/wisp.ts +615 -0
  26. package/src/base.js +325 -0
  27. package/src/boot.js +2037 -0
  28. package/src/calib-worker.js +40 -0
  29. package/src/fs/9p/codec.js +303 -0
  30. package/src/fs/9p/server.js +521 -0
  31. package/src/fs/errno.js +43 -0
  32. package/src/fs/graph.js +380 -0
  33. package/src/fs/overlay.js +1018 -0
  34. package/src/fs/pack-write.js +165 -0
  35. package/src/fs/pack.js +427 -0
  36. package/src/fs/seed.js +22 -0
  37. package/src/fs/svc-snapshot.js +65 -0
  38. package/src/fs/upper-pack.js +112 -0
  39. package/src/host/base-cache.js +195 -0
  40. package/src/host/base-file.js +328 -0
  41. package/src/host/base-io.js +93 -0
  42. package/src/host/boot-events.js +209 -0
  43. package/src/host/chan-service.js +300 -0
  44. package/src/host/chan-services.js +689 -0
  45. package/src/host/chan.js +655 -0
  46. package/src/host/ckpt-file.js +395 -0
  47. package/src/host/ckpt-io.js +207 -0
  48. package/src/host/ckpt-point.js +160 -0
  49. package/src/host/ckpt-save.js +64 -0
  50. package/src/host/ckpt.js +434 -0
  51. package/src/host/clock.js +111 -0
  52. package/src/host/ctl.js +778 -0
  53. package/src/host/errno.js +18 -0
  54. package/src/host/fs.js +167 -0
  55. package/src/host/gcnudge.js +83 -0
  56. package/src/host/i64shim.js +81 -0
  57. package/src/host/jit-cache.js +341 -0
  58. package/src/host/jit-hub-core.js +208 -0
  59. package/src/host/jit-hub.js +93 -0
  60. package/src/host/jit-snap.js +226 -0
  61. package/src/host/jit.js +1903 -0
  62. package/src/host/layout.js +100 -0
  63. package/src/host/monitor.js +110 -0
  64. package/src/host/p9mount.js +27 -0
  65. package/src/host/ram-mem.js +125 -0
  66. package/src/host/stdio.js +143 -0
  67. package/src/host/threads.js +109 -0
  68. package/src/host/wasi.js +335 -0
  69. package/src/host/wmd-imports.js +71 -0
  70. package/src/host/yamux.js +387 -0
  71. package/src/image-files.js +154 -0
  72. package/src/net/dhcp-dns.js +126 -0
  73. package/src/net/net-worker.js +172 -0
  74. package/src/net/packet.js +227 -0
  75. package/src/net/peer.js +230 -0
  76. package/src/net/proto.js +189 -0
  77. package/src/net/relay-client.js +226 -0
  78. package/src/net/stack.js +867 -0
  79. package/src/net/switch.js +160 -0
  80. package/src/net/wisp-client.js +570 -0
  81. package/src/net/wisp-transport.js +196 -0
  82. package/src/net/wisp.js +511 -0
  83. package/src/registry/client.js +176 -0
  84. package/src/registry/converter.js +68 -0
  85. package/src/registry/hub.js +221 -0
  86. package/src/registry/oci.js +76 -0
  87. package/src/registry/pack-worker.js +70 -0
  88. package/src/registry/pull.js +258 -0
  89. package/src/registry/ref.js +110 -0
  90. package/src/registry/wasi-mem.js +355 -0
  91. package/src/worker.js +315 -0
  92. 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
+ }