Skip to content

Commit 4ec0fa6

Browse files
authored
Add a debug option to cheerpjInit, and document how to warm up a library VM (#52)
1 parent 468c0f3 commit 4ec0fa6

5 files changed

Lines changed: 21 additions & 3 deletions

File tree

‎docs/browser.md‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ and `cjFileBlob(path)` also work.
3636
- `preloadProgress(done, total)` reports the bytes of the runtime downloaded at start.
3737
`total` is 0 while the server has not sent a length; the last call has `done === total`.
3838
- `cheerpjInit({ warmSnapshot: true })` saves a library VM once its calls have warmed it up (see below).
39+
- `cheerpjInit({ debug: true })` logs VM events to the console, such as whether a warm snapshot was saved, skipped as busy (and why) or restored.
3940
- A file Java closes under `/files/downloads/` is downloaded by the browser.
4041
`/files/uploads/` exists for files the page adds there.
4142
- The browser console gets whole lines, so two `print` calls and a `println` log one
@@ -135,4 +136,11 @@ starts with the same class path restore that image, after checking that the
135136
files, missing paths and directory listings those calls used are unchanged; the
136137
first javac compile after it took 2.7 s instead of 5.3 s in Chromium on a
137138
Raspberry Pi 5. An image is about 18 MB. IndexedDB keeps the 2 most recent warm images separately
138-
from boot snapshots.
139+
from boot snapshots. With `debug: true`, the console says whether a VM was
140+
saved, skipped as busy (with the reason) or restored, and why a restore missed.
141+
142+
The image is only reused while every file the warm-up read is unchanged, so
143+
warm up with calls that read fixed files: for example, compile a small source
144+
that never changes before the first real compile. A first compile of the user's
145+
own project records that project's files and directory listings, and the image
146+
then misses as soon as one of them changes.

‎docs/node.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,9 @@ are unchanged; a javac compile from it takes about 1 s instead of 6 s on a
7878
Raspberry Pi 5. An image is about 18 MB; warm snapshots share the snapshot
7979
directory and keep the 2 most recent. They build on boot snapshots, so with
8080
`snapshotDir: null` there are none.
81+
82+
The image is only reused while every file the warm-up read is unchanged, so
83+
warm up with calls that read fixed files: for example, compile a small source
84+
that never changes before the first real compile. A first compile of the user's
85+
own project records that project's files and directory listings, and the image
86+
then misses as soon as one of them changes.

‎src/browser/api.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ export interface InitOptions {
2929
env?: string[];
3030
// load the formats and names of locales other than English
3131
localeData?: boolean;
32-
// save and restore library VMs after their first idle call
32+
// save library VMs once their calls have warmed them up, and restore them on later starts
3333
warmSnapshot?: boolean;
34+
// log VM events (snapshots, missing natives) to the console, as --debug does under Node
35+
debug?: boolean;
3436
// where java-web.js, the worker and jdk/ live; defaults to the script's directory
3537
runtime?: string;
3638
// receives output instead of the page console element
@@ -180,7 +182,7 @@ export function cheerpjInit(options: InitOptions = {}): Promise<void> {
180182
};
181183
});
182184
natives = options.natives ?? {};
183-
send({ type: "init", runtime, app: appBase, props, hostName: options.hostName, env, localeData: !!options.localeData, warmSnapshot: !!options.warmSnapshot, natives: Object.keys(natives), progress: !!options.preloadProgress });
185+
send({ type: "init", runtime, app: appBase, props, hostName: options.hostName, env, localeData: !!options.localeData, warmSnapshot: !!options.warmSnapshot, debug: !!options.debug, natives: Object.keys(natives), progress: !!options.preloadProgress });
184186
initStarted(ready);
185187
return ready;
186188
}

‎src/browser/protocol.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface InitMessage {
1313
env: Record<string, string>;
1414
localeData: boolean;
1515
warmSnapshot: boolean;
16+
debug: boolean;
1617
// JNI names of natives the page implements
1718
natives: string[];
1819
// report the download of the runtime

‎src/browser/worker.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ function newVm(classpath: string[], vmId: number, library = false): JVM {
193193
stderr: (data) => post({ type: "out", fd: 2, data }, [data.buffer as ArrayBuffer]),
194194
stdin: readStdin,
195195
hostName: config!.hostName,
196+
debug: config!.debug,
196197
env: config!.env,
197198
loadNativeLibrary,
198199
fileClosed,

0 commit comments

Comments
 (0)