Skip to content

[WasmFS] Replace SyncToAsync with the proxying.h API - #16469

Merged
tlively merged 10 commits into
mainfrom
wasmfs-proxying
Apr 13, 2022
Merged

tlively merged 10 commits into
mainfrom
wasmfs-proxying

Conversation

@tlively

@tlively tlively commented Mar 11, 2022

Copy link
Copy Markdown
Member

Create a ProxyWorker utility that owns a dedicated worker thread and a
ProxyingQueue used to send work to it, then use it to replace the previous
bespoke SyncToAsync utility. This should save code size by reusing the same
underlying proxying API already used by Emscripten's low-level pthread runtime.
It also improves performance scaling by allowing multiple asynchronous tasks to
be in flight at once on the worker thread; the previous utility serialized all
proxied tasks, even if they were asynchronous.

tlively added 2 commits March 10, 2022 18:03
Add a thin C++ wrapper API to proxying.h that wraps the underlying C API in the
same file. The C++ wrapper is defined only when using C++ at least as recent as
C++11. The wrapper handles creating and destroying the underlying
em_proxying_queue objects automatically, defines proxying methods that take
`std::function` rather than void*, and wraps `em_proxying_ctx` in a class with a
`finish()` method.
Create a `ProxyWorker` utility that owns a dedicated worker thread and a
ProxyingQueue used to send work to it, then use it to replace the previous
bespoke SyncToAsync utility. This should save code size by reusing the same
underlying proxying API already used by Emscripten's low-level pthread runtime.
It also improves performance scaling by allowing multiple asynchronous tasks to
be in flight at once on the worker thread; the previous utility serialized all
proxied tasks, even if they were asynchronous.

@kripken kripken left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great simplification!


// Kill the worker thread.
~ProxyWorker() {
pthread_cancel(thread.native_handle());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to avoid using pthread_cancel if at all possible.

Can we come up with standard mechanism for sending some kind shutdown message to the queue?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One option would be to have the worker thread wait for work inside a loop using a traditional condition variable. Then it could exit the loop and shut itself down normally once we signal it to do so. But it's kind of nice to be able to have it so simply wait in the JS event loop instead. Given the need to tell another thread sitting in its event loop to shut down, wouldn't any mechanism for doing so basically be identical to what pthread_cancel does in that case?

Comment thread tools/system_libs.py
cflags = ['-fno-exceptions', '-std=c++17']

includes = ['system/lib/wasmfs']
includes = ['system/lib/wasmfs', 'system/lib/pthread']

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't be needed once we make the header public so should we wait for that change?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it makes too much difference either way.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, as long as we remember to clean this up if it does land.

sched_yield();
}

test_proxy_async();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why has this test changed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized that this new simpler pattern should work when implementing the new utility, so I changed the test to make sure that pattern was being directly tested.

em_proxying_ctx* ctx;

public:
struct ProxyingCtx {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you are using struct here because all things are public? Is that common/accepted practice. My intuition is that struct is makes sense for things that are more like POD data structures... and I'm not sure this is. But maybe my intuition is out of date.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I want the ctx member to be public and this seemed like the simplest way to do it. I don't think we have a strong guideline about when to use class or struct, but maybe we should. I can change this to a class if you prefer.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel strongly

Base automatically changed from proxying-cpp-wrapper to main March 21, 2022 16:13
@tlively

tlively commented Apr 9, 2022

Copy link
Copy Markdown
Member Author

@kripken, the problem here is that the proxying queue API is not available when -pthread is not used, which happens to cause link errors in the dlfcn_self test because it exports wasmfs_create_proxied_backend, which depends on the proxying queue API after this change.

The obvious fix is to provide a stub of the proxying queue API for when threads are not available, but what do we want the proxying backend to do in the absence of threads? There's no way it can work correctly in that situation.

@kripken

kripken commented Apr 11, 2022

Copy link
Copy Markdown
Member

@tlively

How about ifdefing out wasmfs_create_proxied_backend when threads are disabled? We do know that at compile time, and actually it would be nice to get a compile-time error if a user tries to call it in such a build.

(was that comment meant for someone else perhaps? I don't see a comment of mine that it refers to.)

@tlively

tlively commented Apr 11, 2022

Copy link
Copy Markdown
Member Author

That sounds reasonable, and we'll ifdef out the OPFS backend, too, since it will internally require threads to bridge the sync-async gap.

(I wasn't replying to a previous comment, sorry for the confusion!)

@tlively

tlively commented Apr 11, 2022

Copy link
Copy Markdown
Member Author

Actually ifdef doesn't work too well in this situation. The problem is that the failing test ends up including all system libraries, inluding all of wasmfs, into the main module (since it doesn't know what system libraries will be needed by side modules). Since part of wasmfs now depends on the proxying API, that means we need the proxying API to be defined as part of the link. One solution would be to ifdef out every use of the proxying API in wasmfs, but that's messy and fragile. Instead I've included a stub implementation of the proxying API into the single threaded link. We should still make backends that depend on threads unavailable to users at a higher level, though.

@tlively
tlively requested a review from kripken April 12, 2022 00:26

em_proxying_queue* em_proxying_queue_create() {
return NULL;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could these all abort()?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm with that (seems safer to avoid confusion later), or lgtm if there's a good reason not to as well.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where exactly are the references coming from? Perhaps we can instead use the musl trick of using weak aliases to dummy functions?

Alternately, what happens if we just include the regular version and let it call the stub pthreads function?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would almost work out-of-the-box, but it does call out to _emscripten_notify_proxying_queue in library_pthread.js, so we'd at least need to supply a stub for that. But it would still be a user error to use any of the proxying functions except for async proxying to self, which could still work.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where exactly are the references coming from? Perhaps we can instead use the musl trick of using weak aliases to dummy functions?

The references are coming from various WasmFS backends that get pulled in by --whole-archive. If we used weak aliases, how would that differ from the stub solution here. Would it be cleaner?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very little difference I guess. Using the weak aliases has the downside that it could hide real link failures by failling back to dummy functions if the real library is missing/invalid for some other reason.

@sbc100

sbc100 commented Apr 12, 2022

Copy link
Copy Markdown
Collaborator

Actually ifdef doesn't work too well in this situation. The problem is that the failing test ends up including all system libraries, inluding all of wasmfs, into the main module (since it doesn't know what system libraries will be needed by side modules). Since part of wasmfs now depends on the proxying API, that means we need the proxying API to be defined as part of the link. One solution would be to ifdef out every use of the proxying API in wasmfs, but that's messy and fragile. Instead I've included a stub implementation of the proxying API into the single threaded link. We should still make backends that depend on threads unavailable to users at a higher level, though.

The fact the some of these tests/setting cause all library to be included in with --whole-archive is becoming more of a problem over time. There are several places like there where we have to do a bunch of extra work / special casing just for these cases. I wonder if we can remove the --whole-archive at some point.

@tlively

tlively commented Apr 12, 2022

Copy link
Copy Markdown
Member Author

I updated all the functions that do anything with proxying queues to abort. This allows single-threaded applications to safely try to create or destroy queues as long as they don't try to proxy any work, which seems like a sweet spot for portability.

@tlively
tlively enabled auto-merge (squash) April 12, 2022 21:10
@tlively

tlively commented Apr 12, 2022

Copy link
Copy Markdown
Member Author

Hmm, that browser.test_wasmfs_fetch_backend_proxy_to_pthread failure seems real. Testing it locally gives

Uncaught (in promise) ReferenceError: runtimeKeepalivePop is not defined
    at __wasmfs_jsimpl_async_alloc_file (test.js:2678:7)

@tlively

tlively commented Apr 12, 2022

Copy link
Copy Markdown
Member Author

@sbc100 do you know what happened to runtimeKeepalivePop?

@sbc100

sbc100 commented Apr 12, 2022

Copy link
Copy Markdown
Collaborator

@sbc100 do you know what happened to runtimeKeepalivePop?

Its a library function so any callers need to explicitly include it in their __deps .. perhaps it was missing but being included indirectly somewhere before this change?

@tlively

tlively commented Apr 13, 2022

Copy link
Copy Markdown
Member Author

Yep, adding the deps fixed it locally. Thanks!

@tlively
tlively merged commit 0149bcd into main Apr 13, 2022
@tlively
tlively deleted the wasmfs-proxying branch April 13, 2022 01:40
tlively added a commit that referenced this pull request Apr 14, 2022
I accidentally included this file in #16469.
tlively added a commit that referenced this pull request Apr 14, 2022
This file was accidentally included in #16469.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants