diff options
author | Jean Boussier <jean.boussier@gmail.com> | 2022-06-15 14:37:41 +0200 |
---|---|---|
committer | Jean Boussier <jean.boussier@gmail.com> | 2022-06-17 09:08:26 +0200 |
commit | b6c1e1158d71b533b255ae7a2731598455918071 (patch) | |
tree | 9b3fdf4eb4a54e56d77ec7732ff079bdd9f896bc /ext/-test- | |
parent | 20d4168250fb1f534cf2db7c44998b25252a70f8 (diff) | |
download | ruby-b6c1e1158d71b533b255ae7a2731598455918071.tar.gz |
GVL Instrumentation API: add STARTED and EXITED events
[Feature #18339]
After experimenting with the initial version of the API I figured there is a need
for an exit event to cleanup instrumentation data. e.g. if you record data in a
{thread_id -> data} table, you need to free associated data when a thread goes away.
Diffstat (limited to 'ext/-test-')
-rw-r--r-- | ext/-test-/thread/instrumentation/instrumentation.c | 44 |
1 files changed, 30 insertions, 14 deletions
diff --git a/ext/-test-/thread/instrumentation/instrumentation.c b/ext/-test-/thread/instrumentation/instrumentation.c index c298d76ad6..1251029327 100644 --- a/ext/-test-/thread/instrumentation/instrumentation.c +++ b/ext/-test-/thread/instrumentation/instrumentation.c @@ -2,22 +2,30 @@ #include "ruby/atomic.h" #include "ruby/thread.h" -static rb_atomic_t acquire_enter_count = 0; -static rb_atomic_t acquire_exit_count = 0; -static rb_atomic_t release_count = 0; +static rb_atomic_t started_count = 0; +static rb_atomic_t ready_count = 0; +static rb_atomic_t resumed_count = 0; +static rb_atomic_t suspended_count = 0; +static rb_atomic_t exited_count = 0; void ex_callback(rb_event_flag_t event, const rb_internal_thread_event_data_t *event_data, void *user_data) { switch(event) { + case RUBY_INTERNAL_THREAD_EVENT_STARTED: + RUBY_ATOMIC_INC(started_count); + break; case RUBY_INTERNAL_THREAD_EVENT_READY: - RUBY_ATOMIC_INC(acquire_enter_count); + RUBY_ATOMIC_INC(ready_count); break; case RUBY_INTERNAL_THREAD_EVENT_RESUMED: - RUBY_ATOMIC_INC(acquire_exit_count); + RUBY_ATOMIC_INC(resumed_count); break; case RUBY_INTERNAL_THREAD_EVENT_SUSPENDED: - RUBY_ATOMIC_INC(release_count); + RUBY_ATOMIC_INC(suspended_count); + break; + case RUBY_INTERNAL_THREAD_EVENT_EXITED: + RUBY_ATOMIC_INC(exited_count); break; } } @@ -27,19 +35,23 @@ static rb_internal_thread_event_hook_t * single_hook = NULL; static VALUE thread_counters(VALUE thread) { - VALUE array = rb_ary_new2(3); - rb_ary_push(array, UINT2NUM(acquire_enter_count)); - rb_ary_push(array, UINT2NUM(acquire_exit_count)); - rb_ary_push(array, UINT2NUM(release_count)); + VALUE array = rb_ary_new2(5); + rb_ary_push(array, UINT2NUM(started_count)); + rb_ary_push(array, UINT2NUM(ready_count)); + rb_ary_push(array, UINT2NUM(resumed_count)); + rb_ary_push(array, UINT2NUM(suspended_count)); + rb_ary_push(array, UINT2NUM(exited_count)); return array; } static VALUE thread_reset_counters(VALUE thread) { - RUBY_ATOMIC_SET(acquire_enter_count, 0); - RUBY_ATOMIC_SET(acquire_exit_count, 0); - RUBY_ATOMIC_SET(release_count, 0); + RUBY_ATOMIC_SET(started_count, 0); + RUBY_ATOMIC_SET(ready_count, 0); + RUBY_ATOMIC_SET(resumed_count, 0); + RUBY_ATOMIC_SET(suspended_count, 0); + RUBY_ATOMIC_SET(exited_count, 0); return Qtrue; } @@ -48,7 +60,11 @@ thread_register_callback(VALUE thread) { single_hook = rb_internal_thread_add_event_hook( *ex_callback, - RUBY_INTERNAL_THREAD_EVENT_READY | RUBY_INTERNAL_THREAD_EVENT_RESUMED | RUBY_INTERNAL_THREAD_EVENT_SUSPENDED, + RUBY_INTERNAL_THREAD_EVENT_STARTED | + RUBY_INTERNAL_THREAD_EVENT_READY | + RUBY_INTERNAL_THREAD_EVENT_RESUMED | + RUBY_INTERNAL_THREAD_EVENT_SUSPENDED | + RUBY_INTERNAL_THREAD_EVENT_EXITED, NULL ); |