-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[wasm-mt] Use asset loading for dotnet.worker.js; update WasmAppBuilder #73697
Conversation
We want to use our own allocateUnusedWorker because we want to load `dotnet.worker.js` using our asset loading machinery. Unfortunately, Emscripten first calls allocateUnusedWorker very early (from `PThread.init`) to pre-allocate the pthread worker pool. So we set Emscripten's own pthread worker pool to size 0 and make our own. This requires calling `loadWasmModuleToWorker` during our startup because Emscripten deletes their code that normally does it (in "receiveInstance" in "createWasm" in "emscripten/src/preamble.js") when the pthread pool size is 0. Also added a pthreadPoolSize field to MonoConfig to allow specifying the initial pthread pool size in mono-config.json
IncludeThreadsWorker adds the js-module-threads asset to the mono-config PThreadPoolSize can be -1 or >=0 to specify the number of workers that will be pre-allocated at startup for the pthread worker pool. -1 means use the default compiled into dotnet.js
Makes the WasmAppBuilder include the threads worker module
Tagging subscribers to 'arch-wasm': @lewing Issue DetailsEnables using asset loading to get the
|
/azp run runtime-wasm |
Azure Pipelines successfully started running 1 pipeline(s). |
/azp run runtime-wasm |
Azure Pipelines successfully started running 1 pipeline(s). |
@pavelsavara @radical any more feedback? Anything for WasmApp.targets or the Task? |
src/mono/wasm/runtime/pthreads/shared/emscripten-replacements.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: Marek Fišera <mara@neptuo.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not very familiar with src/mono/wasm/runtime
, so I can't much review those changes. Rest of it looks fine. I would suggest running runtime-wasm
though, and if the tests pass then it should be good to merge 👍
/azp run runtime-wasm |
Azure Pipelines successfully started running 1 pipeline(s). |
const asset = resolve_asset_path("js-module-threads"); | ||
const uri = asset.resolvedUrl; | ||
mono_assert(uri !== undefined, "could not resolve the uri for the js-module-threads asset"); | ||
const worker = new Worker(uri); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should subscribe onError here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and maybe proxy the console in the worker, similar way how it was done in crypto worker. See how we passed the json.stringify(runtimeHelpers.config) and then only forward console when enabled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should subscribe onError here
Emscripten installs an onerror handler of their own that will overwrite ours. we can set one up later (in afterLoadWasmModuleToWorker
) but we should probably not use the worker onerror handler for anything that we want to handle - we should catch exceptions around our code and post it using our custom message channel. We have a task to do that in the tracking issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and maybe proxy the console in the worker, similar way how it was done in crypto worker. See how we passed the json.stringify(runtimeHelpers.config) and then only forward console when enabled.
ok, i'll see if I can add that over the weekend; if not, there's a task to do it in the tracking issue
@@ -125,7 +125,7 @@ | |||
<_EmccCommonFlags Condition="'$(WasmEnableSIMD)' == 'true'" Include="-msimd128" /> | |||
<_EmccCommonFlags Condition="'$(MonoWasmThreads)' == 'true'" Include="-s USE_PTHREADS=1" /> | |||
<_EmccLinkFlags Condition="'$(MonoWasmThreads)' == 'true'" Include="-Wno-pthreads-mem-growth" /> | |||
<_EmccLinkFlags Condition="'$(MonoWasmThreads)' == 'true'" Include="-s PTHREAD_POOL_SIZE=4" /> | |||
<_EmccLinkFlags Condition="'$(MonoWasmThreads)' == 'true'" Include="-s PTHREAD_POOL_SIZE=0" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't know if this is the case when the flag would be written into .rsp file in workload and could not be switched back on customer machine. just asking. And maybe it's OK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea if they use wasm-tools, they will inherit this flag. I'm not sure if it's possible to override it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Co-authored-by: Ankit Jain <radical@gmail.com>
use a new MonoThreadMessageApplyMonoConfig message to send the MonoConfig from the main thread to each worker when the workers set up the communication channel to the main thread. then if the diagnosticTracing property is true, redirect the worker console logging to a websocket. Fixes dotnet#72606
Added console proxying. verified that it gets set up in the |
/azp run runtime-wasm |
Azure Pipelines successfully started running 1 pipeline(s). |
Failures seem to be #73901. |
Enables using asset loading to get the
dotnet.worker.js
file that provides the emscripten pthread worker code.Also allows specifying the number of pre-allocated workers that will be created at startup using MSBuild properties.
Fixes #68509 and fixes #68397 and fixes #72606
Override Emscripten
PThread.allocateUnusedWorker
We want to use our own allocateUnusedWorker because we want to load
dotnet.worker.js
using our asset loading machinery.Unfortunately, Emscripten first calls allocateUnusedWorker very early (from
PThread.init
) to pre-allocate the pthread worker pool.So we set Emscripten's own pthread worker pool to size 0 and make our own. This requires calling
loadWasmModuleToWorker
during our startup because Emscripten deletes their code that normally does it (in "receiveInstance" in "createWasm" in "emscripten/src/preamble.js") when the pthread pool size is 0.Also added a pthreadPoolSize field to MonoConfig to allow specifying the initial pthread pool size in mono-config.json
Add
IncludeThreadsWorker
andPThreadPoolSize
props to WasmAppBuilderIncludeThreadsWorker
adds the"js-module-threads"
asset to themono-config.json
PThreadPoolSize
can be -1 or >=0 to specify the number of workers that will be pre-allocated at startup for the pthread worker pool. -1 means use the default compiled intodotnet.js
Reorganize the pthreads TS code by moving
Internals
(access API that digs through Emscripten's pthreads implementation) to its own module. And add types.Replace emscripten's
allocateUnusedWorker
function with our own that goes through the asset loading API.Update samples
Set up console proxying for the workers.
This is done by sending a message from the main thread to the pthread workers with the current
MonoConfig
on ourdedicated channel. (This means the proxying is setup asynchronously, so if the worker is busy before it receives the message, it may not start redirecting messages right away).