2 * Copyright 2001-2003 Ximian, Inc
3 * Copyright 2003-2010 Novell, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include "metadata/sgen-gc.h"
29 #include "metadata/sgen-workers.h"
30 #include "utils/mono-counters.h"
32 static int workers_num;
33 static WorkerData *workers_data;
34 static WorkerData workers_gc_thread_data;
36 static SgenGrayQueue workers_distribute_gray_queue;
38 static volatile gboolean workers_gc_in_progress = FALSE;
39 static volatile gboolean workers_marking = FALSE;
40 static gboolean workers_started = FALSE;
41 static volatile int workers_num_waiting = 0;
42 static MonoSemType workers_waiting_sem;
43 static MonoSemType workers_done_sem;
44 static volatile int workers_done_posted = 0;
46 static volatile int workers_job_queue_num_entries = 0;
47 static volatile JobQueueEntry *workers_job_queue = NULL;
48 static LOCK_DECLARE (workers_job_queue_mutex);
50 static long long stat_workers_stolen_from_self_lock;
51 static long long stat_workers_stolen_from_self_no_lock;
52 static long long stat_workers_stolen_from_others;
53 static long long stat_workers_num_waited;
56 workers_wake_up (int max)
60 for (i = 0; i < max; ++i) {
63 num = workers_num_waiting;
66 } while (InterlockedCompareExchange (&workers_num_waiting, num - 1, num) != num);
67 MONO_SEM_POST (&workers_waiting_sem);
72 workers_wake_up_all (void)
74 workers_wake_up (workers_num);
81 ++stat_workers_num_waited;
83 num = workers_num_waiting;
84 } while (InterlockedCompareExchange (&workers_num_waiting, num + 1, num) != num);
85 if (num + 1 == workers_num && !workers_gc_in_progress) {
86 /* Make sure the done semaphore is only posted once. */
89 posted = workers_done_posted;
92 } while (InterlockedCompareExchange (&workers_done_posted, 1, 0) != 0);
94 MONO_SEM_POST (&workers_done_sem);
96 MONO_SEM_WAIT (&workers_waiting_sem);
100 sgen_workers_enqueue_job (JobFunc func, void *data)
103 JobQueueEntry *entry;
105 if (!sgen_collection_is_parallel ()) {
110 entry = sgen_alloc_internal (INTERNAL_MEM_JOB_QUEUE_ENTRY);
114 mono_mutex_lock (&workers_job_queue_mutex);
115 entry->next = workers_job_queue;
116 workers_job_queue = entry;
117 num_entries = ++workers_job_queue_num_entries;
118 mono_mutex_unlock (&workers_job_queue_mutex);
120 workers_wake_up (num_entries);
124 workers_dequeue_and_do_job (WorkerData *data)
126 JobQueueEntry *entry;
128 g_assert (sgen_collection_is_parallel ());
130 if (!workers_job_queue_num_entries)
133 mono_mutex_lock (&workers_job_queue_mutex);
134 entry = (JobQueueEntry*)workers_job_queue;
136 workers_job_queue = entry->next;
137 --workers_job_queue_num_entries;
139 mono_mutex_unlock (&workers_job_queue_mutex);
144 entry->func (data, entry->data);
145 sgen_free_internal (entry, INTERNAL_MEM_JOB_QUEUE_ENTRY);
150 workers_steal (WorkerData *data, WorkerData *victim_data, gboolean lock)
152 SgenGrayQueue *queue = &data->private_gray_queue;
155 g_assert (!queue->first);
157 if (!victim_data->stealable_stack_fill)
160 if (lock && mono_mutex_trylock (&victim_data->stealable_stack_mutex))
163 n = num = (victim_data->stealable_stack_fill + 1) / 2;
164 /* We're stealing num entries. */
167 int m = MIN (SGEN_GRAY_QUEUE_SECTION_SIZE, n);
170 sgen_gray_object_alloc_queue_section (queue);
171 memcpy (queue->first->objects,
172 victim_data->stealable_stack + victim_data->stealable_stack_fill - num + n,
174 queue->first->end = m;
177 victim_data->stealable_stack_fill -= num;
180 mono_mutex_unlock (&victim_data->stealable_stack_mutex);
182 if (data == victim_data) {
184 stat_workers_stolen_from_self_lock += num;
186 stat_workers_stolen_from_self_no_lock += num;
188 stat_workers_stolen_from_others += num;
195 workers_get_work (WorkerData *data)
199 g_assert (sgen_gray_object_queue_is_empty (&data->private_gray_queue));
201 /* Try to steal from our own stack. */
202 if (workers_steal (data, data, TRUE))
205 /* Then from the GC thread's stack. */
206 if (workers_steal (data, &workers_gc_thread_data, TRUE))
209 /* Finally, from another worker. */
210 for (i = 0; i < workers_num; ++i) {
211 WorkerData *victim_data = &workers_data [i];
212 if (data == victim_data)
214 if (workers_steal (data, victim_data, TRUE))
218 /* Nobody to steal from */
219 g_assert (sgen_gray_object_queue_is_empty (&data->private_gray_queue));
224 workers_gray_queue_share_redirect (SgenGrayQueue *queue)
226 GrayQueueSection *section;
227 WorkerData *data = queue->alloc_prepare_data;
229 if (data->stealable_stack_fill) {
231 * There are still objects in the stealable stack, so
232 * wake up any workers that might be sleeping
234 if (workers_gc_in_progress)
235 workers_wake_up_all ();
239 /* The stealable stack is empty, so fill it. */
240 mono_mutex_lock (&data->stealable_stack_mutex);
242 while (data->stealable_stack_fill < STEALABLE_STACK_SIZE &&
243 (section = sgen_gray_object_dequeue_section (queue))) {
244 int num = MIN (section->end, STEALABLE_STACK_SIZE - data->stealable_stack_fill);
246 memcpy (data->stealable_stack + data->stealable_stack_fill,
247 section->objects + section->end - num,
248 sizeof (char*) * num);
251 data->stealable_stack_fill += num;
254 sgen_gray_object_enqueue_section (queue, section);
256 sgen_gray_object_free_queue_section (section);
259 if (data != &workers_gc_thread_data && sgen_gray_object_queue_is_empty (queue))
260 workers_steal (data, data, FALSE);
262 mono_mutex_unlock (&data->stealable_stack_mutex);
264 if (workers_gc_in_progress)
265 workers_wake_up_all ();
268 static mono_native_thread_return_t
269 workers_thread_func (void *data_untyped)
271 WorkerData *data = data_untyped;
273 mono_thread_info_register_small_id ();
275 if (sgen_get_major_collector ()->init_worker_thread)
276 sgen_get_major_collector ()->init_worker_thread (data->major_collector_data);
278 sgen_gray_object_queue_init_with_alloc_prepare (&data->private_gray_queue,
279 workers_gray_queue_share_redirect, data);
282 gboolean did_work = FALSE;
284 while (workers_dequeue_and_do_job (data)) {
286 /* FIXME: maybe distribute the gray queue here? */
289 if (workers_marking && (!sgen_gray_object_queue_is_empty (&data->private_gray_queue) || workers_get_work (data))) {
290 g_assert (!sgen_gray_object_queue_is_empty (&data->private_gray_queue));
292 while (!sgen_drain_gray_stack (&data->private_gray_queue, 32))
293 workers_gray_queue_share_redirect (&data->private_gray_queue);
294 g_assert (sgen_gray_object_queue_is_empty (&data->private_gray_queue));
296 sgen_gray_object_queue_init (&data->private_gray_queue);
305 /* dummy return to make compilers happy */
310 sgen_workers_distribute_gray_queue_sections (void)
312 if (!sgen_collection_is_parallel ())
315 workers_gray_queue_share_redirect (&workers_distribute_gray_queue);
319 sgen_workers_init_distribute_gray_queue (void)
321 if (!sgen_collection_is_parallel ())
324 sgen_gray_object_queue_init (&workers_distribute_gray_queue);
328 sgen_workers_init (int num_workers)
332 if (!sgen_get_major_collector ()->is_parallel)
335 //g_print ("initing %d workers\n", num_workers);
337 workers_num = num_workers;
339 workers_data = sgen_alloc_internal_dynamic (sizeof (WorkerData) * num_workers, INTERNAL_MEM_WORKER_DATA);
340 memset (workers_data, 0, sizeof (WorkerData) * num_workers);
342 MONO_SEM_INIT (&workers_waiting_sem, 0);
343 MONO_SEM_INIT (&workers_done_sem, 0);
345 sgen_gray_object_queue_init_with_alloc_prepare (&workers_distribute_gray_queue,
346 workers_gray_queue_share_redirect, &workers_gc_thread_data);
347 mono_mutex_init (&workers_gc_thread_data.stealable_stack_mutex, NULL);
348 workers_gc_thread_data.stealable_stack_fill = 0;
350 if (sgen_get_major_collector ()->alloc_worker_data)
351 workers_gc_thread_data.major_collector_data = sgen_get_major_collector ()->alloc_worker_data ();
353 for (i = 0; i < workers_num; ++i) {
354 /* private gray queue is inited by the thread itself */
355 mono_mutex_init (&workers_data [i].stealable_stack_mutex, NULL);
356 workers_data [i].stealable_stack_fill = 0;
358 if (sgen_get_major_collector ()->alloc_worker_data)
359 workers_data [i].major_collector_data = sgen_get_major_collector ()->alloc_worker_data ();
362 LOCK_INIT (workers_job_queue_mutex);
364 sgen_register_fixed_internal_mem_type (INTERNAL_MEM_JOB_QUEUE_ENTRY, sizeof (JobQueueEntry));
366 mono_counters_register ("Stolen from self lock", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_workers_stolen_from_self_lock);
367 mono_counters_register ("Stolen from self no lock", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_workers_stolen_from_self_no_lock);
368 mono_counters_register ("Stolen from others", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_workers_stolen_from_others);
369 mono_counters_register ("# workers waited", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_workers_num_waited);
372 /* only the GC thread is allowed to start and join workers */
375 workers_start_worker (int index)
377 g_assert (index >= 0 && index < workers_num);
379 g_assert (!workers_data [index].thread);
380 mono_native_thread_create (&workers_data [index].thread, workers_thread_func, &workers_data [index]);
384 sgen_workers_start_all_workers (void)
388 if (!sgen_collection_is_parallel ())
391 if (sgen_get_major_collector ()->init_worker_thread)
392 sgen_get_major_collector ()->init_worker_thread (workers_gc_thread_data.major_collector_data);
394 g_assert (!workers_gc_in_progress);
395 workers_gc_in_progress = TRUE;
396 workers_marking = FALSE;
397 workers_done_posted = 0;
399 if (workers_started) {
400 g_assert (workers_num_waiting == workers_num);
401 workers_wake_up_all ();
405 for (i = 0; i < workers_num; ++i)
406 workers_start_worker (i);
408 workers_started = TRUE;
412 sgen_workers_start_marking (void)
414 if (!sgen_collection_is_parallel ())
417 g_assert (workers_started && workers_gc_in_progress);
418 g_assert (!workers_marking);
420 workers_marking = TRUE;
422 workers_wake_up_all ();
426 sgen_workers_join (void)
430 if (!sgen_collection_is_parallel ())
433 g_assert (sgen_gray_object_queue_is_empty (&workers_gc_thread_data.private_gray_queue));
434 g_assert (sgen_gray_object_queue_is_empty (&workers_distribute_gray_queue));
436 g_assert (workers_gc_in_progress);
437 workers_gc_in_progress = FALSE;
438 if (workers_num_waiting == workers_num) {
440 * All the workers might have shut down at this point
441 * and posted the done semaphore but we don't know it
442 * yet. It's not a big deal to wake them up again -
443 * they'll just do one iteration of their loop trying to
444 * find something to do and then go back to waiting
447 workers_wake_up_all ();
449 MONO_SEM_WAIT (&workers_done_sem);
450 workers_marking = FALSE;
452 if (sgen_get_major_collector ()->reset_worker_data) {
453 for (i = 0; i < workers_num; ++i)
454 sgen_get_major_collector ()->reset_worker_data (workers_data [i].major_collector_data);
457 g_assert (workers_done_posted);
459 g_assert (!workers_gc_thread_data.stealable_stack_fill);
460 g_assert (sgen_gray_object_queue_is_empty (&workers_gc_thread_data.private_gray_queue));
461 for (i = 0; i < workers_num; ++i) {
462 g_assert (!workers_data [i].stealable_stack_fill);
463 g_assert (sgen_gray_object_queue_is_empty (&workers_data [i].private_gray_queue));
468 sgen_is_worker_thread (MonoNativeThreadId thread)
472 if (sgen_get_major_collector ()->is_worker_thread && sgen_get_major_collector ()->is_worker_thread (thread))
475 for (i = 0; i < workers_num; ++i) {
476 if (workers_data [i].thread == thread)
483 sgen_workers_is_distributed_queue (SgenGrayQueue *queue)
485 return queue == &workers_distribute_gray_queue;
489 sgen_workers_get_distribute_gray_queue (void)
491 return &workers_distribute_gray_queue;
495 sgen_workers_reset_data (void)
497 if (sgen_get_major_collector ()->reset_worker_data)
498 sgen_get_major_collector ()->reset_worker_data (workers_gc_thread_data.major_collector_data);