Get speedtest1.js working with WASMFS/OPFS.

FossilOrigin-Name: 40e60f570d4f489d58d12e27c1c067b41d6c5a5e374c5fce0baa8881ef183216
This commit is contained in:
stephan 2022-09-06 20:17:15 +00:00
parent 100b496dd2
commit 8fc8b5b35f
6 changed files with 107 additions and 53 deletions

View File

@ -259,10 +259,11 @@ emcc.jsflags += -sMEMORY64=0
# sqlite3.worker.js generated in conjunction with -sWASMFS).
sqlite3.js := sqlite3.js
sqlite3.wasm := sqlite3.wasm
$(dir.api)/sqlite3-wasm.o: emcc.cflags += $(SQLITE_OPT)
$(dir.api)/sqlite3-wasm.o: $(dir.top)/sqlite3.c
sqlite3-wasm.o := $(dir.api)/sqlite3-wasm.o
$(sqlite3-wasm.o): emcc.cflags += $(SQLITE_OPT)
$(sqlite3-wasm.o): $(dir.top)/sqlite3.c
$(dir.api)/wasm_util.o: emcc.cflags += $(SQLITE_OPT)
sqlite3.wasm.c := $(dir.api)/sqlite3-wasm.c \
sqlite3-wasm.c := $(dir.api)/sqlite3-wasm.c \
$(dir.jacc)/jaccwabyt_test.c
# ^^^ FIXME (how?): jaccwabyt_test.c is only needed for the test apps,
# so we don't really want to include it in release builds. However, we
@ -270,7 +271,7 @@ sqlite3.wasm.c := $(dir.api)/sqlite3-wasm.c \
# elide that file in release builds. That component is critical to the
# VFS bindings so needs to be tested along with the core APIs.
ifneq (,$(filter -sWASMFS,$(emcc.jsflags)))
$(dir.api)/sqlite3-wasm.o: emcc.cflags+=-DSQLITE_WASM_OPFS
$(sqlite3-wasm.o): emcc.cflags+=-DSQLITE_WASM_OPFS
endif
define WASM_C_COMPILE
$(1).o := $$(subst .c,.o,$(1))
@ -279,7 +280,7 @@ $$($(1).o): $$(MAKEFILE) $(1)
$$(emcc.bin) $$(emcc_opt) $$(emcc.flags) $$(emcc.cflags) -c $(1) -o $$@
CLEAN_FILES += $$($(1).o)
endef
$(foreach c,$(sqlite3.wasm.c),$(eval $(call WASM_C_COMPILE,$(c))))
$(foreach c,$(sqlite3-wasm.c),$(eval $(call WASM_C_COMPILE,$(c))))
$(sqlite3.js):
$(sqlite3.js): $(MAKEFILE) $(sqlite3.wasm.obj) \
EXPORTED_FUNCTIONS.api \
@ -298,7 +299,7 @@ wasm: $(sqlite3.js)
########################################################################
########################################################################
# Bits for use with batch-runner.js...
# batch-runner.js...
dir.sql := sql
speedtest1 := ../../speedtest1
speedtest1.c := ../../test/speedtest1.c
@ -317,24 +318,38 @@ clean-batch:
# pieces each time is an unnecessary time sink.
batch: batch-runner.list
all: batch
# end batch-runner.js
########################################################################
# speedtest1.js...
emcc.speedtest1-flags := -g $(emcc_opt)
ifneq (0,$(ENABLE_WASMFS))
emcc.speedtest1-flags += -pthread -sWASMFS -sPTHREAD_POOL_SIZE=2
emcc.speedtest1-flags += -DSQLITE_WASM_OPFS
endif
emcc.speedtest1-flags += -sINVOKE_RUN=0
#emcc.speedtest1-flags += --no-entry
emcc.speedtest1-flags += -flto
emcc.speedtest1-flags += -sABORTING_MALLOC
emcc.speedtest1-flags += -sINITIAL_MEMORY=128450560
emcc.speedtest1-flags += -sSTRICT_JS
emcc.speedtest1-flags += $(emcc.environment)
emcc.speedtest1-flags += -sMODULARIZE
emcc.speedtest1-flags += -sEXPORT_NAME=sqlite3Speedtest1InitModule
emcc.speedtest1-flags += -Wno-limited-postlink-optimizations
emcc.speedtest1-flags += -sEXPORTED_FUNCTIONS=_main,_malloc,_free,_sqlite3_wasm_vfs_unlink,_sqlite3_wasm_init_opfs
emcc.speedtest1-flags += -sDYNAMIC_EXECUTION=0
emcc.speedtest1-flags += --minify 0
speedtest1.js := speedtest1.js
speedtest1.wasm := $(subst .js,.wasm,$(speedtest1.js))
$(speedtest1.js): emcc.cflags+=
$(speedtest1.js): $(speedtest1.c) $(MAKEFILE)
$(emcc.bin) -g $(emcc_opt) \
-sINVOKE_RUN=0 \
--no-entry \
-sALLOW_TABLE_GROWTH \
-sABORTING_MALLOC \
-sINITIAL_MEMORY=128450560 \
-sSTRICT_JS \
-sENVIRONMENT=web \
-sMODULARIZE \
-sEXPORT_NAME=sqlite3Speedtest1InitModule \
-Wno-limited-postlink-optimizations \
-sEXPORTED_FUNCTIONS=_main,_malloc,_free \
-sDYNAMIC_EXECUTION=0 \
--minify 0 \
# speedtest1 notes re. sqlite3-wasm.o vs sqlite3-wasm.c: building against
# the latter (predictably) results in a slightly faster binary, but we're
# close enough to the target speed requirements that the 500ms makes a
# difference.
$(speedtest1.js): $(speedtest1.c) $(sqlite3-wasm.c) $(MAKEFILE)
$(emcc.bin) \
$(emcc.speedtest1-flags) \
-I. -I$(dir.top) \
-DSQLITE_THREADSAFE=0 \
-DSQLITE_TEMP_STORE=3 \
@ -343,13 +358,16 @@ $(speedtest1.js): $(speedtest1.c) $(MAKEFILE)
-DSQLITE_OMIT_SHARED_CACHE \
'-DSQLITE_DEFAULT_UNIX_VFS="unix-none"' \
-DSQLITE_SPEEDTEST1_WASM \
$(emcc_flags_wasmfs) \
-o $@ $(speedtest1.c) $(sqlite3.c) -lm
-o $@ $(speedtest1.c) $(sqlite3-wasm.c) -lm
ifneq (,$(wasm-strip))
$(wasm-strip) $(speedtest1.wasm)
endif
ls -la $@ $(speedtest1.wasm)
speedtest1: $(speedtest1.js)
all: $(speedtest1.js)
CLEAN_FILES += $(speedtest1.js) $(subst .js,.wasm,$(speedtest1.js))
CLEAN_FILES += $(speedtest1.js) $(speedtest1.wasm)
# end speedtest1.js
########################################################################
# fiddle_remote is the remote destination for the fiddle app. It
# must be a [user@]HOST:/path for rsync.

View File

@ -473,7 +473,7 @@ int sqlite3_wasm_init_opfs(const char *zMountPoint){
hypothetically suffice for the transient wasm-based virtual
filesystem we're currently running in. */
const int rc = wasmfs_create_directory(zMountPoint, 0777, pOpfs);
emscripten_console_logf("OPFS mkdir rc=%d", rc);
emscripten_console_logf("OPFS mkdir(%s) rc=%d", zMountPoint, rc);
if(rc) return SQLITE_IOERR;
}
return pOpfs ? 0 : SQLITE_NOMEM;

View File

@ -1482,7 +1482,7 @@ self.WhWasmUtilInstaller = function(target){
if(Array.isArray(arguments[3])) args = arguments[3];
return target.xWrap(fname, resultType, argTypes||[]).apply(null, args||[]);
};
return target;
};

View File

@ -31,10 +31,37 @@
<script src="common/whwasmutil.js"></script>
<script src="common/SqliteTestUtil.js"></script>
<script src="speedtest1.js"></script>
<script>
(function(){
<script>(function(){
/**
If this environment contains OPFS, this function initializes it and
returns the name of the dir on which OPFS is mounted, else it returns
an empty string.
*/
const opfsDir = function f(wasmUtil){
if(undefined !== f._) return f._;
const pdir = '/persistent';
if( !self.FileSystemHandle
|| !self.FileSystemDirectoryHandle
|| !self.FileSystemFileHandle){
return f._ = "";
}
try{
if(0===wasmUtil.xCallWrapped(
'sqlite3_wasm_init_opfs', 'i32', ['string'], pdir
)){
return f._ = pdir;
}else{
return f._ = "";
}
}catch(e){
// sqlite3_wasm_init_opfs() is not available
return f._ = "";
}
};
opfsDir._ = undefined;
const eOut = document.querySelector('#test-output');
const logHtml2 = async function(cssClass,...args){
const log2 = async function(cssClass,...args){
const ln = document.createElement('div');
if(cssClass) ln.classList.add(cssClass);
ln.append(document.createTextNode(args.join(' ')));
@ -44,44 +71,53 @@
const doHtmlOutput = false
/* can't update DOM while speedtest is running unless we run
speedtest in a worker thread. */;
const logHtml = (...args)=>{
const log = (...args)=>{
console.log(...args);
if(doHtmlOutput) logHtml2('', ...args);
if(doHtmlOutput) log2('', ...args);
};
const logErr = function(...args){
console.error(...args);
if(doHtmlOutput) this.logHtml2('error', ...args);
if(doHtmlOutput) log2('error', ...args);
};
const runTests = function(EmscriptenModule){
console.log("Module inited.");
console.log("Module inited.",EmscriptenModule);
const wasm = {
exports: EmscriptenModule.asm,
alloc: (n)=>EmscriptenModule._malloc(n),
dealloc: (m)=>EmscriptenModule._free(m)
dealloc: (m)=>EmscriptenModule._free(m),
memory: EmscriptenModule.asm.memory || EmscriptenModule.wasmMemory
};
//console.debug('Emscripten Module =',EmscriptenModule);
//console.debug('wasm =',wasm);
self.WhWasmUtilInstaller(wasm);
const unlink = wasm.xWrap("sqlite3_wasm_vfs_unlink", "int", ["string"]);
const pDir = opfsDir(wasm);
if(pDir){
console.warn("Persistent storage:",pDir);
}
const scope = wasm.scopedAllocPush();
const dbFile = 0 ? "" : pDir+"/speedtest1.db";
try{
const maxArgc = 12;
const aArgs = [
const argv = [
// TODO: accept flags via URL arguments and/or a
// UI control. A multi-SELECT element should do
// nicely.
"speedtest1",
"--memdb",
"--stats"
"--singlethread",
//"--stats",
"--memdb", // note that memdb trumps the filename arg
dbFile
];
wasm.xCall('__main_argc_argv', aArgs.length,
wasm.scopedAllocMainArgv(aArgs));
console.log("argv =",argv);
wasm.xCall('__main_argc_argv', argv.length,
wasm.scopedAllocMainArgv(argv));
}finally{
wasm.scopedAllocPop(scope);
if(pDir) unlink(dbFile);
}
};
self.sqlite3TestModule.print = logHtml;
self.sqlite3TestModule.print = log;
self.sqlite3TestModule.printErr = logErr;
sqlite3Speedtest1InitModule(self.sqlite3TestModule).then(function(M){
setTimeout(()=>runTests(M), 100);

View File

@ -1,5 +1,5 @@
C Initial\sbuild\sof\sspeedtest1.wasm\sand\sspeedtest1.html\swith\swhich\sto\srun\sit.
D 2022-09-06T16:47:43.072
C Get\sspeedtest1.js\sworking\swith\sWASMFS/OPFS.
D 2022-09-06T20:17:15.425
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -474,7 +474,7 @@ F ext/userauth/user-auth.txt e6641021a9210364665fe625d067617d03f27b04
F ext/userauth/userauth.c 7f00cded7dcaa5d47f54539b290a43d2e59f4b1eb5f447545fa865f002fc80cb
F ext/wasm/EXPORTED_FUNCTIONS.fiddle db7a4602f043cf4a5e4135be3609a487f9f1c83f05778bfbdf93766be4541b96
F ext/wasm/EXPORTED_RUNTIME_METHODS.fiddle a004bd5eeeda6d3b28d16779b7f1a80305bfe009dfc7f0721b042967f0d39d02
F ext/wasm/GNUmakefile 445b81c44804f029bcc938aa777ca3fe450a0061a586a21be411362575c7cf61
F ext/wasm/GNUmakefile 2333e782a412c56438e9c08b56b8e26d402d70067760925e6900df5cb4165b66
F ext/wasm/README.md e1ee1e7c321c6a250bf78a84ca6f5882890a237a450ba5a0649c7a8399194c52
F ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api 77ef4bcf37e362b9ad61f9c175dfc0f1b3e571563fb311b96581cf422ee6a8ec
F ext/wasm/api/EXPORTED_RUNTIME_METHODS.sqlite3-api 1ec3c73e7d66e95529c3c64ac3de2470b0e9e7fbf7a5b41261c367cf4f1b7287
@ -488,13 +488,13 @@ F ext/wasm/api/sqlite3-api-opfs.js 011799db398157cbd254264b6ebae00d7234b93d0e9e8
F ext/wasm/api/sqlite3-api-prologue.js 2d5c5d3355f55eefe51922cec5bfedbec0f8300db98a17685ab7a34a03953c7a
F ext/wasm/api/sqlite3-api-worker1.js 73579555563b789785ae83724014eaf31811073aad9be6596c8336ffb51edd71
F ext/wasm/api/sqlite3-wasi.h 25356084cfe0d40458a902afb465df8c21fc4152c1d0a59b563a3fba59a068f9
F ext/wasm/api/sqlite3-wasm.c 0d81282eaeff2a6e9fc5c28a388c5c5b45cf25a9393992fa511ac009b27df982
F ext/wasm/api/sqlite3-wasm.c 19c3797edc35821e362a8b60ce45d1adfe6d24fca7cd1f55f89d2086ef33870e
F ext/wasm/batch-runner.html 2d44d99a556c46f586d3319003dd281dd0eb6a13eeadde3eab05ba81eec9ff8a
F ext/wasm/batch-runner.js a727cbbffe63fd17fb5a590dc679f0b13bd51880e8f84b461d7df246417689e8
F ext/wasm/common/SqliteTestUtil.js 7a543e238c2ebda922c85076abda017d0480944fdfee576692a0c3a580319ebd
F ext/wasm/common/emscripten.css 3d253a6fdb8983a2ac983855bfbdd4b6fa1ff267c28d69513dd6ef1f289ada3f
F ext/wasm/common/testing.css 572cf1ffae0b6eb7ca63684d3392bf350217a07b90e7a896e4fa850700c989b0
F ext/wasm/common/whwasmutil.js ed10ab537b800e1f8fac9d5ecec00908f4d4b0e1a270de196e43782600ae59f7
F ext/wasm/common/whwasmutil.js f7282ef36c9625330d4e6e82d1beec6678cd101e95e7108cd85db587a788c145
F ext/wasm/demo-oo1.html 75646855b38405d82781246fd08c852a2b3bee05dd9f0fe10ab655a8cffb79aa
F ext/wasm/demo-oo1.js aad38cb90b6fa7fd4d1184e759b25056fb4ed45c4957c458896354281259515f
F ext/wasm/fiddle/emscripten.css 3d253a6fdb8983a2ac983855bfbdd4b6fa1ff267c28d69513dd6ef1f289ada3f
@ -510,7 +510,7 @@ F ext/wasm/scratchpad-opfs-main.js 69e960e9161f6412fd0c30f355d4112f1894d6609eb43
F ext/wasm/scratchpad-opfs-worker.html 66c1d15d678f3bd306373d76b61c6c8aef988f61f4a8dd40185d452f9c6d2bf5
F ext/wasm/scratchpad-opfs-worker.js 3ec2868c669713145c76eb5877c64a1b20741f741817b87c907a154b676283a9
F ext/wasm/scratchpad-opfs-worker2.js 5f2237427ac537b8580b1c659ff14ad2621d1694043eaaf41ae18dbfef2e48c0
F ext/wasm/speedtest1.html e5b6ca8d09e15ab77fcec95ab30a330cc5376795f3d3b7cc05b9377b08824071
F ext/wasm/speedtest1.html a1204f5cbbd592baa191535dc7eaa2d875f661aefb2a70a4631df0a925e19f4b
F ext/wasm/split-speedtest1-script.sh a3e271938d4d14ee49105eb05567c6a69ba4c1f1293583ad5af0cd3a3779e205 x
F ext/wasm/sql/000-mandelbrot.sql 775337a4b80938ac8146aedf88808282f04d02d983d82675bd63d9c2d97a15f0
F ext/wasm/sql/001-sudoku.sql 35b7cb7239ba5d5f193bc05ec379bcf66891bce6f2a5b3879f2f78d0917299b5
@ -2016,8 +2016,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 09796cc2bfce7bc4ce11db9673d20737259e9928f0484660cba3a9751f7d01c5
R 98ebaeee264c8d9d604307fc8843d5b6
P 4441535e3e54dc1881f700fa3878964eb8554a6790fd6aa32945f7cc104a8467
R 63d4e2043bd53d54be9ba5bac375f852
U stephan
Z 0a749029dbb2383547531131a6904c9b
Z 29ed13e3400f211a842f97710dcb5fd5
# Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
4441535e3e54dc1881f700fa3878964eb8554a6790fd6aa32945f7cc104a8467
40e60f570d4f489d58d12e27c1c067b41d6c5a5e374c5fce0baa8881ef183216