-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Preallocate web assembly workers #9394
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
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
7669b98
Adding support for pre-allocating worker threads before the webassemb…
belraquib a751593
Fix bad naming of the preallocated worker list.
belraquib 78264f4
add tay@google.com to AUTHORS
belraquib 07c3155
Start adding test for the preallocation.
belraquib 9ec2778
Change parameter for prewarm to a flag instead of a count. You can't …
belraquib 96f4a8e
Remove extra logging.
belraquib eae7353
Remove debugging logs in library_pthread.
belraquib 233271a
Update test due to new flag.
belraquib a4107ba
Remove debugging flags from the test runner.
belraquib d5f62ae
Revert "Change parameter for prewarm to a flag instead of a count. Yo…
belraquib ca22517
Revert "Change parameter for prewarm to a flag instead of a count. Yo…
belraquib 1042598
Merge branch 'incoming' of github.com:belraquib/emscripten into preal…
belraquib 76bc8ce
Merge branch 'preallocate-workers' of github.com:belraquib/emscripten…
belraquib 8027596
Merged changes from head.
belraquib 5ad937f
Removed extra debugging.
belraquib 8f5f690
Merge pull request #1 from belraquib/preallocate-workers
belraquib 4a0b3a8
add missing semicolon.
belraquib b346f36
Various fixes from code review. Comments, renames, and make the test …
belraquib e5e2051
Make the worker pool size the max of the requested size or the thread…
belraquib 865ef87
Change PREWARM_PTHREAD_POOL_WORKERS_SIZE to be PTHREAD_POOL_ONLY_PREW…
belraquib 01fc229
Fix style error in comment.
belraquib a7ba034
Merge remote-tracking branch 'upstream/incoming' into incoming
belraquib 9569d32
We don't want to use PTHREAD_POOL_ONLY_PREWARM on the pthread create …
belraquib 810c85a
Ensure we return an empty array if no workers were required. Switch t…
belraquib 37ea723
Merge remote-tracking branch 'upstream/incoming' into incoming
belraquib d76c8d6
Replace PTHREAD_POOL_ONLY_PREWARM with PTHREAD_POOL_PREALLOCATE
belraquib b44d430
Change the sense of PTHREAD_POOL_PREALLOCATE. 1 means "on" and is the…
belraquib 129aaed
Add test that allocates 50 threads 10x times. Use the default paramet…
belraquib ba3f827
Add missing test file.
belraquib e74a712
Rename PTHREAD_POOL_PREALLOCATE (default 1) to PTHREAD_POOL_DELAY_LOA…
belraquib 59dd629
Merge remote-tracking branch 'upstream/incoming' into incoming
belraquib File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // Copyright 2019 The Emscripten Authors. All rights reserved. | ||
| // Emscripten is available under two separate licenses, the MIT license and the | ||
| // University of Illinois/NCSA Open Source License. Both these licenses can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include <pthread.h> | ||
| #include <sys/types.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <assert.h> | ||
| #include <unistd.h> | ||
| #include <errno.h> | ||
| #include <emscripten.h> | ||
| #include <emscripten/threading.h> | ||
| #include <vector> | ||
|
|
||
| pthread_t threads[50]; | ||
|
|
||
| static void *thread_start(void *arg) | ||
| { | ||
| // This thread quits immediately... | ||
| pthread_exit((void*)0); | ||
| } | ||
|
|
||
| void CreateThread(int idx) { | ||
| int rc = pthread_create(&threads[idx], NULL, thread_start, (void*)idx); | ||
| assert(rc == 0); | ||
| } | ||
|
|
||
| void JoinThread(int idx) { | ||
| int rc = pthread_join(threads[idx], nullptr); | ||
| assert(rc == 0); | ||
| } | ||
|
|
||
| int main() | ||
| { | ||
| if (!emscripten_has_threading_support()) | ||
| { | ||
| #ifdef REPORT_RESULT | ||
| REPORT_RESULT(0); | ||
| #endif | ||
| printf("Skipped: Threading is not supported.\n"); | ||
| return 0; | ||
| } | ||
|
|
||
| // This test should be run with a prewarmed pool of size 50. They should be fully allocated. | ||
| assert(EM_ASM_INT(return PThread.unusedWorkers.length) == 50); | ||
|
|
||
| double total = 0; | ||
| for (int i = 0; i < 10; ++i) { | ||
| double t1 = emscripten_get_now(); | ||
| for (int j = 0; j < 50; ++j) { | ||
| CreateThread(j); | ||
| } | ||
| double t2 = emscripten_get_now(); | ||
| printf("Took %f ms to allocate 50 threads.\n", t2 - t1); | ||
| total += (t2 - t1); | ||
| // Join all the threads to clear the queue.. | ||
| for (int j = 0; j < 50; ++j) { | ||
| JoinThread(j); | ||
| } | ||
| } | ||
|
|
||
| printf("Final average %f ms.\n", total / 10.0); | ||
|
|
||
| #ifdef REPORT_RESULT | ||
| REPORT_RESULT(0); | ||
| #endif | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Copyright 2019 The Emscripten Authors. All rights reserved. | ||
| // Emscripten is available under two separate licenses, the MIT license and the | ||
| // University of Illinois/NCSA Open Source License. Both these licenses can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include <pthread.h> | ||
| #include <sys/types.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <assert.h> | ||
| #include <unistd.h> | ||
| #include <errno.h> | ||
| #include <emscripten.h> | ||
| #include <emscripten/threading.h> | ||
| #include <vector> | ||
|
|
||
| pthread_t threads[5]; | ||
|
|
||
| static void *thread_start(void *arg) | ||
| { | ||
| // This should be long enough for threads to pile up. | ||
| int idx = (int)arg; | ||
| printf("Starting thread %d\n", idx); | ||
| while (true) { | ||
| sleep(1); | ||
| } | ||
| printf("Finishing thread %d\n", idx); | ||
| pthread_exit((void*)0); | ||
| } | ||
|
|
||
| void CreateThread(int idx) { | ||
| EM_ASM(out('Main: Spawning thread '+$0+'...'), idx); | ||
| int rc = pthread_create(&threads[idx], NULL, thread_start, (void*)idx); | ||
| assert(rc == 0); | ||
| } | ||
|
|
||
| int main() | ||
| { | ||
| if (!emscripten_has_threading_support()) | ||
| { | ||
| #ifdef REPORT_RESULT | ||
| REPORT_RESULT(0); | ||
| #endif | ||
| printf("Skipped: Threading is not supported.\n"); | ||
| return 0; | ||
| } | ||
|
|
||
| // This test should be run with a prewarmed pool of size 4. None | ||
| // of the threads are allocated yet. | ||
| assert(EM_ASM_INT(return PThread.preallocatedWorkers.length) == 4); | ||
| assert(EM_ASM_INT(return PThread.unusedWorkers.length) == 0); | ||
| assert(EM_ASM_INT(return PThread.runningWorkers.length) == 0); | ||
|
|
||
| CreateThread(0); | ||
|
|
||
| // We have one running thread, allocated on demand. | ||
| assert(EM_ASM_INT(return PThread.preallocatedWorkers.length) == 3); | ||
| assert(EM_ASM_INT(return PThread.unusedWorkers.length) == 0); | ||
| assert(EM_ASM_INT(return PThread.runningWorkers.length) == 1); | ||
|
|
||
| for (int i = 1; i < 5; ++i) { | ||
| CreateThread(i); | ||
| } | ||
|
|
||
| // All the preallocated workers should be used. | ||
| // We can't join the threads or we'll hang forever. The main thread | ||
| // won't give up the thread to let the 5th thread be created. This is | ||
| // solved in non-test cases by using PROXY_TO_PTHREAD, but we can't | ||
| // do that here since we need to eval the length of the various pthread | ||
| // arrays. | ||
| assert(EM_ASM_INT(return PThread.runningWorkers.length) == 5); | ||
| assert(EM_ASM_INT(return PThread.unusedWorkers.length) == 0); | ||
|
|
||
| #ifdef REPORT_RESULT | ||
| REPORT_RESULT(0); | ||
| #endif | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think we should look at reverting/undoing this PR, the concept of preallocatedWorkers is identical to unusedWorkers, to the contrary what the comment might be stating. There is no reason why we should have two different concepts.
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 measured a speedup here, though - are you saying the measurement might have been wrong somehow? Or is there a way to achieve a similar speedup using just
unusedWorkers?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.
The idea of more eagerly loading up the Worker scripts and decoupling the passing of Wasm modules there is fine. Posted #10269 to remove the
preallocatedWorkersconstruct.