thread.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. Copyright (c) 2016 Anton Valentinov Kirilov
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  4. associated documentation files (the "Software"), to deal in the Software without restriction,
  5. including without limitation the rights to use, copy, modify, merge, publish, distribute,
  6. sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  7. furnished to do so, subject to the following conditions:
  8. The above copyright notice and this permission notice shall be included in all copies or
  9. substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  11. NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  12. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  13. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
  14. OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  15. */
  16. #define _GNU_SOURCE
  17. #include <h2o.h>
  18. #include <limits.h>
  19. #include <numaif.h>
  20. #include <pthread.h>
  21. #include <sched.h>
  22. #include <stdbool.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <h2o/serverutil.h>
  27. #include <sys/syscall.h>
  28. #include "error.h"
  29. #include "event_loop.h"
  30. #include "global_data.h"
  31. #include "request_handler.h"
  32. #include "thread.h"
  33. static void *run_thread(void *arg);
  34. static void set_thread_memory_allocation_policy(void);
  35. static void *run_thread(void *arg)
  36. {
  37. thread_context_t ctx;
  38. initialize_thread_context(arg, false, &ctx);
  39. // There is no need to set a memory allocation policy unless
  40. // the application controls the processor affinity as well.
  41. if (!(ctx.global_thread_data->config->thread_num % h2o_numproc()))
  42. set_thread_memory_allocation_policy();
  43. event_loop(&ctx);
  44. free_thread_context(&ctx);
  45. pthread_exit(NULL);
  46. }
  47. static void set_thread_memory_allocation_policy(void)
  48. {
  49. void *stack_addr;
  50. size_t stack_size;
  51. unsigned memory_node;
  52. pthread_attr_t attr;
  53. CHECK_ERRNO(syscall, SYS_getcpu, NULL, &memory_node, NULL);
  54. CHECK_ERROR(pthread_getattr_np, pthread_self(), &attr);
  55. CHECK_ERROR(pthread_attr_getstack, &attr, &stack_addr, &stack_size);
  56. pthread_attr_destroy(&attr);
  57. unsigned long nodemask[
  58. (memory_node + sizeof(unsigned long) * CHAR_BIT) / (sizeof(unsigned long) * CHAR_BIT)];
  59. memset(nodemask, 0, sizeof(nodemask));
  60. nodemask[memory_node / (sizeof(*nodemask) * CHAR_BIT)] |=
  61. 1UL << (memory_node % (sizeof(*nodemask) * CHAR_BIT));
  62. if (mbind(stack_addr,
  63. stack_size,
  64. MPOL_PREFERRED,
  65. nodemask,
  66. memory_node + 1,
  67. MPOL_MF_MOVE | MPOL_MF_STRICT))
  68. STANDARD_ERROR("mbind");
  69. else if (set_mempolicy(MPOL_PREFERRED, NULL, 0))
  70. STANDARD_ERROR("set_mempolicy");
  71. }
  72. void free_thread_context(thread_context_t *ctx)
  73. {
  74. cleanup_request_handler_thread_data(&ctx->request_handler_data);
  75. free_event_loop(&ctx->event_loop, &ctx->global_thread_data->h2o_receiver);
  76. if (ctx->json_generator)
  77. do {
  78. json_generator_t * const gen = H2O_STRUCT_FROM_MEMBER(json_generator_t,
  79. l,
  80. ctx->json_generator);
  81. ctx->json_generator = gen->l.next;
  82. free_json_generator(gen, NULL, NULL, 0);
  83. } while (ctx->json_generator);
  84. }
  85. global_thread_data_t *initialize_global_thread_data(const config_t *config,
  86. global_data_t *global_data)
  87. {
  88. const size_t sz = config->thread_num * sizeof(thread_context_t);
  89. // The global thread data is modified only at program initialization and termination,
  90. // and is not accessed by performance-sensitive code, so false sharing is not a concern.
  91. global_thread_data_t * const ret = aligned_alloc(global_data->memory_alignment, sz);
  92. if (ret) {
  93. memset(ret, 0, sz);
  94. for (size_t i = 0; i < config->thread_num; i++) {
  95. ret[i].config = config;
  96. ret[i].global_data = global_data;
  97. }
  98. }
  99. else
  100. STANDARD_ERROR("aligned_alloc");
  101. return ret;
  102. }
  103. void initialize_thread_context(global_thread_data_t *global_thread_data,
  104. bool is_main_thread,
  105. thread_context_t *ctx)
  106. {
  107. memset(ctx, 0, sizeof(*ctx));
  108. ctx->global_thread_data = global_thread_data;
  109. ctx->random_seed = syscall(SYS_gettid);
  110. initialize_event_loop(is_main_thread,
  111. global_thread_data->global_data,
  112. &global_thread_data->h2o_receiver,
  113. &ctx->event_loop);
  114. initialize_request_handler_thread_data(ctx);
  115. global_thread_data->ctx = ctx;
  116. }
  117. void start_threads(global_thread_data_t *global_thread_data)
  118. {
  119. pthread_attr_t attr;
  120. const size_t num_cpus = h2o_numproc();
  121. const size_t cpusetsize = CPU_ALLOC_SIZE(num_cpus);
  122. cpu_set_t * const cpuset = CPU_ALLOC(num_cpus);
  123. if (!cpuset) {
  124. STANDARD_ERROR("CPU_ALLOC");
  125. abort();
  126. }
  127. CHECK_ERROR(pthread_attr_init, &attr);
  128. // The first thread context is used by the main thread.
  129. global_thread_data->thread = pthread_self();
  130. // If the number of threads is not a multiple of the number of processors, then
  131. // let the scheduler decide how to balance the load.
  132. if (global_thread_data->config->thread_num % num_cpus == 0) {
  133. CPU_ZERO_S(cpusetsize, cpuset);
  134. CPU_SET_S(0, cpusetsize, cpuset);
  135. CHECK_ERROR(pthread_setaffinity_np, global_thread_data->thread, cpusetsize, cpuset);
  136. }
  137. for (size_t i = global_thread_data->config->thread_num - 1; i > 0; i--) {
  138. if (global_thread_data->config->thread_num % num_cpus == 0) {
  139. CPU_ZERO_S(cpusetsize, cpuset);
  140. CPU_SET_S(i % num_cpus, cpusetsize, cpuset);
  141. CHECK_ERROR(pthread_attr_setaffinity_np, &attr, cpusetsize, cpuset);
  142. }
  143. h2o_multithread_create_thread(&global_thread_data[i].thread,
  144. &attr,
  145. run_thread,
  146. global_thread_data + i);
  147. }
  148. pthread_attr_destroy(&attr);
  149. CPU_FREE(cpuset);
  150. }