threads.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. SPINLOCK detach_lock;
  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. void rrd_collector_finished(void);
  142. static void thread_cleanup(void *ptr) {
  143. if(netdata_thread != ptr) {
  144. NETDATA_THREAD *info = (NETDATA_THREAD *)ptr;
  145. 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);
  146. }
  147. spinlock_lock(&netdata_thread->detach_lock);
  148. if(!(netdata_thread->options & NETDATA_THREAD_OPTION_DONT_LOG_CLEANUP))
  149. netdata_log_info("thread with task id %d finished", gettid());
  150. rrd_collector_finished();
  151. sender_thread_buffer_free();
  152. rrdset_thread_rda_free();
  153. query_target_free();
  154. thread_cache_destroy();
  155. service_exits();
  156. worker_unregister();
  157. netdata_thread->tag[0] = '\0';
  158. spinlock_unlock(&netdata_thread->detach_lock);
  159. freez(netdata_thread);
  160. netdata_thread = NULL;
  161. }
  162. static void thread_set_name_np(NETDATA_THREAD *nt) {
  163. if (nt && nt->tag[0]) {
  164. int ret = 0;
  165. char threadname[NETDATA_THREAD_NAME_MAX+1];
  166. strncpyz(threadname, nt->tag, NETDATA_THREAD_NAME_MAX);
  167. #if defined(__FreeBSD__)
  168. pthread_set_name_np(pthread_self(), threadname);
  169. #elif defined(__APPLE__)
  170. ret = pthread_setname_np(threadname);
  171. #else
  172. ret = pthread_setname_np(pthread_self(), threadname);
  173. #endif
  174. if (ret != 0)
  175. netdata_log_error("cannot set pthread name of %d to %s. ErrCode: %d", gettid(), threadname, ret);
  176. else
  177. netdata_log_info("set name of thread %d to %s", gettid(), threadname);
  178. }
  179. }
  180. void uv_thread_set_name_np(uv_thread_t ut, const char* name) {
  181. int ret = 0;
  182. char threadname[NETDATA_THREAD_NAME_MAX+1];
  183. strncpyz(threadname, name, NETDATA_THREAD_NAME_MAX);
  184. #if defined(__FreeBSD__)
  185. pthread_set_name_np(ut ? ut : pthread_self(), threadname);
  186. #elif defined(__APPLE__)
  187. // Apple can only set its own name
  188. UNUSED(ut);
  189. #else
  190. ret = pthread_setname_np(ut ? ut : pthread_self(), threadname);
  191. #endif
  192. thread_name_get(true);
  193. if (ret)
  194. netdata_log_info("cannot set libuv thread name to %s. Err: %d", threadname, ret);
  195. }
  196. void os_thread_get_current_name_np(char threadname[NETDATA_THREAD_NAME_MAX + 1])
  197. {
  198. threadname[0] = '\0';
  199. #if defined(__FreeBSD__)
  200. pthread_get_name_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX + 1);
  201. #elif defined(HAVE_PTHREAD_GETNAME_NP) /* Linux & macOS */
  202. (void)pthread_getname_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX + 1);
  203. #endif
  204. }
  205. static void *netdata_thread_init(void *ptr) {
  206. netdata_thread = (NETDATA_THREAD *)ptr;
  207. if(!(netdata_thread->options & NETDATA_THREAD_OPTION_DONT_LOG_STARTUP))
  208. netdata_log_info("thread created with task id %d", gettid());
  209. if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
  210. netdata_log_error("cannot set pthread cancel type to DEFERRED.");
  211. if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
  212. netdata_log_error("cannot set pthread cancel state to ENABLE.");
  213. thread_set_name_np(ptr);
  214. void *ret = NULL;
  215. pthread_cleanup_push(thread_cleanup, ptr);
  216. ret = netdata_thread->start_routine(netdata_thread->arg);
  217. pthread_cleanup_pop(1);
  218. return ret;
  219. }
  220. int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THREAD_OPTIONS options, void *(*start_routine) (void *), void *arg) {
  221. NETDATA_THREAD *info = callocz(1, sizeof(NETDATA_THREAD));
  222. info->arg = arg;
  223. info->start_routine = start_routine;
  224. info->options = options;
  225. strncpyz(info->tag, tag, NETDATA_THREAD_NAME_MAX);
  226. spinlock_init(&info->detach_lock);
  227. spinlock_lock(&info->detach_lock);
  228. int ret = pthread_create(thread, netdata_threads_attr, netdata_thread_init, info);
  229. if(ret != 0)
  230. netdata_log_error("failed to create new thread for %s. pthread_create() failed with code %d", tag, ret);
  231. else {
  232. if (!(options & NETDATA_THREAD_OPTION_JOINABLE)) {
  233. int ret2 = pthread_detach(*thread);
  234. if (ret2 != 0)
  235. netdata_log_error("cannot request detach of newly created %s thread. pthread_detach() failed with code %d", tag, ret2);
  236. }
  237. }
  238. spinlock_unlock(&info->detach_lock);
  239. return ret;
  240. }
  241. // ----------------------------------------------------------------------------
  242. // netdata_thread_cancel
  243. #ifdef NETDATA_INTERNAL_CHECKS
  244. int netdata_thread_cancel_with_trace(netdata_thread_t thread, int line, const char *file, const char *function) {
  245. #else
  246. int netdata_thread_cancel(netdata_thread_t thread) {
  247. #endif
  248. int ret = pthread_cancel(thread);
  249. if(ret != 0)
  250. #ifdef NETDATA_INTERNAL_CHECKS
  251. netdata_log_error("cannot cancel thread. pthread_cancel() failed with code %d at %d@%s, function %s()", ret, line, file, function);
  252. #else
  253. netdata_log_error("cannot cancel thread. pthread_cancel() failed with code %d.", ret);
  254. #endif
  255. return ret;
  256. }
  257. // ----------------------------------------------------------------------------
  258. // netdata_thread_join
  259. int netdata_thread_join(netdata_thread_t thread, void **retval) {
  260. int ret = pthread_join(thread, retval);
  261. if(ret != 0)
  262. netdata_log_error("cannot join thread. pthread_join() failed with code %d.", ret);
  263. return ret;
  264. }
  265. int netdata_thread_detach(pthread_t thread) {
  266. int ret = pthread_detach(thread);
  267. if(ret != 0)
  268. netdata_log_error("cannot detach thread. pthread_detach() failed with code %d.", ret);
  269. return ret;
  270. }