ivoanjo / gvl-tracing

Get a timeline view of Global VM Lock usage in your Ruby app
MIT License
114 stars 7 forks source link

Map native thread ids with Ruby thread names? #9

Closed byroot closed 1 year ago

byroot commented 1 year ago

Ruby 3.2 added Thread#native_thread_id

When available I think we could use that to find the Ruby thread and provide more information about the thread.

If Thread#name returns something we could use that, otherwise we can probably report the location of its first frame like Thread#inspect.

cc @benoittgt

ivoanjo commented 1 year ago

I like that! I took a very simple stab at it in 944d4d3aa3c5d42225641340d914456b06762a25 but definitely think it'd be interesting to add more.

I guess the main constraint here is being careful that we only read Thread#name or Thread#inspect when we're holding the GVL (or its modern equivalent) as from what I could tell, not all the callbacks get triggered when that happens (...right?).

But definitely suggestions are welcome!

benoittgt commented 1 year ago

Good idea

Also with the native extension (https://github.com/ivoanjo/gvl-tracing/blob/master/ext/gvl_tracing_native_extension/gvl_tracing.c#L90). How much can we reuse from Thread#to_s code?

byroot commented 1 year ago

I guess the main constraint here is being careful that we only read Thread#name or Thread#inspect when we're holding the GVL

Absolutely. Ideally we'd just dump the native thread id in the file, and only later do the mapping / rewrite. Some epehmeral threads may have been GCed, but I guess that's acceptable (even more so because ephemeral threads are recycled, so they won't show up properly anyway).

ivoanjo commented 1 year ago

Fixed in #12