threads.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "../libnetdata.h"
  3. static pthread_attr_t *netdata_threads_attr = NULL;
  4. // ----------------------------------------------------------------------------
  5. // per thread data
  6. typedef struct {
  7. void *arg;
  8. char tag[NETDATA_THREAD_NAME_MAX + 1];
  9. void *(*start_routine) (void *);
  10. NETDATA_THREAD_OPTIONS options;
  11. } NETDATA_THREAD;
  12. static __thread NETDATA_THREAD *netdata_thread = NULL;
  13. inline int netdata_thread_tag_exists(void) {
  14. return (netdata_thread && *netdata_thread->tag);
  15. }
  16. static const char *thread_name_get(bool recheck) {
  17. static __thread char threadname[NETDATA_THREAD_NAME_MAX + 1] = "";
  18. if(netdata_thread_tag_exists())
  19. strncpyz(threadname, netdata_thread->tag, NETDATA_THREAD_NAME_MAX);
  20. else {
  21. if(!recheck && threadname[0])
  22. return threadname;
  23. #if defined(__FreeBSD__)
  24. pthread_get_name_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX + 1);
  25. if(strcmp(threadname, "netdata") == 0)
  26. strncpyz(threadname, "MAIN", NETDATA_THREAD_NAME_MAX);
  27. #elif defined(__APPLE__)
  28. strncpyz(threadname, "MAIN", NETDATA_THREAD_NAME_MAX);
  29. #elif defined(HAVE_PTHREAD_GETNAME_NP)
  30. pthread_getname_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX + 1);
  31. if(strcmp(threadname, "netdata") == 0)
  32. strncpyz(threadname, "MAIN", NETDATA_THREAD_NAME_MAX);
  33. #else
  34. strncpyz(threadname, "MAIN", NETDATA_THREAD_NAME_MAX);
  35. #endif
  36. }
  37. return threadname;
  38. }
  39. const char *netdata_thread_tag(void) {
  40. return thread_name_get(false);
  41. }
  42. static size_t webrtc_id = 0;
  43. static __thread bool webrtc_name_set = false;
  44. void webrtc_set_thread_name(void) {
  45. if(!netdata_thread && !webrtc_name_set) {
  46. webrtc_name_set = true;
  47. char threadname[NETDATA_THREAD_NAME_MAX + 1];
  48. #if defined(__FreeBSD__)
  49. snprintfz(threadname, NETDATA_THREAD_NAME_MAX, "WEBRTC[%zu]", __atomic_fetch_add(&webrtc_id, 1, __ATOMIC_RELAXED));
  50. pthread_set_name_np(pthread_self(), threadname);
  51. #elif defined(__APPLE__)
  52. snprintfz(threadname, NETDATA_THREAD_NAME_MAX, "WEBRTC[%zu]", __atomic_fetch_add(&webrtc_id, 1, __ATOMIC_RELAXED));
  53. pthread_setname_np(threadname);
  54. #elif defined(HAVE_PTHREAD_GETNAME_NP)
  55. pthread_getname_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX+1);
  56. if(strcmp(threadname, "netdata") == 0) {
  57. snprintfz(threadname, NETDATA_THREAD_NAME_MAX, "WEBRTC[%zu]", __atomic_fetch_add(&webrtc_id, 1, __ATOMIC_RELAXED));
  58. pthread_setname_np(pthread_self(), threadname);
  59. }
  60. #else
  61. snprintfz(threadname, NETDATA_THREAD_NAME_MAX, "WEBRTC[%zu]", __atomic_fetch_add(&webrtc_id, 1, __ATOMIC_RELAXED));
  62. pthread_setname_np(pthread_self(), threadname);
  63. #endif
  64. thread_name_get(true);
  65. }
  66. }
  67. // ----------------------------------------------------------------------------
  68. // compatibility library functions
  69. static __thread pid_t gettid_cached_tid = 0;
  70. pid_t gettid(void) {
  71. pid_t tid = 0;
  72. if(likely(gettid_cached_tid > 0))
  73. return gettid_cached_tid;
  74. #ifdef __FreeBSD__
  75. tid = (pid_t)pthread_getthreadid_np();
  76. #elif defined(__APPLE__)
  77. #if (defined __MAC_OS_X_VERSION_MIN_REQUIRED && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060)
  78. uint64_t curthreadid;
  79. pthread_threadid_np(NULL, &curthreadid);
  80. tid = (pid_t)curthreadid;
  81. #else /* __MAC_OS_X_VERSION_MIN_REQUIRED */
  82. tid = (pid_t)pthread_self;
  83. #endif /* __MAC_OS_X_VERSION_MIN_REQUIRED */
  84. #else /* __APPLE__*/
  85. tid = (pid_t)syscall(SYS_gettid);
  86. #endif /* __FreeBSD__, __APPLE__*/
  87. gettid_cached_tid = tid;
  88. return tid;
  89. }
  90. // ----------------------------------------------------------------------------
  91. // early initialization
  92. size_t netdata_threads_init(void) {
  93. int i;
  94. // --------------------------------------------------------------------
  95. // get the required stack size of the threads of netdata
  96. if(!netdata_threads_attr) {
  97. netdata_threads_attr = callocz(1, sizeof(pthread_attr_t));
  98. i = pthread_attr_init(netdata_threads_attr);
  99. if (i != 0)
  100. fatal("pthread_attr_init() failed with code %d.", i);
  101. }
  102. size_t stacksize = 0;
  103. i = pthread_attr_getstacksize(netdata_threads_attr, &stacksize);
  104. if(i != 0)
  105. fatal("pthread_attr_getstacksize() failed with code %d.", i);
  106. return stacksize;
  107. }
  108. // ----------------------------------------------------------------------------
  109. // late initialization
  110. void netdata_threads_init_after_fork(size_t stacksize) {
  111. int i;
  112. // ------------------------------------------------------------------------
  113. // set pthread stack size
  114. if(netdata_threads_attr && stacksize > (size_t)PTHREAD_STACK_MIN) {
  115. i = pthread_attr_setstacksize(netdata_threads_attr, stacksize);
  116. if(i != 0)
  117. nd_log(NDLS_DAEMON, NDLP_WARNING, "pthread_attr_setstacksize() to %zu bytes, failed with code %d.", stacksize, i);
  118. else
  119. nd_log(NDLS_DAEMON, NDLP_DEBUG, "Set threads stack size to %zu bytes", stacksize);
  120. }
  121. else
  122. nd_log(NDLS_DAEMON, NDLP_WARNING, "Invalid pthread stacksize %zu", stacksize);
  123. }
  124. // ----------------------------------------------------------------------------
  125. // threads init for external plugins
  126. void netdata_threads_init_for_external_plugins(size_t stacksize) {
  127. size_t default_stacksize = netdata_threads_init();
  128. if(default_stacksize < 1 * 1024 * 1024)
  129. default_stacksize = 1 * 1024 * 1024;
  130. netdata_threads_init_after_fork(stacksize ? stacksize : default_stacksize);
  131. }
  132. // ----------------------------------------------------------------------------
  133. // netdata_thread_create
  134. void rrdset_thread_rda_free(void);
  135. void sender_thread_buffer_free(void);
  136. void query_target_free(void);
  137. void service_exits(void);
  138. void rrd_collector_finished(void);
  139. static void thread_cleanup(void *ptr) {
  140. if(netdata_thread != ptr) {
  141. NETDATA_THREAD *info = (NETDATA_THREAD *)ptr;
  142. nd_log(NDLS_DAEMON, NDLP_ERR, "THREADS: internal error - thread local variable does not match the one passed to this function. Expected thread '%s', passed thread '%s'", netdata_thread->tag, info->tag);
  143. }
  144. if(!(netdata_thread->options & NETDATA_THREAD_OPTION_DONT_LOG_CLEANUP))
  145. nd_log(NDLS_DAEMON, NDLP_DEBUG, "thread with task id %d finished", gettid());
  146. rrd_collector_finished();
  147. sender_thread_buffer_free();
  148. rrdset_thread_rda_free();
  149. query_target_free();
  150. thread_cache_destroy();
  151. service_exits();
  152. worker_unregister();
  153. netdata_thread->tag[0] = '\0';
  154. freez(netdata_thread);
  155. netdata_thread = NULL;
  156. }
  157. void netdata_thread_set_tag(const char *tag) {
  158. if(!tag || !*tag)
  159. return;
  160. int ret = 0;
  161. char threadname[NETDATA_THREAD_NAME_MAX+1];
  162. strncpyz(threadname, tag, NETDATA_THREAD_NAME_MAX);
  163. #if defined(__FreeBSD__)
  164. pthread_set_name_np(pthread_self(), threadname);
  165. #elif defined(__APPLE__)
  166. ret = pthread_setname_np(threadname);
  167. #else
  168. ret = pthread_setname_np(pthread_self(), threadname);
  169. #endif
  170. if (ret != 0)
  171. nd_log(NDLS_DAEMON, NDLP_WARNING, "cannot set pthread name of %d to %s. ErrCode: %d", gettid(), threadname, ret);
  172. else
  173. nd_log(NDLS_DAEMON, NDLP_DEBUG, "set name of thread %d to %s", gettid(), threadname);
  174. if(netdata_thread) {
  175. strncpyz(netdata_thread->tag, threadname, sizeof(netdata_thread->tag) - 1);
  176. }
  177. }
  178. void uv_thread_set_name_np(uv_thread_t ut, const char* name) {
  179. int ret = 0;
  180. char threadname[NETDATA_THREAD_NAME_MAX+1];
  181. strncpyz(threadname, name, NETDATA_THREAD_NAME_MAX);
  182. #if defined(__FreeBSD__)
  183. pthread_set_name_np(ut ? ut : pthread_self(), threadname);
  184. #elif defined(__APPLE__)
  185. // Apple can only set its own name
  186. UNUSED(ut);
  187. #else
  188. ret = pthread_setname_np(ut ? ut : pthread_self(), threadname);
  189. #endif
  190. thread_name_get(true);
  191. if (ret)
  192. nd_log(NDLS_DAEMON, NDLP_NOTICE, "cannot set libuv thread name to %s. Err: %d", threadname, ret);
  193. }
  194. void os_thread_get_current_name_np(char threadname[NETDATA_THREAD_NAME_MAX + 1])
  195. {
  196. threadname[0] = '\0';
  197. #if defined(__FreeBSD__)
  198. pthread_get_name_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX + 1);
  199. #elif defined(HAVE_PTHREAD_GETNAME_NP) /* Linux & macOS */
  200. (void)pthread_getname_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX + 1);
  201. #endif
  202. }
  203. static void *netdata_thread_init(void *ptr) {
  204. netdata_thread = (NETDATA_THREAD *)ptr;
  205. if(!(netdata_thread->options & NETDATA_THREAD_OPTION_DONT_LOG_STARTUP))
  206. nd_log(NDLS_DAEMON, NDLP_DEBUG, "thread created with task id %d", gettid());
  207. if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
  208. nd_log(NDLS_DAEMON, NDLP_WARNING, "cannot set pthread cancel type to DEFERRED.");
  209. if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
  210. nd_log(NDLS_DAEMON, NDLP_WARNING, "cannot set pthread cancel state to ENABLE.");
  211. netdata_thread_set_tag(netdata_thread->tag);
  212. if (!(netdata_thread->options & NETDATA_THREAD_OPTION_JOINABLE)) {
  213. int rc = pthread_detach(pthread_self());
  214. if (rc != 0)
  215. nd_log(NDLS_DAEMON, NDLP_WARNING,
  216. "cannot request detach of newly created %s thread. pthread_detach() failed with code %d",
  217. netdata_thread->tag, rc);
  218. }
  219. void *ret = NULL;
  220. pthread_cleanup_push(thread_cleanup, ptr);
  221. ret = netdata_thread->start_routine(netdata_thread->arg);
  222. pthread_cleanup_pop(1);
  223. return ret;
  224. }
  225. int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THREAD_OPTIONS options, void *(*start_routine) (void *), void *arg) {
  226. NETDATA_THREAD *info = callocz(1, sizeof(NETDATA_THREAD));
  227. info->arg = arg;
  228. info->start_routine = start_routine;
  229. info->options = options;
  230. strncpyz(info->tag, tag, NETDATA_THREAD_NAME_MAX);
  231. int ret = pthread_create(thread, netdata_threads_attr, netdata_thread_init, info);
  232. if(ret != 0)
  233. nd_log(NDLS_DAEMON, NDLP_ERR, "failed to create new thread for %s. pthread_create() failed with code %d", tag, ret);
  234. return ret;
  235. }
  236. // ----------------------------------------------------------------------------
  237. // netdata_thread_cancel
  238. #ifdef NETDATA_INTERNAL_CHECKS
  239. int netdata_thread_cancel_with_trace(netdata_thread_t thread, int line, const char *file, const char *function) {
  240. #else
  241. int netdata_thread_cancel(netdata_thread_t thread) {
  242. #endif
  243. int ret = pthread_cancel(thread);
  244. if(ret != 0)
  245. #ifdef NETDATA_INTERNAL_CHECKS
  246. nd_log(NDLS_DAEMON, NDLP_WARNING, "cannot cancel thread. pthread_cancel() failed with code %d at %d@%s, function %s()", ret, line, file, function);
  247. #else
  248. nd_log(NDLS_DAEMON, NDLP_WARNING, "cannot cancel thread. pthread_cancel() failed with code %d.", ret);
  249. #endif
  250. return ret;
  251. }
  252. // ----------------------------------------------------------------------------
  253. // netdata_thread_join
  254. int netdata_thread_join(netdata_thread_t thread, void **retval) {
  255. int ret = pthread_join(thread, retval);
  256. if(ret != 0)
  257. nd_log(NDLS_DAEMON, NDLP_WARNING, "cannot join thread. pthread_join() failed with code %d.", ret);
  258. return ret;
  259. }
  260. int netdata_thread_detach(pthread_t thread) {
  261. int ret = pthread_detach(thread);
  262. if(ret != 0)
  263. nd_log(NDLS_DAEMON, NDLP_WARNING, "cannot detach thread. pthread_detach() failed with code %d.", ret);
  264. return ret;
  265. }