ThreadPoolMonitorThread.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include "il2cpp-config.h"
  2. #include "gc/GarbageCollector.h"
  3. #include "mono/ThreadPool/threadpool-ms.h"
  4. #include "mono/ThreadPool/ThreadPoolDataStructures.h"
  5. #include "mono/ThreadPool/ThreadPoolMacros.h"
  6. #include "mono/ThreadPool/ThreadPoolMonitorThread.h"
  7. #include "mono/ThreadPool/ThreadPoolWorkerThread.h"
  8. #include "vm/Runtime.h"
  9. #include "vm/Thread.h"
  10. #include "os/Time.h"
  11. #include "os/CpuInfo.h"
  12. #define MONITOR_INTERVAL 500 // ms
  13. #define MONITOR_MINIMAL_LIFETIME 60 * 1000 // ms
  14. static int32_t s_MonitorStatus = MONITOR_STATUS_NOT_RUNNING;
  15. MonitorStatus GetMonitorStatus()
  16. {
  17. return static_cast<MonitorStatus>(s_MonitorStatus);
  18. }
  19. static int32_t cpu_info_usage(void* prev)
  20. {
  21. // Note : Implementing CpuInfo on all platforms will be challenging, so for now we are going to cheat
  22. // and always say it's low
  23. #if !IL2CPP_USE_GENERIC_CPU_INFO
  24. return il2cpp::os::CpuInfo::Usage(prev);
  25. #else
  26. return CPU_USAGE_LOW;
  27. #endif
  28. }
  29. static Il2CppException* mono_thread_interruption_checkpoint(void)
  30. {
  31. // For now just do nothing. The one place this is used doesn't care about the return value
  32. return NULL;
  33. }
  34. /* LOCKING: threadpool->domains_lock must be held */
  35. static bool domain_any_has_request(void)
  36. {
  37. unsigned int i;
  38. for (i = 0; i < g_ThreadPool->domains.size(); ++i)
  39. {
  40. ThreadPoolDomain *tmp = g_ThreadPool->domains[i];
  41. if (tmp->outstanding_request > 0)
  42. return true;
  43. }
  44. return false;
  45. }
  46. static bool monitor_sufficient_delay_since_last_dequeue(void)
  47. {
  48. int64_t threshold;
  49. IL2CPP_ASSERT(g_ThreadPool);
  50. if (g_ThreadPool->cpu_usage < CPU_USAGE_LOW)
  51. {
  52. threshold = MONITOR_INTERVAL;
  53. }
  54. else
  55. {
  56. ThreadPoolCounter counter;
  57. counter.as_int64_t = COUNTER_READ();
  58. threshold = counter._.max_working * MONITOR_INTERVAL * 2;
  59. }
  60. return il2cpp::os::Time::GetTicksMillisecondsMonotonic() >= g_ThreadPool->heuristic_last_dequeue + threshold;
  61. }
  62. static bool monitor_should_keep_running(void)
  63. {
  64. static int64_t last_should_keep_running = -1;
  65. IL2CPP_ASSERT(s_MonitorStatus == MONITOR_STATUS_WAITING_FOR_REQUEST || s_MonitorStatus == MONITOR_STATUS_REQUESTED);
  66. if (il2cpp::os::Atomic::Exchange(&s_MonitorStatus, MONITOR_STATUS_WAITING_FOR_REQUEST) == MONITOR_STATUS_WAITING_FOR_REQUEST)
  67. {
  68. bool should_keep_running = true, force_should_keep_running = false;
  69. if (il2cpp::vm::Runtime::IsShuttingDown())
  70. {
  71. should_keep_running = false;
  72. }
  73. else
  74. {
  75. g_ThreadPool->domains_lock.Acquire();
  76. if (!domain_any_has_request())
  77. should_keep_running = false;
  78. g_ThreadPool->domains_lock.Release();
  79. if (!should_keep_running)
  80. {
  81. if (last_should_keep_running == -1 || il2cpp::os::Time::GetTicks100NanosecondsMonotonic() - last_should_keep_running < MONITOR_MINIMAL_LIFETIME * 1000 * 10)
  82. {
  83. should_keep_running = force_should_keep_running = true;
  84. }
  85. }
  86. }
  87. if (should_keep_running)
  88. {
  89. if (last_should_keep_running == -1 || !force_should_keep_running)
  90. last_should_keep_running = il2cpp::os::Time::GetTicks100NanosecondsMonotonic();
  91. }
  92. else
  93. {
  94. last_should_keep_running = -1;
  95. if (il2cpp::os::Atomic::CompareExchange(&s_MonitorStatus, MONITOR_STATUS_NOT_RUNNING, MONITOR_STATUS_WAITING_FOR_REQUEST) == MONITOR_STATUS_WAITING_FOR_REQUEST)
  96. return false;
  97. }
  98. }
  99. IL2CPP_ASSERT(s_MonitorStatus == MONITOR_STATUS_WAITING_FOR_REQUEST || s_MonitorStatus == MONITOR_STATUS_REQUESTED);
  100. return true;
  101. }
  102. static void monitor_thread(void* data)
  103. {
  104. Il2CppInternalThread *current_thread = il2cpp::vm::Thread::CurrentInternal();
  105. unsigned int i;
  106. cpu_info_usage(g_ThreadPool->cpu_usage_state);
  107. //mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, started", mono_native_thread_id_get ());
  108. do
  109. {
  110. ThreadPoolCounter counter;
  111. bool limit_worker_max_reached;
  112. int32_t interval_left = MONITOR_INTERVAL;
  113. int32_t awake = 0; /* number of spurious awakes we tolerate before doing a round of rebalancing */
  114. IL2CPP_ASSERT(s_MonitorStatus != MONITOR_STATUS_NOT_RUNNING);
  115. il2cpp::gc::GarbageCollector::SetSkipThread(true);
  116. do
  117. {
  118. int64_t ts;
  119. bool alerted = false;
  120. if (il2cpp::vm::Runtime::IsShuttingDown())
  121. break;
  122. ts = il2cpp::os::Time::GetTicksMillisecondsMonotonic();
  123. il2cpp::vm::Thread::Sleep(interval_left);
  124. /*if (mono_thread_info_sleep (interval_left, &alerted) == 0)
  125. break;*/
  126. interval_left -= (int32_t)(il2cpp::os::Time::GetTicksMillisecondsMonotonic() - ts);
  127. il2cpp::gc::GarbageCollector::SetSkipThread(false);
  128. if ((current_thread->state & (il2cpp::vm::kThreadStateStopRequested | il2cpp::vm::kThreadStateSuspendRequested)) != 0)
  129. mono_thread_interruption_checkpoint();
  130. il2cpp::gc::GarbageCollector::SetSkipThread(true);
  131. }
  132. while (interval_left > 0 && ++awake < 10);
  133. il2cpp::gc::GarbageCollector::SetSkipThread(false);
  134. if (g_ThreadPool->suspended)
  135. continue;
  136. if (il2cpp::vm::Runtime::IsShuttingDown())
  137. continue;
  138. g_ThreadPool->domains_lock.Acquire();
  139. if (!domain_any_has_request())
  140. {
  141. g_ThreadPool->domains_lock.Release();
  142. continue;
  143. }
  144. g_ThreadPool->domains_lock.Release();
  145. g_ThreadPool->cpu_usage = cpu_info_usage(g_ThreadPool->cpu_usage_state);
  146. if (!monitor_sufficient_delay_since_last_dequeue())
  147. continue;
  148. limit_worker_max_reached = false;
  149. COUNTER_ATOMIC(counter,
  150. {
  151. if (counter._.max_working >= g_ThreadPool->limit_worker_max)
  152. {
  153. limit_worker_max_reached = true;
  154. break;
  155. }
  156. counter._.max_working++;
  157. });
  158. if (limit_worker_max_reached)
  159. continue;
  160. hill_climbing_force_change(counter._.max_working, TRANSITION_STARVATION);
  161. for (i = 0; i < 5; ++i)
  162. {
  163. if (il2cpp::vm::Runtime::IsShuttingDown())
  164. break;
  165. if (worker_try_unpark())
  166. {
  167. //mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, unparked", mono_native_thread_id_get ());
  168. break;
  169. }
  170. if (worker_try_create())
  171. {
  172. //mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, created", mono_native_thread_id_get ());
  173. break;
  174. }
  175. }
  176. }
  177. while (monitor_should_keep_running());
  178. //mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, finished", mono_native_thread_id_get ());
  179. }
  180. void monitor_ensure_running()
  181. {
  182. for (;;)
  183. {
  184. switch (s_MonitorStatus)
  185. {
  186. case MONITOR_STATUS_REQUESTED:
  187. return;
  188. case MONITOR_STATUS_WAITING_FOR_REQUEST:
  189. il2cpp::os::Atomic::CompareExchange(&s_MonitorStatus, MONITOR_STATUS_REQUESTED, MONITOR_STATUS_WAITING_FOR_REQUEST);
  190. break;
  191. case MONITOR_STATUS_NOT_RUNNING:
  192. if (il2cpp::vm::Runtime::IsShuttingDown())
  193. return;
  194. if (il2cpp::os::Atomic::CompareExchange(&s_MonitorStatus, MONITOR_STATUS_REQUESTED, MONITOR_STATUS_NOT_RUNNING) == MONITOR_STATUS_NOT_RUNNING)
  195. {
  196. if (!il2cpp::vm::Thread::CreateInternal(monitor_thread, NULL, true, SMALL_STACK))
  197. s_MonitorStatus = MONITOR_STATUS_NOT_RUNNING;
  198. return;
  199. }
  200. break;
  201. default:
  202. IL2CPP_ASSERT(0 && "should not be reached");
  203. }
  204. }
  205. }