3 * Worker threads for parallel and concurrent GC.
5 * Copyright 2001-2003 Ximian, Inc
6 * Copyright 2003-2010 Novell, Inc.
7 * Copyright (C) 2012 Xamarin Inc
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
17 #include "mono/sgen/sgen-gc.h"
18 #include "mono/sgen/sgen-workers.h"
19 #include "mono/sgen/sgen-thread-pool.h"
20 #include "mono/utils/mono-membar.h"
21 #include "mono/sgen/sgen-client.h"
23 static int workers_num;
24 static int active_workers_num;
25 static volatile gboolean started;
26 static volatile gboolean forced_stop;
27 static WorkerData *workers_data;
28 static SgenWorkerCallback worker_init_cb;
30 static SgenThreadPool pool_inst;
31 static SgenThreadPool *pool; /* null if we're not using workers */
34 * When using multiple workers, we need to have the last worker
35 * enqueue the preclean jobs (if there are any). This lock ensures
36 * that when the last worker takes it, all the other workers have
37 * gracefully finished, so it can restart them.
39 static mono_mutex_t finished_lock;
40 static volatile gboolean workers_finished;
41 static int worker_awakenings;
43 static SgenSectionGrayQueue workers_distribute_gray_queue;
46 * Allowed transitions:
48 * | from \ to | NOT WORKING | WORKING | WORK ENQUEUED |
49 * |--------------------+-------------+---------+---------------+
50 * | NOT WORKING | - | - | main / worker |
51 * | WORKING | worker | - | main / worker |
52 * | WORK ENQUEUED | - | worker | - |
54 * The WORK ENQUEUED state guarantees that the worker thread will inspect the queue again at
55 * least once. Only after looking at the queue will it go back to WORKING, and then,
56 * eventually, to NOT WORKING. After enqueuing work the main thread transitions the state
57 * to WORK ENQUEUED. Signalling the worker thread to wake up is only necessary if the old
58 * state was NOT WORKING.
67 #define SGEN_WORKER_MIN_SECTIONS_SIGNAL 4
71 static SgenObjectOperations * volatile idle_func_object_ops;
72 static SgenObjectOperations *idle_func_object_ops_par, *idle_func_object_ops_nopar;
74 * finished_callback is called only when the workers finish work normally (when they
75 * are not forced to finish). The callback is used to enqueue preclean jobs.
77 static volatile SgenWorkersFinishCallback finish_callback;
79 static guint64 stat_workers_num_finished;
82 set_state (WorkerData *data, State old_state, State new_state)
84 SGEN_ASSERT (0, old_state != new_state, "Why are we transitioning to the same state?");
85 if (new_state == STATE_NOT_WORKING)
86 SGEN_ASSERT (0, old_state == STATE_WORKING, "We can only transition to NOT WORKING from WORKING");
87 else if (new_state == STATE_WORKING)
88 SGEN_ASSERT (0, old_state == STATE_WORK_ENQUEUED, "We can only transition to WORKING from WORK ENQUEUED");
89 if (new_state == STATE_NOT_WORKING || new_state == STATE_WORKING)
90 SGEN_ASSERT (6, sgen_thread_pool_is_thread_pool_thread (pool, mono_native_thread_id_get ()), "Only the worker thread is allowed to transition to NOT_WORKING or WORKING");
92 return InterlockedCompareExchange (&data->state, new_state, old_state) == old_state;
96 state_is_working_or_enqueued (State state)
98 return state == STATE_WORKING || state == STATE_WORK_ENQUEUED;
102 sgen_workers_ensure_awake (void)
105 gboolean need_signal = FALSE;
108 * All workers are awaken, make sure we reset the parallel context.
109 * We call this function only when starting the workers so nobody is running,
110 * or when the last worker is enqueuing preclean work. In both cases we can't
111 * have a worker working using a nopar context, which means it is safe.
113 idle_func_object_ops = (active_workers_num > 1) ? idle_func_object_ops_par : idle_func_object_ops_nopar;
114 workers_finished = FALSE;
116 for (i = 0; i < active_workers_num; i++) {
118 gboolean did_set_state;
121 old_state = workers_data [i].state;
123 if (old_state == STATE_WORK_ENQUEUED)
126 did_set_state = set_state (&workers_data [i], old_state, STATE_WORK_ENQUEUED);
127 } while (!did_set_state);
129 if (!state_is_working_or_enqueued (old_state))
134 sgen_thread_pool_idle_signal (pool);
138 worker_try_finish (WorkerData *data)
143 ++stat_workers_num_finished;
145 mono_os_mutex_lock (&finished_lock);
147 for (i = 0; i < active_workers_num; i++) {
148 if (state_is_working_or_enqueued (workers_data [i].state))
153 SgenWorkersFinishCallback callback = finish_callback;
154 SGEN_ASSERT (0, idle_func_object_ops == idle_func_object_ops_nopar, "Why are we finishing with parallel context");
155 /* We are the last one left. Enqueue preclean job if we have one and awake everybody */
156 SGEN_ASSERT (0, data->state != STATE_NOT_WORKING, "How did we get from doing idle work to NOT WORKING without setting it ourselves?");
158 finish_callback = NULL;
160 worker_awakenings = 0;
161 /* Make sure each worker has a chance of seeing the enqueued jobs */
162 sgen_workers_ensure_awake ();
163 SGEN_ASSERT (0, data->state == STATE_WORK_ENQUEUED, "Why did we fail to set our own state to ENQUEUED");
169 old_state = data->state;
171 SGEN_ASSERT (0, old_state != STATE_NOT_WORKING, "How did we get from doing idle work to NOT WORKING without setting it ourselves?");
172 if (old_state == STATE_WORK_ENQUEUED)
174 SGEN_ASSERT (0, old_state == STATE_WORKING, "What other possibility is there?");
175 } while (!set_state (data, old_state, STATE_NOT_WORKING));
178 * If we are second to last to finish, we set the scan context to the non-parallel
179 * version so we can speed up the last worker. This helps us maintain same level
180 * of performance as non-parallel mode even if we fail to distribute work properly.
183 idle_func_object_ops = idle_func_object_ops_nopar;
185 workers_finished = TRUE;
186 mono_os_mutex_unlock (&finished_lock);
188 binary_protocol_worker_finish (sgen_timestamp (), forced_stop);
190 sgen_gray_object_queue_trim_free_list (&data->private_gray_queue);
194 mono_os_mutex_unlock (&finished_lock);
198 sgen_workers_enqueue_job (SgenThreadPoolJob *job, gboolean enqueue)
201 job->func (NULL, job);
202 sgen_thread_pool_job_free (job);
206 sgen_thread_pool_job_enqueue (pool, job);
210 workers_get_work (WorkerData *data)
212 SgenMajorCollector *major = sgen_get_major_collector ();
213 SgenMinorCollector *minor = sgen_get_minor_collector ();
214 GrayQueueSection *section;
216 g_assert (sgen_gray_object_queue_is_empty (&data->private_gray_queue));
217 g_assert (major->is_concurrent || minor->is_parallel);
219 section = sgen_section_gray_queue_dequeue (&workers_distribute_gray_queue);
221 sgen_gray_object_enqueue_section (&data->private_gray_queue, section, major->is_parallel);
225 /* Nobody to steal from */
226 g_assert (sgen_gray_object_queue_is_empty (&data->private_gray_queue));
231 workers_steal_work (WorkerData *data)
233 SgenMajorCollector *major = sgen_get_major_collector ();
234 SgenMinorCollector *minor = sgen_get_minor_collector ();
235 int generation = sgen_get_current_collection_generation ();
236 GrayQueueSection *section = NULL;
237 int i, current_worker;
239 if ((generation == GENERATION_OLD && !major->is_parallel) ||
240 (generation == GENERATION_NURSERY && !minor->is_parallel))
243 /* If we're parallel, steal from other workers' private gray queues */
244 g_assert (sgen_gray_object_queue_is_empty (&data->private_gray_queue));
246 current_worker = (int) (data - workers_data);
248 for (i = 1; i < active_workers_num && !section; i++) {
249 int steal_worker = (current_worker + i) % active_workers_num;
250 if (state_is_working_or_enqueued (workers_data [steal_worker].state))
251 section = sgen_gray_object_steal_section (&workers_data [steal_worker].private_gray_queue);
255 sgen_gray_object_enqueue_section (&data->private_gray_queue, section, TRUE);
259 /* Nobody to steal from */
260 g_assert (sgen_gray_object_queue_is_empty (&data->private_gray_queue));
265 concurrent_enqueue_check (GCObject *obj)
267 g_assert (sgen_concurrent_collection_in_progress ());
268 g_assert (!sgen_ptr_in_nursery (obj));
269 g_assert (SGEN_LOAD_VTABLE (obj));
273 init_private_gray_queue (WorkerData *data)
275 sgen_gray_object_queue_init (&data->private_gray_queue,
276 sgen_get_major_collector ()->is_concurrent ? concurrent_enqueue_check : NULL,
281 thread_pool_init_func (void *data_untyped)
283 WorkerData *data = (WorkerData *)data_untyped;
284 SgenMajorCollector *major = sgen_get_major_collector ();
285 SgenMinorCollector *minor = sgen_get_minor_collector ();
287 sgen_client_thread_register_worker ();
289 if (!major->is_concurrent && !minor->is_parallel)
292 init_private_gray_queue (data);
295 worker_init_cb (data);
299 continue_idle_func (void *data_untyped)
302 WorkerData *data = (WorkerData *)data_untyped;
303 return state_is_working_or_enqueued (data->state);
305 /* Return if any of the threads is working */
306 return !sgen_workers_all_done ();
311 should_work_func (void *data_untyped)
313 WorkerData *data = (WorkerData*)data_untyped;
314 int current_worker = (int) (data - workers_data);
316 return started && current_worker < active_workers_num && state_is_working_or_enqueued (data->state);
320 marker_idle_func (void *data_untyped)
322 WorkerData *data = (WorkerData *)data_untyped;
324 SGEN_ASSERT (0, continue_idle_func (data_untyped), "Why are we called when we're not supposed to work?");
326 if (data->state == STATE_WORK_ENQUEUED) {
327 set_state (data, STATE_WORK_ENQUEUED, STATE_WORKING);
328 SGEN_ASSERT (0, data->state != STATE_NOT_WORKING, "How did we get from WORK ENQUEUED to NOT WORKING?");
331 if (!forced_stop && (!sgen_gray_object_queue_is_empty (&data->private_gray_queue) || workers_get_work (data) || workers_steal_work (data))) {
332 ScanCopyContext ctx = CONTEXT_FROM_OBJECT_OPERATIONS (idle_func_object_ops, &data->private_gray_queue);
334 SGEN_ASSERT (0, !sgen_gray_object_queue_is_empty (&data->private_gray_queue), "How is our gray queue empty if we just got work?");
336 sgen_drain_gray_stack (ctx);
338 if (data->private_gray_queue.num_sections >= SGEN_WORKER_MIN_SECTIONS_SIGNAL
339 && workers_finished && worker_awakenings < active_workers_num) {
340 /* We bound the number of worker awakenings just to be sure */
342 mono_os_mutex_lock (&finished_lock);
343 sgen_workers_ensure_awake ();
344 mono_os_mutex_unlock (&finished_lock);
347 worker_try_finish (data);
352 init_distribute_gray_queue (void)
354 sgen_section_gray_queue_init (&workers_distribute_gray_queue, TRUE,
355 sgen_get_major_collector ()->is_concurrent ? concurrent_enqueue_check : NULL);
359 sgen_workers_init (int num_workers, SgenWorkerCallback callback)
362 WorkerData **workers_data_ptrs = (WorkerData**)alloca(num_workers * sizeof(WorkerData*));
364 mono_os_mutex_init (&finished_lock);
365 //g_print ("initing %d workers\n", num_workers);
367 workers_num = num_workers;
368 active_workers_num = num_workers;
370 workers_data = (WorkerData *)sgen_alloc_internal_dynamic (sizeof (WorkerData) * num_workers, INTERNAL_MEM_WORKER_DATA, TRUE);
371 memset (workers_data, 0, sizeof (WorkerData) * num_workers);
373 init_distribute_gray_queue ();
375 for (i = 0; i < num_workers; ++i)
376 workers_data_ptrs [i] = &workers_data [i];
378 worker_init_cb = callback;
381 sgen_thread_pool_init (pool, num_workers, thread_pool_init_func, marker_idle_func, continue_idle_func, should_work_func, (SgenThreadPoolData**)workers_data_ptrs);
383 mono_counters_register ("# workers finished", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_workers_num_finished);
387 sgen_workers_shutdown (void)
390 sgen_thread_pool_shutdown (pool);
394 sgen_workers_stop_all_workers (void)
396 finish_callback = NULL;
397 mono_memory_write_barrier ();
400 sgen_thread_pool_wait_for_all_jobs (pool);
401 sgen_thread_pool_idle_wait (pool);
402 SGEN_ASSERT (0, sgen_workers_all_done (), "Can only signal enqueue work when in no work state");
408 sgen_workers_set_num_active_workers (int num_workers)
411 SGEN_ASSERT (0, active_workers_num <= workers_num, "We can't start more workers than we initialized");
412 active_workers_num = num_workers;
414 active_workers_num = workers_num;
419 sgen_workers_start_all_workers (SgenObjectOperations *object_ops_nopar, SgenObjectOperations *object_ops_par, SgenWorkersFinishCallback callback)
421 SGEN_ASSERT (0, !started, "Why are we starting to work without finishing previous cycle");
423 idle_func_object_ops_par = object_ops_par;
424 idle_func_object_ops_nopar = object_ops_nopar;
426 finish_callback = callback;
427 worker_awakenings = 0;
429 mono_memory_write_barrier ();
432 * We expect workers to start finishing only after all of them were awaken.
433 * Otherwise we might think that we have fewer workers and use wrong context.
435 mono_os_mutex_lock (&finished_lock);
436 sgen_workers_ensure_awake ();
437 mono_os_mutex_unlock (&finished_lock);
441 sgen_workers_join (void)
445 sgen_thread_pool_wait_for_all_jobs (pool);
446 sgen_thread_pool_idle_wait (pool);
447 SGEN_ASSERT (0, sgen_workers_all_done (), "Can only signal enqueue work when in no work state");
449 /* At this point all the workers have stopped. */
451 SGEN_ASSERT (0, sgen_section_gray_queue_is_empty (&workers_distribute_gray_queue), "Why is there still work left to do?");
452 for (i = 0; i < active_workers_num; ++i)
453 SGEN_ASSERT (0, sgen_gray_object_queue_is_empty (&workers_data [i].private_gray_queue), "Why is there still work left to do?");
459 * Can only be called if the workers are stopped.
460 * If we're stopped, there are also no pending jobs.
463 sgen_workers_have_idle_work (void)
467 SGEN_ASSERT (0, forced_stop && sgen_workers_all_done (), "Checking for idle work should only happen if the workers are stopped.");
469 if (!sgen_section_gray_queue_is_empty (&workers_distribute_gray_queue))
472 for (i = 0; i < active_workers_num; ++i) {
473 if (!sgen_gray_object_queue_is_empty (&workers_data [i].private_gray_queue))
481 sgen_workers_all_done (void)
485 for (i = 0; i < active_workers_num; i++) {
486 if (state_is_working_or_enqueued (workers_data [i].state))
492 /* Must only be used for debugging */
494 sgen_workers_are_working (void)
496 return !sgen_workers_all_done ();
500 sgen_workers_assert_gray_queue_is_empty (void)
502 SGEN_ASSERT (0, sgen_section_gray_queue_is_empty (&workers_distribute_gray_queue), "Why is the workers gray queue not empty?");
506 sgen_workers_take_from_queue (SgenGrayQueue *queue)
508 sgen_gray_object_spread (queue, sgen_workers_get_job_split_count ());
511 GrayQueueSection *section = sgen_gray_object_dequeue_section (queue);
514 sgen_section_gray_queue_enqueue (&workers_distribute_gray_queue, section);
517 SGEN_ASSERT (0, !sgen_workers_are_working (), "We should fully populate the distribute gray queue before we start the workers");
520 SgenObjectOperations*
521 sgen_workers_get_idle_func_object_ops (void)
523 g_assert (idle_func_object_ops);
524 return idle_func_object_ops;
528 * If we have a single worker, splitting into multiple jobs makes no sense. With
529 * more than one worker, we split into a larger number of jobs so that, in case
530 * the work load is uneven, a worker that finished quickly can take up more jobs
534 sgen_workers_get_job_split_count (void)
536 return (active_workers_num > 1) ? active_workers_num * 4 : 1;
540 sgen_workers_foreach (SgenWorkerCallback callback)
544 for (i = 0; i < workers_num; i++)
545 callback (&workers_data [i]);
549 sgen_workers_is_worker_thread (MonoNativeThreadId id)
553 return sgen_thread_pool_is_thread_pool_thread (pool, id);