diff options
Diffstat (limited to 'libnetdata/threads/threads.c')
-rw-r--r-- | libnetdata/threads/threads.c | 74 |
1 files changed, 65 insertions, 9 deletions
diff --git a/libnetdata/threads/threads.c b/libnetdata/threads/threads.c index 16de45fd..a4591d5a 100644 --- a/libnetdata/threads/threads.c +++ b/libnetdata/threads/threads.c @@ -10,7 +10,7 @@ static pthread_attr_t *netdata_threads_attr = NULL; typedef struct { void *arg; pthread_t *thread; - const char *tag; + char tag[NETDATA_THREAD_NAME_MAX + 1]; void *(*start_routine) (void *); NETDATA_THREAD_OPTIONS options; } NETDATA_THREAD; @@ -18,11 +18,66 @@ typedef struct { static __thread NETDATA_THREAD *netdata_thread = NULL; inline int netdata_thread_tag_exists(void) { - return (netdata_thread && netdata_thread->tag && *netdata_thread->tag); + return (netdata_thread && *netdata_thread->tag); +} + +static const char *thread_name_get(bool recheck) { + static __thread char threadname[NETDATA_THREAD_NAME_MAX + 1] = ""; + + if(netdata_thread_tag_exists()) + strncpyz(threadname, netdata_thread->tag, NETDATA_THREAD_NAME_MAX); + else { + if(!recheck && threadname[0]) + return threadname; + +#if defined(__FreeBSD__) + pthread_get_name_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX + 1); + if(strcmp(threadname, "netdata") == 0) + strncpyz(threadname, "MAIN", NETDATA_THREAD_NAME_MAX); +#elif defined(__APPLE__) + strncpyz(threadname, "MAIN", NETDATA_THREAD_NAME_MAX); +#elif defined(HAVE_PTHREAD_GETNAME_NP) + pthread_getname_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX + 1); + if(strcmp(threadname, "netdata") == 0) + strncpyz(threadname, "MAIN", NETDATA_THREAD_NAME_MAX); +#else + strncpyz(threadname, "MAIN", NETDATA_THREAD_NAME_MAX); +#endif + } + + return threadname; } const char *netdata_thread_tag(void) { - return (netdata_thread_tag_exists() ? netdata_thread->tag : "MAIN"); + return thread_name_get(false); +} + +static size_t webrtc_id = 0; +static __thread bool webrtc_name_set = false; +void webrtc_set_thread_name(void) { + if(!netdata_thread && !webrtc_name_set) { + webrtc_name_set = true; + char threadname[NETDATA_THREAD_NAME_MAX + 1]; + +#if defined(__FreeBSD__) + snprintfz(threadname, NETDATA_THREAD_NAME_MAX, "WEBRTC[%zu]", __atomic_fetch_add(&webrtc_id, 1, __ATOMIC_RELAXED)); + pthread_set_name_np(pthread_self(), threadname); +#elif defined(__APPLE__) + snprintfz(threadname, NETDATA_THREAD_NAME_MAX, "WEBRTC[%zu]", __atomic_fetch_add(&webrtc_id, 1, __ATOMIC_RELAXED)); + pthread_setname_np(threadname); +#elif defined(HAVE_PTHREAD_GETNAME_NP) + pthread_getname_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX+1); + if(strcmp(threadname, "netdata") == 0) { + snprintfz(threadname, NETDATA_THREAD_NAME_MAX, "WEBRTC[%zu]", __atomic_fetch_add(&webrtc_id, 1, __ATOMIC_RELAXED)); + pthread_setname_np(pthread_self(), threadname); + } +#else + snprintfz(threadname, NETDATA_THREAD_NAME_MAX, "WEBRTC[%zu]", __atomic_fetch_add(&webrtc_id, 1, __ATOMIC_RELAXED)); + pthread_setname_np(pthread_self(), threadname); +#endif + + thread_name_get(true); + } } // ---------------------------------------------------------------------------- @@ -127,8 +182,7 @@ static void thread_cleanup(void *ptr) { service_exits(); worker_unregister(); - freez((void *)netdata_thread->tag); - netdata_thread->tag = NULL; + netdata_thread->tag[0] = '\0'; freez(netdata_thread); netdata_thread = NULL; @@ -136,7 +190,7 @@ static void thread_cleanup(void *ptr) { static void thread_set_name_np(NETDATA_THREAD *nt) { - if (nt->tag) { + if (nt && nt->tag[0]) { int ret = 0; char threadname[NETDATA_THREAD_NAME_MAX+1]; @@ -173,6 +227,8 @@ void uv_thread_set_name_np(uv_thread_t ut, const char* name) { ret = pthread_setname_np(ut, threadname); #endif + thread_name_get(true); + if (ret) info("cannot set libuv thread name to %s. Err: %d", threadname, ret); } @@ -187,7 +243,7 @@ void os_thread_get_current_name_np(char threadname[NETDATA_THREAD_NAME_MAX + 1]) #endif } -static void *thread_start(void *ptr) { +static void *netdata_thread_init(void *ptr) { netdata_thread = (NETDATA_THREAD *)ptr; if(!(netdata_thread->options & NETDATA_THREAD_OPTION_DONT_LOG_STARTUP)) @@ -213,11 +269,11 @@ int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THR NETDATA_THREAD *info = mallocz(sizeof(NETDATA_THREAD)); info->arg = arg; info->thread = thread; - info->tag = strdupz(tag); info->start_routine = start_routine; info->options = options; + strncpyz(info->tag, tag, NETDATA_THREAD_NAME_MAX); - int ret = pthread_create(thread, netdata_threads_attr, thread_start, info); + int ret = pthread_create(thread, netdata_threads_attr, netdata_thread_init, info); if(ret != 0) error("failed to create new thread for %s. pthread_create() failed with code %d", tag, ret); |