threads.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "../libnetdata.h"
  3. static size_t default_stacksize = 0, wanted_stacksize = 0;
  4. static pthread_attr_t *attr = NULL;
  5. // ----------------------------------------------------------------------------
  6. // per thread data
  7. typedef struct {
  8. void *arg;
  9. pthread_t *thread;
  10. const char *tag;
  11. void *(*start_routine) (void *);
  12. NETDATA_THREAD_OPTIONS options;
  13. } NETDATA_THREAD;
  14. static __thread NETDATA_THREAD *netdata_thread = NULL;
  15. inline int netdata_thread_tag_exists(void) {
  16. return (netdata_thread && netdata_thread->tag && *netdata_thread->tag);
  17. }
  18. const char *netdata_thread_tag(void) {
  19. return (netdata_thread_tag_exists() ? netdata_thread->tag : "MAIN");
  20. }
  21. // ----------------------------------------------------------------------------
  22. // compatibility library functions
  23. pid_t gettid(void) {
  24. #ifdef __FreeBSD__
  25. return (pid_t)pthread_getthreadid_np();
  26. #elif defined(__APPLE__)
  27. #if (defined __MAC_OS_X_VERSION_MIN_REQUIRED && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060)
  28. uint64_t curthreadid;
  29. pthread_threadid_np(NULL, &curthreadid);
  30. return (pid_t)curthreadid;
  31. #else /* __MAC_OS_X_VERSION_MIN_REQUIRED */
  32. return (pid_t)pthread_self;
  33. #endif /* __MAC_OS_X_VERSION_MIN_REQUIRED */
  34. #else /* __APPLE__*/
  35. return (pid_t)syscall(SYS_gettid);
  36. #endif /* __FreeBSD__, __APPLE__*/
  37. }
  38. // ----------------------------------------------------------------------------
  39. // early initialization
  40. size_t netdata_threads_init(void) {
  41. int i;
  42. // --------------------------------------------------------------------
  43. // get the required stack size of the threads of netdata
  44. attr = callocz(1, sizeof(pthread_attr_t));
  45. i = pthread_attr_init(attr);
  46. if(i != 0)
  47. fatal("pthread_attr_init() failed with code %d.", i);
  48. i = pthread_attr_getstacksize(attr, &default_stacksize);
  49. if(i != 0)
  50. fatal("pthread_attr_getstacksize() failed with code %d.", i);
  51. else
  52. debug(D_OPTIONS, "initial pthread stack size is %zu bytes", default_stacksize);
  53. return default_stacksize;
  54. }
  55. // ----------------------------------------------------------------------------
  56. // late initialization
  57. void netdata_threads_init_after_fork(size_t stacksize) {
  58. wanted_stacksize = stacksize;
  59. int i;
  60. // ------------------------------------------------------------------------
  61. // set default pthread stack size
  62. if(attr && default_stacksize < wanted_stacksize && wanted_stacksize > 0) {
  63. i = pthread_attr_setstacksize(attr, wanted_stacksize);
  64. if(i != 0)
  65. fatal("pthread_attr_setstacksize() to %zu bytes, failed with code %d.", wanted_stacksize, i);
  66. else
  67. debug(D_SYSTEM, "Successfully set pthread stacksize to %zu bytes", wanted_stacksize);
  68. }
  69. }
  70. // ----------------------------------------------------------------------------
  71. // netdata_thread_create
  72. static void thread_cleanup(void *ptr) {
  73. if(netdata_thread != ptr) {
  74. NETDATA_THREAD *info = (NETDATA_THREAD *)ptr;
  75. 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);
  76. }
  77. if(!(netdata_thread->options & NETDATA_THREAD_OPTION_DONT_LOG_CLEANUP))
  78. info("thread with task id %d finished", gettid());
  79. freez((void *)netdata_thread->tag);
  80. netdata_thread->tag = NULL;
  81. freez(netdata_thread);
  82. netdata_thread = NULL;
  83. }
  84. static void thread_set_name_np(NETDATA_THREAD *nt) {
  85. if (nt->tag) {
  86. int ret = 0;
  87. char threadname[NETDATA_THREAD_NAME_MAX+1];
  88. strncpyz(threadname, nt->tag, NETDATA_THREAD_NAME_MAX);
  89. #if defined(__FreeBSD__)
  90. pthread_set_name_np(pthread_self(), threadname);
  91. #elif defined(__APPLE__)
  92. ret = pthread_setname_np(threadname);
  93. #else
  94. ret = pthread_setname_np(pthread_self(), threadname);
  95. #endif
  96. if (ret != 0)
  97. error("cannot set pthread name of %d to %s. ErrCode: %d", gettid(), threadname, ret);
  98. else
  99. info("set name of thread %d to %s", gettid(), threadname);
  100. }
  101. }
  102. void uv_thread_set_name_np(uv_thread_t ut, const char* name) {
  103. int ret = 0;
  104. char threadname[NETDATA_THREAD_NAME_MAX+1];
  105. strncpyz(threadname, name, NETDATA_THREAD_NAME_MAX);
  106. #if defined(__FreeBSD__)
  107. pthread_set_name_np(ut, threadname);
  108. #elif defined(__APPLE__)
  109. // Apple can only set its own name
  110. UNUSED(ut);
  111. #else
  112. ret = pthread_setname_np(ut, threadname);
  113. #endif
  114. if (ret)
  115. info("cannot set libuv thread name to %s. Err: %d", threadname, ret);
  116. }
  117. void os_thread_get_current_name_np(char threadname[NETDATA_THREAD_NAME_MAX + 1])
  118. {
  119. threadname[0] = '\0';
  120. #if defined(__FreeBSD__)
  121. pthread_get_name_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX + 1);
  122. #elif defined(HAVE_PTHREAD_GETNAME_NP) /* Linux & macOS */
  123. (void)pthread_getname_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX + 1);
  124. #endif
  125. }
  126. static void *thread_start(void *ptr) {
  127. netdata_thread = (NETDATA_THREAD *)ptr;
  128. if(!(netdata_thread->options & NETDATA_THREAD_OPTION_DONT_LOG_STARTUP))
  129. info("thread created with task id %d", gettid());
  130. if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
  131. error("cannot set pthread cancel type to DEFERRED.");
  132. if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
  133. error("cannot set pthread cancel state to ENABLE.");
  134. thread_set_name_np(ptr);
  135. void *ret = NULL;
  136. pthread_cleanup_push(thread_cleanup, ptr);
  137. ret = netdata_thread->start_routine(netdata_thread->arg);
  138. pthread_cleanup_pop(1);
  139. return ret;
  140. }
  141. int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THREAD_OPTIONS options, void *(*start_routine) (void *), void *arg) {
  142. NETDATA_THREAD *info = mallocz(sizeof(NETDATA_THREAD));
  143. info->arg = arg;
  144. info->thread = thread;
  145. info->tag = strdupz(tag);
  146. info->start_routine = start_routine;
  147. info->options = options;
  148. int ret = pthread_create(thread, attr, thread_start, info);
  149. if(ret != 0)
  150. error("failed to create new thread for %s. pthread_create() failed with code %d", tag, ret);
  151. else {
  152. if (!(options & NETDATA_THREAD_OPTION_JOINABLE)) {
  153. int ret2 = pthread_detach(*thread);
  154. if (ret2 != 0)
  155. error("cannot request detach of newly created %s thread. pthread_detach() failed with code %d", tag, ret2);
  156. }
  157. }
  158. return ret;
  159. }
  160. // ----------------------------------------------------------------------------
  161. // netdata_thread_cancel
  162. int netdata_thread_cancel(netdata_thread_t thread) {
  163. int ret = pthread_cancel(thread);
  164. if(ret != 0)
  165. error("cannot cancel thread. pthread_cancel() failed with code %d.", ret);
  166. return ret;
  167. }
  168. // ----------------------------------------------------------------------------
  169. // netdata_thread_join
  170. int netdata_thread_join(netdata_thread_t thread, void **retval) {
  171. int ret = pthread_join(thread, retval);
  172. if(ret != 0)
  173. error("cannot join thread. pthread_join() failed with code %d.", ret);
  174. return ret;
  175. }
  176. int netdata_thread_detach(pthread_t thread) {
  177. int ret = pthread_detach(thread);
  178. if(ret != 0)
  179. error("cannot detach thread. pthread_detach() failed with code %d.", ret);
  180. return ret;
  181. }