@threadedstack/wmd-web 0.1.53 → 0.1.54
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/RELEASE.json +6 -5
- package/package.json +3 -3
- package/src/boot.js +26 -12
- package/src/edge/pins.gen.js +1 -1
- package/src/host/ckpt-chain.js +299 -0
- package/src/host/ckpt-file.js +35 -0
- package/src/host/ckpt-io.js +39 -2
- package/src/host/ckpt.js +38 -11
- package/wmdpack.wasm +0 -0
package/RELEASE.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wmd-web",
|
|
3
3
|
"version": "0.5.1",
|
|
4
|
-
"commit": "
|
|
4
|
+
"commit": "3807c88c08611e8ccece4ad96c9c1a755d87ef3f",
|
|
5
5
|
"previous": null,
|
|
6
6
|
"decisions": [
|
|
7
7
|
"D-388",
|
|
@@ -180,7 +180,7 @@
|
|
|
180
180
|
"package.json": "6de9aa9fabdb395651d39997c17e094f01a574f54a36f0af31bd3ffaae53cda1",
|
|
181
181
|
"src/base.js": "982f8e46dbb727ec542a494ebdb884ccd19a31ff4e091425b2d1fc01b0d4754a",
|
|
182
182
|
"src/blob.js": "bb2a2f0d87ce236280a7be2f246c926010dfe07f649c27da5e70d8eb5eaf7061",
|
|
183
|
-
"src/boot.js": "
|
|
183
|
+
"src/boot.js": "e61b07b71d505c7280af65598b3d82d93bfe65ef525879d89a900694c3823a30",
|
|
184
184
|
"src/build/builder.js": "c7794ec15b183213228776c8f4fb0950dec7e621f948358da7de8f90ef4f70ec",
|
|
185
185
|
"src/build/context.js": "d38097c61dbdadc4d41290c010425a4422f499aba473fa2b83f2a5c41e543571",
|
|
186
186
|
"src/build/dockerfile.js": "5957fe3d4ac8ff8b312f980ee04bd1d98ad4373a61f9a33e02f1ff1108b6ea53",
|
|
@@ -289,11 +289,12 @@
|
|
|
289
289
|
"src/host/chan-service.js": "d61afdcbe1a6929dd93a2f7e842c8b94c8d742db59359004d08ec3997b154288",
|
|
290
290
|
"src/host/chan-services.js": "e7f52931b6ef76a8b97b793d0a1d86fb35d20dbca3dd333d18cc4ded0806c9f7",
|
|
291
291
|
"src/host/chan.js": "bb9e76890b0efeee7cfbd8481b9fb4620b3b184459d96d62ff68ddcdc8a46f83",
|
|
292
|
-
"src/host/ckpt-
|
|
293
|
-
"src/host/ckpt-
|
|
292
|
+
"src/host/ckpt-chain.js": "c208a882da772acafe1373ea1d7e04723e26e870396d4202ef330d4eaf758596",
|
|
293
|
+
"src/host/ckpt-file.js": "f5955d730cc968d07811a31728c5c1beef8a618ec13d88dd3b5d1905e254621e",
|
|
294
|
+
"src/host/ckpt-io.js": "e2674314239bd05d8ab6fc89703ededdd26ed8e2c3e820cd3597883b66f3a2e4",
|
|
294
295
|
"src/host/ckpt-point.js": "e08441b52d65b18c91f7e14b6ab3ad347572a0ac96a6642cf8072b47dde404eb",
|
|
295
296
|
"src/host/ckpt-save.js": "7bb07f536005fb5e7d9db6a09169a5ce1e3865d337ab65ce798a111334c6aa07",
|
|
296
|
-
"src/host/ckpt.js": "
|
|
297
|
+
"src/host/ckpt.js": "b1160b5a97e535b2d9eede937af909ae9ee92edcd52a2d73c066ecaf57764ada",
|
|
297
298
|
"src/host/clock.js": "aba6af3a8b1a985ae340892222e4b482d33afe20f5c8b3a4c7403ce87d1e247c",
|
|
298
299
|
"src/host/csp-reason.js": "ecec797e31006896176ff4e2972f11e5d5df160fe0cd80071c39d7808ff922fc",
|
|
299
300
|
"src/host/ctl.js": "86066c8d3be8e6f6b05e831344cd72ebc446ced391fc8290e1c584940b8ab3e0",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@threadedstack/wmd-web",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.54",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"homepage": "https://github.com/threadedstack/wmd-releases",
|
|
6
6
|
"repository": {
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
},
|
|
10
10
|
"description": "Browser host for wmd's guest vm.wasm (Web Workers + shared memory + WASI preview1)",
|
|
11
11
|
"wmd": {
|
|
12
|
-
"tag": "v0.1.
|
|
13
|
-
"commit": "
|
|
12
|
+
"tag": "v0.1.54",
|
|
13
|
+
"commit": "3807c88c08611e8ccece4ad96c9c1a755d87ef3f",
|
|
14
14
|
"web": "0.5.1"
|
|
15
15
|
},
|
|
16
16
|
"type": "module",
|
package/src/boot.js
CHANGED
|
@@ -1413,9 +1413,9 @@ async function bootMachine(o) {
|
|
|
1413
1413
|
restoreFdTable(ctl, restoreIn.ctl, files.length)
|
|
1414
1414
|
ckptT.pagesRestored = restoreIn.index.length
|
|
1415
1415
|
}
|
|
1416
|
-
function restoreMemory() {
|
|
1416
|
+
async function restoreMemory() {
|
|
1417
1417
|
try {
|
|
1418
|
-
restoreMemoryNow()
|
|
1418
|
+
await restoreMemoryNow()
|
|
1419
1419
|
// the snapshot restored: its cache copy may be committed now (not before: a snapshot that fails to restore must not stay cached)
|
|
1420
1420
|
const lazy = baseLazy
|
|
1421
1421
|
baseLazy = null
|
|
@@ -1433,18 +1433,26 @@ async function bootMachine(o) {
|
|
|
1433
1433
|
finish(4, `restore failed: ${err && err.message ? err.message : err}`)
|
|
1434
1434
|
}
|
|
1435
1435
|
}
|
|
1436
|
-
function restoreMemoryNow() {
|
|
1436
|
+
async function restoreMemoryNow() {
|
|
1437
1437
|
const tm = absMs()
|
|
1438
1438
|
const have = memory.buffer.byteLength / CKPT_PAGE // the size the instance made: its start function wrote nothing above it
|
|
1439
1439
|
if (restoreIn.header.memPages > have) memory.grow(restoreIn.header.memPages - have)
|
|
1440
|
-
const u8 = new Uint8Array(memory.buffer)
|
|
1441
1440
|
const kept = new Uint8Array(restoreIn.header.memPages)
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1441
|
+
if (restoreIn.into) {
|
|
1442
|
+
// IOS-2: a store's lazy load: its I/O Worker reads the memory section straight into this memory (the page never holds it)
|
|
1443
|
+
const r = await restoreIn.into(memory, restoreIn.index)
|
|
1444
|
+
if (r.pages !== restoreIn.index.length) throw new Error(`the store wrote ${r.pages} of the checkpoint's ${restoreIn.index.length} memory pages`)
|
|
1445
|
+
for (const p of restoreIn.index) kept[p] = 1
|
|
1446
|
+
ckptT.memoryInto = true
|
|
1447
|
+
} else {
|
|
1448
|
+
const u8 = new Uint8Array(memory.buffer)
|
|
1449
|
+
let k = 0
|
|
1450
|
+
for (const c of restoreIn.memChunks) {
|
|
1451
|
+
for (let off = 0; off < c.length; off += CKPT_PAGE) {
|
|
1452
|
+
const p = restoreIn.index[k++]
|
|
1453
|
+
u8.set(c.subarray(off, off + CKPT_PAGE), p * CKPT_PAGE)
|
|
1454
|
+
kept[p] = 1
|
|
1455
|
+
}
|
|
1448
1456
|
}
|
|
1449
1457
|
}
|
|
1450
1458
|
// the flat layout: the guest's RAM, into its own memory (a fresh one: all zero but for these pages)
|
|
@@ -1499,6 +1507,7 @@ async function bootMachine(o) {
|
|
|
1499
1507
|
}
|
|
1500
1508
|
be.now('restore', { t: 'boot', stage: 'restore', state: 'done', used: true, bytes: baseBytes, total: baseTotal, source: baseSource, tookMs: Math.round(ckptT.pagesWrittenAt) })
|
|
1501
1509
|
restoreIn.memChunks = null
|
|
1510
|
+
restoreIn.into = null
|
|
1502
1511
|
restoreIn.index = null
|
|
1503
1512
|
restoreIn.ram = null
|
|
1504
1513
|
restoreIn.jit = null
|
|
@@ -2611,7 +2620,9 @@ async function bootMachine(o) {
|
|
|
2611
2620
|
if (c.mem.ram) c.mem.ram.chunks = memChunksOf(ramMem, c.mem.ram.index)
|
|
2612
2621
|
const ckpt = ckptBuild(c, names)
|
|
2613
2622
|
ckpt.header.stats.streamed = true
|
|
2614
|
-
|
|
2623
|
+
// IOS-2 parent: a diff over that checkpoint, made by the store's Worker from the frozen memory itself
|
|
2624
|
+
if (c.stream.parent && c.mem.ram) throw new CheckpointError('malformed', 'a diff checkpoint (save.parent) needs the RAM in memory 0; this machine keeps it in a memory of its own')
|
|
2625
|
+
const saved = await c.stream.store.save(c.stream.name, ckpt, c.stream.parent ? { parent: c.stream.parent, memory, index: c.mem.index } : undefined)
|
|
2615
2626
|
if (ck !== c) return
|
|
2616
2627
|
c.savedResult = { header: ckpt.header, saved, streamed: true }
|
|
2617
2628
|
c.streamDone = true
|
|
@@ -2630,7 +2641,10 @@ async function bootMachine(o) {
|
|
|
2630
2641
|
function checkpoint({ label = '', timeoutMs = 60_000, save = null } = {}) {
|
|
2631
2642
|
// IOS-2: save {store, name}: the checkpoint goes straight to the store and the answer is {header, saved, streamed}, no sections. With an
|
|
2632
2643
|
// OPFS store the page never holds the guest's memory (see ckptStreamSave); with another store it is taken in memory and saved, as before
|
|
2633
|
-
if (save && (!save.store || typeof save.store.save !== 'function' || !save.name)) return Promise.reject(new TypeError('checkpoint({save}): save is {store, name}'))
|
|
2644
|
+
if (save && (!save.store || typeof save.store.save !== 'function' || !save.name)) return Promise.reject(new TypeError('checkpoint({save}): save is {store, name[, parent]}'))
|
|
2645
|
+
// IOS-2 save.parent: only the pages that changed since that checkpoint are written (host/ckpt-chain.js); a restore of this one applies the chain
|
|
2646
|
+
if (save?.parent !== undefined && (typeof save.parent !== 'string' || !save.parent)) return Promise.reject(new TypeError('checkpoint({save}): save.parent is the name of a checkpoint in the same store'))
|
|
2647
|
+
if (save?.parent && save.store.kind !== 'opfs') return Promise.reject(new TypeError('checkpoint({save: {parent}}): a diff checkpoint needs an OPFS store (opfsStore())'))
|
|
2634
2648
|
if (save && save.store.kind !== 'opfs') {
|
|
2635
2649
|
return checkpoint({ label, timeoutMs }).then(async ckpt => ({ header: ckpt.header, saved: await save.store.save(save.name, ckpt), streamed: false }))
|
|
2636
2650
|
}
|
package/src/edge/pins.gen.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// The files of node in the sandboxed frame: the path under a source (the option edge: { sources } of bootWmd), the role the frame gives the file, its
|
|
3
3
|
// sha256 and its size. The page accepts a file only when it has exactly this sha256 and size. The notice files travel with the rest.
|
|
4
4
|
// EDGE_PACKAGE is the package that holds these files, for the {name} {version} {file} of a registry source's url (null: none named).
|
|
5
|
-
export const EDGE_PACKAGE = { name: '@threadedstack/wmd-node', version: '0.1.
|
|
5
|
+
export const EDGE_PACKAGE = { name: '@threadedstack/wmd-node', version: '0.1.54' }
|
|
6
6
|
export const EDGE_PINS = [
|
|
7
7
|
{ path: 'wmd_browser_bg.wasm', role: 'wasm', sha256: '1e3db7f2c5f4be0ee44e5f2c0205cf8ca11f906d9addd5512915fad4821661c2', size: 6334959 },
|
|
8
8
|
{ path: 'wmd_browser.js', role: 'glue', sha256: 'b389b072096881ba39b22cd705d3a230af02acc5409c61fb4f997d1081c4df6f', size: 75647 },
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
// IOS-2: diff checkpoints. A checkpoint saved with a parent holds only the memory pages that changed since that parent
|
|
2
|
+
// (its own memory image, resolved through the parent's chain), and the pages that were non-zero there and are zero now;
|
|
3
|
+
// every other section is whole. header.parent = {name, version} names the file it builds on, and so on down to a full
|
|
4
|
+
// checkpoint. Sections of a diff: 'mem-index' (the changed pages, increasing), 'mem' (their bytes), 'mem-zero' (pages
|
|
5
|
+
// now zero, increasing). A full checkpoint has no 'mem-zero' and no parent.
|
|
6
|
+
//
|
|
7
|
+
// The save compares the frozen machine's memory with the parent chain's pages straight from the files, and writes the
|
|
8
|
+
// changed pages from the memory through one scratch buffer; a restore applies the chain oldest first. Both run in the
|
|
9
|
+
// store's I/O Worker (ckpt-io.js), over a "dir" as saveVersioned takes it (ckpt-file.js).
|
|
10
|
+
import { CheckpointError, concat, fileOf, readHeader, readSection, readSectionInto, saveVersioned, versionsOf } from './ckpt-file.js'
|
|
11
|
+
|
|
12
|
+
export const MAX_CHAIN = 64
|
|
13
|
+
const ALIGN = 8
|
|
14
|
+
const pad = n => (ALIGN - (n % ALIGN)) % ALIGN
|
|
15
|
+
const u32 = b => new Uint32Array(b.buffer.slice(b.byteOffset, b.byteOffset + b.length))
|
|
16
|
+
|
|
17
|
+
async function openOne(dir, name, version) {
|
|
18
|
+
if (version === undefined || version === null) {
|
|
19
|
+
const vs = versionsOf(await dir.names(), name)
|
|
20
|
+
for (const v of vs.reverse()) {
|
|
21
|
+
const h = await dir.open(fileOf(name, v), false)
|
|
22
|
+
try {
|
|
23
|
+
return { h, header: readHeader(h), version: v }
|
|
24
|
+
} catch (e) {
|
|
25
|
+
h.close()
|
|
26
|
+
if (!(e instanceof CheckpointError) || !['incomplete', 'truncated'].includes(e.code)) throw e
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
throw new CheckpointError('missing', `no checkpoint named ${JSON.stringify(name)} in this browser's storage`)
|
|
30
|
+
}
|
|
31
|
+
let h
|
|
32
|
+
try {
|
|
33
|
+
h = await dir.open(fileOf(name, version), false)
|
|
34
|
+
} catch (e) {
|
|
35
|
+
if (e instanceof CheckpointError && e.code === 'missing') throw new CheckpointError('missing', `checkpoint ${JSON.stringify(name)} version ${version}, which a diff checkpoint builds on, is gone`)
|
|
36
|
+
throw e
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
return { h, header: readHeader(h), version }
|
|
40
|
+
} catch (e) {
|
|
41
|
+
h.close()
|
|
42
|
+
throw e
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Where each page of a link's 'mem' section lies in its file (page k at offs[k]). */
|
|
47
|
+
function pageOffsets(header, count, page) {
|
|
48
|
+
const s = header.sections.find(x => x.name === 'mem')
|
|
49
|
+
if (!s) throw new CheckpointError('malformed', 'checkpoint has no section mem')
|
|
50
|
+
const offs = new Float64Array(count)
|
|
51
|
+
let off = s.off
|
|
52
|
+
let k = 0
|
|
53
|
+
for (const c of s.chunks) {
|
|
54
|
+
if (c.len % page) throw new CheckpointError('malformed', `a memory chunk is ${c.len} bytes, not whole ${page}-byte pages`)
|
|
55
|
+
for (let at = 0; at < c.len; at += page) {
|
|
56
|
+
if (k >= count) throw new CheckpointError('malformed', 'the memory section holds more pages than its index')
|
|
57
|
+
offs[k++] = off + at
|
|
58
|
+
}
|
|
59
|
+
off += c.len + pad(off + c.len)
|
|
60
|
+
}
|
|
61
|
+
if (k !== count) throw new CheckpointError('malformed', `the memory section holds ${k} pages for an index of ${count}`)
|
|
62
|
+
return offs
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The chain that ends at `name` (its newest valid version, or `version`), oldest first: [{name, version, h, header,
|
|
67
|
+
* index, zero, offs}]. The handles stay open until closeChain.
|
|
68
|
+
*/
|
|
69
|
+
export async function openChain(dir, name, { version, page = 65536 } = {}) {
|
|
70
|
+
const links = []
|
|
71
|
+
try {
|
|
72
|
+
let ref = { name, version }
|
|
73
|
+
for (;;) {
|
|
74
|
+
if (links.length >= MAX_CHAIN) throw new CheckpointError('malformed', `the chain of diff checkpoints under ${JSON.stringify(name)} is longer than ${MAX_CHAIN} (or loops)`)
|
|
75
|
+
const one = await openOne(dir, ref.name, ref.version)
|
|
76
|
+
const link = { name: ref.name, version: one.version, h: one.h, header: one.header }
|
|
77
|
+
links.unshift(link)
|
|
78
|
+
link.index = u32(concat(await readSection(one.h, one.header, 'mem-index')))
|
|
79
|
+
link.zero = one.header.sections.some(s => s.name === 'mem-zero') ? u32(concat(await readSection(one.h, one.header, 'mem-zero'))) : new Uint32Array(0)
|
|
80
|
+
link.offs = pageOffsets(one.header, link.index.length, page)
|
|
81
|
+
const p = one.header.parent
|
|
82
|
+
if (!p) break
|
|
83
|
+
if (typeof p.name !== 'string' || !Number.isInteger(p.version)) throw new CheckpointError('malformed', `checkpoint ${JSON.stringify(ref.name)} names a parent that is not {name, version}`)
|
|
84
|
+
ref = { name: p.name, version: p.version }
|
|
85
|
+
}
|
|
86
|
+
const head = links.at(-1).header
|
|
87
|
+
for (const l of links) {
|
|
88
|
+
if (l.header.vmKey !== head.vmKey || l.header.bootKey !== head.bootKey) throw new CheckpointError('stale', `diff checkpoint ${JSON.stringify(name)} builds on ${JSON.stringify(l.name)}, which another vm.wasm or boot took`)
|
|
89
|
+
}
|
|
90
|
+
return links
|
|
91
|
+
} catch (e) {
|
|
92
|
+
closeChain(links)
|
|
93
|
+
throw e
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function closeChain(links) {
|
|
98
|
+
for (const l of links || []) {
|
|
99
|
+
try {
|
|
100
|
+
l.h.close()
|
|
101
|
+
} catch {}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** The memory image the chain makes: for page p, src[p] is the link that holds it (-1: zero) and pos[p] its place there; index lists the non-zero pages. */
|
|
106
|
+
export function resolveChain(links) {
|
|
107
|
+
const pages = links.reduce((m, l) => Math.max(m, l.header.memPages || 0, l.index.length ? l.index[l.index.length - 1] + 1 : 0, l.zero.length ? l.zero[l.zero.length - 1] + 1 : 0), 0)
|
|
108
|
+
const src = new Int32Array(pages).fill(-1)
|
|
109
|
+
const pos = new Int32Array(pages)
|
|
110
|
+
links.forEach((l, li) => {
|
|
111
|
+
for (const z of l.zero) src[z] = -1
|
|
112
|
+
for (let k = 0; k < l.index.length; k++) {
|
|
113
|
+
src[l.index[k]] = li
|
|
114
|
+
pos[l.index[k]] = k
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
let n = 0
|
|
118
|
+
for (let p = 0; p < pages; p++) if (src[p] >= 0) n++
|
|
119
|
+
const index = new Uint32Array(n)
|
|
120
|
+
for (let p = 0, j = 0; p < pages; p++) if (src[p] >= 0) index[j++] = p
|
|
121
|
+
return { src, pos, index }
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The pages of `mem` (a view of the frozen memory) listed non-zero in `index` that differ from the chain's image, and the
|
|
126
|
+
* pages the image holds that are zero now. Each page of the image that is compared is read from its file (no copy of the
|
|
127
|
+
* image is made). Returns {changed, zero} (increasing Uint32Arrays).
|
|
128
|
+
*/
|
|
129
|
+
export function diffAgainst(links, res, mem, index, page = 65536) {
|
|
130
|
+
const scratch = new Uint8Array(page)
|
|
131
|
+
const a32 = new Uint32Array(scratch.buffer)
|
|
132
|
+
const changed = []
|
|
133
|
+
for (let k = 0; k < index.length; k++) {
|
|
134
|
+
const p = index[k]
|
|
135
|
+
const li = p < res.src.length ? res.src[p] : -1
|
|
136
|
+
if (li < 0) {
|
|
137
|
+
changed.push(p)
|
|
138
|
+
continue
|
|
139
|
+
}
|
|
140
|
+
const l = links[li]
|
|
141
|
+
const at = l.offs[res.pos[p]]
|
|
142
|
+
let got = 0
|
|
143
|
+
while (got < page) {
|
|
144
|
+
const n = l.h.read(scratch.subarray(got), { at: at + got })
|
|
145
|
+
if (!(n > 0)) break
|
|
146
|
+
got += n
|
|
147
|
+
}
|
|
148
|
+
if (got !== page) throw new CheckpointError('truncated', `checkpoint ${JSON.stringify(l.name)} is cut short at ${at}`)
|
|
149
|
+
const b32 = new Uint32Array(mem.buffer, mem.byteOffset + p * page, page / 4)
|
|
150
|
+
let same = true
|
|
151
|
+
for (let i = 0; i < a32.length; i++) {
|
|
152
|
+
if (a32[i] !== b32[i]) {
|
|
153
|
+
same = false
|
|
154
|
+
break
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (!same) changed.push(p)
|
|
158
|
+
}
|
|
159
|
+
const zero = []
|
|
160
|
+
const now = new Uint8Array(res.src.length)
|
|
161
|
+
for (const p of index) if (p < now.length) now[p] = 1
|
|
162
|
+
for (const p of res.index) if (!now[p]) zero.push(p)
|
|
163
|
+
return { changed: Uint32Array.from(changed), zero: Uint32Array.from(zero) }
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* The memory sections of a diff: 'mem-index', 'mem' (chunks of at most chunkPages pages, each copied from `mem` into one
|
|
168
|
+
* scratch buffer when the writer asks for it), 'mem-zero'.
|
|
169
|
+
*/
|
|
170
|
+
export function diffSections(mem, changed, zero, page = 65536, chunkPages = 128) {
|
|
171
|
+
const scratch = new Uint8Array(Math.min(chunkPages, Math.max(1, changed.length)) * page)
|
|
172
|
+
const chunks = []
|
|
173
|
+
for (let at = 0; at < changed.length; at += chunkPages) {
|
|
174
|
+
const m = Math.min(chunkPages, changed.length - at)
|
|
175
|
+
const f = () => {
|
|
176
|
+
const out = scratch.subarray(0, m * page)
|
|
177
|
+
for (let j = 0; j < m; j++) out.set(new Uint8Array(mem.buffer, mem.byteOffset + changed[at + j] * page, page), j * page)
|
|
178
|
+
return out
|
|
179
|
+
}
|
|
180
|
+
f.size = m * page
|
|
181
|
+
Object.defineProperty(f, 'length', { value: m * page })
|
|
182
|
+
chunks.push(f)
|
|
183
|
+
}
|
|
184
|
+
const bytesOf = a => new Uint8Array(a.buffer, a.byteOffset, a.byteLength)
|
|
185
|
+
return [
|
|
186
|
+
{ name: 'mem-index', chunks: [bytesOf(changed)] },
|
|
187
|
+
{ name: 'mem', chunks },
|
|
188
|
+
{ name: 'mem-zero', chunks: [bytesOf(zero)] },
|
|
189
|
+
]
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* A restore of a chain: each link's pages into `dest` (a view of the machine's memory), oldest first, its zero pages
|
|
194
|
+
* cleared after. {pages}: the non-zero pages of the image (what a restore of one full checkpoint of it writes).
|
|
195
|
+
*/
|
|
196
|
+
export async function chainInto(links, dest, { verify = true, page = 65536 } = {}) {
|
|
197
|
+
let written = 0
|
|
198
|
+
let scratchBytes = 0
|
|
199
|
+
for (const l of links) {
|
|
200
|
+
const r = await readSectionInto(l.h, l.header, 'mem', l.index, dest, { verify, page })
|
|
201
|
+
written += r.pages
|
|
202
|
+
scratchBytes = Math.max(scratchBytes, r.scratchBytes)
|
|
203
|
+
for (const z of l.zero) dest.fill(0, z * page, (z + 1) * page)
|
|
204
|
+
}
|
|
205
|
+
return { pages: resolveChain(links).index.length, written, scratchBytes, links: links.length }
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const MEM_SECTIONS = ['mem-index', 'mem', 'mem-zero']
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Saves `name` as a diff over `parent` (a checkpoint in the same dir, its newest version): `header` and `sections` are the
|
|
212
|
+
* checkpoint's own (its memory sections are replaced), `mem` a view of the frozen memory, `index` its non-zero pages.
|
|
213
|
+
* Resolves with saveVersioned's result plus {changed, zero, links}.
|
|
214
|
+
*/
|
|
215
|
+
export async function saveDiff(dir, name, { parent, header, sections, mem, index, page = 65536, chunkPages = 128, o = {} }) {
|
|
216
|
+
if (typeof parent !== 'string' || !parent) throw new CheckpointError('malformed', 'a diff save names its parent checkpoint')
|
|
217
|
+
if (parent === name) throw new CheckpointError('malformed', `checkpoint ${JSON.stringify(name)} cannot be its own parent: save the diff under another name (a new version would remove the one it builds on)`)
|
|
218
|
+
const links = await openChain(dir, parent, { page })
|
|
219
|
+
let changed, zero, head
|
|
220
|
+
try {
|
|
221
|
+
head = links.at(-1)
|
|
222
|
+
if (head.header.vmKey !== header.vmKey || head.header.bootKey !== header.bootKey) {
|
|
223
|
+
throw new CheckpointError('stale', `checkpoint ${JSON.stringify(parent)} was taken by another vm.wasm or boot than this machine: a diff over it cannot be restored`)
|
|
224
|
+
}
|
|
225
|
+
;({ changed, zero } = diffAgainst(links, resolveChain(links), mem, index, page))
|
|
226
|
+
} finally {
|
|
227
|
+
closeChain(links)
|
|
228
|
+
}
|
|
229
|
+
const memSecs = diffSections(mem, changed, zero, page, chunkPages)
|
|
230
|
+
const out = []
|
|
231
|
+
for (const s of sections) {
|
|
232
|
+
if (s.name === 'mem-index') out.push(memSecs[0])
|
|
233
|
+
else if (s.name === 'mem') out.push(memSecs[1], memSecs[2])
|
|
234
|
+
else if (s.name !== 'mem-zero') out.push(s)
|
|
235
|
+
}
|
|
236
|
+
const size = s => s.chunks.reduce((n, c) => n + c.length, 0)
|
|
237
|
+
const hdr = { ...header, parent: { name: parent, version: head.version }, diff: { changed: changed.length, zero: zero.length, links: links.length } }
|
|
238
|
+
hdr.sizes = Object.fromEntries(out.map(s => [s.name, size(s)]))
|
|
239
|
+
hdr.bytes = Object.values(hdr.sizes).reduce((a, b) => a + b, 0)
|
|
240
|
+
const r = await saveVersioned(dir, name, hdr, out, o)
|
|
241
|
+
return { ...r, changed, zero, links: links.length }
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Loads `name` (its newest version, or `version`) through its chain: the newest link's sections, with the memory image
|
|
246
|
+
* the chain makes ('mem-index': its non-zero pages). memory 'lazy': the 'mem' chunk is {lazy, length} and into(memory)
|
|
247
|
+
* applies the chain straight into the memory; 'copy': the image in hand, compact.
|
|
248
|
+
*/
|
|
249
|
+
export async function loadChain(dir, name, { memory = 'lazy', verify = true, page = 65536, version } = {}) {
|
|
250
|
+
const links = await openChain(dir, name, { version, page })
|
|
251
|
+
try {
|
|
252
|
+
const head = links.at(-1)
|
|
253
|
+
const res = resolveChain(links)
|
|
254
|
+
const sections = []
|
|
255
|
+
for (const s of head.header.sections) {
|
|
256
|
+
if (s.name === 'mem-zero') continue
|
|
257
|
+
if (s.name === 'mem-index') sections.push({ name: s.name, chunks: [new Uint8Array(res.index.buffer)] })
|
|
258
|
+
else if (s.name === 'mem') sections.push({ name: s.name, chunks: memory === 'lazy' ? [{ lazy: true, length: res.index.length * page }] : [await chainCompact(links, res, { verify, page })] })
|
|
259
|
+
else sections.push({ name: s.name, chunks: await readSection(head.h, head.header, s.name, { verify }) })
|
|
260
|
+
}
|
|
261
|
+
const v = head.version
|
|
262
|
+
const out = { header: head.header, sections, version: v, chain: links.map(l => ({ name: l.name, version: l.version })) }
|
|
263
|
+
if (memory === 'lazy') out.into = mem => chainIntoFile(dir, name, v, mem, { verify, page })
|
|
264
|
+
return out
|
|
265
|
+
} finally {
|
|
266
|
+
closeChain(links)
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** into() of a loaded chain: opens it again at the version loaded (its parents at the versions it names) and applies it. */
|
|
271
|
+
export async function chainIntoFile(dir, name, version, memory, { verify = true, page = 65536 } = {}) {
|
|
272
|
+
const links = await openChain(dir, name, { version, page })
|
|
273
|
+
try {
|
|
274
|
+
return await chainInto(links, new Uint8Array(memory.buffer), { verify, page })
|
|
275
|
+
} finally {
|
|
276
|
+
closeChain(links)
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export const isMemSection = n => MEM_SECTIONS.includes(n)
|
|
281
|
+
|
|
282
|
+
/** The chain's image as one compact memory section (the pages of res.index, in order), each link's chunks checked: for a load with {memory: 'copy'}. */
|
|
283
|
+
export async function chainCompact(links, res, { verify = true, page = 65536 } = {}) {
|
|
284
|
+
const out = new Uint8Array(res.index.length * page)
|
|
285
|
+
const place = new Int32Array(res.src.length).fill(-1)
|
|
286
|
+
res.index.forEach((p, j) => (place[p] = j))
|
|
287
|
+
for (let li = 0; li < links.length; li++) {
|
|
288
|
+
const l = links[li]
|
|
289
|
+
const chunks = await readSection(l.h, l.header, 'mem', { verify })
|
|
290
|
+
let k = 0
|
|
291
|
+
for (const c of chunks) {
|
|
292
|
+
for (let at = 0; at < c.length; at += page, k++) {
|
|
293
|
+
const p = l.index[k]
|
|
294
|
+
if (res.src[p] === li) out.set(c.subarray(at, at + page), place[p] * page)
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return out
|
|
299
|
+
}
|
package/src/host/ckpt-file.js
CHANGED
|
@@ -232,6 +232,41 @@ export async function readSection(h, header, name, { verify = true } = {}) {
|
|
|
232
232
|
return out
|
|
233
233
|
}
|
|
234
234
|
|
|
235
|
+
/**
|
|
236
|
+
* IOS-2: a memory section (pages listed by `index`, PAGE bytes each, in file order) read straight into `dest` (a view of
|
|
237
|
+
* the machine's memory): each chunk goes through one scratch buffer, is checked against its SHA-256 unless verify is
|
|
238
|
+
* false, and its pages are put at index[k] * PAGE. The page never holds the section. Returns {pages, scratchBytes}.
|
|
239
|
+
*/
|
|
240
|
+
export async function readSectionInto(h, header, name, index, dest, { verify = true, page = 65536 } = {}) {
|
|
241
|
+
const s = header.sections.find(x => x.name === name)
|
|
242
|
+
if (!s) throw new CheckpointError('malformed', `checkpoint has no section ${name}`)
|
|
243
|
+
const total = s.chunks.reduce((n, c) => n + c.len, 0)
|
|
244
|
+
if (total !== index.length * page) throw new CheckpointError('malformed', `section ${name} holds ${total} bytes for ${index.length} pages`)
|
|
245
|
+
const scratch = new Uint8Array(s.chunks.reduce((m, c) => Math.max(m, c.len), 0))
|
|
246
|
+
let off = s.off
|
|
247
|
+
let k = 0
|
|
248
|
+
for (let i = 0; i < s.chunks.length; i++) {
|
|
249
|
+
const c = s.chunks[i]
|
|
250
|
+
if (c.len % page) throw new CheckpointError('malformed', `memory chunk ${i} is ${c.len} bytes, not a whole number of ${page}-byte pages`)
|
|
251
|
+
const b = scratch.subarray(0, c.len)
|
|
252
|
+
let got = 0
|
|
253
|
+
while (got < c.len) {
|
|
254
|
+
const n = h.read(b.subarray(got), { at: off + got })
|
|
255
|
+
if (!(n > 0)) break
|
|
256
|
+
got += n
|
|
257
|
+
}
|
|
258
|
+
if (got !== c.len) throw new CheckpointError('truncated', `checkpoint is cut short: wanted ${c.len} bytes at ${off}, the file has ${got}`)
|
|
259
|
+
if (verify && (await sha256Hex(b)) !== c.sha256) throw new CheckpointError('corrupt', `checkpoint section ${name}, chunk ${i} (bytes ${off}..${off + c.len}) does not match its SHA-256: the file is damaged`)
|
|
260
|
+
for (let at = 0; at < c.len; at += page) {
|
|
261
|
+
const p = index[k++]
|
|
262
|
+
if ((p + 1) * page > dest.length) throw new CheckpointError('malformed', `page ${p} is past the ${dest.length / page} pages of memory`)
|
|
263
|
+
dest.set(b.subarray(at, at + page), p * page)
|
|
264
|
+
}
|
|
265
|
+
off += c.len + pad(off + c.len)
|
|
266
|
+
}
|
|
267
|
+
return { pages: k, scratchBytes: scratch.length }
|
|
268
|
+
}
|
|
269
|
+
|
|
235
270
|
export const sectionBytes = (header, name) => header.sections.find(s => s.name === name)?.chunks.reduce((n, c) => n + c.len, 0) ?? 0
|
|
236
271
|
|
|
237
272
|
/** Concatenates chunks (for small sections). */
|
package/src/host/ckpt-io.js
CHANGED
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
// OPFS API that all three browsers give a Worker). One file per checkpoint in the directory
|
|
3
3
|
// `wmd-ckpt` of the origin's private file system: <id>.ckpt.
|
|
4
4
|
//
|
|
5
|
-
// in: {id, op:'save', name, header, sections:[{name, lens:[n]}]} -> {ok, bytes, ms, shaMs, version, leftover}
|
|
5
|
+
// in: {id, op:'save', name, header, sections:[{name, lens:[n]}], parent?, memory?, index?, page?} -> {ok, bytes, ms, shaMs, version, leftover[, changed, zero, links]}
|
|
6
|
+
// IOS-2 parent: a diff over that checkpoint (ckpt-chain.js); the memory sections come from `memory` here, not from the page
|
|
6
7
|
// The bytes are pulled, one chunk at a time: when the save is ready for chunk (si, ci) the Worker posts
|
|
7
8
|
// {need:true, saveId:id, si, ci, back?}; the page answers {op:'chunk', saveId, si, ci, buf, len?} (back: the last chunk's buffer, reused, IOS-2). At most one chunk (32 MiB)
|
|
8
9
|
// waits here, and a chunk is dropped as soon as it is written. One save at a time (a second is refused: busy).
|
|
9
10
|
// {id, op:'info', name} -> {ok, header} (header only: fast, no digests)
|
|
10
|
-
// {id, op:'load', name, sections:[names] | null (all), verify} -> {ok, header, sections:{name:[ArrayBuffer]}, ms}
|
|
11
|
+
// {id, op:'load', name, sections:[names] | null (all), verify, skip?:[names]} -> {ok, header, sections:{name:[ArrayBuffer]}, ms}
|
|
12
|
+
// {id, op:'into', name, version, section, index, memory, verify, page} -> {ok, pages, scratchBytes, ms} (IOS-2: a section a load skipped, into a shared memory)
|
|
11
13
|
// {id, op:'verify', name} -> {ok, header, ms} (every digest)
|
|
12
14
|
// {id, op:'list'} -> {ok, items:[{name, size, modified}]}
|
|
13
15
|
// {id, op:'delete', name} -> {ok}
|
|
@@ -16,6 +18,7 @@
|
|
|
16
18
|
// out: {id, ok:false, code, error} on failure (code: CheckpointError.code, or 'io').
|
|
17
19
|
import { createSaver } from './ckpt-save.js'
|
|
18
20
|
import { CheckpointError, baseNames, fileOf, readHeader, readSection, saveVersioned, versionsOf, withNewest } from './ckpt-file.js'
|
|
21
|
+
import { chainIntoFile, loadChain, saveDiff } from './ckpt-chain.js'
|
|
19
22
|
|
|
20
23
|
const DIR = 'wmd-ckpt'
|
|
21
24
|
const now = () => performance.now()
|
|
@@ -76,6 +79,11 @@ async function handle(m) {
|
|
|
76
79
|
case 'save': {
|
|
77
80
|
// the chunks are pulled one at a time (startSave): the save asks for each when it is ready for it
|
|
78
81
|
try {
|
|
82
|
+
if (m.parent) {
|
|
83
|
+
// IOS-2: a diff over m.parent: the pages of the frozen memory that changed since the parent chain, read here from the memory itself
|
|
84
|
+
const r = await saveDiff(await opfsDir(), checkName(m.name), { parent: checkName(m.parent), header: m.header, sections: m._sections, mem: new Uint8Array(m.memory.buffer), index: m.index, page: m.page })
|
|
85
|
+
return { bytes: r.bytes, ms: r.ms, shaMs: r.shaMs, version: r.version, leftover: r.leftover, changed: r.changed.length, zero: r.zero.length, links: r.links }
|
|
86
|
+
}
|
|
79
87
|
const r = await saveVersioned(await opfsDir(), checkName(m.name), m.header, m._sections, {})
|
|
80
88
|
return { bytes: r.bytes, ms: r.ms, shaMs: r.shaMs, version: r.version, leftover: r.leftover }
|
|
81
89
|
} finally {
|
|
@@ -88,14 +96,43 @@ async function handle(m) {
|
|
|
88
96
|
return withHandle(m.name, async (h, version, note) => {
|
|
89
97
|
const t0 = now()
|
|
90
98
|
const header = readHeader(h)
|
|
99
|
+
if (header.parent) return { chainAt: version, skipped: note.skipped } // a diff: read through its chain below, once this handle is closed (an OPFS file has one access handle at a time)
|
|
91
100
|
const out = {}
|
|
92
101
|
const transfer = []
|
|
93
102
|
for (const name of m.sections || header.sections.map(x => x.name)) {
|
|
103
|
+
if (m.skip?.includes(name)) continue // IOS-2: left in the file ('into' reads it later)
|
|
94
104
|
out[name] = (await readSection(h, header, name, { verify: m.verify !== false })).map(u => u.buffer)
|
|
95
105
|
for (const b of out[name]) transfer.push(b)
|
|
96
106
|
}
|
|
97
107
|
return { header, sections: out, ms: now() - t0, transfer, version, skipped: note.skipped }
|
|
108
|
+
}).then(async r => {
|
|
109
|
+
if (r.chainAt === undefined) return r
|
|
110
|
+
{
|
|
111
|
+
// IOS-2: a diff checkpoint: the newest link's sections and the memory image its chain makes
|
|
112
|
+
const t0 = now()
|
|
113
|
+
const version = r.chainAt
|
|
114
|
+
const c = await loadChain(await opfsDir(), checkName(m.name), { memory: m.skip?.includes('mem') ? 'lazy' : 'copy', verify: m.verify !== false, page: m.page, version })
|
|
115
|
+
const header = c.header
|
|
116
|
+
const note = { skipped: r.skipped }
|
|
117
|
+
const out = {}
|
|
118
|
+
const transfer = []
|
|
119
|
+
for (const sec of c.sections) {
|
|
120
|
+
if (sec.chunks[0]?.lazy) continue
|
|
121
|
+
out[sec.name] = sec.chunks.map(u => (u.byteOffset === 0 && u.byteLength === u.buffer.byteLength ? u.buffer : u.slice().buffer))
|
|
122
|
+
for (const b of out[sec.name]) transfer.push(b)
|
|
123
|
+
}
|
|
124
|
+
const memLen = c.sections.find(x => x.name === 'mem').chunks.reduce((n, x) => n + x.length, 0)
|
|
125
|
+
return { header, sections: out, ms: now() - t0, transfer, version, skipped: note.skipped, chain: c.chain, memBytes: memLen }
|
|
126
|
+
}
|
|
98
127
|
})
|
|
128
|
+
case 'into': {
|
|
129
|
+
// IOS-2: one section of the version a lazy load read, straight into a shared memory (the restoring machine's), one chunk at a time
|
|
130
|
+
// a diff checkpoint's chain is applied oldest first (a full checkpoint is a chain of one)
|
|
131
|
+
if (m.section !== 'mem') throw new CheckpointError('malformed', `into: only the memory section is read this way, not ${m.section}`)
|
|
132
|
+
const t0 = now()
|
|
133
|
+
const r = await chainIntoFile(await opfsDir(), checkName(m.name), m.version, m.memory, { verify: m.verify !== false, page: m.page })
|
|
134
|
+
return { pages: r.pages, scratchBytes: r.scratchBytes, links: r.links, ms: now() - t0 }
|
|
135
|
+
}
|
|
99
136
|
case 'verify':
|
|
100
137
|
return withHandle(m.name, async h => {
|
|
101
138
|
const t0 = now()
|
package/src/host/ckpt.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
// svc-<port> the 9P service's state for that port (fs/svc-snapshot.js)
|
|
10
10
|
// ctl the control block's WASI fd table (Int32 cells)
|
|
11
11
|
// Taken by vm.checkpoint() (boot.js), restored by bootWmd({restore}).
|
|
12
|
-
import { FORMAT_VERSION, CheckpointError, concat, memoryFile, readHeader, readSection, sectionBytes, sha256Hex, writeCheckpoint } from './ckpt-file.js'
|
|
12
|
+
import { FORMAT_VERSION, CheckpointError, concat, memoryFile, readHeader, readSection, readSectionInto, sectionBytes, sha256Hex, writeCheckpoint } from './ckpt-file.js'
|
|
13
13
|
import { answerNeed } from './ckpt-save.js'
|
|
14
14
|
import { PAGE } from './ckpt-point.js'
|
|
15
15
|
import { validateSnap, widenRecs } from './jit-snap.js'
|
|
@@ -305,7 +305,9 @@ export function parseCheckpoint(ck) {
|
|
|
305
305
|
const svc = {}
|
|
306
306
|
for (const port of h.svcPorts || []) svc[port] = concat(get(`svc-${port}`))
|
|
307
307
|
const ctlBytes = concat(get('ctl'))
|
|
308
|
-
|
|
308
|
+
// IOS-2: a store's lazy load (load(name, {memory: 'lazy'})): the memory section is still in the file, and into(memory, index) reads it there
|
|
309
|
+
const into = typeof ck.into === 'function' ? ck.into : null
|
|
310
|
+
return { header: h, index, memChunks, ram, jit, svc, ctl: new Uint8Array(ctlBytes), names: parseNames(ck), into }
|
|
309
311
|
}
|
|
310
312
|
|
|
311
313
|
/** The flat layout's RAM sections (`<name>-index` and `<name>`): {index, chunks}, checked as the memory's are. */
|
|
@@ -521,14 +523,17 @@ export function opfsStore({ workerUrl } = {}) {
|
|
|
521
523
|
* in-memory checkpoint stays usable: the page never holds a second copy of the whole file, and the Worker holds at
|
|
522
524
|
* most one chunk. Saves are queued, one at a time. Resolves {bytes, ms, shaMs, version, leftover}.
|
|
523
525
|
*/
|
|
524
|
-
save(name, ck) {
|
|
526
|
+
save(name, ck, { parent, memory, index } = {}) {
|
|
527
|
+
// a loaded checkpoint whose memory was left in its file has no bytes to write
|
|
528
|
+
if (ck.sections.some(sec => sec.chunks.some(c => c && c.lazy && typeof c.read !== 'function'))) return Promise.reject(new CheckpointError('malformed', "this checkpoint's memory is still in its file (a lazy load): load it with {memory: 'copy'} to save it again"))
|
|
525
529
|
const run = saveQueue.catch(() => {}).then(async () => {
|
|
526
530
|
const id = next++
|
|
527
531
|
activeSaves.set(id, ck)
|
|
528
532
|
try {
|
|
529
533
|
const sections = ck.sections.map(s => ({ name: s.name, lens: s.chunks.map(c => c.length) }))
|
|
530
|
-
|
|
531
|
-
|
|
534
|
+
// IOS-2 parent: a diff over that checkpoint; the Worker reads the changed pages from `memory` (the frozen machine's), listed non-zero in `index`
|
|
535
|
+
const r = await call({ op: 'save', name, header: ck.header, sections, ...(parent ? { parent, memory, index, page: PAGE } : {}) }, [], id)
|
|
536
|
+
return { bytes: r.bytes, ms: r.ms, shaMs: r.shaMs, version: r.version, leftover: r.leftover, ...(parent ? { changed: r.changed, zero: r.zero, links: r.links } : {}) }
|
|
532
537
|
} finally {
|
|
533
538
|
activeSaves.delete(id)
|
|
534
539
|
}
|
|
@@ -541,10 +546,22 @@ export function opfsStore({ workerUrl } = {}) {
|
|
|
541
546
|
return { header: r.header, size: r.size, version: r.version, skipped: r.skipped }
|
|
542
547
|
},
|
|
543
548
|
/** Reads and verifies a checkpoint into memory: {header, sections, ms}. */
|
|
544
|
-
|
|
549
|
+
/**
|
|
550
|
+
* Reads and verifies a checkpoint: {header, sections, ms}. IOS-2, the default: the memory section stays in the file
|
|
551
|
+
* (its chunks are {lazy, length}) and into(memory, index) is added: bootWmd({restore}) / vm.restore() then have the
|
|
552
|
+
* I/O Worker read it straight into the new machine's memory, one chunk at a time (the version read here, by name).
|
|
553
|
+
* {memory: 'copy'}: every section in hand, as before (to save it again or read its bytes).
|
|
554
|
+
*/
|
|
555
|
+
async load(name, { verify = true, memory } = {}) {
|
|
556
|
+
const { lazy, skip } = opfsLoadPlan({ memory })
|
|
545
557
|
// one op: a save that lands between an info and a load could make them name different versions
|
|
546
|
-
const r = await call({ op: 'load', name, sections: null, verify })
|
|
547
|
-
|
|
558
|
+
const r = await call({ op: 'load', name, sections: null, verify, skip, page: PAGE })
|
|
559
|
+
// a diff checkpoint (r.chain): 'mem-index' is the image its chain makes, 'mem' that image (r.memBytes), no 'mem-zero'
|
|
560
|
+
const memLazy = s => (r.chain ? [{ lazy: true, length: r.memBytes }] : lazyChunks(s))
|
|
561
|
+
const sections = r.header.sections.filter(s => !(r.chain && s.name === 'mem-zero')).map(s => ({ name: s.name, chunks: lazy && s.name === 'mem' ? memLazy(s) : r.sections[s.name].map(b => new Uint8Array(b)) }))
|
|
562
|
+
const out = { header: r.header, sections, ms: r.ms, version: r.version, skipped: r.skipped, ...(r.chain ? { chain: r.chain } : {}) }
|
|
563
|
+
if (lazy) out.into = async (mem, index) => call({ op: 'into', name, version: r.version, section: 'mem', index, memory: mem, verify, page: PAGE })
|
|
564
|
+
return out
|
|
548
565
|
},
|
|
549
566
|
async verify(name) {
|
|
550
567
|
return call({ op: 'verify', name })
|
|
@@ -582,6 +599,15 @@ export function opfsStore({ workerUrl } = {}) {
|
|
|
582
599
|
}
|
|
583
600
|
}
|
|
584
601
|
|
|
602
|
+
/** IOS-2: what an OPFS load reads now: the memory section stays in the file unless {memory: 'copy'} */
|
|
603
|
+
export function opfsLoadPlan({ memory = 'lazy' } = {}) {
|
|
604
|
+
if (memory !== 'lazy' && memory !== 'copy') throw new TypeError(`load: memory is 'lazy' (the default) or 'copy', not ${JSON.stringify(memory)}`)
|
|
605
|
+
return memory === 'lazy' ? { lazy: true, skip: ['mem'] } : { lazy: false, skip: [] }
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/** IOS-2: a section left in its file: each chunk's size, no bytes */
|
|
609
|
+
const lazyChunks = s => s.chunks.map(c => ({ lazy: true, length: c.len }))
|
|
610
|
+
|
|
585
611
|
/** The same interface over memory files (tests; a page without OPFS). */
|
|
586
612
|
export function memoryStore() {
|
|
587
613
|
const files = new Map()
|
|
@@ -599,13 +625,14 @@ export function memoryStore() {
|
|
|
599
625
|
if (!f) throw new CheckpointError('missing', `no checkpoint named ${JSON.stringify(name)}`)
|
|
600
626
|
return { header: readHeader(f), size: f.getSize() }
|
|
601
627
|
},
|
|
602
|
-
async load(name, { verify = true } = {}) {
|
|
628
|
+
async load(name, { verify = true, memory } = {}) {
|
|
603
629
|
const f = files.get(name)
|
|
604
630
|
if (!f) throw new CheckpointError('missing', `no checkpoint named ${JSON.stringify(name)}`)
|
|
605
631
|
const header = readHeader(f)
|
|
606
632
|
const sections = []
|
|
607
|
-
|
|
608
|
-
|
|
633
|
+
const lazy = memory === 'lazy'
|
|
634
|
+
for (const s of header.sections) sections.push({ name: s.name, chunks: lazy && s.name === 'mem' ? lazyChunks(s) : await readSection(f, header, s.name, { verify }) })
|
|
635
|
+
return { header, sections, ms: 0, ...(lazy ? { into: (mem, index) => readSectionInto(f, header, 'mem', index, new Uint8Array(mem.buffer), { verify, page: PAGE }) } : {}) }
|
|
609
636
|
},
|
|
610
637
|
async verify(name) {
|
|
611
638
|
await this.load(name, { verify: true })
|
package/wmdpack.wasm
CHANGED
|
Binary file
|