3 * native threadpool worker
6 * Ludovic Henry (ludovic.henry@xamarin.com)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
12 #define _USE_MATH_DEFINES // needed by MSVC to define math constants
17 #include <mono/metadata/class-internals.h>
18 #include <mono/metadata/exception.h>
19 #include <mono/metadata/gc-internals.h>
20 #include <mono/metadata/object.h>
21 #include <mono/metadata/object-internals.h>
22 #include <mono/metadata/threadpool.h>
23 #include <mono/metadata/threadpool-worker.h>
24 #include <mono/metadata/threadpool-io.h>
25 #include <mono/metadata/w32event.h>
26 #include <mono/utils/atomic.h>
27 #include <mono/utils/mono-compiler.h>
28 #include <mono/utils/mono-complex.h>
29 #include <mono/utils/mono-logger.h>
30 #include <mono/utils/mono-logger-internals.h>
31 #include <mono/utils/mono-proclib.h>
32 #include <mono/utils/mono-threads.h>
33 #include <mono/utils/mono-time.h>
34 #include <mono/utils/mono-rand.h>
35 #include <mono/utils/refcount.h>
36 #include <mono/utils/w32api.h>
38 #define CPU_USAGE_LOW 80
39 #define CPU_USAGE_HIGH 95
41 #define MONITOR_INTERVAL 500 // ms
42 #define MONITOR_MINIMAL_LIFETIME 60 * 1000 // ms
44 #define WORKER_CREATION_MAX_PER_SEC 10
46 /* The exponent to apply to the gain. 1.0 means to use linear gain,
47 * higher values will enhance large moves and damp small ones.
49 #define HILL_CLIMBING_GAIN_EXPONENT 2.0
51 /* The 'cost' of a thread. 0 means drive for increased throughput regardless
52 * of thread count, higher values bias more against higher thread counts.
54 #define HILL_CLIMBING_BIAS 0.15
56 #define HILL_CLIMBING_WAVE_PERIOD 4
57 #define HILL_CLIMBING_MAX_WAVE_MAGNITUDE 20
58 #define HILL_CLIMBING_WAVE_MAGNITUDE_MULTIPLIER 1.0
59 #define HILL_CLIMBING_WAVE_HISTORY_SIZE 8
60 #define HILL_CLIMBING_TARGET_SIGNAL_TO_NOISE_RATIO 3.0
61 #define HILL_CLIMBING_MAX_CHANGE_PER_SECOND 4
62 #define HILL_CLIMBING_MAX_CHANGE_PER_SAMPLE 20
63 #define HILL_CLIMBING_SAMPLE_INTERVAL_LOW 10
64 #define HILL_CLIMBING_SAMPLE_INTERVAL_HIGH 200
65 #define HILL_CLIMBING_ERROR_SMOOTHING_FACTOR 0.01
66 #define HILL_CLIMBING_MAX_SAMPLE_ERROR_PERCENT 0.15
70 TRANSITION_INITIALIZING,
71 TRANSITION_RANDOM_MOVE,
72 TRANSITION_CLIMBING_MOVE,
73 TRANSITION_CHANGE_POINT,
74 TRANSITION_STABILIZING,
75 TRANSITION_STARVATION,
76 TRANSITION_THREAD_TIMED_OUT,
78 } ThreadPoolHeuristicStateTransition;
82 gint32 samples_to_measure;
83 gdouble target_throughput_ratio;
84 gdouble target_signal_to_noise_ratio;
85 gdouble max_change_per_second;
86 gdouble max_change_per_sample;
87 gint32 max_thread_wave_magnitude;
88 gint32 sample_interval_low;
89 gdouble thread_magnitude_multiplier;
90 gint32 sample_interval_high;
91 gdouble throughput_error_smoothing_factor;
92 gdouble gain_exponent;
93 gdouble max_sample_error;
95 gdouble current_control_setting;
97 gint16 last_thread_count;
98 gdouble elapsed_since_last_change;
99 gdouble completions_since_last_change;
101 gdouble average_throughput_noise;
104 gdouble *thread_counts;
106 guint32 current_sample_interval;
107 gpointer random_interval_generator;
109 gint32 accumulated_completion_count;
110 gdouble accumulated_sample_duration;
111 } ThreadPoolHillClimbing;
115 gint16 max_working; /* determined by heuristic */
116 gint16 starting; /* starting, but not yet in worker_thread */
117 gint16 working; /* executing worker_thread */
118 gint16 parked; /* parked */
121 } ThreadPoolWorkerCounter;
126 MonoThreadPoolWorkerCallback callback;
128 ThreadPoolWorkerCounter counters;
130 MonoCoopMutex parked_threads_lock;
131 gint32 parked_threads_count;
132 MonoCoopCond parked_threads_cond;
134 volatile gint32 work_items_count;
136 guint32 worker_creation_current_second;
137 guint32 worker_creation_current_count;
138 MonoCoopMutex worker_creation_lock;
140 gint32 heuristic_completions;
141 gint64 heuristic_sample_start;
142 gint64 heuristic_last_dequeue; // ms
143 gint64 heuristic_last_adjustment; // ms
144 gint64 heuristic_adjustment_interval; // ms
145 ThreadPoolHillClimbing heuristic_hill_climbing;
146 MonoCoopMutex heuristic_lock;
148 gint32 limit_worker_min;
149 gint32 limit_worker_max;
151 MonoCpuUsageState *cpu_usage_state;
154 /* suspended by the debugger */
157 gint32 monitor_status;
161 MONITOR_STATUS_REQUESTED,
162 MONITOR_STATUS_WAITING_FOR_REQUEST,
163 MONITOR_STATUS_NOT_RUNNING,
166 static ThreadPoolWorker worker;
168 #define COUNTER_CHECK(counter) \
170 g_assert (counter._.max_working > 0); \
171 g_assert (counter._.starting >= 0); \
172 g_assert (counter._.working >= 0); \
175 #define COUNTER_ATOMIC(var,block) \
177 ThreadPoolWorkerCounter __old; \
179 __old = COUNTER_READ (); \
182 COUNTER_CHECK (var); \
183 } while (InterlockedCompareExchange64 (&worker.counters.as_gint64, (var).as_gint64, __old.as_gint64) != __old.as_gint64); \
186 static inline ThreadPoolWorkerCounter
189 ThreadPoolWorkerCounter counter;
190 counter.as_gint64 = InterlockedRead64 (&worker.counters.as_gint64);
198 return mono_rand_init (NULL, 0);
202 rand_next (gpointer *handle, guint32 min, guint32 max)
206 mono_rand_try_get_uint32 (handle, &val, min, max, &error);
207 // FIXME handle error
208 mono_error_assert_ok (&error);
213 destroy (gpointer data)
215 mono_coop_mutex_destroy (&worker.parked_threads_lock);
216 mono_coop_cond_destroy (&worker.parked_threads_cond);
218 mono_coop_mutex_destroy (&worker.worker_creation_lock);
220 mono_coop_mutex_destroy (&worker.heuristic_lock);
222 g_free (worker.cpu_usage_state);
226 mono_threadpool_worker_init (MonoThreadPoolWorkerCallback callback)
228 ThreadPoolHillClimbing *hc;
229 const char *threads_per_cpu_env;
230 gint threads_per_cpu;
233 mono_refcount_init (&worker, destroy);
235 worker.callback = callback;
237 mono_coop_mutex_init (&worker.parked_threads_lock);
238 worker.parked_threads_count = 0;
239 mono_coop_cond_init (&worker.parked_threads_cond);
241 worker.worker_creation_current_second = -1;
242 mono_coop_mutex_init (&worker.worker_creation_lock);
244 worker.heuristic_adjustment_interval = 10;
245 mono_coop_mutex_init (&worker.heuristic_lock);
249 hc = &worker.heuristic_hill_climbing;
251 hc->wave_period = HILL_CLIMBING_WAVE_PERIOD;
252 hc->max_thread_wave_magnitude = HILL_CLIMBING_MAX_WAVE_MAGNITUDE;
253 hc->thread_magnitude_multiplier = (gdouble) HILL_CLIMBING_WAVE_MAGNITUDE_MULTIPLIER;
254 hc->samples_to_measure = hc->wave_period * HILL_CLIMBING_WAVE_HISTORY_SIZE;
255 hc->target_throughput_ratio = (gdouble) HILL_CLIMBING_BIAS;
256 hc->target_signal_to_noise_ratio = (gdouble) HILL_CLIMBING_TARGET_SIGNAL_TO_NOISE_RATIO;
257 hc->max_change_per_second = (gdouble) HILL_CLIMBING_MAX_CHANGE_PER_SECOND;
258 hc->max_change_per_sample = (gdouble) HILL_CLIMBING_MAX_CHANGE_PER_SAMPLE;
259 hc->sample_interval_low = HILL_CLIMBING_SAMPLE_INTERVAL_LOW;
260 hc->sample_interval_high = HILL_CLIMBING_SAMPLE_INTERVAL_HIGH;
261 hc->throughput_error_smoothing_factor = (gdouble) HILL_CLIMBING_ERROR_SMOOTHING_FACTOR;
262 hc->gain_exponent = (gdouble) HILL_CLIMBING_GAIN_EXPONENT;
263 hc->max_sample_error = (gdouble) HILL_CLIMBING_MAX_SAMPLE_ERROR_PERCENT;
264 hc->current_control_setting = 0;
265 hc->total_samples = 0;
266 hc->last_thread_count = 0;
267 hc->average_throughput_noise = 0;
268 hc->elapsed_since_last_change = 0;
269 hc->accumulated_completion_count = 0;
270 hc->accumulated_sample_duration = 0;
271 hc->samples = g_new0 (gdouble, hc->samples_to_measure);
272 hc->thread_counts = g_new0 (gdouble, hc->samples_to_measure);
273 hc->random_interval_generator = rand_create ();
274 hc->current_sample_interval = rand_next (&hc->random_interval_generator, hc->sample_interval_low, hc->sample_interval_high);
276 if (!(threads_per_cpu_env = g_getenv ("MONO_THREADS_PER_CPU")))
279 threads_per_cpu = CLAMP (atoi (threads_per_cpu_env), 1, 50);
281 threads_count = mono_cpu_count () * threads_per_cpu;
283 worker.limit_worker_min = threads_count;
285 #if defined (PLATFORM_ANDROID) || defined (HOST_IOS)
286 worker.limit_worker_max = CLAMP (threads_count * 100, MIN (threads_count, 200), MAX (threads_count, 200));
288 worker.limit_worker_max = threads_count * 100;
291 worker.counters._.max_working = worker.limit_worker_min;
293 worker.cpu_usage_state = g_new0 (MonoCpuUsageState, 1);
295 worker.suspended = FALSE;
297 worker.monitor_status = MONITOR_STATUS_NOT_RUNNING;
301 mono_threadpool_worker_cleanup (void)
303 mono_refcount_dec (&worker);
307 work_item_push (void)
312 old = InterlockedRead (&worker.work_items_count);
316 } while (InterlockedCompareExchange (&worker.work_items_count, new, old) != old);
320 work_item_try_pop (void)
325 old = InterlockedRead (&worker.work_items_count);
332 } while (InterlockedCompareExchange (&worker.work_items_count, new, old) != old);
338 work_item_count (void)
340 return InterlockedRead (&worker.work_items_count);
343 static void worker_request (void);
346 mono_threadpool_worker_request (void)
348 if (!mono_refcount_tryinc (&worker))
355 mono_refcount_dec (&worker);
359 worker_wait_interrupt (gpointer unused)
361 /* If the runtime is not shutting down, we are not using this mechanism to wake up a unparked thread, and if the
362 * runtime is shutting down, then we need to wake up ALL the threads.
363 * It might be a bit wasteful, but I witnessed shutdown hang where the main thread would abort and then wait for all
364 * background threads to exit (see mono_thread_manage). This would go wrong because not all threadpool threads would
365 * be unparked. It would end up getting unstucked because of the timeout, but that would delay shutdown by 5-60s. */
366 if (!mono_runtime_is_shutting_down ())
369 if (!mono_refcount_tryinc (&worker))
372 mono_coop_mutex_lock (&worker.parked_threads_lock);
373 mono_coop_cond_broadcast (&worker.parked_threads_cond);
374 mono_coop_mutex_unlock (&worker.parked_threads_lock);
376 mono_refcount_dec (&worker);
379 /* return TRUE if timeout, FALSE otherwise (worker unpark or interrupt) */
383 gboolean timeout = FALSE;
384 gboolean interrupted = FALSE;
386 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_THREADPOOL, "[%p] worker parking", mono_native_thread_id_get ());
388 mono_coop_mutex_lock (&worker.parked_threads_lock);
390 if (!mono_runtime_is_shutting_down ()) {
391 static gpointer rand_handle = NULL;
392 MonoInternalThread *thread;
393 ThreadPoolWorkerCounter counter;
396 rand_handle = rand_create ();
397 g_assert (rand_handle);
399 thread = mono_thread_internal_current ();
402 COUNTER_ATOMIC (counter, {
403 counter._.working --;
407 worker.parked_threads_count += 1;
409 mono_thread_info_install_interrupt (worker_wait_interrupt, NULL, &interrupted);
413 if (mono_coop_cond_timedwait (&worker.parked_threads_cond, &worker.parked_threads_lock, rand_next (&rand_handle, 5 * 1000, 60 * 1000)) != 0)
416 mono_thread_info_uninstall_interrupt (&interrupted);
419 worker.parked_threads_count -= 1;
421 COUNTER_ATOMIC (counter, {
422 counter._.working ++;
427 mono_coop_mutex_unlock (&worker.parked_threads_lock);
429 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_THREADPOOL, "[%p] worker unparking, timeout? %s interrupted? %s",
430 mono_native_thread_id_get (), timeout ? "yes" : "no", interrupted ? "yes" : "no");
436 worker_try_unpark (void)
438 gboolean res = FALSE;
440 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try unpark worker", mono_native_thread_id_get ());
442 mono_coop_mutex_lock (&worker.parked_threads_lock);
443 if (worker.parked_threads_count > 0) {
444 mono_coop_cond_signal (&worker.parked_threads_cond);
447 mono_coop_mutex_unlock (&worker.parked_threads_lock);
449 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try unpark worker, success? %s", mono_native_thread_id_get (), res ? "yes" : "no");
455 worker_thread (gpointer unused)
457 MonoInternalThread *thread;
458 ThreadPoolWorkerCounter counter;
460 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_THREADPOOL, "[%p] worker starting", mono_native_thread_id_get ());
462 if (!mono_refcount_tryinc (&worker))
465 COUNTER_ATOMIC (counter, {
466 counter._.starting --;
467 counter._.working ++;
470 thread = mono_thread_internal_current ();
473 while (!mono_runtime_is_shutting_down ()) {
474 if (mono_thread_interruption_checkpoint ())
477 if (!work_item_try_pop ()) {
480 timeout = worker_park ();
487 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] worker executing",
488 mono_native_thread_id_get ());
493 COUNTER_ATOMIC (counter, {
494 counter._.working --;
497 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_THREADPOOL, "[%p] worker finishing", mono_native_thread_id_get ());
499 mono_refcount_dec (&worker);
505 worker_try_create (void)
508 MonoInternalThread *thread;
509 gint64 current_ticks;
511 ThreadPoolWorkerCounter counter;
513 if (mono_runtime_is_shutting_down ())
516 mono_coop_mutex_lock (&worker.worker_creation_lock);
518 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try create worker", mono_native_thread_id_get ());
520 current_ticks = mono_100ns_ticks ();
521 if (0 == current_ticks) {
522 g_warning ("failed to get 100ns ticks");
524 now = current_ticks / (10 * 1000 * 1000);
525 if (worker.worker_creation_current_second != now) {
526 worker.worker_creation_current_second = now;
527 worker.worker_creation_current_count = 0;
529 g_assert (worker.worker_creation_current_count <= WORKER_CREATION_MAX_PER_SEC);
530 if (worker.worker_creation_current_count == WORKER_CREATION_MAX_PER_SEC) {
531 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",
532 mono_native_thread_id_get (), worker.worker_creation_current_count);
533 mono_coop_mutex_unlock (&worker.worker_creation_lock);
539 COUNTER_ATOMIC (counter, {
540 if (counter._.working >= counter._.max_working) {
541 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try create worker, failed: maximum number of working threads reached",
542 mono_native_thread_id_get ());
543 mono_coop_mutex_unlock (&worker.worker_creation_lock);
546 counter._.starting ++;
549 thread = mono_thread_create_internal (mono_get_root_domain (), worker_thread, NULL, MONO_THREAD_CREATE_FLAGS_THREADPOOL, &error);
551 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try create worker, failed: could not create thread due to %s", mono_native_thread_id_get (), mono_error_get_message (&error));
552 mono_error_cleanup (&error);
554 COUNTER_ATOMIC (counter, {
555 counter._.starting --;
558 mono_coop_mutex_unlock (&worker.worker_creation_lock);
563 worker.worker_creation_current_count += 1;
565 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] try create worker, created %p, now = %d count = %d",
566 mono_native_thread_id_get (), (gpointer) thread->tid, now, worker.worker_creation_current_count);
568 mono_coop_mutex_unlock (&worker.worker_creation_lock);
572 static void monitor_ensure_running (void);
575 worker_request (void)
577 if (worker.suspended)
580 monitor_ensure_running ();
582 if (worker_try_unpark ()) {
583 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] request worker, unparked", mono_native_thread_id_get ());
587 if (worker_try_create ()) {
588 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] request worker, created", mono_native_thread_id_get ());
592 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] request worker, failed", mono_native_thread_id_get ());
596 monitor_should_keep_running (void)
598 static gint64 last_should_keep_running = -1;
600 g_assert (worker.monitor_status == MONITOR_STATUS_WAITING_FOR_REQUEST || worker.monitor_status == MONITOR_STATUS_REQUESTED);
602 if (InterlockedExchange (&worker.monitor_status, MONITOR_STATUS_WAITING_FOR_REQUEST) == MONITOR_STATUS_WAITING_FOR_REQUEST) {
603 gboolean should_keep_running = TRUE, force_should_keep_running = FALSE;
605 if (mono_runtime_is_shutting_down ()) {
606 should_keep_running = FALSE;
608 if (work_item_count () == 0)
609 should_keep_running = FALSE;
611 if (!should_keep_running) {
612 if (last_should_keep_running == -1 || mono_100ns_ticks () - last_should_keep_running < MONITOR_MINIMAL_LIFETIME * 1000 * 10) {
613 should_keep_running = force_should_keep_running = TRUE;
618 if (should_keep_running) {
619 if (last_should_keep_running == -1 || !force_should_keep_running)
620 last_should_keep_running = mono_100ns_ticks ();
622 last_should_keep_running = -1;
623 if (InterlockedCompareExchange (&worker.monitor_status, MONITOR_STATUS_NOT_RUNNING, MONITOR_STATUS_WAITING_FOR_REQUEST) == MONITOR_STATUS_WAITING_FOR_REQUEST)
628 g_assert (worker.monitor_status == MONITOR_STATUS_WAITING_FOR_REQUEST || worker.monitor_status == MONITOR_STATUS_REQUESTED);
634 monitor_sufficient_delay_since_last_dequeue (void)
638 if (worker.cpu_usage < CPU_USAGE_LOW) {
639 threshold = MONITOR_INTERVAL;
641 ThreadPoolWorkerCounter counter;
642 counter = COUNTER_READ ();
643 threshold = counter._.max_working * MONITOR_INTERVAL * 2;
646 return mono_msec_ticks () >= worker.heuristic_last_dequeue + threshold;
649 static void hill_climbing_force_change (gint16 new_thread_count, ThreadPoolHeuristicStateTransition transition);
652 monitor_thread (gpointer unused)
654 MonoInternalThread *internal;
657 if (!mono_refcount_tryinc (&worker))
660 internal = mono_thread_internal_current ();
663 mono_cpu_usage (worker.cpu_usage_state);
665 // printf ("monitor_thread: start\n");
667 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, started", mono_native_thread_id_get ());
670 ThreadPoolWorkerCounter counter;
671 gboolean limit_worker_max_reached;
672 gint32 interval_left = MONITOR_INTERVAL;
673 gint32 awake = 0; /* number of spurious awakes we tolerate before doing a round of rebalancing */
675 g_assert (worker.monitor_status != MONITOR_STATUS_NOT_RUNNING);
677 // counter = COUNTER_READ ();
678 // printf ("monitor_thread: starting = %d working = %d parked = %d max_working = %d\n",
679 // counter._.starting, counter._.working, counter._.parked, counter._.max_working);
683 gboolean alerted = FALSE;
685 if (mono_runtime_is_shutting_down ())
688 ts = mono_msec_ticks ();
689 if (mono_thread_info_sleep (interval_left, &alerted) == 0)
691 interval_left -= mono_msec_ticks () - ts;
693 mono_thread_interruption_checkpoint ();
694 } while (interval_left > 0 && ++awake < 10);
696 if (mono_runtime_is_shutting_down ())
699 if (worker.suspended)
702 if (work_item_count () == 0)
705 worker.cpu_usage = mono_cpu_usage (worker.cpu_usage_state);
707 if (!monitor_sufficient_delay_since_last_dequeue ())
710 limit_worker_max_reached = FALSE;
712 COUNTER_ATOMIC (counter, {
713 if (counter._.max_working >= worker.limit_worker_max) {
714 limit_worker_max_reached = TRUE;
717 counter._.max_working ++;
720 if (limit_worker_max_reached)
723 hill_climbing_force_change (counter._.max_working, TRANSITION_STARVATION);
725 for (i = 0; i < 5; ++i) {
726 if (mono_runtime_is_shutting_down ())
729 if (worker_try_unpark ()) {
730 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, unparked", mono_native_thread_id_get ());
734 if (worker_try_create ()) {
735 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, created", mono_native_thread_id_get ());
739 } while (monitor_should_keep_running ());
741 // printf ("monitor_thread: stop\n");
743 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] monitor thread, finished", mono_native_thread_id_get ());
745 mono_refcount_dec (&worker);
750 monitor_ensure_running (void)
754 switch (worker.monitor_status) {
755 case MONITOR_STATUS_REQUESTED:
756 // printf ("monitor_thread: requested\n");
758 case MONITOR_STATUS_WAITING_FOR_REQUEST:
759 // printf ("monitor_thread: waiting for request\n");
760 InterlockedCompareExchange (&worker.monitor_status, MONITOR_STATUS_REQUESTED, MONITOR_STATUS_WAITING_FOR_REQUEST);
762 case MONITOR_STATUS_NOT_RUNNING:
763 // printf ("monitor_thread: not running\n");
764 if (mono_runtime_is_shutting_down ())
766 if (InterlockedCompareExchange (&worker.monitor_status, MONITOR_STATUS_REQUESTED, MONITOR_STATUS_NOT_RUNNING) == MONITOR_STATUS_NOT_RUNNING) {
767 // printf ("monitor_thread: creating\n");
768 if (!mono_thread_create_internal (mono_get_root_domain (), monitor_thread, NULL, MONO_THREAD_CREATE_FLAGS_THREADPOOL | MONO_THREAD_CREATE_FLAGS_SMALL_STACK, &error)) {
769 // printf ("monitor_thread: creating failed\n");
770 worker.monitor_status = MONITOR_STATUS_NOT_RUNNING;
771 mono_error_cleanup (&error);
772 mono_refcount_dec (&worker);
777 default: g_assert_not_reached ();
783 hill_climbing_change_thread_count (gint16 new_thread_count, ThreadPoolHeuristicStateTransition transition)
785 ThreadPoolHillClimbing *hc;
787 hc = &worker.heuristic_hill_climbing;
789 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_THREADPOOL, "[%p] hill climbing, change max number of threads %d", mono_native_thread_id_get (), new_thread_count);
791 hc->last_thread_count = new_thread_count;
792 hc->current_sample_interval = rand_next (&hc->random_interval_generator, hc->sample_interval_low, hc->sample_interval_high);
793 hc->elapsed_since_last_change = 0;
794 hc->completions_since_last_change = 0;
798 hill_climbing_force_change (gint16 new_thread_count, ThreadPoolHeuristicStateTransition transition)
800 ThreadPoolHillClimbing *hc;
802 hc = &worker.heuristic_hill_climbing;
804 if (new_thread_count != hc->last_thread_count) {
805 hc->current_control_setting += new_thread_count - hc->last_thread_count;
806 hill_climbing_change_thread_count (new_thread_count, transition);
810 static double_complex
811 hill_climbing_get_wave_component (gdouble *samples, guint sample_count, gdouble period)
813 ThreadPoolHillClimbing *hc;
814 gdouble w, cosine, sine, coeff, q0, q1, q2;
817 g_assert (sample_count >= period);
818 g_assert (period >= 2);
820 hc = &worker.heuristic_hill_climbing;
822 w = 2.0 * M_PI / period;
825 coeff = 2.0 * cosine;
828 for (i = 0; i < sample_count; ++i) {
829 q0 = coeff * q1 - q2 + samples [(hc->total_samples - sample_count + i) % hc->samples_to_measure];
834 return mono_double_complex_scalar_div (mono_double_complex_make (q1 - q2 * cosine, (q2 * sine)), ((gdouble)sample_count));
838 hill_climbing_update (gint16 current_thread_count, guint32 sample_duration, gint32 completions, gint64 *adjustment_interval)
840 ThreadPoolHillClimbing *hc;
841 ThreadPoolHeuristicStateTransition transition;
843 gdouble throughput_error_estimate;
849 gint new_thread_wave_magnitude;
850 gint new_thread_count;
851 double_complex thread_wave_component;
852 double_complex throughput_wave_component;
853 double_complex ratio;
855 g_assert (adjustment_interval);
857 hc = &worker.heuristic_hill_climbing;
859 /* If someone changed the thread count without telling us, update our records accordingly. */
860 if (current_thread_count != hc->last_thread_count)
861 hill_climbing_force_change (current_thread_count, TRANSITION_INITIALIZING);
863 /* Update the cumulative stats for this thread count */
864 hc->elapsed_since_last_change += sample_duration;
865 hc->completions_since_last_change += completions;
867 /* Add in any data we've already collected about this sample */
868 sample_duration += hc->accumulated_sample_duration;
869 completions += hc->accumulated_completion_count;
871 /* We need to make sure we're collecting reasonably accurate data. Since we're just counting the end
872 * of each work item, we are goinng to be missing some data about what really happened during the
873 * sample interval. The count produced by each thread includes an initial work item that may have
874 * started well before the start of the interval, and each thread may have been running some new
875 * work item for some time before the end of the interval, which did not yet get counted. So
876 * our count is going to be off by +/- threadCount workitems.
878 * The exception is that the thread that reported to us last time definitely wasn't running any work
879 * at that time, and the thread that's reporting now definitely isn't running a work item now. So
880 * we really only need to consider threadCount-1 threads.
882 * Thus the percent error in our count is +/- (threadCount-1)/numCompletions.
884 * We cannot rely on the frequency-domain analysis we'll be doing later to filter out this error, because
885 * of the way it accumulates over time. If this sample is off by, say, 33% in the negative direction,
886 * then the next one likely will be too. The one after that will include the sum of the completions
887 * we missed in the previous samples, and so will be 33% positive. So every three samples we'll have
888 * two "low" samples and one "high" sample. This will appear as periodic variation right in the frequency
889 * range we're targeting, which will not be filtered by the frequency-domain translation. */
890 if (hc->total_samples > 0 && ((current_thread_count - 1.0) / completions) >= hc->max_sample_error) {
891 /* Not accurate enough yet. Let's accumulate the data so
892 * far, and tell the ThreadPoolWorker to collect a little more. */
893 hc->accumulated_sample_duration = sample_duration;
894 hc->accumulated_completion_count = completions;
895 *adjustment_interval = 10;
896 return current_thread_count;
899 /* We've got enouugh data for our sample; reset our accumulators for next time. */
900 hc->accumulated_sample_duration = 0;
901 hc->accumulated_completion_count = 0;
903 /* Add the current thread count and throughput sample to our history. */
904 throughput = ((gdouble) completions) / sample_duration;
906 sample_index = hc->total_samples % hc->samples_to_measure;
907 hc->samples [sample_index] = throughput;
908 hc->thread_counts [sample_index] = current_thread_count;
909 hc->total_samples ++;
911 /* Set up defaults for our metrics. */
912 thread_wave_component = mono_double_complex_make(0, 0);
913 throughput_wave_component = mono_double_complex_make(0, 0);
914 throughput_error_estimate = 0;
915 ratio = mono_double_complex_make(0, 0);
918 transition = TRANSITION_WARMUP;
920 /* How many samples will we use? It must be at least the three wave periods we're looking for, and it must also
921 * be a whole multiple of the primary wave's period; otherwise the frequency we're looking for will fall between
922 * two frequency bands in the Fourier analysis, and we won't be able to measure it accurately. */
923 sample_count = ((gint) MIN (hc->total_samples - 1, hc->samples_to_measure) / hc->wave_period) * hc->wave_period;
925 if (sample_count > hc->wave_period) {
927 gdouble average_throughput;
928 gdouble average_thread_count;
929 gdouble sample_sum = 0;
930 gdouble thread_sum = 0;
932 /* Average the throughput and thread count samples, so we can scale the wave magnitudes later. */
933 for (i = 0; i < sample_count; ++i) {
934 guint j = (hc->total_samples - sample_count + i) % hc->samples_to_measure;
935 sample_sum += hc->samples [j];
936 thread_sum += hc->thread_counts [j];
939 average_throughput = sample_sum / sample_count;
940 average_thread_count = thread_sum / sample_count;
942 if (average_throughput > 0 && average_thread_count > 0) {
943 gdouble noise_for_confidence, adjacent_period_1, adjacent_period_2;
945 /* Calculate the periods of the adjacent frequency bands we'll be using to
946 * measure noise levels. We want the two adjacent Fourier frequency bands. */
947 adjacent_period_1 = sample_count / (((gdouble) sample_count) / ((gdouble) hc->wave_period) + 1);
948 adjacent_period_2 = sample_count / (((gdouble) sample_count) / ((gdouble) hc->wave_period) - 1);
950 /* Get the the three different frequency components of the throughput (scaled by average
951 * throughput). Our "error" estimate (the amount of noise that might be present in the
952 * frequency band we're really interested in) is the average of the adjacent bands. */
953 throughput_wave_component = mono_double_complex_scalar_div (hill_climbing_get_wave_component (hc->samples, sample_count, hc->wave_period), average_throughput);
954 throughput_error_estimate = cabs (mono_double_complex_scalar_div (hill_climbing_get_wave_component (hc->samples, sample_count, adjacent_period_1), average_throughput));
956 if (adjacent_period_2 <= sample_count) {
957 throughput_error_estimate = MAX (throughput_error_estimate, cabs (mono_double_complex_scalar_div (hill_climbing_get_wave_component (
958 hc->samples, sample_count, adjacent_period_2), average_throughput)));
961 /* Do the same for the thread counts, so we have something to compare to. We don't
962 * measure thread count noise, because there is none; these are exact measurements. */
963 thread_wave_component = mono_double_complex_scalar_div (hill_climbing_get_wave_component (hc->thread_counts, sample_count, hc->wave_period), average_thread_count);
965 /* Update our moving average of the throughput noise. We'll use this
966 * later as feedback to determine the new size of the thread wave. */
967 if (hc->average_throughput_noise == 0) {
968 hc->average_throughput_noise = throughput_error_estimate;
970 hc->average_throughput_noise = (hc->throughput_error_smoothing_factor * throughput_error_estimate)
971 + ((1.0 + hc->throughput_error_smoothing_factor) * hc->average_throughput_noise);
974 if (cabs (thread_wave_component) > 0) {
975 /* Adjust the throughput wave so it's centered around the target wave,
976 * and then calculate the adjusted throughput/thread ratio. */
977 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);
978 transition = TRANSITION_CLIMBING_MOVE;
980 ratio = mono_double_complex_make (0, 0);
981 transition = TRANSITION_STABILIZING;
984 noise_for_confidence = MAX (hc->average_throughput_noise, throughput_error_estimate);
985 if (noise_for_confidence > 0) {
986 confidence = cabs (thread_wave_component) / noise_for_confidence / hc->target_signal_to_noise_ratio;
988 /* there is no noise! */
994 /* We use just the real part of the complex ratio we just calculated. If the throughput signal
995 * is exactly in phase with the thread signal, this will be the same as taking the magnitude of
996 * the complex move and moving that far up. If they're 180 degrees out of phase, we'll move
997 * backward (because this indicates that our changes are having the opposite of the intended effect).
998 * If they're 90 degrees out of phase, we won't move at all, because we can't tell wether we're
999 * having a negative or positive effect on throughput. */
1000 move = creal (ratio);
1001 move = CLAMP (move, -1.0, 1.0);
1003 /* Apply our confidence multiplier. */
1004 move *= CLAMP (confidence, -1.0, 1.0);
1006 /* Now apply non-linear gain, such that values around zero are attenuated, while higher values
1007 * are enhanced. This allows us to move quickly if we're far away from the target, but more slowly
1008 * if we're getting close, giving us rapid ramp-up without wild oscillations around the target. */
1009 gain = hc->max_change_per_second * sample_duration;
1010 move = pow (fabs (move), hc->gain_exponent) * (move >= 0.0 ? 1 : -1) * gain;
1011 move = MIN (move, hc->max_change_per_sample);
1013 /* If the result was positive, and CPU is > 95%, refuse the move. */
1014 if (move > 0.0 && worker.cpu_usage > CPU_USAGE_HIGH)
1017 /* Apply the move to our control setting. */
1018 hc->current_control_setting += move;
1020 /* Calculate the new thread wave magnitude, which is based on the moving average we've been keeping of the
1021 * throughput error. This average starts at zero, so we'll start with a nice safe little wave at first. */
1022 new_thread_wave_magnitude = (gint)(0.5 + (hc->current_control_setting * hc->average_throughput_noise
1023 * hc->target_signal_to_noise_ratio * hc->thread_magnitude_multiplier * 2.0));
1024 new_thread_wave_magnitude = CLAMP (new_thread_wave_magnitude, 1, hc->max_thread_wave_magnitude);
1026 /* Make sure our control setting is within the ThreadPoolWorker's limits. */
1027 hc->current_control_setting = CLAMP (hc->current_control_setting, worker.limit_worker_min, worker.limit_worker_max - new_thread_wave_magnitude);
1029 /* Calculate the new thread count (control setting + square wave). */
1030 new_thread_count = (gint)(hc->current_control_setting + new_thread_wave_magnitude * ((hc->total_samples / (hc->wave_period / 2)) % 2));
1032 /* Make sure the new thread count doesn't exceed the ThreadPoolWorker's limits. */
1033 new_thread_count = CLAMP (new_thread_count, worker.limit_worker_min, worker.limit_worker_max);
1035 if (new_thread_count != current_thread_count)
1036 hill_climbing_change_thread_count (new_thread_count, transition);
1038 if (creal (ratio) < 0.0 && new_thread_count == worker.limit_worker_min)
1039 *adjustment_interval = (gint)(0.5 + hc->current_sample_interval * (10.0 * MAX (-1.0 * creal (ratio), 1.0)));
1041 *adjustment_interval = hc->current_sample_interval;
1043 return new_thread_count;
1047 heuristic_should_adjust (void)
1049 if (worker.heuristic_last_dequeue > worker.heuristic_last_adjustment + worker.heuristic_adjustment_interval) {
1050 ThreadPoolWorkerCounter counter;
1051 counter = COUNTER_READ ();
1052 if (counter._.working <= counter._.max_working)
1060 heuristic_adjust (void)
1062 if (mono_coop_mutex_trylock (&worker.heuristic_lock) == 0) {
1063 gint32 completions = InterlockedExchange (&worker.heuristic_completions, 0);
1064 gint64 sample_end = mono_msec_ticks ();
1065 gint64 sample_duration = sample_end - worker.heuristic_sample_start;
1067 if (sample_duration >= worker.heuristic_adjustment_interval / 2) {
1068 ThreadPoolWorkerCounter counter;
1069 gint16 new_thread_count;
1071 counter = COUNTER_READ ();
1072 new_thread_count = hill_climbing_update (counter._.max_working, sample_duration, completions, &worker.heuristic_adjustment_interval);
1074 COUNTER_ATOMIC (counter, {
1075 counter._.max_working = new_thread_count;
1078 if (new_thread_count > counter._.max_working)
1081 worker.heuristic_sample_start = sample_end;
1082 worker.heuristic_last_adjustment = mono_msec_ticks ();
1085 mono_coop_mutex_unlock (&worker.heuristic_lock);
1090 heuristic_notify_work_completed (void)
1092 InterlockedIncrement (&worker.heuristic_completions);
1093 worker.heuristic_last_dequeue = mono_msec_ticks ();
1095 if (heuristic_should_adjust ())
1096 heuristic_adjust ();
1100 mono_threadpool_worker_notify_completed (void)
1102 ThreadPoolWorkerCounter counter;
1104 heuristic_notify_work_completed ();
1106 counter = COUNTER_READ ();
1107 return counter._.working <= counter._.max_working;
1111 mono_threadpool_worker_get_min (void)
1115 if (!mono_refcount_tryinc (&worker))
1118 ret = worker.limit_worker_min;
1120 mono_refcount_dec (&worker);
1125 mono_threadpool_worker_set_min (gint32 value)
1127 if (value <= 0 || value > worker.limit_worker_max)
1130 if (!mono_refcount_tryinc (&worker))
1133 worker.limit_worker_min = value;
1135 mono_refcount_dec (&worker);
1140 mono_threadpool_worker_get_max (void)
1144 if (!mono_refcount_tryinc (&worker))
1147 ret = worker.limit_worker_max;
1149 mono_refcount_dec (&worker);
1154 mono_threadpool_worker_set_max (gint32 value)
1158 cpu_count = mono_cpu_count ();
1159 if (value < worker.limit_worker_min || value < cpu_count)
1162 if (!mono_refcount_tryinc (&worker))
1165 worker.limit_worker_max = value;
1167 mono_refcount_dec (&worker);
1172 mono_threadpool_worker_set_suspended (gboolean suspended)
1174 if (!mono_refcount_tryinc (&worker))
1177 worker.suspended = suspended;
1181 mono_refcount_dec (&worker);