threadpool-worker-default.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. /**
  2. * \file
  3. * native threadpool worker
  4. *
  5. * Author:
  6. * Ludovic Henry (ludovic.henry@xamarin.com)
  7. *
  8. * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  9. */
  10. #include <stdlib.h>
  11. #define _USE_MATH_DEFINES // needed by MSVC to define math constants
  12. #include <math.h>
  13. #include <config.h>
  14. #include <glib.h>
  15. #ifndef ENABLE_NETCORE
  16. #include <mono/metadata/class-internals.h>
  17. #include <mono/metadata/exception.h>
  18. #include <mono/metadata/gc-internals.h>
  19. #include <mono/metadata/object.h>
  20. #include <mono/metadata/object-internals.h>
  21. #include <mono/metadata/threadpool.h>
  22. #include <mono/metadata/threadpool-worker.h>
  23. #include <mono/metadata/threadpool-io.h>
  24. #include <mono/metadata/w32event.h>
  25. #include <mono/utils/atomic.h>
  26. #include <mono/utils/mono-compiler.h>
  27. #include <mono/utils/mono-logger.h>
  28. #include <mono/utils/mono-logger-internals.h>
  29. #include <mono/utils/mono-proclib.h>
  30. #include <mono/utils/mono-threads.h>
  31. #include <mono/utils/mono-time.h>
  32. #include <mono/utils/refcount.h>
  33. #include <mono/utils/w32api.h>
  34. #include <mono/utils/mono-complex.h> // This header has defines to muck with names, so put it late.
  35. #define CPU_USAGE_LOW 80
  36. #define CPU_USAGE_HIGH 95
  37. #define MONITOR_INTERVAL 500 // ms
  38. #define MONITOR_MINIMAL_LIFETIME 60 * 1000 // ms
  39. #define WORKER_CREATION_MAX_PER_SEC 10
  40. /* The exponent to apply to the gain. 1.0 means to use linear gain,
  41. * higher values will enhance large moves and damp small ones.
  42. * default: 2.0 */
  43. #define HILL_CLIMBING_GAIN_EXPONENT 2.0
  44. /* The 'cost' of a thread. 0 means drive for increased throughput regardless
  45. * of thread count, higher values bias more against higher thread counts.
  46. * default: 0.15 */
  47. #define HILL_CLIMBING_BIAS 0.15
  48. #define HILL_CLIMBING_WAVE_PERIOD 4
  49. #define HILL_CLIMBING_MAX_WAVE_MAGNITUDE 20
  50. #define HILL_CLIMBING_WAVE_MAGNITUDE_MULTIPLIER 1.0
  51. #define HILL_CLIMBING_WAVE_HISTORY_SIZE 8
  52. #define HILL_CLIMBING_TARGET_SIGNAL_TO_NOISE_RATIO 3.0
  53. #define HILL_CLIMBING_MAX_CHANGE_PER_SECOND 4
  54. #define HILL_CLIMBING_MAX_CHANGE_PER_SAMPLE 20
  55. #define HILL_CLIMBING_SAMPLE_INTERVAL_LOW 10
  56. #define HILL_CLIMBING_SAMPLE_INTERVAL_HIGH 200
  57. #define HILL_CLIMBING_ERROR_SMOOTHING_FACTOR 0.01
  58. #define HILL_CLIMBING_MAX_SAMPLE_ERROR_PERCENT 0.15
  59. typedef enum {
  60. TRANSITION_WARMUP,
  61. TRANSITION_INITIALIZING,
  62. TRANSITION_RANDOM_MOVE,
  63. TRANSITION_CLIMBING_MOVE,
  64. TRANSITION_CHANGE_POINT,
  65. TRANSITION_STABILIZING,
  66. TRANSITION_STARVATION,
  67. TRANSITION_THREAD_TIMED_OUT,
  68. TRANSITION_UNDEFINED,
  69. } ThreadPoolHeuristicStateTransition;
  70. typedef struct {
  71. gint32 wave_period;
  72. gint32 samples_to_measure;
  73. gdouble target_throughput_ratio;
  74. gdouble target_signal_to_noise_ratio;
  75. gdouble max_change_per_second;
  76. gdouble max_change_per_sample;
  77. gint32 max_thread_wave_magnitude;
  78. gint32 sample_interval_low;
  79. gdouble thread_magnitude_multiplier;
  80. gint32 sample_interval_high;
  81. gdouble throughput_error_smoothing_factor;
  82. gdouble gain_exponent;
  83. gdouble max_sample_error;
  84. gdouble current_control_setting;
  85. gint64 total_samples;
  86. gint16 last_thread_count;
  87. gdouble elapsed_since_last_change;
  88. gdouble completions_since_last_change;
  89. gdouble average_throughput_noise;
  90. gdouble *samples;
  91. gdouble *thread_counts;
  92. guint32 current_sample_interval;
  93. gint32 accumulated_completion_count;
  94. gdouble accumulated_sample_duration;
  95. } ThreadPoolHillClimbing;
  96. typedef union {
  97. struct {
  98. gint16 max_working; /* determined by heuristic */
  99. gint16 starting; /* starting, but not yet in worker_thread */
  100. gint16 working; /* executing worker_thread */
  101. gint16 parked; /* parked */
  102. } _;
  103. gint64 as_gint64;
  104. } ThreadPoolWorkerCounter
  105. #ifdef __GNUC__
  106. __attribute__((aligned(64)))
  107. #endif
  108. ;
  109. typedef struct {
  110. MonoRefCount ref;
  111. MonoThreadPoolWorkerCallback callback;
  112. ThreadPoolWorkerCounter counters;
  113. MonoCoopSem parked_threads_sem;
  114. gint32 parked_threads_count;
  115. volatile gint32 work_items_count;
  116. guint32 worker_creation_current_second;
  117. guint32 worker_creation_current_count;
  118. MonoCoopMutex worker_creation_lock;
  119. gint32 heuristic_completions;
  120. gint64 heuristic_sample_start;
  121. gint64 heuristic_last_dequeue; // ms
  122. gint64 heuristic_last_adjustment; // ms
  123. gint64 heuristic_adjustment_interval; // ms
  124. ThreadPoolHillClimbing heuristic_hill_climbing;
  125. MonoCoopMutex heuristic_lock;
  126. gint32 limit_worker_min;
  127. gint32 limit_worker_max;
  128. MonoCpuUsageState *cpu_usage_state;
  129. gint32 cpu_usage;
  130. /* suspended by the debugger */
  131. gboolean suspended;
  132. gint32 monitor_status;
  133. } ThreadPoolWorker;
  134. enum {
  135. MONITOR_STATUS_REQUESTED,
  136. MONITOR_STATUS_WAITING_FOR_REQUEST,
  137. MONITOR_STATUS_NOT_RUNNING,
  138. };
  139. static ThreadPoolWorker worker;
  140. #define COUNTER_CHECK(counter) \
  141. do { \
  142. g_assert (counter._.max_working > 0); \
  143. g_assert (counter._.starting >= 0); \
  144. g_assert (counter._.working >= 0); \
  145. } while (0)
  146. #define COUNTER_ATOMIC(var,block) \
  147. do { \
  148. ThreadPoolWorkerCounter __old; \
  149. do { \
  150. __old = COUNTER_READ (); \
  151. (var) = __old; \
  152. { block; } \
  153. COUNTER_CHECK (var); \
  154. } while (mono_atomic_cas_i64 (&worker.counters.as_gint64, (var).as_gint64, __old.as_gint64) != __old.as_gint64); \
  155. } while (0)
  156. static ThreadPoolWorkerCounter
  157. COUNTER_READ (void)
  158. {
  159. ThreadPoolWorkerCounter counter;
  160. counter.as_gint64 = mono_atomic_load_i64 (&worker.counters.as_gint64);
  161. return counter;
  162. }
  163. static gint16
  164. counter_num_active (ThreadPoolWorkerCounter counter)
  165. {
  166. gint16 num_active = counter._.starting + counter._.working + counter._.parked;
  167. g_assert (num_active >= 0);
  168. return num_active;
  169. }
  170. static guint32
  171. rand_next (guint32 min, guint32 max)
  172. {
  173. ERROR_DECL (error);
  174. #ifdef HOST_WIN32
  175. guint32 val = (rand () % (max - min)) + min;
  176. #else
  177. guint32 val = (random () % (max - min)) + min;
  178. #endif
  179. // FIXME handle error
  180. mono_error_assert_ok (error);
  181. return val;
  182. }
  183. static void
  184. destroy (gpointer data)
  185. {
  186. mono_coop_sem_destroy (&worker.parked_threads_sem);
  187. mono_coop_mutex_destroy (&worker.worker_creation_lock);
  188. mono_coop_mutex_destroy (&worker.heuristic_lock);
  189. g_free (worker.cpu_usage_state);
  190. }
  191. void
  192. mono_threadpool_worker_init (MonoThreadPoolWorkerCallback callback)
  193. {
  194. ThreadPoolHillClimbing *hc;
  195. const char *threads_per_cpu_env;
  196. gint threads_per_cpu;
  197. gint threads_count;
  198. mono_refcount_init (&worker, destroy);
  199. worker.callback = callback;
  200. mono_coop_sem_init (&worker.parked_threads_sem, 0);
  201. worker.parked_threads_count = 0;
  202. worker.worker_creation_current_second = -1;
  203. mono_coop_mutex_init (&worker.worker_creation_lock);
  204. worker.heuristic_adjustment_interval = 10;
  205. mono_coop_mutex_init (&worker.heuristic_lock);
  206. hc = &worker.heuristic_hill_climbing;
  207. hc->wave_period = HILL_CLIMBING_WAVE_PERIOD;
  208. hc->max_thread_wave_magnitude = HILL_CLIMBING_MAX_WAVE_MAGNITUDE;
  209. hc->thread_magnitude_multiplier = (gdouble) HILL_CLIMBING_WAVE_MAGNITUDE_MULTIPLIER;
  210. hc->samples_to_measure = hc->wave_period * HILL_CLIMBING_WAVE_HISTORY_SIZE;
  211. hc->target_throughput_ratio = (gdouble) HILL_CLIMBING_BIAS;
  212. hc->target_signal_to_noise_ratio = (gdouble) HILL_CLIMBING_TARGET_SIGNAL_TO_NOISE_RATIO;
  213. hc->max_change_per_second = (gdouble) HILL_CLIMBING_MAX_CHANGE_PER_SECOND;
  214. hc->max_change_per_sample = (gdouble) HILL_CLIMBING_MAX_CHANGE_PER_SAMPLE;
  215. hc->sample_interval_low = HILL_CLIMBING_SAMPLE_INTERVAL_LOW;
  216. hc->sample_interval_high = HILL_CLIMBING_SAMPLE_INTERVAL_HIGH;
  217. hc->throughput_error_smoothing_factor = (gdouble) HILL_CLIMBING_ERROR_SMOOTHING_FACTOR;
  218. hc->gain_exponent = (gdouble) HILL_CLIMBING_GAIN_EXPONENT;
  219. hc->max_sample_error = (gdouble) HILL_CLIMBING_MAX_SAMPLE_ERROR_PERCENT;
  220. hc->current_control_setting = 0;
  221. hc->total_samples = 0;
  222. hc->last_thread_count = 0;
  223. hc->average_throughput_noise = 0;
  224. hc->elapsed_since_last_change = 0;
  225. hc->accumulated_completion_count = 0;
  226. hc->accumulated_sample_duration = 0;
  227. hc->samples = g_new0 (gdouble, hc->samples_to_measure);
  228. hc->thread_counts = g_new0 (gdouble, hc->samples_to_measure);
  229. hc->current_sample_interval = rand_next (hc->sample_interval_low, hc->sample_interval_high);
  230. if (!(threads_per_cpu_env = g_getenv ("MONO_THREADS_PER_CPU")))
  231. threads_per_cpu = 1;
  232. else
  233. threads_per_cpu = CLAMP (atoi (threads_per_cpu_env), 1, 50);
  234. threads_count = mono_cpu_count () * threads_per_cpu;
  235. worker.limit_worker_min = threads_count;
  236. #if defined (HOST_ANDROID) || defined (HOST_IOS)
  237. worker.limit_worker_max = CLAMP (threads_count * 100, MIN (threads_count, 200), MAX (threads_count, 200));
  238. #else
  239. worker.limit_worker_max = threads_count * 100;
  240. #endif
  241. worker.counters._.max_working = worker.limit_worker_min;
  242. worker.cpu_usage_state = g_new0 (MonoCpuUsageState, 1);
  243. worker.suspended = FALSE;
  244. worker.monitor_status = MONITOR_STATUS_NOT_RUNNING;
  245. }
  246. void
  247. mono_threadpool_worker_cleanup (void)
  248. {
  249. mono_refcount_dec (&worker);
  250. }
  251. static void
  252. work_item_push (void)
  253. {
  254. gint32 old, new_;
  255. do {
  256. old = mono_atomic_load_i32 (&worker.work_items_count);
  257. g_assert (old >= 0);
  258. new_ = old + 1;
  259. } while (mono_atomic_cas_i32 (&worker.work_items_count, new_, old) != old);
  260. }
  261. static gboolean
  262. work_item_try_pop (void)
  263. {
  264. gint32 old, new_;
  265. do {
  266. old = mono_atomic_load_i32 (&worker.work_items_count);
  267. g_assert (old >= 0);
  268. if (old == 0)
  269. return FALSE;
  270. new_ = old - 1;
  271. } while (mono_atomic_cas_i32 (&worker.work_items_count, new_, old) != old);
  272. return TRUE;
  273. }
  274. static gint32
  275. work_item_count (void)
  276. {
  277. return mono_atomic_load_i32 (&worker.work_items_count);
  278. }
  279. static void worker_request (void);
  280. void
  281. mono_threadpool_worker_request (void)
  282. {
  283. if (!mono_refcount_tryinc (&worker))
  284. return;
  285. work_item_push ();
  286. worker_request ();
  287. mono_refcount_dec (&worker);
  288. }
  289. /* return TRUE if timeout, FALSE otherwise (worker unpark or interrupt) */
  290. static gboolean
  291. worker_park (void)
  292. {
  293. gboolean timeout = FALSE;
  294. gboolean interrupted = FALSE;
  295. gint32 old, new_;
  296. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] worker parking",
  297. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
  298. if (!mono_runtime_is_shutting_down ()) {
  299. ThreadPoolWorkerCounter counter;
  300. COUNTER_ATOMIC (counter, {
  301. counter._.working --;
  302. counter._.parked ++;
  303. });
  304. do {
  305. old = mono_atomic_load_i32 (&worker.parked_threads_count);
  306. g_assert (old >= G_MININT32);
  307. new_ = old + 1;
  308. } while (mono_atomic_cas_i32 (&worker.parked_threads_count, new_, old) != old);
  309. switch (mono_coop_sem_timedwait (&worker.parked_threads_sem, rand_next (5 * 1000, 60 * 1000), MONO_SEM_FLAGS_ALERTABLE)) {
  310. case MONO_SEM_TIMEDWAIT_RET_SUCCESS:
  311. break;
  312. case MONO_SEM_TIMEDWAIT_RET_ALERTED:
  313. interrupted = TRUE;
  314. break;
  315. case MONO_SEM_TIMEDWAIT_RET_TIMEDOUT:
  316. timeout = TRUE;
  317. break;
  318. default:
  319. g_assert_not_reached ();
  320. }
  321. if (interrupted || timeout) {
  322. /* If the semaphore was posted, then worker.parked_threads_count was decremented in worker_try_unpark */
  323. do {
  324. old = mono_atomic_load_i32 (&worker.parked_threads_count);
  325. g_assert (old > G_MININT32);
  326. new_ = old - 1;
  327. } while (mono_atomic_cas_i32 (&worker.parked_threads_count, new_, old) != old);
  328. }
  329. COUNTER_ATOMIC (counter, {
  330. counter._.working ++;
  331. counter._.parked --;
  332. });
  333. }
  334. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] worker unparking, timeout? %s interrupted? %s",
  335. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())), timeout ? "yes" : "no", interrupted ? "yes" : "no");
  336. return timeout;
  337. }
  338. static gboolean
  339. worker_try_unpark (void)
  340. {
  341. gboolean res = TRUE;
  342. gint32 old, new_;
  343. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try unpark worker",
  344. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
  345. do {
  346. old = mono_atomic_load_i32 (&worker.parked_threads_count);
  347. g_assert (old > G_MININT32);
  348. if (old <= 0) {
  349. res = FALSE;
  350. break;
  351. }
  352. new_ = old - 1;
  353. } while (mono_atomic_cas_i32 (&worker.parked_threads_count, new_, old) != old);
  354. if (res)
  355. mono_coop_sem_post (&worker.parked_threads_sem);
  356. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try unpark worker, success? %s",
  357. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())), res ? "yes" : "no");
  358. return res;
  359. }
  360. static void hill_climbing_force_change (gint16 new_thread_count, ThreadPoolHeuristicStateTransition transition);
  361. static gsize WINAPI
  362. worker_thread (gpointer unused)
  363. {
  364. MonoInternalThread *thread;
  365. ThreadPoolWorkerCounter counter;
  366. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] worker starting",
  367. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
  368. if (!mono_refcount_tryinc (&worker))
  369. return 0;
  370. COUNTER_ATOMIC (counter, {
  371. counter._.starting --;
  372. counter._.working ++;
  373. });
  374. thread = mono_thread_internal_current ();
  375. g_assert (thread);
  376. gboolean worker_timed_out = FALSE;
  377. while (!mono_runtime_is_shutting_down ()) {
  378. if (mono_thread_interruption_checkpoint_bool ())
  379. continue;
  380. // If a worker thread is in its native top, not running managed code,
  381. // there is no point in raising thread abort, and no code will clear
  382. // the abort request. As such, the subsequent timedwait, would
  383. // not be interrupted at runtime shutdown, because an abort is already requested.
  384. // Clear the abort request.
  385. // This avoids a shutdown hang in tests thread6 and thread7.
  386. if (thread->state & ThreadState_AbortRequested)
  387. mono_thread_internal_reset_abort (thread);
  388. if (!work_item_try_pop ()) {
  389. gboolean const timeout = worker_park ();
  390. if (timeout) {
  391. worker_timed_out = TRUE;
  392. break;
  393. }
  394. continue;
  395. }
  396. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] worker executing",
  397. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
  398. worker.callback ();
  399. }
  400. COUNTER_ATOMIC (counter, {
  401. counter._.working --;
  402. });
  403. if (worker_timed_out) {
  404. gint16 decr_max_working;
  405. COUNTER_ATOMIC (counter, {
  406. decr_max_working = MAX (worker.limit_worker_min, MIN (counter_num_active (counter), counter._.max_working));
  407. counter._.max_working = decr_max_working;
  408. });
  409. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] worker timed out, starting = %d working = %d parked = %d, setting max_working to %d",
  410. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())),
  411. counter._.starting, counter._.working, counter._.parked,
  412. decr_max_working);
  413. hill_climbing_force_change (decr_max_working, TRANSITION_THREAD_TIMED_OUT);
  414. }
  415. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] worker finishing",
  416. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
  417. mono_refcount_dec (&worker);
  418. return 0;
  419. }
  420. static gboolean
  421. worker_try_create (void)
  422. {
  423. ERROR_DECL (error);
  424. MonoInternalThread *thread;
  425. gint64 current_ticks;
  426. gint32 now = 0;
  427. ThreadPoolWorkerCounter counter;
  428. if (mono_runtime_is_shutting_down ())
  429. return FALSE;
  430. mono_coop_mutex_lock (&worker.worker_creation_lock);
  431. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try create worker",
  432. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
  433. current_ticks = mono_100ns_ticks ();
  434. if (0 == current_ticks) {
  435. g_warning ("failed to get 100ns ticks");
  436. } else {
  437. now = current_ticks / (10 * 1000 * 1000);
  438. if (worker.worker_creation_current_second != now) {
  439. worker.worker_creation_current_second = now;
  440. worker.worker_creation_current_count = 0;
  441. } else {
  442. g_assert (worker.worker_creation_current_count <= WORKER_CREATION_MAX_PER_SEC);
  443. if (worker.worker_creation_current_count == WORKER_CREATION_MAX_PER_SEC) {
  444. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try create worker, failed: maximum number of worker created per second reached, current count = %d",
  445. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())), worker.worker_creation_current_count);
  446. mono_coop_mutex_unlock (&worker.worker_creation_lock);
  447. return FALSE;
  448. }
  449. }
  450. }
  451. COUNTER_ATOMIC (counter, {
  452. if (counter._.working >= counter._.max_working) {
  453. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try create worker, failed: maximum number of working threads reached",
  454. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
  455. mono_coop_mutex_unlock (&worker.worker_creation_lock);
  456. return FALSE;
  457. }
  458. counter._.starting ++;
  459. });
  460. thread = mono_thread_create_internal (mono_get_root_domain (), (gpointer)worker_thread, NULL, MONO_THREAD_CREATE_FLAGS_THREADPOOL, error);
  461. if (!thread) {
  462. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try create worker, failed: could not create thread due to %s",
  463. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())), mono_error_get_message (error));
  464. mono_error_cleanup (error);
  465. COUNTER_ATOMIC (counter, {
  466. counter._.starting --;
  467. });
  468. mono_coop_mutex_unlock (&worker.worker_creation_lock);
  469. return FALSE;
  470. }
  471. #ifndef DISABLE_PERFCOUNTERS
  472. mono_atomic_inc_i32 (&mono_perfcounters->threadpool_threads);
  473. #endif
  474. worker.worker_creation_current_count += 1;
  475. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try create worker, created %p, now = %d count = %d",
  476. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())), (gpointer) thread->tid, now, worker.worker_creation_current_count);
  477. mono_coop_mutex_unlock (&worker.worker_creation_lock);
  478. return TRUE;
  479. }
  480. static void monitor_ensure_running (void);
  481. static void
  482. worker_request (void)
  483. {
  484. if (worker.suspended)
  485. return;
  486. monitor_ensure_running ();
  487. if (worker_try_unpark ()) {
  488. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] request worker, unparked",
  489. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
  490. return;
  491. }
  492. if (worker_try_create ()) {
  493. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] request worker, created",
  494. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
  495. return;
  496. }
  497. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] request worker, failed",
  498. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
  499. }
  500. static gboolean
  501. monitor_should_keep_running (void)
  502. {
  503. static gint64 last_should_keep_running = -1;
  504. g_assert (worker.monitor_status == MONITOR_STATUS_WAITING_FOR_REQUEST || worker.monitor_status == MONITOR_STATUS_REQUESTED);
  505. if (mono_atomic_xchg_i32 (&worker.monitor_status, MONITOR_STATUS_WAITING_FOR_REQUEST) == MONITOR_STATUS_WAITING_FOR_REQUEST) {
  506. gboolean should_keep_running = TRUE, force_should_keep_running = FALSE;
  507. if (mono_runtime_is_shutting_down ()) {
  508. should_keep_running = FALSE;
  509. } else {
  510. if (work_item_count () == 0)
  511. should_keep_running = FALSE;
  512. if (!should_keep_running) {
  513. if (last_should_keep_running == -1 || mono_100ns_ticks () - last_should_keep_running < MONITOR_MINIMAL_LIFETIME * 1000 * 10) {
  514. should_keep_running = force_should_keep_running = TRUE;
  515. }
  516. }
  517. }
  518. if (should_keep_running) {
  519. if (last_should_keep_running == -1 || !force_should_keep_running)
  520. last_should_keep_running = mono_100ns_ticks ();
  521. } else {
  522. last_should_keep_running = -1;
  523. if (mono_atomic_cas_i32 (&worker.monitor_status, MONITOR_STATUS_NOT_RUNNING, MONITOR_STATUS_WAITING_FOR_REQUEST) == MONITOR_STATUS_WAITING_FOR_REQUEST)
  524. return FALSE;
  525. }
  526. }
  527. g_assert (worker.monitor_status == MONITOR_STATUS_WAITING_FOR_REQUEST || worker.monitor_status == MONITOR_STATUS_REQUESTED);
  528. return TRUE;
  529. }
  530. static gboolean
  531. monitor_sufficient_delay_since_last_dequeue (void)
  532. {
  533. gint64 threshold;
  534. if (worker.cpu_usage < CPU_USAGE_LOW) {
  535. threshold = MONITOR_INTERVAL;
  536. } else {
  537. threshold = COUNTER_READ ()._.max_working * MONITOR_INTERVAL * 2;
  538. }
  539. return mono_msec_ticks () >= worker.heuristic_last_dequeue + threshold;
  540. }
  541. static gsize WINAPI
  542. monitor_thread (gpointer unused)
  543. {
  544. MonoInternalThread *internal;
  545. guint i;
  546. if (!mono_refcount_tryinc (&worker))
  547. return 0;
  548. internal = mono_thread_internal_current ();
  549. g_assert (internal);
  550. mono_cpu_usage (worker.cpu_usage_state);
  551. // printf ("monitor_thread: start\n");
  552. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, started",
  553. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
  554. do {
  555. ThreadPoolWorkerCounter counter;
  556. gboolean limit_worker_max_reached;
  557. gint32 interval_left = MONITOR_INTERVAL;
  558. gint32 awake = 0; /* number of spurious awakes we tolerate before doing a round of rebalancing */
  559. g_assert (worker.monitor_status != MONITOR_STATUS_NOT_RUNNING);
  560. #if 0
  561. // This is ifdef'd out because otherwise we flood the log every
  562. // MONITOR_INTERVAL ms, which is pretty noisy.
  563. if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL)) {
  564. ThreadPoolWorkerCounter trace_counter = COUNTER_READ ();
  565. gint32 work_items = work_item_count ();
  566. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "monitor_thread: work items = %d, starting = %d working = %d parked = %d max_working = %d\n",
  567. work_items, trace_counter._.starting, trace_counter._.working, trace_counter._.parked, trace_counter._.max_working);
  568. }
  569. #endif
  570. do {
  571. gint64 ts;
  572. gboolean alerted = FALSE;
  573. if (mono_runtime_is_shutting_down ())
  574. break;
  575. ts = mono_msec_ticks ();
  576. if (mono_thread_info_sleep (interval_left, &alerted) == 0)
  577. break;
  578. interval_left -= mono_msec_ticks () - ts;
  579. mono_thread_interruption_checkpoint_void ();
  580. } while (interval_left > 0 && ++awake < 10);
  581. if (mono_runtime_is_shutting_down ())
  582. continue;
  583. if (worker.suspended)
  584. continue;
  585. if (work_item_count () == 0)
  586. continue;
  587. worker.cpu_usage = mono_cpu_usage (worker.cpu_usage_state);
  588. if (!monitor_sufficient_delay_since_last_dequeue ())
  589. continue;
  590. gboolean active_max_reached;
  591. COUNTER_ATOMIC (counter, {
  592. limit_worker_max_reached = FALSE;
  593. active_max_reached = FALSE;
  594. if (counter._.max_working >= worker.limit_worker_max) {
  595. limit_worker_max_reached = TRUE;
  596. if (counter_num_active (counter) >= counter._.max_working)
  597. active_max_reached = TRUE;
  598. break;
  599. }
  600. counter._.max_working ++;
  601. });
  602. if (limit_worker_max_reached) {
  603. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, limit_worker_max (%d) reached",
  604. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())),
  605. worker.limit_worker_max);
  606. if (active_max_reached)
  607. continue;
  608. else
  609. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, num_active (%d) < max_working, allowing active thread increase",
  610. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())),
  611. counter_num_active (counter));
  612. }
  613. else
  614. hill_climbing_force_change (counter._.max_working, TRANSITION_STARVATION);
  615. for (i = 0; i < 5; ++i) {
  616. if (mono_runtime_is_shutting_down ())
  617. break;
  618. if (worker_try_unpark ()) {
  619. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, unparked",
  620. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
  621. break;
  622. }
  623. if (worker_try_create ()) {
  624. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, created",
  625. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
  626. break;
  627. }
  628. }
  629. } while (monitor_should_keep_running ());
  630. // printf ("monitor_thread: stop\n");
  631. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, finished",
  632. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())));
  633. mono_refcount_dec (&worker);
  634. return 0;
  635. }
  636. static void
  637. monitor_ensure_running (void)
  638. {
  639. ERROR_DECL (error);
  640. for (;;) {
  641. switch (worker.monitor_status) {
  642. case MONITOR_STATUS_REQUESTED:
  643. // printf ("monitor_thread: requested\n");
  644. return;
  645. case MONITOR_STATUS_WAITING_FOR_REQUEST:
  646. // printf ("monitor_thread: waiting for request\n");
  647. mono_atomic_cas_i32 (&worker.monitor_status, MONITOR_STATUS_REQUESTED, MONITOR_STATUS_WAITING_FOR_REQUEST);
  648. break;
  649. case MONITOR_STATUS_NOT_RUNNING:
  650. // printf ("monitor_thread: not running\n");
  651. if (mono_runtime_is_shutting_down ())
  652. return;
  653. if (mono_atomic_cas_i32 (&worker.monitor_status, MONITOR_STATUS_REQUESTED, MONITOR_STATUS_NOT_RUNNING) == MONITOR_STATUS_NOT_RUNNING) {
  654. // printf ("monitor_thread: creating\n");
  655. if (!mono_thread_create_internal (mono_get_root_domain (), (gpointer)monitor_thread, NULL, (MonoThreadCreateFlags)(MONO_THREAD_CREATE_FLAGS_THREADPOOL | MONO_THREAD_CREATE_FLAGS_SMALL_STACK), error)) {
  656. // printf ("monitor_thread: creating failed\n");
  657. worker.monitor_status = MONITOR_STATUS_NOT_RUNNING;
  658. mono_error_cleanup (error);
  659. mono_refcount_dec (&worker);
  660. }
  661. return;
  662. }
  663. break;
  664. default: g_assert_not_reached ();
  665. }
  666. }
  667. }
  668. static void
  669. hill_climbing_change_thread_count (gint16 new_thread_count, ThreadPoolHeuristicStateTransition transition)
  670. {
  671. ThreadPoolHillClimbing *hc;
  672. hc = &worker.heuristic_hill_climbing;
  673. mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] hill climbing, change max number of threads %d",
  674. GUINT_TO_POINTER (MONO_NATIVE_THREAD_ID_TO_UINT (mono_native_thread_id_get ())), new_thread_count);
  675. hc->last_thread_count = new_thread_count;
  676. hc->current_sample_interval = rand_next (hc->sample_interval_low, hc->sample_interval_high);
  677. hc->elapsed_since_last_change = 0;
  678. hc->completions_since_last_change = 0;
  679. }
  680. static void
  681. hill_climbing_force_change (gint16 new_thread_count, ThreadPoolHeuristicStateTransition transition)
  682. {
  683. ThreadPoolHillClimbing *hc;
  684. hc = &worker.heuristic_hill_climbing;
  685. if (new_thread_count != hc->last_thread_count) {
  686. hc->current_control_setting += new_thread_count - hc->last_thread_count;
  687. hill_climbing_change_thread_count (new_thread_count, transition);
  688. }
  689. }
  690. static double_complex
  691. hill_climbing_get_wave_component (gdouble *samples, guint sample_count, gdouble period)
  692. {
  693. ThreadPoolHillClimbing *hc;
  694. gdouble w, cosine, sine, coeff, q0, q1, q2;
  695. guint i;
  696. g_assert (sample_count >= period);
  697. g_assert (period >= 2);
  698. hc = &worker.heuristic_hill_climbing;
  699. w = 2.0 * M_PI / period;
  700. cosine = cos (w);
  701. sine = sin (w);
  702. coeff = 2.0 * cosine;
  703. q0 = q1 = q2 = 0;
  704. for (i = 0; i < sample_count; ++i) {
  705. q0 = coeff * q1 - q2 + samples [(hc->total_samples - sample_count + i) % hc->samples_to_measure];
  706. q2 = q1;
  707. q1 = q0;
  708. }
  709. return mono_double_complex_scalar_div (mono_double_complex_make (q1 - q2 * cosine, (q2 * sine)), ((gdouble)sample_count));
  710. }
  711. static gint16
  712. hill_climbing_update (gint16 current_thread_count, guint32 sample_duration, gint32 completions, gint64 *adjustment_interval)
  713. {
  714. ThreadPoolHillClimbing *hc;
  715. ThreadPoolHeuristicStateTransition transition;
  716. gdouble throughput;
  717. gdouble throughput_error_estimate;
  718. gdouble confidence;
  719. gdouble move;
  720. gdouble gain;
  721. gint sample_index;
  722. gint sample_count;
  723. gint new_thread_wave_magnitude;
  724. gint new_thread_count;
  725. double_complex thread_wave_component;
  726. double_complex throughput_wave_component;
  727. double_complex ratio;
  728. g_assert (adjustment_interval);
  729. hc = &worker.heuristic_hill_climbing;
  730. /* If someone changed the thread count without telling us, update our records accordingly. */
  731. if (current_thread_count != hc->last_thread_count)
  732. hill_climbing_force_change (current_thread_count, TRANSITION_INITIALIZING);
  733. /* Update the cumulative stats for this thread count */
  734. hc->elapsed_since_last_change += sample_duration;
  735. hc->completions_since_last_change += completions;
  736. /* Add in any data we've already collected about this sample */
  737. sample_duration += hc->accumulated_sample_duration;
  738. completions += hc->accumulated_completion_count;
  739. /* We need to make sure we're collecting reasonably accurate data. Since we're just counting the end
  740. * of each work item, we are goinng to be missing some data about what really happened during the
  741. * sample interval. The count produced by each thread includes an initial work item that may have
  742. * started well before the start of the interval, and each thread may have been running some new
  743. * work item for some time before the end of the interval, which did not yet get counted. So
  744. * our count is going to be off by +/- threadCount workitems.
  745. *
  746. * The exception is that the thread that reported to us last time definitely wasn't running any work
  747. * at that time, and the thread that's reporting now definitely isn't running a work item now. So
  748. * we really only need to consider threadCount-1 threads.
  749. *
  750. * Thus the percent error in our count is +/- (threadCount-1)/numCompletions.
  751. *
  752. * We cannot rely on the frequency-domain analysis we'll be doing later to filter out this error, because
  753. * of the way it accumulates over time. If this sample is off by, say, 33% in the negative direction,
  754. * then the next one likely will be too. The one after that will include the sum of the completions
  755. * we missed in the previous samples, and so will be 33% positive. So every three samples we'll have
  756. * two "low" samples and one "high" sample. This will appear as periodic variation right in the frequency
  757. * range we're targeting, which will not be filtered by the frequency-domain translation. */
  758. if (hc->total_samples > 0 && ((current_thread_count - 1.0) / completions) >= hc->max_sample_error) {
  759. /* Not accurate enough yet. Let's accumulate the data so
  760. * far, and tell the ThreadPoolWorker to collect a little more. */
  761. hc->accumulated_sample_duration = sample_duration;
  762. hc->accumulated_completion_count = completions;
  763. *adjustment_interval = 10;
  764. return current_thread_count;
  765. }
  766. /* We've got enouugh data for our sample; reset our accumulators for next time. */
  767. hc->accumulated_sample_duration = 0;
  768. hc->accumulated_completion_count = 0;
  769. /* Add the current thread count and throughput sample to our history. */
  770. throughput = ((gdouble) completions) / sample_duration;
  771. sample_index = hc->total_samples % hc->samples_to_measure;
  772. hc->samples [sample_index] = throughput;
  773. hc->thread_counts [sample_index] = current_thread_count;
  774. hc->total_samples ++;
  775. /* Set up defaults for our metrics. */
  776. thread_wave_component = mono_double_complex_make(0, 0);
  777. throughput_wave_component = mono_double_complex_make(0, 0);
  778. throughput_error_estimate = 0;
  779. ratio = mono_double_complex_make(0, 0);
  780. confidence = 0;
  781. transition = TRANSITION_WARMUP;
  782. /* How many samples will we use? It must be at least the three wave periods we're looking for, and it must also
  783. * be a whole multiple of the primary wave's period; otherwise the frequency we're looking for will fall between
  784. * two frequency bands in the Fourier analysis, and we won't be able to measure it accurately. */
  785. sample_count = ((gint) MIN (hc->total_samples - 1, hc->samples_to_measure) / hc->wave_period) * hc->wave_period;
  786. if (sample_count > hc->wave_period) {
  787. guint i;
  788. gdouble average_throughput;
  789. gdouble average_thread_count;
  790. gdouble sample_sum = 0;
  791. gdouble thread_sum = 0;
  792. /* Average the throughput and thread count samples, so we can scale the wave magnitudes later. */
  793. for (i = 0; i < sample_count; ++i) {
  794. guint j = (hc->total_samples - sample_count + i) % hc->samples_to_measure;
  795. sample_sum += hc->samples [j];
  796. thread_sum += hc->thread_counts [j];
  797. }
  798. average_throughput = sample_sum / sample_count;
  799. average_thread_count = thread_sum / sample_count;
  800. if (average_throughput > 0 && average_thread_count > 0) {
  801. gdouble noise_for_confidence, adjacent_period_1, adjacent_period_2;
  802. /* Calculate the periods of the adjacent frequency bands we'll be using to
  803. * measure noise levels. We want the two adjacent Fourier frequency bands. */
  804. adjacent_period_1 = sample_count / (((gdouble) sample_count) / ((gdouble) hc->wave_period) + 1);
  805. adjacent_period_2 = sample_count / (((gdouble) sample_count) / ((gdouble) hc->wave_period) - 1);
  806. /* Get the the three different frequency components of the throughput (scaled by average
  807. * throughput). Our "error" estimate (the amount of noise that might be present in the
  808. * frequency band we're really interested in) is the average of the adjacent bands. */
  809. throughput_wave_component = mono_double_complex_scalar_div (hill_climbing_get_wave_component (hc->samples, sample_count, hc->wave_period), average_throughput);
  810. throughput_error_estimate = mono_cabs (mono_double_complex_scalar_div (hill_climbing_get_wave_component (hc->samples, sample_count, adjacent_period_1), average_throughput));
  811. if (adjacent_period_2 <= sample_count) {
  812. throughput_error_estimate = MAX (throughput_error_estimate, mono_cabs (mono_double_complex_scalar_div (hill_climbing_get_wave_component (
  813. hc->samples, sample_count, adjacent_period_2), average_throughput)));
  814. }
  815. /* Do the same for the thread counts, so we have something to compare to. We don't
  816. * measure thread count noise, because there is none; these are exact measurements. */
  817. thread_wave_component = mono_double_complex_scalar_div (hill_climbing_get_wave_component (hc->thread_counts, sample_count, hc->wave_period), average_thread_count);
  818. /* Update our moving average of the throughput noise. We'll use this
  819. * later as feedback to determine the new size of the thread wave. */
  820. if (hc->average_throughput_noise == 0) {
  821. hc->average_throughput_noise = throughput_error_estimate;
  822. } else {
  823. hc->average_throughput_noise = (hc->throughput_error_smoothing_factor * throughput_error_estimate)
  824. + ((1.0 + hc->throughput_error_smoothing_factor) * hc->average_throughput_noise);
  825. }
  826. if (mono_cabs (thread_wave_component) > 0) {
  827. /* Adjust the throughput wave so it's centered around the target wave,
  828. * and then calculate the adjusted throughput/thread ratio. */
  829. ratio = mono_double_complex_div (mono_double_complex_sub (throughput_wave_component, mono_double_complex_scalar_mul(thread_wave_component, hc->target_throughput_ratio)), thread_wave_component);
  830. transition = TRANSITION_CLIMBING_MOVE;
  831. } else {
  832. ratio = mono_double_complex_make (0, 0);
  833. transition = TRANSITION_STABILIZING;
  834. }
  835. noise_for_confidence = MAX (hc->average_throughput_noise, throughput_error_estimate);
  836. if (noise_for_confidence > 0) {
  837. confidence = mono_cabs (thread_wave_component) / noise_for_confidence / hc->target_signal_to_noise_ratio;
  838. } else {
  839. /* there is no noise! */
  840. confidence = 1.0;
  841. }
  842. }
  843. }
  844. /* We use just the real part of the complex ratio we just calculated. If the throughput signal
  845. * is exactly in phase with the thread signal, this will be the same as taking the magnitude of
  846. * the complex move and moving that far up. If they're 180 degrees out of phase, we'll move
  847. * backward (because this indicates that our changes are having the opposite of the intended effect).
  848. * If they're 90 degrees out of phase, we won't move at all, because we can't tell wether we're
  849. * having a negative or positive effect on throughput. */
  850. move = mono_creal (ratio);
  851. move = CLAMP (move, -1.0, 1.0);
  852. /* Apply our confidence multiplier. */
  853. move *= CLAMP (confidence, -1.0, 1.0);
  854. /* Now apply non-linear gain, such that values around zero are attenuated, while higher values
  855. * are enhanced. This allows us to move quickly if we're far away from the target, but more slowly
  856. * if we're getting close, giving us rapid ramp-up without wild oscillations around the target. */
  857. gain = hc->max_change_per_second * sample_duration;
  858. move = pow (fabs (move), hc->gain_exponent) * (move >= 0.0 ? 1 : -1) * gain;
  859. move = MIN (move, hc->max_change_per_sample);
  860. /* If the result was positive, and CPU is > 95%, refuse the move. */
  861. if (move > 0.0 && worker.cpu_usage > CPU_USAGE_HIGH)
  862. move = 0.0;
  863. /* Apply the move to our control setting. */
  864. hc->current_control_setting += move;
  865. /* Calculate the new thread wave magnitude, which is based on the moving average we've been keeping of the
  866. * throughput error. This average starts at zero, so we'll start with a nice safe little wave at first. */
  867. new_thread_wave_magnitude = (gint)(0.5 + (hc->current_control_setting * hc->average_throughput_noise
  868. * hc->target_signal_to_noise_ratio * hc->thread_magnitude_multiplier * 2.0));
  869. new_thread_wave_magnitude = CLAMP (new_thread_wave_magnitude, 1, hc->max_thread_wave_magnitude);
  870. /* Make sure our control setting is within the ThreadPoolWorker's limits. */
  871. hc->current_control_setting = CLAMP (hc->current_control_setting, worker.limit_worker_min, worker.limit_worker_max - new_thread_wave_magnitude);
  872. /* Calculate the new thread count (control setting + square wave). */
  873. new_thread_count = (gint)(hc->current_control_setting + new_thread_wave_magnitude * ((hc->total_samples / (hc->wave_period / 2)) % 2));
  874. /* Make sure the new thread count doesn't exceed the ThreadPoolWorker's limits. */
  875. new_thread_count = CLAMP (new_thread_count, worker.limit_worker_min, worker.limit_worker_max);
  876. if (new_thread_count != current_thread_count)
  877. hill_climbing_change_thread_count (new_thread_count, transition);
  878. if (mono_creal (ratio) < 0.0 && new_thread_count == worker.limit_worker_min)
  879. *adjustment_interval = (gint)(0.5 + hc->current_sample_interval * (10.0 * MAX (-1.0 * mono_creal (ratio), 1.0)));
  880. else
  881. *adjustment_interval = hc->current_sample_interval;
  882. return new_thread_count;
  883. }
  884. static gboolean
  885. heuristic_should_adjust (void)
  886. {
  887. if (worker.heuristic_last_dequeue > worker.heuristic_last_adjustment + worker.heuristic_adjustment_interval) {
  888. ThreadPoolWorkerCounter const counter = COUNTER_READ ();
  889. if (counter._.working <= counter._.max_working)
  890. return TRUE;
  891. }
  892. return FALSE;
  893. }
  894. static void
  895. heuristic_adjust (void)
  896. {
  897. if (mono_coop_mutex_trylock (&worker.heuristic_lock) == 0) {
  898. gint32 completions = mono_atomic_xchg_i32 (&worker.heuristic_completions, 0);
  899. gint64 sample_end = mono_msec_ticks ();
  900. gint64 sample_duration = sample_end - worker.heuristic_sample_start;
  901. if (sample_duration >= worker.heuristic_adjustment_interval / 2) {
  902. ThreadPoolWorkerCounter counter = COUNTER_READ ();
  903. gint16 const new_thread_count = hill_climbing_update (counter._.max_working, sample_duration, completions, &worker.heuristic_adjustment_interval);
  904. COUNTER_ATOMIC (counter, {
  905. counter._.max_working = new_thread_count;
  906. });
  907. /* FIXME: this can never be true. we only leave COUNTER_ATOMIC() if the assignment and CAS succeeded */
  908. if (new_thread_count > counter._.max_working)
  909. worker_request ();
  910. worker.heuristic_sample_start = sample_end;
  911. worker.heuristic_last_adjustment = mono_msec_ticks ();
  912. }
  913. mono_coop_mutex_unlock (&worker.heuristic_lock);
  914. }
  915. }
  916. static void
  917. heuristic_notify_work_completed (void)
  918. {
  919. mono_atomic_inc_i32 (&worker.heuristic_completions);
  920. worker.heuristic_last_dequeue = mono_msec_ticks ();
  921. if (heuristic_should_adjust ())
  922. heuristic_adjust ();
  923. }
  924. gboolean
  925. mono_threadpool_worker_notify_completed (void)
  926. {
  927. heuristic_notify_work_completed ();
  928. ThreadPoolWorkerCounter const counter = COUNTER_READ ();
  929. return counter._.working <= counter._.max_working;
  930. }
  931. gint32
  932. mono_threadpool_worker_get_min (void)
  933. {
  934. gint32 ret;
  935. if (!mono_refcount_tryinc (&worker))
  936. return 0;
  937. ret = worker.limit_worker_min;
  938. mono_refcount_dec (&worker);
  939. return ret;
  940. }
  941. gboolean
  942. mono_threadpool_worker_set_min (gint32 value)
  943. {
  944. if (value <= 0 || value > worker.limit_worker_max)
  945. return FALSE;
  946. if (!mono_refcount_tryinc (&worker))
  947. return FALSE;
  948. worker.limit_worker_min = value;
  949. mono_refcount_dec (&worker);
  950. return TRUE;
  951. }
  952. gint32
  953. mono_threadpool_worker_get_max (void)
  954. {
  955. gint32 ret;
  956. if (!mono_refcount_tryinc (&worker))
  957. return 0;
  958. ret = worker.limit_worker_max;
  959. mono_refcount_dec (&worker);
  960. return ret;
  961. }
  962. gboolean
  963. mono_threadpool_worker_set_max (gint32 value)
  964. {
  965. gint32 cpu_count;
  966. cpu_count = mono_cpu_count ();
  967. if (value < worker.limit_worker_min || value < cpu_count)
  968. return FALSE;
  969. if (!mono_refcount_tryinc (&worker))
  970. return FALSE;
  971. worker.limit_worker_max = value;
  972. mono_refcount_dec (&worker);
  973. return TRUE;
  974. }
  975. void
  976. mono_threadpool_worker_set_suspended (gboolean suspended)
  977. {
  978. if (!mono_refcount_tryinc (&worker))
  979. return;
  980. worker.suspended = suspended;
  981. if (!suspended)
  982. worker_request ();
  983. mono_refcount_dec (&worker);
  984. }
  985. #endif /* ENABLE_NETCORE */