diff options
author | Adrien Mahieux <adrien.mahieux@gmail.com> | 2019-12-20 13:31:25 +0100 |
---|---|---|
committer | Vladimir Kobal <vlad@prokk.net> | 2019-12-20 14:31:25 +0200 |
commit | c0e1f63f4da29a03cd3b214834e90b9cc043717a (patch) | |
tree | f41f161453612fd76d4e144d8f57037e11021b67 | |
parent | d0e424144749a9748c5de4f0888d8e8ec5647b78 (diff) |
Set standard name to non-libnetdata threads (libuv, pthread) (#7584)
* [libnetdata/threads] Add uv_thread_set_name
This is inspired from thread_set_name() but for libuv threads.
Both are based on pthread, but for uv we need to call it with the
uv_thread_t pointer, instead of being the thread that calls the
function for itself.
* [exporting] Set libuv threadname to "EXPORTING-index"
* [database/engine] Set libuv thread name to "DBENGINE"
* [daemon/command] Set libuv thread name to "DAEMON-COMMAND"
* [collectors/proc] Set pthread name to "PLUGIN[cpuidle]"
* Use new 'thread_set_name_np' name
-rw-r--r-- | collectors/proc.plugin/proc_stat.c | 2 | ||||
-rw-r--r-- | daemon/commands.c | 1 | ||||
-rwxr-xr-x | database/engine/rrdengineapi.c | 3 | ||||
-rw-r--r-- | exporting/init_connectors.c | 3 | ||||
-rw-r--r-- | libnetdata/threads/threads.c | 27 | ||||
-rw-r--r-- | libnetdata/threads/threads.h | 3 |
6 files changed, 33 insertions, 6 deletions
diff --git a/collectors/proc.plugin/proc_stat.c b/collectors/proc.plugin/proc_stat.c index 5e6b79fc8c..50fab90e64 100644 --- a/collectors/proc.plugin/proc_stat.c +++ b/collectors/proc.plugin/proc_stat.c @@ -1007,6 +1007,8 @@ int do_proc_stat(int update_every, usec_t dt) { error("Cannot create wake_cpu_thread"); else if(unlikely(pthread_join(thread, NULL))) error("Cannot join wake_cpu_thread"); + if(thread) + pthread_setname_np(thread, "PLUGIN[cpuidle]"); cpu_states_updated = 1; } } diff --git a/daemon/commands.c b/daemon/commands.c index b12c19216b..cbf71c47f9 100644 --- a/daemon/commands.c +++ b/daemon/commands.c @@ -575,6 +575,7 @@ void commands_init(void) /* wait for worker thread to initialize */ wait_for_completion(&completion); destroy_completion(&completion); + uv_thread_set_name_np(thread, "DAEMON_COMMAND"); if (command_thread_error) { error = uv_thread_join(&thread); diff --git a/database/engine/rrdengineapi.c b/database/engine/rrdengineapi.c index fd41a0cd89..ce465e7638 100755 --- a/database/engine/rrdengineapi.c +++ b/database/engine/rrdengineapi.c @@ -792,6 +792,7 @@ int rrdeng_init(struct rrdengine_instance **ctxp, char *dbfiles_path, unsigned p /* wait for worker thread to initialize */ wait_for_completion(&ctx->rrdengine_completion); destroy_completion(&ctx->rrdengine_completion); + uv_thread_set_name_np(ctx->worker_config.thread, "DBENGINE"); if (ctx->worker_config.error) { goto error_after_rrdeng_worker; } @@ -834,4 +835,4 @@ int rrdeng_exit(struct rrdengine_instance *ctx) } rrd_stat_atomic_add(&rrdeng_reserved_file_descriptors, -RRDENG_FD_BUDGET_PER_INSTANCE); return 0; -}
\ No newline at end of file +} diff --git a/exporting/init_connectors.c b/exporting/init_connectors.c index a15665a966..ba3802780a 100644 --- a/exporting/init_connectors.c +++ b/exporting/init_connectors.c @@ -65,6 +65,9 @@ int init_connectors(struct engine *engine) // dispatch the instance worker thread uv_thread_create(&instance->thread, connector->worker, instance); + char threadname[NETDATA_THREAD_NAME_MAX+1]; + snprintfz(threadname, NETDATA_THREAD_NAME_MAX, "EXPORTING-%zu", instance->index); + uv_thread_set_name_np(instance->thread, threadname); } } diff --git a/libnetdata/threads/threads.c b/libnetdata/threads/threads.c index 3b3f30415b..c310d528bc 100644 --- a/libnetdata/threads/threads.c +++ b/libnetdata/threads/threads.c @@ -109,14 +109,13 @@ static void thread_cleanup(void *ptr) { netdata_thread = NULL; } -static void thread_set_name(NETDATA_THREAD *nt) { +static void thread_set_name_np(NETDATA_THREAD *nt) { if (nt->tag) { int ret = 0; - // Name is limited to 16 chars - char threadname[16]; - strncpyz(threadname, nt->tag, 15); + char threadname[NETDATA_THREAD_NAME_MAX+1]; + strncpyz(threadname, nt->tag, NETDATA_THREAD_NAME_MAX); #if defined(__FreeBSD__) pthread_set_name_np(pthread_self(), threadname); @@ -134,6 +133,24 @@ static void thread_set_name(NETDATA_THREAD *nt) { } } +void uv_thread_set_name_np(uv_thread_t ut, const char* name) { + int ret = 0; + + char threadname[NETDATA_THREAD_NAME_MAX+1]; + strncpyz(threadname, name, NETDATA_THREAD_NAME_MAX); + +#if defined(__FreeBSD__) + pthread_set_name_np(ut, threadname); +#elif defined(__APPLE__) + // Apple can only set its own name +#else + ret = pthread_setname_np(ut, threadname); +#endif + + if (ret) + error("cannot set libuv thread name to %s. Err: %d", threadname, ret); +} + static void *thread_start(void *ptr) { netdata_thread = (NETDATA_THREAD *)ptr; @@ -146,7 +163,7 @@ 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); + thread_set_name_np(ptr); void *ret = NULL; pthread_cleanup_push(thread_cleanup, ptr); diff --git a/libnetdata/threads/threads.h b/libnetdata/threads/threads.h index eec6ad0e31..b7cb0e15a0 100644 --- a/libnetdata/threads/threads.h +++ b/libnetdata/threads/threads.h @@ -31,6 +31,9 @@ extern int netdata_thread_cancel(netdata_thread_t thread); extern int netdata_thread_join(netdata_thread_t thread, void **retval); extern int netdata_thread_detach(pthread_t thread); +#define NETDATA_THREAD_NAME_MAX 15 +extern void uv_thread_set_name_np(uv_thread_t ut, const char* name); + #define netdata_thread_self pthread_self #define netdata_thread_testcancel pthread_testcancel |