There is a central macro in CRuby, RUBY_VM_CHECK_INTS, which is a very hot path for the Ruby runtime. It’s an important part of how threads are managed, and it’s called constantly. I was curious just how often it was called, and it turned out CRuby comes with some handy debugging functionality for just this scenario.
Inside of debug_counter.h, I changed #define USE_DEBUG_COUNTER 0 to #define USE_DEBUG_COUNTER 1 and added this line later in that file:
RB_DEBUG_COUNTER(rb_vm_check_ints)
Then inside vm_core.h I updated RUBY_VM_CHECK_INTS to add a debug increment:
#define RUBY_VM_CHECK_INTS(ec) rb_vm_check_ints(ec)
static inline void
rb_vm_check_ints(rb_execution_context_t *ec)
{
RB_DEBUG_COUNTER_INC(rb_vm_check_ints); // increment!
After that I ran the following simple Ruby program:
10_000.times {}
And this was printed after it ran:
[RUBY_DEBUG_COUNTER] rb_vm_check_ints 21,055
Iterating a loop ten thousand times results in twenty thousand calls to RUBY_VM_CHECK_INTS, exactly what I was looking to measure!
I’d like to know the proper configuration to compile without having to manually modify USE_DEBUG_COUNTER in the header file. Maybe someone can comment and let me know how? It has something to do with CFLAGS, I think.
Update 12/5/24 Thanks to Mohit Sindhwani for some advice on how to add the CFLAGS!
