Add debug counters to RubyVM.stat (#6086)

* Add debug counters to RubyVM.stat

* Use SIZET2NUM

Co-author: Nobuyoshi Nakada <nobu@ruby-lang.org>

* Prefix debug_counter_names
This commit is contained in:
Chris Seaton 2022-12-07 19:09:30 -06:00 committed by GitHub
parent a2d3f5606a
commit 645cd94d9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
Notes: git 2022-12-08 01:09:49 +00:00
Merged-By: k0kubun <takashikkbn@gmail.com>
2 changed files with 23 additions and 4 deletions

View File

@ -16,7 +16,7 @@
#if USE_DEBUG_COUNTER #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 */ #define DEBUG_COUNTER_NAME_EMPTY "" /* Suppress -Wstring-concatenation */
DEBUG_COUNTER_NAME_EMPTY DEBUG_COUNTER_NAME_EMPTY
#undef DEBUG_COUNTER_NAME_EMPTY #undef DEBUG_COUNTER_NAME_EMPTY
@ -26,7 +26,7 @@ static const char *const debug_counter_names[] = {
}; };
MJIT_SYMBOL_EXPORT_BEGIN 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); void rb_debug_counter_add_atomic(enum rb_debug_counter_type type, int add);
MJIT_SYMBOL_EXPORT_END MJIT_SYMBOL_EXPORT_END
@ -67,7 +67,7 @@ ruby_debug_counter_get(const char **names_ptr, size_t *counters_ptr)
int i; int i;
if (names_ptr != NULL) { if (names_ptr != NULL) {
for (i=0; i<RB_DEBUG_COUNTER_MAX; i++) { for (i=0; i<RB_DEBUG_COUNTER_MAX; i++) {
names_ptr[i] = debug_counter_names[i]; names_ptr[i] = rb_debug_counter_names[i];
} }
} }
if (counters_ptr != NULL) { if (counters_ptr != NULL) {
@ -97,7 +97,7 @@ rb_debug_counter_show_results(const char *msg)
fprintf(stderr, "[RUBY_DEBUG_COUNTER]\t%d %s\n", getpid(), msg); fprintf(stderr, "[RUBY_DEBUG_COUNTER]\t%d %s\n", getpid(), msg);
for (i=0; i<RB_DEBUG_COUNTER_MAX; i++) { for (i=0; i<RB_DEBUG_COUNTER_MAX; i++) {
fprintf(stderr, "[RUBY_DEBUG_COUNTER]\t%-30s\t%'14"PRIuSIZE"\n", fprintf(stderr, "[RUBY_DEBUG_COUNTER]\t%-30s\t%'14"PRIuSIZE"\n",
debug_counter_names[i], rb_debug_counter_names[i],
rb_debug_counter[i]); rb_debug_counter[i]);
} }
} }

19
vm.c
View File

@ -66,6 +66,8 @@ MJIT_FUNC_EXPORTED
#endif #endif
VALUE vm_exec(rb_execution_context_t *, bool); VALUE vm_exec(rb_execution_context_t *, bool);
extern const char *const rb_debug_counter_names[];
PUREFUNC(static inline const VALUE *VM_EP_LEP(const VALUE *)); PUREFUNC(static inline const VALUE *VM_EP_LEP(const VALUE *));
static inline const VALUE * static inline const VALUE *
VM_EP_LEP(const VALUE *ep) VM_EP_LEP(const VALUE *ep)
@ -578,6 +580,8 @@ rb_dtrace_setup(rb_execution_context_t *ec, VALUE klass, ID id,
* :global_cvar_state=>27 * :global_cvar_state=>27
* } * }
* *
* If <tt>USE_DEBUG_COUNTER</tt> is enabled, debug counters will be included.
*
* The contents of the hash are implementation specific and may be changed in * The contents of the hash are implementation specific and may be changed in
* the future. * 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); SET(next_shape_id, (rb_serial_t)GET_VM()->next_shape_id);
#undef SET #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 */ if (!NIL_P(key)) { /* matched key should return above */
rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(key)); rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(key));
} }