ruby-changes:71934
From: Koichi <ko1@a...>
Date: Tue, 24 May 2022 10:07:22 +0900 (JST)
Subject: [ruby-changes:71934] 4111028a5c (master): use `RUBY_DEBUG_LOG` instead of `thread_debug`
https://git.ruby-lang.org/ruby.git/commit/?id=4111028a5c From 4111028a5cc229c314c2b93271d205206b207088 Mon Sep 17 00:00:00 2001 From: Koichi Sasada <ko1@a...> Date: Tue, 24 May 2022 03:58:18 +0900 Subject: use `RUBY_DEBUG_LOG` instead of `thread_debug` `thread_debug()` was introduced to print debug messages on `THREAD_DEBUG > 0` but `RUBY_DEBUG_LOG()` is more controllable. --- thread.c | 202 +++++++++++++++++-------------------------------------- thread_pthread.c | 22 +++--- thread_win32.c | 96 +++++++++++++++----------- 3 files changed, 128 insertions(+), 192 deletions(-) diff --git a/thread.c b/thread.c index cee0c6c8de..26c46ed9de 100644 --- a/thread.c +++ b/thread.c @@ -106,10 +106,6 @@ https://github.com/ruby/ruby/blob/trunk/thread.c#L106 #define RUBY_THREAD_PRIORITY_MIN -3 #endif -#ifndef THREAD_DEBUG -#define THREAD_DEBUG 0 -#endif - static VALUE rb_cThreadShield; static VALUE sym_immediate; @@ -257,60 +253,6 @@ timeout_prepare(rb_hrtime_t **to, rb_hrtime_t *rel, rb_hrtime_t *end, https://github.com/ruby/ruby/blob/trunk/thread.c#L253 } } -#if THREAD_DEBUG -#ifdef HAVE_VA_ARGS_MACRO -void rb_thread_debug(const char *file, int line, const char *fmt, ...); -#define thread_debug(...) rb_thread_debug(__FILE__, __LINE__, __VA_ARGS__) -#define POSITION_FORMAT "%s:%d:" -#define POSITION_ARGS ,file, line -#else -void rb_thread_debug(const char *fmt, ...); -#define thread_debug rb_thread_debug -#define POSITION_FORMAT -#define POSITION_ARGS -#endif - -# if THREAD_DEBUG < 0 -static int rb_thread_debug_enabled; - -/* - * call-seq: - * Thread.DEBUG -> num - * - * Returns the thread debug level. Available only if compiled with - * THREAD_DEBUG=-1. - */ - -static VALUE -rb_thread_s_debug(VALUE _) -{ - return INT2NUM(rb_thread_debug_enabled); -} - -/* - * call-seq: - * Thread.DEBUG = num - * - * Sets the thread debug level. Available only if compiled with - * THREAD_DEBUG=-1. - */ - -static VALUE -rb_thread_s_debug_set(VALUE self, VALUE val) -{ - rb_thread_debug_enabled = RTEST(val) ? NUM2INT(val) : 0; - return val; -} -# else -# define rb_thread_debug_enabled THREAD_DEBUG -# endif -#else -#define thread_debug if(0)printf -#endif - -#define thread_id_str(th) ((void *)(uintptr_t)(th)->nt->thread_id) -#define PRI_THREAD_ID "p" - MAYBE_UNUSED(NOINLINE(static int thread_start_func_2(rb_thread_t *th, VALUE *stack_start))); void ruby_sigchld_handler(rb_vm_t *); /* signal.c */ @@ -335,35 +277,6 @@ ubf_sigwait(void *ignore) https://github.com/ruby/ruby/blob/trunk/thread.c#L277 # define USE_EVENTFD (0) #endif -#if THREAD_DEBUG -static int debug_mutex_initialized = 1; -static rb_nativethread_lock_t debug_mutex; - -void -rb_thread_debug( -#ifdef HAVE_VA_ARGS_MACRO - const char *file, int line, -#endif - const char *fmt, ...) -{ - va_list args; - char buf[BUFSIZ]; - - if (!rb_thread_debug_enabled) return; - - if (debug_mutex_initialized == 1) { - debug_mutex_initialized = 0; - rb_native_mutex_initialize(&debug_mutex); - } - - va_start(args, fmt); - vsnprintf(buf, BUFSIZ, fmt, args); - va_end(args); - - DEBUG_OUT(); -} -#endif - #include "thread_sync.c" void @@ -463,15 +376,15 @@ terminate_all(rb_ractor_t *r, const rb_thread_t *main_thread) https://github.com/ruby/ruby/blob/trunk/thread.c#L376 ccan_list_for_each(&r->threads.set, th, lt_node) { if (th != main_thread) { - thread_debug("terminate_all: begin (thid: %"PRI_THREAD_ID", status: %s)\n", - thread_id_str(th), thread_status_name(th, TRUE)); - rb_threadptr_pending_interrupt_enque(th, eTerminateSignal); + RUBY_DEBUG_LOG("terminate start th:%u status:%s", rb_th_serial(th), thread_status_name(th, TRUE)); + + rb_threadptr_pending_interrupt_enque(th, eTerminateSignal); rb_threadptr_interrupt(th); - thread_debug("terminate_all: end (thid: %"PRI_THREAD_ID", status: %s)\n", - thread_id_str(th), thread_status_name(th, TRUE)); - } + + RUBY_DEBUG_LOG("terminate done th:%u status:%s", rb_th_serial(th), thread_status_name(th, TRUE)); + } else { - thread_debug("terminate_all: main thread (%p)\n", (void *)th); + RUBY_DEBUG_LOG("main thread th:%u", rb_th_serial(th)); } } } @@ -536,8 +449,9 @@ rb_thread_terminate_all(rb_thread_t *th) https://github.com/ruby/ruby/blob/trunk/thread.c#L449 EC_PUSH_TAG(ec); if (EC_EXEC_TAG() == TAG_NONE) { retry: - thread_debug("rb_thread_terminate_all (main thread: %p)\n", (void *)th); - terminate_all(cr, th); + RUBY_DEBUG_LOG("th:%u", rb_th_serial(th)); + + terminate_all(cr, th); while (rb_ractor_living_thread_num(cr) > 1) { rb_hrtime_t rel = RB_HRTIME_PER_SEC; @@ -730,12 +644,14 @@ thread_start_func_2(rb_thread_t *th, VALUE *stack_start) https://github.com/ruby/ruby/blob/trunk/thread.c#L644 VALUE * vm_stack = NULL; VM_ASSERT(th != th->vm->ractor.main_thread); - thread_debug("thread start: %p\n", (void *)th); + RUBY_DEBUG_LOG("th:%u", rb_th_serial(th)); // setup native thread thread_sched_to_running(TH_SCHED(th), th); ruby_thread_set_native(th); + RUBY_DEBUG_LOG("got lock. th:%u", rb_th_serial(th)); + // setup ractor if (rb_ractor_status_p(th->ractor, ractor_blocking)) { RB_VM_LOCK(); @@ -760,8 +676,6 @@ thread_start_func_2(rb_thread_t *th, VALUE *stack_start) https://github.com/ruby/ruby/blob/trunk/thread.c#L676 th->ec->machine.stack_start = STACK_DIR_UPPER(vm_stack + size, vm_stack); th->ec->machine.stack_maxsize -= size * sizeof(VALUE); - thread_debug("thread start (get lock): %p\n", (void *)th); - // Ensure that we are not joinable. VM_ASSERT(th->value == Qundef); @@ -820,7 +734,7 @@ thread_start_func_2(rb_thread_t *th, VALUE *stack_start) https://github.com/ruby/ruby/blob/trunk/thread.c#L734 } th->status = THREAD_KILLED; - thread_debug("thread end: %p\n", (void *)th); + RUBY_DEBUG_LOG("killed th:%u", rb_th_serial(th)); if (th->vm->ractor.main_thread == th) { ruby_stop(0); @@ -936,7 +850,7 @@ thread_create_core(VALUE thval, struct thread_create_params *params) https://github.com/ruby/ruby/blob/trunk/thread.c#L850 rb_native_mutex_initialize(&th->interrupt_lock); - RUBY_DEBUG_LOG("r:%u th:%p", rb_ractor_id(th->ractor), (void *)th); + RUBY_DEBUG_LOG("r:%u th:%u", rb_ractor_id(th->ractor), rb_th_serial(th)); rb_ractor_living_threads_insert(th->ractor, th); @@ -1138,8 +1052,7 @@ thread_join_sleep(VALUE arg) https://github.com/ruby/ruby/blob/trunk/thread.c#L1052 } else { if (hrtime_update_expire(limit, end)) { - thread_debug("thread_join: timeout (thid: %"PRI_THREAD_ID")\n", - thread_id_str(target_th)); + RUBY_DEBUG_LOG("timeout target_th:%u", rb_th_serial(target_th)); return Qfalse; } th->status = THREAD_STOPPED; @@ -1147,8 +1060,8 @@ thread_join_sleep(VALUE arg) https://github.com/ruby/ruby/blob/trunk/thread.c#L1060 } RUBY_VM_CHECK_INTS_BLOCKING(th->ec); th->status = THREAD_RUNNABLE; - thread_debug("thread_join: interrupted (thid: %"PRI_THREAD_ID", status: %s)\n", - thread_id_str(target_th), thread_status_name(target_th, TRUE)); + + RUBY_DEBUG_LOG("interrupted target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE)); } return Qtrue; } @@ -1168,8 +1081,7 @@ thread_join(rb_thread_t *target_th, VALUE timeout, rb_hrtime_t *limit) https://github.com/ruby/ruby/blob/trunk/thread.c#L1081 rb_raise(rb_eThreadError, "Target thread must not be main thread"); } - thread_debug("thread_join (thid: %"PRI_THREAD_ID", status: %s)\n", - thread_id_str(target_th), thread_status_name(target_th, TRUE)); + RUBY_DEBUG_LOG("target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE)); if (target_th->status != THREAD_KILLED) { struct rb_waiting_list waiter; @@ -1189,8 +1101,7 @@ thread_join(rb_thread_t *target_th, VALUE timeout, rb_hrtime_t *limit) https://github.com/ruby/ruby/blob/trunk/thread.c#L1101 } } - thread_debug("thread_join: success (thid: %"PRI_THREAD_ID", status: %s)\n", - thread_id_str(target_th), thread_status_name(target_th, TRUE)); + RUBY_DEBUG_LOG("success target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE)); if (target_th->ec->errinfo != Qnil) { VALUE err = target_th->ec->errinfo; @@ -1198,8 +1109,7 @@ thread_join(rb_thread_t *target_th, VALUE timeout, rb_hrtime_t *limit) https://github.com/ruby/ruby/blob/trunk/thread.c#L1109 if (FIXNUM_P(err)) { switch (err) { case INT2FIX(TAG_FATAL): - thread_debug("thread_join: terminated (thid: %"PRI_THREAD_ID", status: %s)\n", - thread_id_str(target_th), thread_status_name(target_th, TRUE)); + RUBY_DEBUG_LOG("terminated target_th:%u status:%s", rb_th_serial(target_th), thread_status_name(target_th, TRUE)); /* OK. killed. */ break; @@ -1384,9 +1294,9 @@ hrtime_update_expire(rb_hrtime_t *timeout, const rb_hrtime_t end) https://github.com/ruby/ruby/blob/trunk/thread.c#L1294 rb_hrtime_t now = rb_hrtime_now(); if (now > end) return 1; - thread_debug("hrtime_update_expire: " - "%"PRIu64" > %"PRIu64"\n", - (uint64_t)end, (uint64_t)now); + + RUBY_DEBUG_LOG("%"PRIu64" > %"PRIu64"", (uint64_t)end, (uint64_t)now); + *timeout = end - now; return 0; } @@ -1417,14 +1327,14 @@ sleep_hrtime(rb_thread_t *th, rb_hrtime_t rel, unsigned int fl) https://github.com/ruby/ruby/blob/trunk/thread.c#L1327 void rb_thread_sleep_forever(void) { - thread_debug("rb_thread_sleep_forever\n"); + RUBY_DEBUG_LOG("%s", ""); (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/