osyoyu / pf2

A sampling-based profiler for Ruby
https://rubygems.org/gems/pf2
MIT License
48 stars 3 forks source link

Use pthread_getcpuclockid for timer installation #11

Closed osyoyu closed 8 months ago

osyoyu commented 8 months ago

The timer installation procedure needed to run on the target pthread in order to use the per-thread CPU time timer. This limitation came from the fact that Ruby not exposing the underlying pthread's ID which can be passed to pthread_getcpuclockid() (note that Thread#native_thread_id returns the kernel thread ID, which is different from the pthread ID).

This patch overcomes this by copying CRuby-internal structs into our code, enabling access to rb_thread_t->nt->thread_id. We lose some robustness here since we will now need to catch up with changes on the Ruby side, but we'll gain simpler code and more predictable behavior (less callbacks!).