[sgen] Remove unused function
[mono.git] / mono / sgen / sgen-workers.c
1 /*
2  * sgen-workers.c: Worker threads for parallel and concurrent GC.
3  *
4  * Copyright 2001-2003 Ximian, Inc
5  * Copyright 2003-2010 Novell, Inc.
6  * Copyright (C) 2012 Xamarin Inc
7  *
8  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
9  */
10
11 #include "config.h"
12 #ifdef HAVE_SGEN_GC
13
14 #include <string.h>
15
16 #include "mono/sgen/sgen-gc.h"
17 #include "mono/sgen/sgen-workers.h"
18 #include "mono/sgen/sgen-thread-pool.h"
19 #include "mono/utils/mono-membar.h"
20 #include "mono/sgen/sgen-client.h"
21
22 static int workers_num;
23 static volatile gboolean forced_stop;
24 static WorkerData *workers_data;
25 static SgenWorkerCallback worker_init_cb;
26
27 /*
28  * When using multiple workers, we need to have the last worker
29  * enqueue the preclean jobs (if there are any). This lock ensures
30  * that when the last worker takes it, all the other workers have
31  * gracefully finished, so it can restart them.
32  */
33 static mono_mutex_t finished_lock;
34
35 static SgenSectionGrayQueue workers_distribute_gray_queue;
36 static gboolean workers_distribute_gray_queue_inited;
37
38 /*
39  * Allowed transitions:
40  *
41  * | from \ to          | NOT WORKING | WORKING | WORK ENQUEUED |
42  * |--------------------+-------------+---------+---------------+
43  * | NOT WORKING        | -           | -       | main / worker |
44  * | WORKING            | worker      | -       | main / worker |
45  * | WORK ENQUEUED      | -           | worker  | -             |
46  *
47  * The WORK ENQUEUED state guarantees that the worker thread will inspect the queue again at
48  * least once.  Only after looking at the queue will it go back to WORKING, and then,
49  * eventually, to NOT WORKING.  After enqueuing work the main thread transitions the state
50  * to WORK ENQUEUED.  Signalling the worker thread to wake up is only necessary if the old
51  * state was NOT WORKING.
52  */
53
54 enum {
55         STATE_NOT_WORKING,
56         STATE_WORKING,
57         STATE_WORK_ENQUEUED
58 };
59
60 typedef gint32 State;
61
62 static SgenObjectOperations * volatile idle_func_object_ops;
63 /*
64  * finished_callback is called only when the workers finish work normally (when they
65  * are not forced to finish). The callback is used to enqueue preclean jobs.
66  */
67 static volatile SgenWorkersFinishCallback finish_callback;
68
69 static guint64 stat_workers_num_finished;
70
71 static gboolean
72 set_state (WorkerData *data, State old_state, State new_state)
73 {
74         SGEN_ASSERT (0, old_state != new_state, "Why are we transitioning to the same state?");
75         if (new_state == STATE_NOT_WORKING)
76                 SGEN_ASSERT (0, old_state == STATE_WORKING, "We can only transition to NOT WORKING from WORKING");
77         else if (new_state == STATE_WORKING)
78                 SGEN_ASSERT (0, old_state == STATE_WORK_ENQUEUED, "We can only transition to WORKING from WORK ENQUEUED");
79         if (new_state == STATE_NOT_WORKING || new_state == STATE_WORKING)
80                 SGEN_ASSERT (6, sgen_thread_pool_is_thread_pool_thread (mono_native_thread_id_get ()), "Only the worker thread is allowed to transition to NOT_WORKING or WORKING");
81
82         return InterlockedCompareExchange (&data->state, new_state, old_state) == old_state;
83 }
84
85 static gboolean
86 state_is_working_or_enqueued (State state)
87 {
88         return state == STATE_WORKING || state == STATE_WORK_ENQUEUED;
89 }
90
91 static void
92 sgen_workers_ensure_awake (void)
93 {
94         int i;
95         gboolean need_signal = FALSE;
96
97         for (i = 0; i < workers_num; i++) {
98                 State old_state;
99                 gboolean did_set_state;
100
101                 do {
102                         old_state = workers_data [i].state;
103
104                         if (old_state == STATE_WORK_ENQUEUED)
105                                 break;
106
107                         did_set_state = set_state (&workers_data [i], old_state, STATE_WORK_ENQUEUED);
108                 } while (!did_set_state);
109
110                 if (!state_is_working_or_enqueued (old_state))
111                         need_signal = TRUE;
112         }
113
114         if (need_signal)
115                 sgen_thread_pool_idle_signal ();
116 }
117
118 static void
119 worker_try_finish (WorkerData *data)
120 {
121         State old_state;
122         int i, working = 0;
123
124         ++stat_workers_num_finished;
125
126         mono_os_mutex_lock (&finished_lock);
127
128         for (i = 0; i < workers_num; i++) {
129                 if (state_is_working_or_enqueued (workers_data [i].state))
130                         working++;
131         }
132
133         if (working == 1) {
134                 SgenWorkersFinishCallback callback = finish_callback;
135                 /* We are the last one left. Enqueue preclean job if we have one and awake everybody */
136                 SGEN_ASSERT (0, data->state != STATE_NOT_WORKING, "How did we get from doing idle work to NOT WORKING without setting it ourselves?");
137                 if (callback) {
138                         finish_callback = NULL;
139                         callback ();
140                         /* Make sure each worker has a chance of seeing the enqueued jobs */
141                         sgen_workers_ensure_awake ();
142                         SGEN_ASSERT (0, data->state == STATE_WORK_ENQUEUED, "Why did we fail to set our own state to ENQUEUED");
143                         goto work_available;
144                 }
145         }
146
147         do {
148                 old_state = data->state;
149
150                 SGEN_ASSERT (0, old_state != STATE_NOT_WORKING, "How did we get from doing idle work to NOT WORKING without setting it ourselves?");
151                 if (old_state == STATE_WORK_ENQUEUED)
152                         goto work_available;
153                 SGEN_ASSERT (0, old_state == STATE_WORKING, "What other possibility is there?");
154         } while (!set_state (data, old_state, STATE_NOT_WORKING));
155
156         mono_os_mutex_unlock (&finished_lock);
157
158         binary_protocol_worker_finish (sgen_timestamp (), forced_stop);
159
160         sgen_gray_object_queue_trim_free_list (&data->private_gray_queue);
161         return;
162
163 work_available:
164         mono_os_mutex_unlock (&finished_lock);
165 }
166
167 void
168 sgen_workers_enqueue_job (SgenThreadPoolJob *job, gboolean enqueue)
169 {
170         if (!enqueue) {
171                 job->func (NULL, job);
172                 sgen_thread_pool_job_free (job);
173                 return;
174         }
175
176         sgen_thread_pool_job_enqueue (job);
177 }
178
179 static gboolean
180 workers_get_work (WorkerData *data)
181 {
182         SgenMajorCollector *major;
183
184         g_assert (sgen_gray_object_queue_is_empty (&data->private_gray_queue));
185
186         /* If we're concurrent, steal from the workers distribute gray queue. */
187         major = sgen_get_major_collector ();
188         if (major->is_concurrent) {
189                 GrayQueueSection *section = sgen_section_gray_queue_dequeue (&workers_distribute_gray_queue);
190                 if (section) {
191                         sgen_gray_object_enqueue_section (&data->private_gray_queue, section);
192                         return TRUE;
193                 }
194         }
195
196         /* Nobody to steal from */
197         g_assert (sgen_gray_object_queue_is_empty (&data->private_gray_queue));
198         return FALSE;
199 }
200
201 static void
202 concurrent_enqueue_check (GCObject *obj)
203 {
204         g_assert (sgen_concurrent_collection_in_progress ());
205         g_assert (!sgen_ptr_in_nursery (obj));
206         g_assert (SGEN_LOAD_VTABLE (obj));
207 }
208
209 static void
210 init_private_gray_queue (WorkerData *data)
211 {
212         sgen_gray_object_queue_init (&data->private_gray_queue,
213                         sgen_get_major_collector ()->is_concurrent ? concurrent_enqueue_check : NULL,
214                         FALSE);
215 }
216
217 static void
218 thread_pool_init_func (void *data_untyped)
219 {
220         WorkerData *data = (WorkerData *)data_untyped;
221         SgenMajorCollector *major = sgen_get_major_collector ();
222
223         sgen_client_thread_register_worker ();
224
225         if (!major->is_concurrent)
226                 return;
227
228         init_private_gray_queue (data);
229
230         if (worker_init_cb)
231                 worker_init_cb (data);
232 }
233
234 static gboolean
235 continue_idle_func (void *data_untyped)
236 {
237         if (data_untyped) {
238                 WorkerData *data = (WorkerData *)data_untyped;
239                 return state_is_working_or_enqueued (data->state);
240         } else {
241                 /* Return if any of the threads is working */
242                 return !sgen_workers_all_done ();
243         }
244 }
245
246 static void
247 marker_idle_func (void *data_untyped)
248 {
249         WorkerData *data = (WorkerData *)data_untyped;
250
251         SGEN_ASSERT (0, continue_idle_func (data_untyped), "Why are we called when we're not supposed to work?");
252         SGEN_ASSERT (0, sgen_concurrent_collection_in_progress (), "The worker should only mark in concurrent collections.");
253
254         if (data->state == STATE_WORK_ENQUEUED) {
255                 set_state (data, STATE_WORK_ENQUEUED, STATE_WORKING);
256                 SGEN_ASSERT (0, data->state != STATE_NOT_WORKING, "How did we get from WORK ENQUEUED to NOT WORKING?");
257         }
258
259         if (!forced_stop && (!sgen_gray_object_queue_is_empty (&data->private_gray_queue) || workers_get_work (data))) {
260                 ScanCopyContext ctx = CONTEXT_FROM_OBJECT_OPERATIONS (idle_func_object_ops, &data->private_gray_queue);
261
262                 SGEN_ASSERT (0, !sgen_gray_object_queue_is_empty (&data->private_gray_queue), "How is our gray queue empty if we just got work?");
263
264                 sgen_drain_gray_stack (ctx);
265         } else {
266                 worker_try_finish (data);
267         }
268 }
269
270 static void
271 init_distribute_gray_queue (void)
272 {
273         if (workers_distribute_gray_queue_inited) {
274                 g_assert (sgen_section_gray_queue_is_empty (&workers_distribute_gray_queue));
275                 g_assert (workers_distribute_gray_queue.locked);
276                 return;
277         }
278
279         sgen_section_gray_queue_init (&workers_distribute_gray_queue, TRUE,
280                         sgen_get_major_collector ()->is_concurrent ? concurrent_enqueue_check : NULL);
281         workers_distribute_gray_queue_inited = TRUE;
282 }
283
284 void
285 sgen_workers_init_distribute_gray_queue (void)
286 {
287         SGEN_ASSERT (0, sgen_get_major_collector ()->is_concurrent,
288                         "Why should we init the distribute gray queue if we don't need it?");
289         init_distribute_gray_queue ();
290 }
291
292 void
293 sgen_workers_init (int num_workers, SgenWorkerCallback callback)
294 {
295         int i;
296         void **workers_data_ptrs = (void **)alloca(num_workers * sizeof(void *));
297
298         if (!sgen_get_major_collector ()->is_concurrent) {
299                 sgen_thread_pool_init (num_workers, thread_pool_init_func, NULL, NULL, NULL);
300                 return;
301         }
302
303         mono_os_mutex_init (&finished_lock);
304         //g_print ("initing %d workers\n", num_workers);
305
306         workers_num = num_workers;
307
308         workers_data = (WorkerData *)sgen_alloc_internal_dynamic (sizeof (WorkerData) * num_workers, INTERNAL_MEM_WORKER_DATA, TRUE);
309         memset (workers_data, 0, sizeof (WorkerData) * num_workers);
310
311         init_distribute_gray_queue ();
312
313         for (i = 0; i < num_workers; ++i)
314                 workers_data_ptrs [i] = (void *) &workers_data [i];
315
316         worker_init_cb = callback;
317
318         sgen_thread_pool_init (num_workers, thread_pool_init_func, marker_idle_func, continue_idle_func, workers_data_ptrs);
319
320         mono_counters_register ("# workers finished", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_workers_num_finished);
321 }
322
323 void
324 sgen_workers_stop_all_workers (void)
325 {
326         finish_callback = NULL;
327         mono_memory_write_barrier ();
328         forced_stop = TRUE;
329
330         sgen_thread_pool_wait_for_all_jobs ();
331         sgen_thread_pool_idle_wait ();
332         SGEN_ASSERT (0, sgen_workers_all_done (), "Can only signal enqueue work when in no work state");
333 }
334
335 void
336 sgen_workers_start_all_workers (SgenObjectOperations *object_ops, SgenWorkersFinishCallback callback)
337 {
338         forced_stop = FALSE;
339         idle_func_object_ops = object_ops;
340         finish_callback = callback;
341         mono_memory_write_barrier ();
342
343         sgen_workers_ensure_awake ();
344 }
345
346 void
347 sgen_workers_join (void)
348 {
349         int i;
350
351         sgen_thread_pool_wait_for_all_jobs ();
352         sgen_thread_pool_idle_wait ();
353         SGEN_ASSERT (0, sgen_workers_all_done (), "Can only signal enqueue work when in no work state");
354
355         /* At this point all the workers have stopped. */
356
357         SGEN_ASSERT (0, sgen_section_gray_queue_is_empty (&workers_distribute_gray_queue), "Why is there still work left to do?");
358         for (i = 0; i < workers_num; ++i)
359                 SGEN_ASSERT (0, sgen_gray_object_queue_is_empty (&workers_data [i].private_gray_queue), "Why is there still work left to do?");
360 }
361
362 /*
363  * Can only be called if the workers are stopped.
364  * If we're stopped, there are also no pending jobs.
365  */
366 gboolean
367 sgen_workers_have_idle_work (void)
368 {
369         int i;
370
371         SGEN_ASSERT (0, forced_stop && sgen_workers_all_done (), "Checking for idle work should only happen if the workers are stopped.");
372
373         if (!sgen_section_gray_queue_is_empty (&workers_distribute_gray_queue))
374                 return TRUE;
375
376         for (i = 0; i < workers_num; ++i) {
377                 if (!sgen_gray_object_queue_is_empty (&workers_data [i].private_gray_queue))
378                         return TRUE;
379         }
380
381         return FALSE;
382 }
383
384 gboolean
385 sgen_workers_all_done (void)
386 {
387         int i;
388
389         for (i = 0; i < workers_num; i++) {
390                 if (state_is_working_or_enqueued (workers_data [i].state))
391                         return FALSE;
392         }
393         return TRUE;
394 }
395
396 /* Must only be used for debugging */
397 gboolean
398 sgen_workers_are_working (void)
399 {
400         return !sgen_workers_all_done ();
401 }
402
403 void
404 sgen_workers_assert_gray_queue_is_empty (void)
405 {
406         SGEN_ASSERT (0, sgen_section_gray_queue_is_empty (&workers_distribute_gray_queue), "Why is the workers gray queue not empty?");
407 }
408
409 void
410 sgen_workers_take_from_queue (SgenGrayQueue *queue)
411 {
412         for (;;) {
413                 GrayQueueSection *section = sgen_gray_object_dequeue_section (queue);
414                 if (!section)
415                         break;
416                 sgen_section_gray_queue_enqueue (&workers_distribute_gray_queue, section);
417         }
418
419         SGEN_ASSERT (0, !sgen_workers_are_working (), "We should fully populate the distribute gray queue before we start the workers");
420 }
421
422 SgenObjectOperations*
423 sgen_workers_get_idle_func_object_ops (void)
424 {
425         return idle_func_object_ops;
426 }
427
428 /*
429  * If we have a single worker, splitting into multiple jobs makes no sense. With
430  * more than one worker, we split into a larger number of jobs so that, in case
431  * the work load is uneven, a worker that finished quickly can take up more jobs
432  * than another one.
433  */
434 int
435 sgen_workers_get_job_split_count (void)
436 {
437         return (workers_num > 1) ? workers_num * 4 : 1;
438 }
439
440 void
441 sgen_workers_foreach (SgenWorkerCallback callback)
442 {
443         int i;
444
445         for (i = 0; i < workers_num; i++)
446                 callback (&workers_data [i]);
447 }
448
449 #endif