Skip to content

Working (but slow) support for pthreads + memory growth#8365

Merged
kripken merged 37 commits into
incomingfrom
growableHeap
Apr 8, 2019
Merged

Working (but slow) support for pthreads + memory growth#8365
kripken merged 37 commits into
incomingfrom
growableHeap

Conversation

@kripken

@kripken kripken commented Mar 28, 2019

Copy link
Copy Markdown
Member

(in wasm - no support in asm.js)

Background: WebAssembly/design#1271

  • Add GROWABLE_HEAP_* methods that replace each load and store in JS. These safely check if memory grew, and updates the buffer and views if so. The JS optimizer is used to instrument the JS in this way, so it works for user JS as well.
  • Show a warning about the slowness during compilation.
  • Proxy emscripten_resize_heap to the main thread, and handle races where a thread asks for growth but another thread grows it to a lower or higher value meanwhile - loop until successful, or an error is hit.
  • Make sbrk threadsafe using that.
  • Rewrite brk using sbrk, to avoid writing two pieces of threadsafe code.

Blocked on https://bugs.chromium.org/p/v8/issues/detail?id=9062 , so this is not fully tested yet - it doesn't test an actual memory growth, but it does test that instrumenting the code works.

@kripken kripken requested a review from dschuff March 28, 2019 22:18
@kripken

kripken commented Mar 28, 2019

Copy link
Copy Markdown
Member Author

Ah, I see some errors on CI that I missed in the subset of tests I ran locally. I don't see a way to turn this into a draft PR - is there? Anyhow, this is still a wip.

@kripken kripken removed the request for review from dschuff March 28, 2019 23:18
pthread_t thr;

printf("start\n");
EM_ASM({ assert(HEAP8.length === 32 * 1024 * 1024, "start at 32MB") });

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.

use EM_JS?

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.

Hmm, it's just test code, and convenient to do it all on a single line? Maybe I don't see the benefit to using EM_JS here?

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.

There's no benefit, other than everything should use EM_JS unless there's a good reason not to?

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.

In general yeah, but it's just not inline, so it's more typing and less immediately obvious to the reader - the only advantages of EM_ASM, basically. I agree EM_JS is the preferred thing for most normal code though.

@bvibber

bvibber commented Mar 30, 2019

Copy link
Copy Markdown
Collaborator

Yeah these are in my custom JS library mix-ins, where I either malloc a buffer and write into it or create a typed array view for a C-created buffer to take data out. I'm not doing a lot of individual loads/stores; mostly data's coming as arguments on a callback, some of which are pointers/length pairs for buffers to export, which may be beyond the end of the old buffer views.

I think it should work to run updateGlobalBufferViews() manually before making a subview directly from HEAPU8, HEAPF32 etc, but I think that's going to be error-prone as forgetting will sometimes work, sometimes not.

It might feel more natural to have view-creation accessors that wrap that check-and-update call, like:

GROWABLE_HEAP_VIEW_U32(start, end)

@kripken

kripken commented Apr 1, 2019

Copy link
Copy Markdown
Member Author

Yeah, view creators are a natural fit too, added now.

Also added docs about this.

@bvibber

bvibber commented Apr 2, 2019

Copy link
Copy Markdown
Collaborator

The view accessors look perfect, thanks!

Comment thread src/preamble.js Outdated
#endif

#if MODULARIZE
// In modularize mode the wasmMemory and others are received in an onmessage, and that

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.

should this be "in pthreads mode" rather than "modularize mode"

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.

Fixing comment.

pthread_t thr;

printf("start\n");
EM_ASM({ assert(HEAP8.length === 32 * 1024 * 1024, "start at 32MB") });

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.

There's no benefit, other than everything should use EM_JS unless there's a good reason not to?

Comment thread tests/test_browser.py
def test_pthread_growth_mainthread(self):
def run(emcc_args=[]):
# Note that this test may not pass on Chrome until 75, due to https://bugs.chromium.org/p/v8/issues/detail?id=9062
self.btest(path_from_root('tests', 'pthread', 'test_pthread_memory_growth_mainthread.c'), expected='1', args=['-s', 'USE_PTHREADS=1', '-s', 'PTHREAD_POOL_SIZE=2', '-s', 'ALLOW_MEMORY_GROWTH=1', '-s', 'TOTAL_MEMORY=32MB', '-s', 'WASM_MEM_MAX=256MB'] + emcc_args, also_asmjs=False)

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.

should test in PROXY_TO_PTHREAD mode too?

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 won't work yet because of chromium issue 9065, same as test_pthread_growth. I'll add a PROXY_TO_PTHREAD mode in that test.

Comment thread tools/js-optimizer.js
});
}

function growableHeap(ast) {

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.

should this have some comment like "transform HEAP8 etc heap accesses to calls to GROWABLE_HEAP_LOAD_I8" or something?

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.

Adding a comment.

@kripken

kripken commented Apr 5, 2019

Copy link
Copy Markdown
Member Author

I tried to use chrome unstable so that at least part of the new tests here could pass, but it fails on unrelated things on circle, so that'll need to be looked at separately. As a result, for now the new tests are disabled.

@kripken kripken merged commit e172838 into incoming Apr 8, 2019
@kripken kripken deleted the growableHeap branch April 8, 2019 21:43
VirtualTim pushed a commit to VirtualTim/emscripten that referenced this pull request May 21, 2019
…ore#8365)

(in wasm - no support in asm.js)

Background: WebAssembly/design#1271

* Add GROWABLE_HEAP_* methods that replace each load and store in JS. These safely check if memory grew, and updates the buffer and views if so. The JS optimizer is used to instrument the JS in this way, so it works for user JS as well.
* Show a warning about the slowness during compilation.
* Make sbrk etc. threadsafe.
 * Rewrite brk using sbrk, to avoid writing two pieces of threadsafe code.
VirtualTim added a commit to VirtualTim/emscripten that referenced this pull request May 23, 2019
VirtualTim added a commit to VirtualTim/emscripten that referenced this pull request May 23, 2019
belraquib pushed a commit to belraquib/emscripten that referenced this pull request Dec 23, 2020
…ore#8365)

(in wasm - no support in asm.js)

Background: WebAssembly/design#1271

* Add GROWABLE_HEAP_* methods that replace each load and store in JS. These safely check if memory grew, and updates the buffer and views if so. The JS optimizer is used to instrument the JS in this way, so it works for user JS as well.
* Show a warning about the slowness during compilation.
* Make sbrk etc. threadsafe.
 * Rewrite brk using sbrk, to avoid writing two pieces of threadsafe code.
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