From 645cd94d9a2383f973ae2fd83fef7024ae59c679 Mon Sep 17 00:00:00 2001 From: Chris Seaton Date: Wed, 7 Dec 2022 19:09:30 -0600 Subject: [PATCH] Add debug counters to RubyVM.stat (#6086) * Add debug counters to RubyVM.stat * Use SIZET2NUM Co-author: Nobuyoshi Nakada * Prefix debug_counter_names --- debug_counter.c | 8 ++++---- vm.c | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/debug_counter.c b/debug_counter.c index cbaf15d496..463bebf849 100644 --- a/debug_counter.c +++ b/debug_counter.c @@ -16,7 +16,7 @@ #if USE_DEBUG_COUNTER -static const char *const debug_counter_names[] = { +const char *const rb_debug_counter_names[] = { #define DEBUG_COUNTER_NAME_EMPTY "" /* Suppress -Wstring-concatenation */ DEBUG_COUNTER_NAME_EMPTY #undef DEBUG_COUNTER_NAME_EMPTY @@ -26,7 +26,7 @@ static const char *const debug_counter_names[] = { }; MJIT_SYMBOL_EXPORT_BEGIN -size_t rb_debug_counter[numberof(debug_counter_names)]; +size_t rb_debug_counter[numberof(rb_debug_counter_names)]; void rb_debug_counter_add_atomic(enum rb_debug_counter_type type, int add); MJIT_SYMBOL_EXPORT_END @@ -67,7 +67,7 @@ ruby_debug_counter_get(const char **names_ptr, size_t *counters_ptr) int i; if (names_ptr != NULL) { for (i=0; i27 * } * + * If USE_DEBUG_COUNTER is enabled, debug counters will be included. + * * The contents of the hash are implementation specific and may be changed in * the future. * @@ -622,6 +626,21 @@ vm_stat(int argc, VALUE *argv, VALUE self) SET(next_shape_id, (rb_serial_t)GET_VM()->next_shape_id); #undef SET +#if USE_DEBUG_COUNTER + ruby_debug_counter_show_at_exit(FALSE); + for (size_t i = 0; i < RB_DEBUG_COUNTER_MAX; i++) { + const VALUE name = rb_sym_intern_ascii_cstr(rb_debug_counter_names[i]); + const VALUE boxed_value = SIZET2NUM(rb_debug_counter[i]); + + if (key == name) { + return boxed_value; + } + else if (hash != Qnil) { + rb_hash_aset(hash, name, boxed_value); + } + } +#endif + if (!NIL_P(key)) { /* matched key should return above */ rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(key)); }