@cryptotaxi247 / netdata-1 / commits / c0e1f63f4

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

Adrien Mahieux committed Dec 20, 2019 at 13:31 UTC c0e1f63f4da29a03cd3b214834e90b9cc043717a
6 files changed +33 -6
collectors/proc.plugin/proc_stat.c
+2
@@ -1007,6 +1007,8 @@ int do_proc_stat(int update_every, usec_t dt) {
1007 error("Cannot create wake_cpu_thread");
1008 else if(unlikely(pthread_join(thread, NULL)))
1009 error("Cannot join wake_cpu_thread");
1010 + if(thread)
1011 + pthread_setname_np(thread, "PLUGIN[cpuidle]");
1012 cpu_states_updated = 1;
1013 }
1014 }
daemon/commands.c
+1
@@ -575,6 +575,7 @@ void commands_init(void)
575 /* wait for worker thread to initialize */
576 wait_for_completion(&completion);
577 destroy_completion(&completion);
578 + uv_thread_set_name_np(thread, "DAEMON_COMMAND");
579
580 if (command_thread_error) {
581 error = uv_thread_join(&thread);
database/engine/rrdengineapi.c
+2 -1
@@ -792,6 +792,7 @@ int rrdeng_init(struct rrdengine_instance **ctxp, char *dbfiles_path, unsigned p
792 /* wait for worker thread to initialize */
793 wait_for_completion(&ctx->rrdengine_completion);
794 destroy_completion(&ctx->rrdengine_completion);
795 + uv_thread_set_name_np(ctx->worker_config.thread, "DBENGINE");
796 if (ctx->worker_config.error) {
797 goto error_after_rrdeng_worker;
798 }
@@ -834,4 +835,4 @@ int rrdeng_exit(struct rrdengine_instance *ctx)
835 }
836 rrd_stat_atomic_add(&rrdeng_reserved_file_descriptors, -RRDENG_FD_BUDGET_PER_INSTANCE);
837 return 0;
837 -}
\ No newline at end of file
838 +}
exporting/init_connectors.c
+3
@@ -65,6 +65,9 @@ int init_connectors(struct engine *engine)
65
66 // dispatch the instance worker thread
67 uv_thread_create(&instance->thread, connector->worker, instance);
68 + char threadname[NETDATA_THREAD_NAME_MAX+1];
69 + snprintfz(threadname, NETDATA_THREAD_NAME_MAX, "EXPORTING-%zu", instance->index);
70 + uv_thread_set_name_np(instance->thread, threadname);
71 }
72 }
73
libnetdata/threads/threads.c
+22 -5
@@ -109,14 +109,13 @@ static void thread_cleanup(void *ptr) {
109 netdata_thread = NULL;
110 }
111
112 -static void thread_set_name(NETDATA_THREAD *nt) {
112 +static void thread_set_name_np(NETDATA_THREAD *nt) {
113
114 if (nt->tag) {
115 int ret = 0;
116
117 - // Name is limited to 16 chars
118 - char threadname[16];
119 - strncpyz(threadname, nt->tag, 15);
117 + char threadname[NETDATA_THREAD_NAME_MAX+1];
118 + strncpyz(threadname, nt->tag, NETDATA_THREAD_NAME_MAX);
119
120 #if defined(__FreeBSD__)
121 pthread_set_name_np(pthread_self(), threadname);
@@ -134,6 +133,24 @@ static void thread_set_name(NETDATA_THREAD *nt) {
133 }
134 }
135
136 +void uv_thread_set_name_np(uv_thread_t ut, const char* name) {
137 + int ret = 0;
138 +
139 + char threadname[NETDATA_THREAD_NAME_MAX+1];
140 + strncpyz(threadname, name, NETDATA_THREAD_NAME_MAX);
141 +
142 +#if defined(__FreeBSD__)
143 + pthread_set_name_np(ut, threadname);
144 +#elif defined(__APPLE__)
145 + // Apple can only set its own name
146 +#else
147 + ret = pthread_setname_np(ut, threadname);
148 +#endif
149 +
150 + if (ret)
151 + error("cannot set libuv thread name to %s. Err: %d", threadname, ret);
152 +}
153 +
154 static void *thread_start(void *ptr) {
155 netdata_thread = (NETDATA_THREAD *)ptr;
156
@@ -146,7 +163,7 @@ static void *thread_start(void *ptr) {
163 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
164 error("cannot set pthread cancel state to ENABLE.");
165
149 - thread_set_name(ptr);
166 + thread_set_name_np(ptr);
167
168 void *ret = NULL;
169 pthread_cleanup_push(thread_cleanup, ptr);
libnetdata/threads/threads.h
+3
@@ -31,6 +31,9 @@ extern int netdata_thread_cancel(netdata_thread_t thread);
31 extern int netdata_thread_join(netdata_thread_t thread, void **retval);
32 extern int netdata_thread_detach(pthread_t thread);
33
34 +#define NETDATA_THREAD_NAME_MAX 15
35 +extern void uv_thread_set_name_np(uv_thread_t ut, const char* name);
36 +
37 #define netdata_thread_self pthread_self
38 #define netdata_thread_testcancel pthread_testcancel
39