emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

Name pthread workers for easier debugging

Open psmarshall opened this issue 5 years ago • 12 comments

This is a feature suggestion.

Workers accept an optional name which can be used for debugging, e.g. in DevTools we show the name in the Sources panel. Screen Shot 2020-09-15 at 10 55 05 am Screen Shot 2020-09-15 at 10 55 12 am

The name is also available to the worker via globalThis.name.

My suggestion is that in src/library_pthread.js, in allocateUnusedWorker when we call new Worker(), we give a useful name like pthread #1 etc. Right now threads just show up with the file name (which also means all the threads have the same name).

psmarshall avatar Sep 15 '20 09:09 psmarshall

Nice. We could also support https://man7.org/linux/man-pages/man3/pthread_setname_np.3.html ?

sbc100 avatar Sep 15 '20 12:09 sbc100

I would like to implement this feature!

sabtt17 avatar Apr 07 '21 04:04 sabtt17

It looks like we already have emscripten_set_thread_name in the public C API and then we have PThread.getThreadName and PThread.setThreadName in the JS API.

However it doesn't look like we set globalThis.name based on this name.

On simple thing to do would be to set globalThis.name during PThread.setThreadName if the name is being set for the current thread. If the name is being set of a different thread is a little more tricky.. we would need to set a message to that thread to change its own name.

We could also add pthread_setname_np as a more standard way to set the name, rather than the emscripten-specific emscripten_set_thread_name

sbc100 avatar Apr 07 '21 14:04 sbc100

Has this issue been resolved?

sabtt17 avatar Apr 21 '21 02:04 sabtt17

@sabtt17 I don't think work has been done here - it's free to work on.

Adding a simple name to new Worker would be a good start. @sbc100 's additional suggestions are also worth doing but those could be separate.

kripken avatar Apr 21 '21 20:04 kripken

I started working on this, but I haven't been able to run pthread tests locally. After running ./tests/runner.py pthread, I keep getting:

ERROR: could not find the following tests: pthread

even though the directory exists, and running the same command with browser works fine. Could this be a problem with my installation?

sabtt17 avatar Apr 23 '21 01:04 sabtt17

@sabtt17

Tests need to be run with their full name, which starts with test_. For example, ./tests/runner.py test_pthread_create will run the test with that specific name. Wildcards also work, so many of the pthreads tests can be run with ./tests/runner.py test_pthread* (for the shell versions) or ./tests/runner.py browser.test_pthread* (for the browser versions).

The reason ./tests/runner.py browser works is that "browser" is an entire suite of tests; browser is the same as browser.*, but there isn't a separate suite for pthreads. (Browser tests have their own suite because they are run very differently than shell tests - we spin up a browser and a webserver etc.)

kripken avatar Apr 23 '21 15:04 kripken

Thanks! That worked!

sabtt17 avatar Apr 24 '21 21:04 sabtt17

FYI the debugging story for naming web workers isn't all in place yet for this feature request to really work (in either Chrome, FF or Safari), but I've kicked off discussion of it with the Chrome DevTools folks.

connorjclark avatar Apr 12 '22 01:04 connorjclark

I just also stumbled across this, when the linker complained that it wasn't able to find pthread_setname_np . I now replaced it with the emscripten_set_thread_name and just hope that it will work somehow, but it would be nice if the standard API could be supported in the future.

But this could probably wait until support from browser vendors is really there.

major-mayer avatar May 31 '22 09:05 major-mayer

Is it straightforward to implement pthread_getname_np? I was compiling emilk/loguru with emscripten and ran into an undefined symbol error for pthread_getname_np

jspanchu avatar Feb 27 '24 17:02 jspanchu

Sadly neither FF or chrome support changing the name of a thread after its created. Chrome will show the name of thread, but it is currently fixed at thread creation time. So I don't think we can fix this until https://github.com/whatwg/html/issues/7838 is resolved.

sbc100 avatar Jul 12 '24 17:07 sbc100