From cdc614cd0a21b2e72d953438438c78bafdfd9f80 Mon Sep 17 00:00:00 2001 From: ko1 Date: Tue, 25 Sep 2018 18:13:29 +0000 Subject: [PATCH] refactoring debug_counter. * debug_counter.h: add comments for each counters. * debug_counter.h: add some counters (see added comments for details). * obj_newobj * obj_newobj_slowpath * obj_newobj_wb_unprotected * obj_hash_empty * obj_hash_under4 * obj_hash_ge4 * obj_hash_ge8 * heap_xmalloc * heap_xrealloc * heap_xfree * gc.c: add some debug counters (see the above list). * debug_counter.c (rb_debug_counter_show_results): accept a header message. * signal.c (ruby_default_signal): show debug counter results and malloc info (rb_malloc_info_show_results()) before SIGNAL exit. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64841 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- debug_counter.c | 26 +++++++++++---- debug_counter.h | 88 ++++++++++++++++++++++++++++++++++++++++++++----- gc.c | 34 ++++++++++++++++--- signal.c | 8 +++++ 4 files changed, 137 insertions(+), 19 deletions(-) diff --git a/debug_counter.c b/debug_counter.c index 6000f98c54..7225b6a08e 100644 --- a/debug_counter.c +++ b/debug_counter.c @@ -9,9 +9,9 @@ **********************************************************************/ #include "debug_counter.h" -#include - #if USE_DEBUG_COUNTER +#include +#include #include "internal.h" static const char *const debug_counter_names[] = { @@ -23,19 +23,33 @@ static const char *const debug_counter_names[] = { size_t rb_debug_counter[numberof(debug_counter_names)]; -__attribute__((destructor)) -static void -rb_debug_counter_show_results(void) +void +rb_debug_counter_show_results(const char *msg) { const char *env = getenv("RUBY_DEBUG_COUNTER_DISABLE"); + + setlocale(LC_NUMERIC, ""); + if (env == NULL || strcmp("1", env) != 0) { int i; + fprintf(stderr, "[RUBY_DEBUG_COUNTER]\t%d %s\n", getpid(), msg); for (i=0; ias.hash.ntbl) { st_free_table(RANY(obj)->as.hash.ntbl); + + if (RHASH_SIZE(obj) >= 8) { + RB_DEBUG_COUNTER_INC(obj_hash_ge8); + } + if (RHASH_SIZE(obj) >= 4) { + RB_DEBUG_COUNTER_INC(obj_hash_ge4); + } + else { + RB_DEBUG_COUNTER_INC(obj_hash_under4); + } } + else { + RB_DEBUG_COUNTER_INC(obj_hash_empty); + } break; case T_REGEXP: if (RANY(obj)->as.regexp.ptr) { @@ -7959,6 +7977,7 @@ objspace_xmalloc0(rb_objspace_t *objspace, size_t size) size = objspace_malloc_prepare(objspace, size); TRY_WITH_GC(mem = malloc(size)); + RB_DEBUG_COUNTER_INC(heap_xmalloc); return objspace_malloc_fixup(objspace, mem, size); } @@ -8012,11 +8031,11 @@ objspace_xrealloc(rb_objspace_t *objspace, void *ptr, size_t new_size, size_t ol objspace_malloc_increase(objspace, mem, new_size, old_size, MEMOP_TYPE_REALLOC); + RB_DEBUG_COUNTER_INC(heap_xrealloc); return mem; } -#if CALC_EXACT_MALLOC_SIZE -#if USE_GC_MALLOC_OBJ_INFO_DETAILS +#if CALC_EXACT_MALLOC_SIZE && USE_GC_MALLOC_OBJ_INFO_DETAILS #define MALLOC_INFO_GEN_SIZE 100 #define MALLOC_INFO_SIZE_SIZE 10 @@ -8037,8 +8056,8 @@ mmalloc_info_file_i(st_data_t key, st_data_t val, st_data_t dmy) } __attribute__((destructor)) -static void -malloc_info_show_results(void) +void +rb_malloc_info_show_results(void) { int i; @@ -8064,7 +8083,11 @@ malloc_info_show_results(void) st_foreach(malloc_info_file_table, mmalloc_info_file_i, 0); } } -#endif +#else +void +rb_malloc_info_show_results(void) +{ +} #endif static void @@ -8129,6 +8152,7 @@ objspace_xfree(rb_objspace_t *objspace, void *ptr, size_t old_size) old_size = objspace_malloc_size(objspace, ptr, old_size); free(ptr); + RB_DEBUG_COUNTER_INC(heap_xfree); objspace_malloc_increase(objspace, ptr, 0, old_size, MEMOP_TYPE_FREE); } diff --git a/signal.c b/signal.c index d24bda07f0..02c8393730 100644 --- a/signal.c +++ b/signal.c @@ -395,9 +395,17 @@ interrupt_init(int argc, VALUE *argv, VALUE self) return rb_call_super(2, args); } +#include "debug_counter.h" +void rb_malloc_info_show_results(void); /* gc.c */ + void ruby_default_signal(int sig) { +#if USE_DEBUG_COUNTER + rb_debug_counter_show_results("killed by signal."); +#endif + rb_malloc_info_show_results(); + signal(sig, SIG_DFL); raise(sig); }