summaryrefslogtreecommitdiffstats
path: root/libnetdata/threads
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-05-08 16:27:08 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-05-08 16:27:08 +0000
commit81581f9719bc56f01d5aa08952671d65fda9867a (patch)
tree0f5c6b6138bf169c23c9d24b1fc0a3521385cb18 /libnetdata/threads
parentReleasing debian version 1.38.1-1. (diff)
downloadnetdata-81581f9719bc56f01d5aa08952671d65fda9867a.tar.xz
netdata-81581f9719bc56f01d5aa08952671d65fda9867a.zip
Merging upstream version 1.39.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'libnetdata/threads')
-rw-r--r--libnetdata/threads/README.md2
-rw-r--r--libnetdata/threads/threads.c74
-rw-r--r--libnetdata/threads/threads.h2
3 files changed, 68 insertions, 10 deletions
diff --git a/libnetdata/threads/README.md b/libnetdata/threads/README.md
index 71979feac..7e9e493f4 100644
--- a/libnetdata/threads/README.md
+++ b/libnetdata/threads/README.md
@@ -4,7 +4,7 @@ custom_edit_url: https://github.com/netdata/netdata/edit/master/libnetdata/threa
sidebar_label: "Threads"
learn_status: "Published"
learn_topic_type: "Tasks"
-learn_rel_path: "Developers/libnetdata libraries"
+learn_rel_path: "Developers/libnetdata"
-->
# Threads
diff --git a/libnetdata/threads/threads.c b/libnetdata/threads/threads.c
index 16de45fd1..a4591d5ac 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);
diff --git a/libnetdata/threads/threads.h b/libnetdata/threads/threads.h
index ccc18aff0..ad31b8816 100644
--- a/libnetdata/threads/threads.h
+++ b/libnetdata/threads/threads.h
@@ -43,6 +43,8 @@ int netdata_thread_detach(pthread_t thread);
void uv_thread_set_name_np(uv_thread_t ut, const char* name);
void os_thread_get_current_name_np(char threadname[NETDATA_THREAD_NAME_MAX + 1]);
+void webrtc_set_thread_name(void);
+
#define netdata_thread_self pthread_self
#define netdata_thread_testcancel pthread_testcancel