From eea7adde7c3fc751a3eb6bd9858903432c491285 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Thu, 29 Apr 2021 21:33:05 +0800 Subject: [PATCH] set thread name as necessary --- src/idempotency/client/receipt_handler.c | 2 +- src/sf_binlog_writer.c | 13 +++++++++++- src/sf_binlog_writer.h | 12 ++++++----- src/sf_connection_manager.c | 4 ++++ src/sf_global.c | 2 +- src/sf_service.c | 26 ++++++++++++++++++++++-- src/sf_service.h | 14 ++++++------- src/sf_types.h | 1 + 8 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/idempotency/client/receipt_handler.c b/src/idempotency/client/receipt_handler.c index 253e576..5517d67 100644 --- a/src/idempotency/client/receipt_handler.c +++ b/src/idempotency/client/receipt_handler.c @@ -480,7 +480,7 @@ static int do_init() } memset(receipt_thread_contexts, 0, bytes); - return sf_service_init_ex2(&g_sf_context, + return sf_service_init_ex2(&g_sf_context, "idemp-receipt", receipt_alloc_thread_extra_data, receipt_thread_loop_callback, NULL, sf_proto_set_body_length, receipt_deal_task, receipt_task_finish_cleanup, receipt_recv_timeout_callback, diff --git a/src/sf_binlog_writer.c b/src/sf_binlog_writer.c index f6c7d13..402c221 100644 --- a/src/sf_binlog_writer.c +++ b/src/sf_binlog_writer.c @@ -535,6 +535,16 @@ static void *binlog_writer_func(void *arg) SFBinlogWriterBuffer *wb_head; thread = (SFBinlogWriterThread *)arg; + +#ifdef OS_LINUX + { + char thread_name[64]; + snprintf(thread_name, sizeof(thread_name), + "writer-%s", thread->name); + prctl(PR_SET_NAME, thread_name); + } +#endif + thread->running = true; while (SF_G_CONTINUE_FLAG) { wb_head = (SFBinlogWriterBuffer *)fc_queue_pop_all(&thread->queue); @@ -641,7 +651,7 @@ int sf_binlog_writer_init_by_version(SFBinlogWriterInfo *writer, } int sf_binlog_writer_init_thread_ex(SFBinlogWriterThread *thread, - SFBinlogWriterInfo *writer, const short order_mode, + const char *name, SFBinlogWriterInfo *writer, const short order_mode, const short order_by, const int max_record_size, const int writer_count, const bool use_fixed_buffer_size) { @@ -650,6 +660,7 @@ int sf_binlog_writer_init_thread_ex(SFBinlogWriterThread *thread, pthread_t tid; int result; + snprintf(thread->name, sizeof(thread->name), "%s", name); thread->order_mode = order_mode; thread->order_by = order_by; thread->use_fixed_buffer_size = use_fixed_buffer_size; diff --git a/src/sf_binlog_writer.h b/src/sf_binlog_writer.h index f5a8092..5db1d99 100644 --- a/src/sf_binlog_writer.h +++ b/src/sf_binlog_writer.h @@ -67,6 +67,7 @@ typedef struct sf_binlog_writer_buffer_ring { typedef struct binlog_writer_thread { struct fast_mblock_man mblock; struct fc_queue queue; + char name[64]; bool running; bool use_fixed_buffer_size; short order_mode; @@ -127,13 +128,13 @@ int sf_binlog_writer_init_by_version(SFBinlogWriterInfo *writer, const int buffer_size, const int ring_size); int sf_binlog_writer_init_thread_ex(SFBinlogWriterThread *thread, - SFBinlogWriterInfo *writer, const short order_mode, + const char *name, SFBinlogWriterInfo *writer, const short order_mode, const short order_by, const int max_record_size, const int writer_count, const bool use_fixed_buffer_size); -#define sf_binlog_writer_init_thread(thread, \ +#define sf_binlog_writer_init_thread(thread, name, \ writer, order_by, max_record_size) \ - sf_binlog_writer_init_thread_ex(thread, writer, \ + sf_binlog_writer_init_thread_ex(thread, name, writer, \ SF_BINLOG_THREAD_ORDER_MODE_FIXED, \ order_by, max_record_size, 1, true) @@ -148,8 +149,9 @@ static inline int sf_binlog_writer_init(SFBinlogWriterContext *context, return result; } - return sf_binlog_writer_init_thread(&context->thread, &context->writer, - SF_BINLOG_THREAD_TYPE_ORDER_BY_NONE, max_record_size); + return sf_binlog_writer_init_thread(&context->thread, subdir_name, + &context->writer, SF_BINLOG_THREAD_TYPE_ORDER_BY_NONE, + max_record_size); } int sf_binlog_writer_change_order_by(SFBinlogWriterInfo *writer, diff --git a/src/sf_connection_manager.c b/src/sf_connection_manager.c index 08fd4f0..2f7e872 100644 --- a/src/sf_connection_manager.c +++ b/src/sf_connection_manager.c @@ -817,6 +817,10 @@ static void *connection_manager_thread_func(void *arg) SFConnectionManager *cm; struct common_blocked_node *head; +#ifdef OS_LINUX + prctl(PR_SET_NAME, "cm-alive-detect"); +#endif + cm = (SFConnectionManager *)arg; logInfo("file: "__FILE__", line: %d, " "[%s] connection manager thread start", diff --git a/src/sf_global.c b/src/sf_global.c index e97cc71..aeb00be 100644 --- a/src/sf_global.c +++ b/src/sf_global.c @@ -41,7 +41,7 @@ SFGlobalVariables g_sf_global_vars = { }; SFContext g_sf_context = { - NULL, 0, -1, -1, 0, 0, 1, DEFAULT_WORK_THREADS, + {'\0'}, NULL, 0, -1, -1, 0, 0, 1, DEFAULT_WORK_THREADS, {'\0'}, {'\0'}, 0, true, true, NULL, NULL, NULL, sf_task_finish_clean_up, NULL }; diff --git a/src/sf_service.c b/src/sf_service.c index 67f5c15..7068ba3 100644 --- a/src/sf_service.c +++ b/src/sf_service.c @@ -115,7 +115,7 @@ static int sf_init_free_queues(const int task_arg_size, return 0; } -int sf_service_init_ex2(SFContext *sf_context, +int sf_service_init_ex2(SFContext *sf_context, const char *name, sf_alloc_thread_extra_data_callback alloc_thread_extra_data_callback, ThreadLoopCallback thread_loop_callback, @@ -135,6 +135,7 @@ int sf_service_init_ex2(SFContext *sf_context, pthread_t tid; pthread_attr_t thread_attr; + snprintf(sf_context->name, sizeof(sf_context->name), "%s", name); sf_context->realloc_task_buffer = g_sf_global_vars. min_buff_size < g_sf_global_vars.max_buff_size; sf_context->accept_done_func = accept_done_callback; @@ -234,7 +235,7 @@ int sf_service_init_ex2(SFContext *sf_context, thread_ctx->sf_context = sf_context; thread_ctx->thread_data = thread_data; if ((result=pthread_create(&tid, &thread_attr, - worker_thread_entrance, thread_ctx)) != 0) + worker_thread_entrance, thread_ctx)) != 0) { logError("file: "__FILE__", line: %d, " "create thread failed, startup threads: %d, " @@ -284,6 +285,17 @@ static void *worker_thread_entrance(void *arg) int thread_count; thread_ctx = (struct worker_thread_context *)arg; + +#ifdef OS_LINUX + { + char thread_name[32]; + snprintf(thread_name, sizeof(thread_name), "%s-net[%d]", + thread_ctx->sf_context->name, (int)(thread_ctx-> + thread_data - thread_ctx->sf_context->thread_data)); + prctl(PR_SET_NAME, thread_name); + } +#endif + thread_count = __sync_add_and_fetch(&thread_ctx-> sf_context->thread_count, 1); @@ -375,6 +387,16 @@ static void *accept_thread_entrance(void *arg) struct fast_task_info *task; accept_context = (struct accept_thread_context *)arg; + +#ifdef OS_LINUX + { + char thread_name[32]; + snprintf(thread_name, sizeof(thread_name), "%s-listen", + accept_context->sf_context->name); + prctl(PR_SET_NAME, thread_name); + } +#endif + while (g_sf_global_vars.continue_flag) { sockaddr_len = sizeof(inaddr); incomesock = accept(accept_context->server_sock, diff --git a/src/sf_service.h b/src/sf_service.h index c4b7c4c..12a9c22 100644 --- a/src/sf_service.h +++ b/src/sf_service.h @@ -33,7 +33,7 @@ typedef void (*sf_sig_quit_handler)(int sig); extern "C" { #endif -int sf_service_init_ex2(SFContext *sf_context, +int sf_service_init_ex2(SFContext *sf_context, const char *name, sf_alloc_thread_extra_data_callback alloc_thread_extra_data_callback, ThreadLoopCallback thread_loop_callback, @@ -44,20 +44,20 @@ int sf_service_init_ex2(SFContext *sf_context, const int proto_header_size, const int task_arg_size, TaskInitCallback init_callback); -#define sf_service_init_ex(sf_context, alloc_thread_extra_data_callback, \ +#define sf_service_init_ex(sf_context, name, alloc_thread_extra_data_callback,\ thread_loop_callback, accept_done_callback, set_body_length_func, \ deal_func, task_cleanup_func, timeout_callback, net_timeout_ms, \ proto_header_size, task_arg_size) \ - sf_service_init_ex2(sf_context, alloc_thread_extra_data_callback, \ - thread_loop_callback, accept_done_callback, set_body_length_func, \ - deal_func, task_cleanup_func, timeout_callback, net_timeout_ms, \ + sf_service_init_ex2(sf_context, name, alloc_thread_extra_data_callback, \ + thread_loop_callback, accept_done_callback, set_body_length_func, \ + deal_func, task_cleanup_func, timeout_callback, net_timeout_ms, \ proto_header_size, task_arg_size, NULL) -#define sf_service_init(alloc_thread_extra_data_callback, \ +#define sf_service_init(name, alloc_thread_extra_data_callback, \ thread_loop_callback, accept_done_callback, set_body_length_func, \ deal_func, task_cleanup_func, timeout_callback, net_timeout_ms, \ proto_header_size, task_arg_size) \ - sf_service_init_ex2(&g_sf_context, alloc_thread_extra_data_callback, \ + sf_service_init_ex2(&g_sf_context, name, alloc_thread_extra_data_callback, \ thread_loop_callback, accept_done_callback, set_body_length_func, \ deal_func, task_cleanup_func, timeout_callback, net_timeout_ms, \ proto_header_size, task_arg_size, NULL) diff --git a/src/sf_types.h b/src/sf_types.h index 5700f91..7f0b8cc 100644 --- a/src/sf_types.h +++ b/src/sf_types.h @@ -41,6 +41,7 @@ typedef int (*sf_deal_task_func)(struct fast_task_info *task, const int stage); typedef int (*sf_recv_timeout_callback)(struct fast_task_info *task); typedef struct sf_context { + char name[64]; struct nio_thread_data *thread_data; volatile int thread_count; int outer_sock;