Debian Codegraph @

testsuite/test-scheduler.c

raw symbols SCIP
1 /* test-scheduler.c
2 *
3 * Copyright 2022 Christian Hergert <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #include <libdex.h>
22
23 #include "dex-thread-pool-scheduler-private.h"
24
25 typedef struct _DirectedSteal DirectedSteal;
26
27 typedef struct _DirectedStealItem
28 {
29 DirectedSteal *state;
30 gboolean completed;
31 } DirectedStealItem;
32
33 struct _DirectedSteal
34 {
35 GMutex mutex;
36 GCond cond;
37 DexScheduler *scheduler;
38 GThread *producer_thread;
39 DirectedStealItem *items;
40 guint n_items;
41 guint n_completed;
42 guint n_completed_by_peer;
43 gboolean producer_finished;
44 gboolean peer_timed_out;
45 };
46
47 typedef struct _MixedLocalSteal MixedLocalSteal;
48
49 typedef struct _MixedLocalStealItem
50 {
51 MixedLocalSteal *state;
52 gboolean completed;
53 } MixedLocalStealItem;
54
55 typedef struct _MixedLocalStealWorker
56 {
57 MixedLocalSteal *state;
58 guint index;
59 } MixedLocalStealWorker;
60
61 struct _MixedLocalSteal
62 {
63 GMutex mutex;
64 GCond cond;
65 DexScheduler *scheduler;
66 GThread *producer_thread;
67 GThread *thief_thread;
68 GPtrArray *fibers;
69 MixedLocalStealItem *items;
70 MixedLocalStealWorker *workers;
71 guint n_workers;
72 guint n_ready;
73 guint n_finished;
74 guint n_local_completed;
75 guint n_batch_items;
76 guint n_batch_completed;
77 guint n_batch_completed_by_peer;
78 guint n_sentinel_completed_by_thief;
79 gboolean producer_go;
80 gboolean producer_published;
81 gboolean thief_go;
82 gboolean release_all;
83 };
84
85 static DexScheduler *thread_pool;
86 static GMainLoop *main_loop;
87
88 static void
89 test_main_scheduler_simple_cb (gpointer data)
90 {
91 gboolean *count = data;
92 *count = 123;
93 g_main_loop_quit (main_loop);
94 }
95
96 static void
97 test_main_scheduler_simple (void)
98 {
99 DexScheduler *scheduler = dex_scheduler_get_default ();
100 gboolean count = 0;
101
102 g_assert_nonnull (scheduler);
103 g_assert_true (DEX_IS_MAIN_SCHEDULER (scheduler));
104
105 main_loop = g_main_loop_new (NULL, FALSE);
106 dex_scheduler_push (scheduler,
107 test_main_scheduler_simple_cb,
108 &count);
109 g_main_loop_run (main_loop);
110 g_clear_pointer (&main_loop, g_main_loop_unref);
111
112 g_assert_cmpint (count, ==, 123);
113 }
114
115 static DexFuture *
116 test_fiber2_func (gpointer user_data)
117 {
118 guint *count = user_data;
119 g_atomic_int_inc (count);
120 return dex_future_new_for_boolean (TRUE);
121 }
122
123 static DexFuture *
124 test_fiber_func (gpointer user_data)
125 {
126 GPtrArray *all = g_ptr_array_new_with_free_func (dex_unref);
127
128 for (guint i = 0; i < 10; i++)
129 g_ptr_array_add (all, dex_scheduler_spawn (dex_scheduler_get_thread_default (),
130 dex_get_min_stack_size (),
131 test_fiber2_func, user_data, NULL));
132
133 dex_await (dex_future_allv ((DexFuture **)all->pdata, all->len), NULL);
134
135 g_ptr_array_unref (all);
136
137 return NULL;
138 }
139
140 static DexFuture *
141 spawner (gpointer user_data)
142 {
143 GPtrArray *all = g_ptr_array_new_with_free_func (dex_unref);
144
145 for (guint i = 0; i < 1000; i++)
146 g_ptr_array_add (all, dex_scheduler_spawn (thread_pool,
147 dex_get_min_stack_size (),
148 test_fiber_func, user_data, NULL));
149
150 dex_await (dex_future_allv ((DexFuture **)(gpointer)all->pdata, all->len), NULL);
151
152 g_ptr_array_unref (all);
153
154 return NULL;
155 }
156
157 static DexFuture *
158 named_fiber_func (gpointer user_data)
159 {
160 return dex_future_new_for_boolean (TRUE);
161 }
162
163 static void
164 test_scheduler_spawn_static_name (void)
165 {
166 DexFuture * future = NULL;
167
168 future = dex_scheduler_spawn (NULL, 0, named_fiber_func, NULL, NULL);
169
170 g_assert_cmpstr (dex_future_get_name (future), ==, "named_fiber_func");
171
172 while (dex_future_get_status (future) == DEX_FUTURE_STATUS_PENDING)
173 g_main_context_iteration (NULL, TRUE);
174
175 dex_clear (&future);
176 }
177
178 static DexFuture *
179 quit_cb (DexFuture *completed,
180 gpointer user_data)
181 {
182 g_test_message ("Quiting main loop");
183 g_main_loop_quit (main_loop);
184 return NULL;
185 }
186
187 static void
188 test_thread_pool_scheduler_spawn (void)
189 {
190 DexFuture *future;
191 guint count = 0;
192
193 thread_pool = dex_thread_pool_scheduler_new ();
194 main_loop = g_main_loop_new (NULL, FALSE);
195
196 g_test_message ("Spawning with stack size %u",
197 (guint)dex_get_min_stack_size ());
198
199 future = dex_scheduler_spawn (NULL, 0, spawner, &count, NULL);
200 future = dex_future_finally (future, quit_cb, NULL, NULL);
201
202 g_test_message ("Running main loop");
203 g_main_loop_run (main_loop);
204
205 g_assert_cmpint (count, ==, 10*1000);
206
207 dex_unref (future);
208 dex_unref (thread_pool);
209 }
210
211 static void
212 test_thread_pool_scheduler_push_cb (gpointer data)
213 {
214 struct {
215 GMutex mutex;
216 GCond cond;
217 } *syncobj = data;
218
219 g_mutex_lock (&syncobj->mutex);
220 g_cond_signal (&syncobj->cond);
221 g_mutex_unlock (&syncobj->mutex);
222 }
223
224 static void
225 test_thread_pool_scheduler_push (void)
226 {
227 struct {
228 GMutex mutex;
229 GCond cond;
230 } syncobj;
231
232 g_mutex_init (&syncobj.mutex);
233 g_cond_init (&syncobj.cond);
234
235 g_mutex_lock (&syncobj.mutex);
236 dex_scheduler_push (dex_thread_pool_scheduler_get_default (),
237 test_thread_pool_scheduler_push_cb,
238 &syncobj);
239 g_cond_wait (&syncobj.cond, &syncobj.mutex);
240 g_mutex_unlock (&syncobj.mutex);
241
242 g_mutex_clear (&syncobj.mutex);
243 g_cond_clear (&syncobj.cond);
244 }
245
246 static void
247 test_thread_pool_scheduler_directed_steal_item (gpointer data)
248 {
249 DirectedStealItem *item = data;
250 DirectedSteal *state = item->state;
251
252 g_mutex_lock (&state->mutex);
253
254 g_assert_false (item->completed);
255 item->completed = TRUE;
256 state->n_completed++;
257
258 if (g_thread_self () != state->producer_thread)
259 state->n_completed_by_peer++;
260
261 g_cond_broadcast (&state->cond);
262 g_mutex_unlock (&state->mutex);
263 }
264
265 static void
266 test_thread_pool_scheduler_directed_steal_producer (gpointer data)
267 {
268 DirectedSteal *state = data;
269 gint64 deadline;
270
271 g_mutex_lock (&state->mutex);
272 state->producer_thread = g_thread_self ();
273 g_mutex_unlock (&state->mutex);
274
275 for (guint i = 0; i < state->n_items; i++)
276 {
277 state->items[i].state = state;
278 dex_scheduler_push (state->scheduler,
279 test_thread_pool_scheduler_directed_steal_item,
280 &state->items[i]);
281 }
282
283 deadline = g_get_monotonic_time () + (5 * G_TIME_SPAN_SECOND);
284
285 g_mutex_lock (&state->mutex);
286
287 while (state->n_completed_by_peer == 0)
288 {
289 if (!g_cond_wait_until (&state->cond, &state->mutex, deadline))
290 {
291 state->peer_timed_out = TRUE;
292 break;
293 }
294 }
295
296 state->producer_finished = TRUE;
297 g_cond_broadcast (&state->cond);
298 g_mutex_unlock (&state->mutex);
299 }
300
301 static void
302 test_thread_pool_scheduler_directed_steal (void)
303 {
304 DirectedSteal state;
305 gint64 deadline;
306
307 state = (DirectedSteal) {
308 .scheduler = dex_thread_pool_scheduler_new (),
309 .n_items = 128,
310 };
311
312 if (_dex_thread_pool_scheduler_get_n_workers (state.scheduler) < 2)
313 {
314 g_test_skip ("At least two thread-pool workers are required");
315 dex_clear (&state.scheduler);
316 return;
317 }
318
319 g_mutex_init (&state.mutex);
320 g_cond_init (&state.cond);
321 state.items = g_new0 (DirectedStealItem, state.n_items);
322
323 dex_scheduler_push (state.scheduler,
324 test_thread_pool_scheduler_directed_steal_producer,
325 &state);
326
327 deadline = g_get_monotonic_time () + (10 * G_TIME_SPAN_SECOND);
328
329 g_mutex_lock (&state.mutex);
330
331 while (!state.producer_finished || state.n_completed < state.n_items)
332 {
333 if (!g_cond_wait_until (&state.cond, &state.mutex, deadline))
334 g_error ("Timed out waiting for directed thread-pool work");
335 }
336
337 g_mutex_unlock (&state.mutex);
338
339 g_assert_false (state.peer_timed_out);
340 g_assert_cmpuint (state.n_completed_by_peer, >, 0);
341 g_assert_cmpuint (state.n_completed, ==, state.n_items);
342
343 dex_clear (&state.scheduler);
344 g_clear_pointer (&state.items, g_free);
345 g_mutex_clear (&state.mutex);
346 g_cond_clear (&state.cond);
347 }
348
349 static void
350 test_thread_pool_scheduler_mixed_local_item (gpointer data)
351 {
352 MixedLocalStealWorker *worker = data;
353 MixedLocalSteal *state = worker->state;
354
355 g_mutex_lock (&state->mutex);
356 state->n_local_completed++;
357
358 if (worker->index == 0 && g_thread_self () == state->thief_thread)
359 state->n_sentinel_completed_by_thief++;
360
361 g_cond_broadcast (&state->cond);
362 g_mutex_unlock (&state->mutex);
363 }
364
365 static void
366 test_thread_pool_scheduler_mixed_batch_item (gpointer data)
367 {
368 MixedLocalStealItem *item = data;
369 MixedLocalSteal *state = item->state;
370
371 g_mutex_lock (&state->mutex);
372
373 g_assert_false (item->completed);
374 item->completed = TRUE;
375 state->n_batch_completed++;
376
377 if (g_thread_self () != state->producer_thread)
378 state->n_batch_completed_by_peer++;
379
380 g_cond_broadcast (&state->cond);
381 g_mutex_unlock (&state->mutex);
382 }
383
384 static DexFuture *
385 test_thread_pool_scheduler_mixed_worker (gpointer data)
386 {
387 MixedLocalStealWorker *worker = data;
388 MixedLocalSteal *state = worker->state;
389
390 g_mutex_lock (&state->mutex);
391
392 if (worker->index == 1)
393 state->producer_thread = g_thread_self ();
394 else if (worker->index == state->n_workers - 1)
395 state->thief_thread = g_thread_self ();
396
397 g_mutex_unlock (&state->mutex);
398
399 dex_scheduler_push (state->scheduler,
400 test_thread_pool_scheduler_mixed_local_item,
401 worker);
402
403 g_mutex_lock (&state->mutex);
404 state->n_ready++;
405 g_cond_broadcast (&state->cond);
406
407 if (worker->index == 1)
408 {
409 while (!state->producer_go)
410 g_cond_wait (&state->cond, &state->mutex);
411
412 g_mutex_unlock (&state->mutex);
413
414 for (guint i = 0; i < state->n_batch_items; i++)
415 {
416 state->items[i].state = state;
417 dex_scheduler_push (state->scheduler,
418 test_thread_pool_scheduler_mixed_batch_item,
419 &state->items[i]);
420 }
421
422 g_mutex_lock (&state->mutex);
423 state->producer_published = TRUE;
424 g_cond_broadcast (&state->cond);
425 }
426 else if (worker->index == state->n_workers - 1)
427 {
428 while (!state->thief_go)
429 g_cond_wait (&state->cond, &state->mutex);
430 }
431
432 if (worker->index != state->n_workers - 1)
433 {
434 while (!state->release_all)
435 g_cond_wait (&state->cond, &state->mutex);
436 }
437
438 state->n_finished++;
439 g_cond_broadcast (&state->cond);
440 g_mutex_unlock (&state->mutex);
441
442 return dex_future_new_for_boolean (TRUE);
443 }
444
445 static void
446 test_thread_pool_scheduler_mixed_local_steal (void)
447 {
448 MixedLocalSteal state;
449 gint64 deadline;
450 gboolean peer_timed_out = FALSE;
451
452 state = (MixedLocalSteal) {
453 .scheduler = dex_thread_pool_scheduler_new (),
454 };
455 state.n_workers = _dex_thread_pool_scheduler_get_n_workers (state.scheduler);
456
457 if (state.n_workers < 3)
458 {
459 g_test_skip ("At least three thread-pool workers are required");
460 dex_clear (&state.scheduler);
461 return;
462 }
463
464 state.n_batch_items = MAX (128, state.n_workers * 2);
465 state.fibers = g_ptr_array_new_with_free_func (dex_unref);
466 state.items = g_new0 (MixedLocalStealItem, state.n_batch_items);
467 state.workers = g_new0 (MixedLocalStealWorker, state.n_workers);
468 g_mutex_init (&state.mutex);
469 g_cond_init (&state.cond);
470
471 for (guint i = 0; i < state.n_workers; i++)
472 {
473 state.workers[i].state = &state;
474 state.workers[i].index = i;
475 g_ptr_array_add (state.fibers,
476 dex_scheduler_spawn (state.scheduler,
477 dex_get_min_stack_size (),
478 test_thread_pool_scheduler_mixed_worker,
479 &state.workers[i],
480 NULL));
481 }
482
483 deadline = g_get_monotonic_time () + (10 * G_TIME_SPAN_SECOND);
484
485 g_mutex_lock (&state.mutex);
486
487 while (state.n_ready < state.n_workers)
488 {
489 if (!g_cond_wait_until (&state.cond, &state.mutex, deadline))
490 g_error ("Timed out preparing mixed local thread-pool work");
491 }
492
493 state.producer_go = TRUE;
494 g_cond_broadcast (&state.cond);
495
496 while (!state.producer_published)
497 {
498 if (!g_cond_wait_until (&state.cond, &state.mutex, deadline))
499 g_error ("Timed out publishing mixed local thread-pool work");
500 }
501
502 state.thief_go = TRUE;
503 g_cond_broadcast (&state.cond);
504
505 while (state.n_batch_completed_by_peer == 0)
506 {
507 if (!g_cond_wait_until (&state.cond, &state.mutex, deadline))
508 {
509 peer_timed_out = TRUE;
510 break;
511 }
512 }
513
514 state.release_all = TRUE;
515 g_cond_broadcast (&state.cond);
516
517 deadline = g_get_monotonic_time () + (10 * G_TIME_SPAN_SECOND);
518
519 while (state.n_finished < state.n_workers ||
520 state.n_local_completed < state.n_workers ||
521 state.n_batch_completed < state.n_batch_items)
522 {
523 if (!g_cond_wait_until (&state.cond, &state.mutex, deadline))
524 g_error ("Timed out draining mixed local thread-pool work");
525 }
526
527 g_test_message ("Mixed local steal: workers=%u local=%u batch=%u peer=%u sentinel=%u",
528 state.n_workers,
529 state.n_local_completed,
530 state.n_batch_completed,
531 state.n_batch_completed_by_peer,
532 state.n_sentinel_completed_by_thief);
533
534 g_assert_false (peer_timed_out);
535 g_assert_cmpuint (state.n_sentinel_completed_by_thief, ==, 1);
536 g_assert_cmpuint (state.n_batch_completed_by_peer, >, 0);
537 g_assert_cmpuint (state.n_batch_completed, ==, state.n_batch_items);
538
539 g_mutex_unlock (&state.mutex);
540
541 g_clear_pointer (&state.fibers, g_ptr_array_unref);
542 dex_clear (&state.scheduler);
543 g_clear_pointer (&state.items, g_free);
544 g_clear_pointer (&state.workers, g_free);
545 g_mutex_clear (&state.mutex);
546 g_cond_clear (&state.cond);
547 }
548
549 int
550 main (int argc,
551 char *argv[])
552 {
553 dex_init ();
554 g_test_init (&argc, &argv, NULL);
555 g_test_add_func ("/Dex/TestSuite/MainScheduler/simple", test_main_scheduler_simple);
556 g_test_add_func ("/Dex/TestSuite/Scheduler/spawn_static_name", test_scheduler_spawn_static_name);
557 g_test_add_func ("/Dex/TestSuite/ThreadPoolScheduler/10_000_fibers", test_thread_pool_scheduler_spawn);
558 g_test_add_func ("/Dex/TestSuite/ThreadPoolScheduler/push", test_thread_pool_scheduler_push);
559 g_test_add_func ("/Dex/TestSuite/ThreadPoolScheduler/directed_steal",
560 test_thread_pool_scheduler_directed_steal);
561 g_test_add_func ("/Dex/TestSuite/ThreadPoolScheduler/mixed_local_steal",
562 test_thread_pool_scheduler_mixed_local_steal);
563 return g_test_run ();
564 }