threads.c 11 KB

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