From 8ece9b0932a9e5ed94581e984e6947378d32b17e Mon Sep 17 00:00:00 2001 From: Adrien Mahieux Date: Wed, 4 Sep 2019 19:07:47 +0200 Subject: [PATCH] [libnetdata/thread] Set thread name from tag (#6745) * [libnetdata/thread] Set thread name from tag When calling netdata_thread_create, you can provide a tag to identify the thread in the error message This patch uses this tag to rename the thread if supported by the pthread distribution to allow better identification of the thread. This have helped identify the bug #6741 Sample output: ps -L -o tid,pid,comm,args -p $(pgrep netdata) TID PID COMMAND COMMAND 5123 5123 netdata ./netdata -D 5133 5123 PLUGIN[proc] ./netdata -D 5134 5123 netdata ./netdata -D 5135 5123 PLUGIN[cgroups] ./netdata -D 5136 5123 netdata ./netdata -D 5137 5123 STATSD ./netdata -D 5138 5123 BACKENDS ./netdata -D 5139 5123 netdata ./netdata -D 5140 5123 PLUGINSD ./netdata -D 5141 5123 netdata ./netdata -D 5142 5123 HEALTH ./netdata -D 5143 5123 netdata ./netdata -D 5145 5123 netdata ./netdata -D 5146 5123 PLUGINSD ./netdata -D 5147 5123 PLUGINSD[apps] ./netdata -D 5148 5123 netdata ./netdata -D 5150 5123 netdata ./netdata -D 5151 5123 STATSD ./netdata -D * [libnetdata/thread] Improve set thread title Added error check and fixed limit of 15 chars for thread name. ``` ps -L -o tid,pid,comm,args -p $(pgrep netdata) TID PID COMMAND COMMAND 14521 14521 netdata ./netdata -D 14531 14521 PLUGIN[proc] ./netdata -D 14532 14521 PLUGIN[diskspac ./netdata -D 14533 14521 PLUGIN[cgroups] ./netdata -D 14534 14521 PLUGIN[idlejitt ./netdata -D 14535 14521 STATSD ./netdata -D 14536 14521 BACKENDS ./netdata -D 14537 14521 WEB_SERVER[stat ./netdata -D 14538 14521 PLUGINSD ./netdata -D 14539 14521 HEALTH ./netdata -D 14540 14521 WEB_SERVER[stat ./netdata -D 14541 14521 WEB_SERVER[stat ./netdata -D 14542 14521 WEB_SERVER[stat ./netdata -D 14543 14521 WEB_SERVER[stat ./netdata -D 14544 14521 WEB_SERVER[stat ./netdata -D 14546 14521 STATSD_COLLECTO ./netdata -D 14547 14521 PLUGINSD[python ./netdata -D 14548 14521 PLUGINSD[apps] ./netdata -D ``` * [libnetdata/thread] Moved code to dedicated func * [libnetdata/thread] Wrong return value for thread_set_name * [libnetdata/thread] Remove lval for FreeBSD as defined void * [libnetdata/thread] Change allocation method to stack --- libnetdata/threads/threads.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/libnetdata/threads/threads.c b/libnetdata/threads/threads.c index 133d9a5471..3b3f30415b 100644 --- a/libnetdata/threads/threads.c +++ b/libnetdata/threads/threads.c @@ -109,6 +109,31 @@ static void thread_cleanup(void *ptr) { netdata_thread = NULL; } +static void thread_set_name(NETDATA_THREAD *nt) { + + if (nt->tag) { + int ret = 0; + + // Name is limited to 16 chars + char threadname[16]; + strncpyz(threadname, nt->tag, 15); + +#if defined(__FreeBSD__) + pthread_set_name_np(pthread_self(), threadname); +#elif defined(__APPLE__) + ret = pthread_setname_np(threadname); +#else + ret = pthread_setname_np(pthread_self(), threadname); +#endif + + if (ret != 0) + error("cannot set pthread name of %d to %s. ErrCode: %d", gettid(), threadname, ret); + else + info("set name of thread %d to %s", gettid(), threadname); + + } +} + static void *thread_start(void *ptr) { netdata_thread = (NETDATA_THREAD *)ptr; @@ -121,6 +146,8 @@ static void *thread_start(void *ptr) { if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0) error("cannot set pthread cancel state to ENABLE."); + thread_set_name(ptr); + void *ret = NULL; pthread_cleanup_push(thread_cleanup, ptr); ret = netdata_thread->start_routine(netdata_thread->arg);