-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[wip] Allow callbacks to be registered for GVL related events #119
Draft
casperisfine
wants to merge
11
commits into
master
Choose a base branch
from
mvh-jb-gvl-callbacks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c3fa0fd
[wip] Allow callbacks to be registered for GVL related events
eightbitraptor e0c541b
Basic unregister GVL callbacks API (thread unsafe)
byroot 4d48cfe
Make rb_gvl hook API thread safe
byroot 316c44d
Call the gvl hooks
byroot cd2222b
Move GVL instrumentation tests in their own files
byroot 720262b
Make gvl->waiting atomic
byroot b867e19
Hard crash on pthread_rwlock errors
byroot 1f599a6
Stub rb_gvl_event_* on win32
byroot ed89a1b
More pthread_rwlock error handling
byroot 5c9e48c
Fix depend
byroot fed36e0
Fix callback type
byroot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
# AUTOGENERATED DEPENDENCIES START | ||
instrumentation.o: $(RUBY_EXTCONF_H) | ||
instrumentation.o: $(arch_hdrdir)/ruby/config.h | ||
instrumentation.o: $(hdrdir)/ruby/assert.h | ||
instrumentation.o: $(hdrdir)/ruby/atomic.h | ||
instrumentation.o: $(hdrdir)/ruby/backward.h | ||
instrumentation.o: $(hdrdir)/ruby/backward/2/assume.h | ||
instrumentation.o: $(hdrdir)/ruby/backward/2/attributes.h | ||
instrumentation.o: $(hdrdir)/ruby/backward/2/bool.h | ||
instrumentation.o: $(hdrdir)/ruby/backward/2/inttypes.h | ||
instrumentation.o: $(hdrdir)/ruby/backward/2/limits.h | ||
instrumentation.o: $(hdrdir)/ruby/backward/2/long_long.h | ||
instrumentation.o: $(hdrdir)/ruby/backward/2/stdalign.h | ||
instrumentation.o: $(hdrdir)/ruby/backward/2/stdarg.h | ||
instrumentation.o: $(hdrdir)/ruby/defines.h | ||
instrumentation.o: $(hdrdir)/ruby/intern.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/anyargs.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/arithmetic.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/arithmetic/char.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/arithmetic/double.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/arithmetic/fixnum.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/arithmetic/gid_t.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/arithmetic/int.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/arithmetic/intptr_t.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/arithmetic/long.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/arithmetic/long_long.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/arithmetic/mode_t.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/arithmetic/off_t.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/arithmetic/pid_t.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/arithmetic/short.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/arithmetic/size_t.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/arithmetic/st_data_t.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/arithmetic/uid_t.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/assume.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/alloc_size.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/artificial.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/cold.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/const.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/constexpr.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/deprecated.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/diagnose_if.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/enum_extensibility.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/error.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/flag_enum.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/forceinline.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/format.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/maybe_unused.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/noalias.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/nodiscard.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/noexcept.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/noinline.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/nonnull.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/noreturn.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/pure.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/restrict.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/returns_nonnull.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/warning.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/attr/weakref.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/cast.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/compiler_is.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/compiler_is/apple.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/compiler_is/clang.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/compiler_is/gcc.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/compiler_is/intel.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/compiler_is/msvc.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/compiler_is/sunpro.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/compiler_since.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/config.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/constant_p.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/core.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/core/rarray.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/core/rbasic.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/core/rbignum.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/core/rclass.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/core/rdata.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/core/rfile.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/core/rhash.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/core/robject.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/core/rregexp.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/core/rstring.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/core/rstruct.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/core/rtypeddata.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/ctype.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/dllexport.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/dosish.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/error.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/eval.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/event.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/fl_type.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/gc.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/glob.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/globals.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/has/attribute.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/has/builtin.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/has/c_attribute.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/has/cpp_attribute.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/has/declspec_attribute.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/has/extension.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/has/feature.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/has/warning.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/array.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/bignum.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/class.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/compar.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/complex.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/cont.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/dir.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/enum.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/enumerator.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/error.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/eval.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/file.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/gc.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/hash.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/io.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/load.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/marshal.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/numeric.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/object.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/parse.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/proc.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/process.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/random.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/range.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/rational.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/re.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/ruby.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/select.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/select/largesize.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/signal.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/sprintf.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/string.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/struct.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/thread.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/time.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/variable.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/intern/vm.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/interpreter.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/iterator.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/memory.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/method.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/module.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/newobj.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/rgengc.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/scan_args.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/special_consts.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/static_assert.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/stdalign.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/stdbool.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/symbol.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/value.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/value_type.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/variable.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/warning_push.h | ||
instrumentation.o: $(hdrdir)/ruby/internal/xmalloc.h | ||
instrumentation.o: $(hdrdir)/ruby/missing.h | ||
instrumentation.o: $(hdrdir)/ruby/ruby.h | ||
instrumentation.o: $(hdrdir)/ruby/st.h | ||
instrumentation.o: $(hdrdir)/ruby/subst.h | ||
instrumentation.o: $(hdrdir)/ruby/thread.h | ||
instrumentation.o: $(hdrdir)/ruby/thread_native.h | ||
instrumentation.o: instrumentation.c | ||
# AUTOGENERATED DEPENDENCIES END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# frozen_string_literal: false | ||
create_makefile("-test-/gvl/instrumentation") |
95 changes: 95 additions & 0 deletions
95
ext/-test-/gvl/instrumentation/instrumentation/instrumentation.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include "ruby/ruby.h" | ||
#include "ruby/atomic.h" | ||
#include "ruby/thread.h" | ||
#include "ruby/thread_native.h" | ||
|
||
static rb_atomic_t acquire_enter_count = 0; | ||
static rb_atomic_t acquire_exit_count = 0; | ||
static rb_atomic_t release_count = 0; | ||
|
||
void | ||
ex_callback(rb_event_flag_t event, gvl_hook_event_args_t args) | ||
{ | ||
switch(event) { | ||
case RUBY_INTERNAL_EVENT_GVL_ACQUIRE_ENTER: | ||
RUBY_ATOMIC_INC(acquire_enter_count); | ||
break; | ||
case RUBY_INTERNAL_EVENT_GVL_ACQUIRE_EXIT: | ||
RUBY_ATOMIC_INC(acquire_exit_count); | ||
break; | ||
case RUBY_INTERNAL_EVENT_GVL_RELEASE: | ||
RUBY_ATOMIC_INC(release_count); | ||
break; | ||
} | ||
} | ||
|
||
static gvl_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)); | ||
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); | ||
return Qtrue; | ||
} | ||
|
||
static VALUE | ||
thread_register_gvl_callback(VALUE thread) | ||
{ | ||
single_hook = rb_gvl_event_new( | ||
*ex_callback, | ||
RUBY_INTERNAL_EVENT_GVL_ACQUIRE_ENTER | RUBY_INTERNAL_EVENT_GVL_ACQUIRE_EXIT | RUBY_INTERNAL_EVENT_GVL_RELEASE | ||
); | ||
|
||
return Qnil; | ||
} | ||
|
||
static VALUE | ||
thread_unregister_gvl_callback(VALUE thread) | ||
{ | ||
if (single_hook) { | ||
rb_gvl_event_delete(single_hook); | ||
single_hook = NULL; | ||
} | ||
|
||
return Qnil; | ||
} | ||
|
||
static VALUE | ||
thread_register_and_unregister_gvl_callback(VALUE thread) | ||
{ | ||
gvl_hook_t * hooks[5]; | ||
for (int i = 0; i < 5; i++) { | ||
hooks[i] = rb_gvl_event_new(*ex_callback, RUBY_INTERNAL_EVENT_GVL_ACQUIRE_ENTER); | ||
} | ||
|
||
if (!rb_gvl_event_delete(hooks[4])) return Qfalse; | ||
if (!rb_gvl_event_delete(hooks[0])) return Qfalse; | ||
if (!rb_gvl_event_delete(hooks[3])) return Qfalse; | ||
if (!rb_gvl_event_delete(hooks[2])) return Qfalse; | ||
if (!rb_gvl_event_delete(hooks[1])) return Qfalse; | ||
return Qtrue; | ||
} | ||
|
||
void | ||
Init_instrumentation(void) | ||
{ | ||
VALUE mBug = rb_define_module("Bug"); | ||
VALUE klass = rb_define_module_under(mBug, "GVLInstrumentation"); | ||
rb_define_singleton_method(klass, "counters", thread_counters, 0); | ||
rb_define_singleton_method(klass, "reset_counters", thread_reset_counters, 0); | ||
rb_define_singleton_method(klass, "register_callback", thread_register_gvl_callback, 0); | ||
rb_define_singleton_method(klass, "unregister_callback", thread_unregister_gvl_callback, 0); | ||
rb_define_singleton_method(klass, "register_and_unregister_callbacks", thread_register_and_unregister_gvl_callback, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: false | ||
class TestGVLInstrumentation < Test::Unit::TestCase | ||
def setup | ||
pend("TODO: No windows support yet") if /mswin|mingw|bccwin/ =~ RUBY_PLATFORM | ||
end | ||
|
||
def test_gvl_instrumentation | ||
require '-test-/gvl/instrumentation' | ||
Bug::GVLInstrumentation.reset_counters | ||
Bug::GVLInstrumentation::register_callback | ||
|
||
begin | ||
threads = 5.times.map { Thread.new { sleep 0.05; 1 + 1; sleep 0.02 } } | ||
threads.each(&:join) | ||
Bug::GVLInstrumentation.counters.each do |c| | ||
assert_predicate c,:nonzero? | ||
end | ||
ensure | ||
Bug::GVLInstrumentation::unregister_callback | ||
end | ||
end | ||
|
||
def test_gvl_instrumentation_fork_safe | ||
skip "No fork()" unless Process.respond_to?(:fork) | ||
|
||
require '-test-/gvl/instrumentation' | ||
Bug::GVLInstrumentation::register_callback | ||
|
||
begin | ||
pid = fork do | ||
Bug::GVLInstrumentation.reset_counters | ||
threads = 5.times.map { Thread.new { sleep 0.05; 1 + 1; sleep 0.02 } } | ||
threads.each(&:join) | ||
Bug::GVLInstrumentation.counters.each do |c| | ||
assert_predicate c,:nonzero? | ||
end | ||
end | ||
_, status = Process.wait2(pid) | ||
assert_predicate status, :success? | ||
ensure | ||
Bug::GVLInstrumentation::unregister_callback | ||
end | ||
end | ||
|
||
def test_gvl_instrumentation_unregister | ||
require '-test-/gvl/instrumentation' | ||
assert Bug::GVLInstrumentation::register_and_unregister_callbacks | ||
end | ||
end | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's just me, but I find using hex for bit flags and bit masks is much harder to read than using left shifts for bit flags and
&
ing bit flags for bit masks. 😅There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's just you :) I just figured I'd match the existing pattern.