Debian Codegraph @

src/dex-thread-pool-scheduler.c

raw symbols SCIP
1 /*
2 * dex-thread-pool-scheduler.c
3 *
4 * Copyright 2022 Christian Hergert <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #include "config.h"
23
24 #include <stdatomic.h>
25
26 #include "dex-scheduler-private.h"
27 #include "dex-thread-pool-scheduler-private.h"
28 #include "dex-thread-pool-worker-private.h"
29 #include "dex-thread-storage-private.h"
30 #include "dex-work-queue-private.h"
31
32 #define MAX_WORKERS 32
33
34 /**
35 * DexThreadPoolScheduler:
36 *
37 * `DexThreadPoolScheduler` is a [[email protected]] that will dispatch work
38 * items and fibers to sub-schedulers on a specific operating system thread.
39 *
40 * [[email protected]] will never migrate from the thread they are created on to
41 * reduce chances of safety issues involved in tracking state between CPU.
42 *
43 * New work items are placed into a global work queue and then dispatched
44 * efficiently to a single thread pool worker using a specialized async
45 * semaphore. On modern Linux using io_uring, this wakes up a single worker
46 * thread and therefore is not subject to "thundering herd" common with
47 * global work queues.
48 *
49 * When a worker creates a new work item, it is placed into a work stealing
50 * queue owned by the thread. Other worker threads may steal work items when
51 * they have exhausted their own work queue.
52 */
53
54 struct _DexThreadPoolScheduler
55 {
56 DexScheduler parent_instance;
57 DexWorkQueue *global_work_queue;
58 DexThreadPoolWorkerSet *set;
59 guint fiber_rrobin;
60 guint n_workers;
61 DexThreadPoolWorker *workers[MAX_WORKERS];
62 };
63
64 typedef struct _DexThreadPoolSchedulerClass
65 {
66 DexSchedulerClass parent_class;
67 } DexThreadPoolSchedulerClass;
68
69 DEX_DEFINE_FINAL_TYPE (DexThreadPoolScheduler, dex_thread_pool_scheduler, DEX_TYPE_SCHEDULER)
70
71 #undef DEX_TYPE_THREAD_POOL_SCHEDULER
72 #define DEX_TYPE_THREAD_POOL_SCHEDULER dex_thread_pool_scheduler_type
73
74 static DexScheduler *default_thread_pool;
75
76 static void
77 dex_thread_pool_scheduler_push (DexScheduler *scheduler,
78 DexWorkItem work_item)
79 {
80 DexThreadPoolScheduler *thread_pool_scheduler = DEX_THREAD_POOL_SCHEDULER (scheduler);
81 DexThreadPoolWorker *worker = DEX_THREAD_POOL_WORKER_CURRENT;
82
83 if (worker != NULL)
84 DEX_SCHEDULER_GET_CLASS (worker)->push (DEX_SCHEDULER (worker), work_item);
85 else
86 dex_work_queue_push (thread_pool_scheduler->global_work_queue, work_item);
87 }
88
89 static GMainContext *
90 dex_thread_pool_scheduler_get_main_context (DexScheduler *scheduler)
91 {
92 DexThreadPoolWorker *worker = DEX_THREAD_POOL_WORKER_CURRENT;
93
94 /* Give the worker's main context if we're on a pooled thread */
95 if (worker != NULL)
96 return dex_scheduler_get_main_context (DEX_SCHEDULER (worker));
97
98 /* Otherwise give the application default (main thread) context */
99 return dex_scheduler_get_main_context (dex_scheduler_get_default ());
100 }
101
102 static DexAioContext *
103 dex_thread_pool_scheduler_get_aio_context (DexScheduler *scheduler)
104 {
105 DexThreadPoolWorker *worker = DEX_THREAD_POOL_WORKER_CURRENT;
106
107 /* Give the worker's aio context if we're on a pooled thread */
108 if (worker != NULL)
109 return dex_scheduler_get_aio_context (DEX_SCHEDULER (worker));
110
111 /* Otherwise give the application default (main thread) aio context */
112 return dex_scheduler_get_aio_context (dex_scheduler_get_default ());
113 }
114
115 static inline DexThreadPoolWorker *
116 rrobin_next (DexScheduler *scheduler)
117 {
118 DexThreadPoolScheduler *thread_pool_scheduler = (DexThreadPoolScheduler *)scheduler;
119 guint worker_index = g_atomic_int_add (&thread_pool_scheduler->fiber_rrobin, 1) % thread_pool_scheduler->n_workers;
120
121 /* TODO: This is just doing a dumb round robin for assigning a fiber to a
122 * specific thread pool worker. We probably want something more interesting
123 * than that so we can have weighted workers or even keep affinity to a small
124 * number of them until latency reaches some threshold.
125 */
126
127 return thread_pool_scheduler->workers[worker_index];
128 }
129
130 static void
131 dex_thread_pool_scheduler_spawn (DexScheduler *scheduler,
132 DexFiber *fiber)
133 {
134 DexThreadPoolWorker *worker = rrobin_next (scheduler);
135
136 DEX_SCHEDULER_GET_CLASS (worker)->spawn (DEX_SCHEDULER (worker), fiber);
137 }
138
139 static void
140 dex_thread_pool_scheduler_spawn_coroutine (DexScheduler *scheduler,
141 DexCoroutine *coroutine)
142 {
143 DexThreadPoolWorker *worker = rrobin_next (scheduler);
144
145 DEX_SCHEDULER_GET_CLASS (worker)->spawn_coroutine (DEX_SCHEDULER (worker), coroutine);
146 }
147
148 static void
149 dex_thread_pool_scheduler_finalize (DexObject *object)
150 {
151 DexThreadPoolScheduler *thread_pool_scheduler = (DexThreadPoolScheduler *)object;
152
153 if ((DexScheduler *)thread_pool_scheduler == default_thread_pool)
154 {
155 g_critical ("Attempt to finalize default thread pool. "
156 "This should not happen and is an error in the application.");
157 return;
158 }
159
160 for (guint i = 0; i < thread_pool_scheduler->n_workers; i++)
161 dex_clear (&thread_pool_scheduler->workers[i]);
162
163 dex_clear (&thread_pool_scheduler->global_work_queue);
164 g_clear_pointer (&thread_pool_scheduler->set, dex_thread_pool_worker_set_unref);
165
166 DEX_OBJECT_CLASS (dex_thread_pool_scheduler_parent_class)->finalize (object);
167 }
168
169 static void
170 dex_thread_pool_scheduler_class_init (DexThreadPoolSchedulerClass *thread_pool_scheduler_class)
171 {
172 DexObjectClass *object_class = DEX_OBJECT_CLASS (thread_pool_scheduler_class);
173 DexSchedulerClass *scheduler_class = DEX_SCHEDULER_CLASS (thread_pool_scheduler_class);
174
175 object_class->finalize = dex_thread_pool_scheduler_finalize;
176
177 scheduler_class->get_main_context = dex_thread_pool_scheduler_get_main_context;
178 scheduler_class->get_aio_context = dex_thread_pool_scheduler_get_aio_context;
179 scheduler_class->push = dex_thread_pool_scheduler_push;
180 scheduler_class->spawn = dex_thread_pool_scheduler_spawn;
181 scheduler_class->spawn_coroutine = dex_thread_pool_scheduler_spawn_coroutine;
182 }
183
184 static void
185 dex_thread_pool_scheduler_init (DexThreadPoolScheduler *thread_pool_scheduler)
186 {
187 thread_pool_scheduler->global_work_queue = dex_work_queue_new ();
188 thread_pool_scheduler->set = dex_thread_pool_worker_set_new ();
189 }
190
191 guint
192 _dex_thread_pool_scheduler_get_n_workers (DexScheduler *scheduler)
193 {
194 DexThreadPoolScheduler *thread_pool_scheduler;
195
196 g_return_val_if_fail (DEX_IS_THREAD_POOL_SCHEDULER (scheduler), 0);
197
198 thread_pool_scheduler = DEX_THREAD_POOL_SCHEDULER (scheduler);
199
200 return thread_pool_scheduler->n_workers;
201 }
202
203 /**
204 * dex_thread_pool_scheduler_new:
205 *
206 * Creates a new [[email protected]] that executes work items on a thread pool.
207 *
208 * Returns: (transfer full): a [[email protected]]
209 */
210 DexScheduler *
211 dex_thread_pool_scheduler_new (void)
212 {
213 DexThreadPoolScheduler *thread_pool_scheduler;
214 guint n_procs;
215 guint n_workers;
216
217 thread_pool_scheduler = (DexThreadPoolScheduler *)dex_object_create_instance (DEX_TYPE_THREAD_POOL_SCHEDULER);
218
219 /* TODO: let this be dynamic and tunable, as well as thread pinning */
220
221 n_procs = MIN (MAX_WORKERS, g_get_num_processors ());
222
223 /* Couple things here, which we should take a look at in the future to
224 * see how we can tune them correctly, but:
225 *
226 * Remove one as the main thread has an AIO context too and we don't want
227 * to create contention there. Also, io_uring may limit us in the number
228 * of io_uring we can create.
229 *
230 * g_get_num_processors() includes hyperthreads, so take the result and
231 * cut it in half. It would be nicer to actually verify this on the system
232 * for cases where we don't have that.
233 *
234 * Additionally, bail if the worker fails to be created.
235 */
236 n_workers = MAX (1, (n_procs/2));
237
238 for (guint i = 0; i < n_workers; i++)
239 {
240 DexThreadPoolWorker *thread_pool_worker;
241
242 thread_pool_worker = dex_thread_pool_worker_new (thread_pool_scheduler->global_work_queue,
243 thread_pool_scheduler->set,
244 i == 0);
245
246 if (thread_pool_worker == NULL)
247 break;
248
249 thread_pool_scheduler->workers[thread_pool_scheduler->n_workers++] = thread_pool_worker;
250 }
251
252 g_assert (thread_pool_scheduler->n_workers > 0);
253
254 atomic_thread_fence (memory_order_seq_cst);
255
256 return DEX_SCHEDULER (thread_pool_scheduler);
257 }
258
259 /**
260 * dex_thread_pool_scheduler_get_default:
261 *
262 * Gets the default thread pool scheduler for the instance.
263 *
264 * This function is useful to allow programs and libraries to share
265 * an off-main-thread scheduler without having to coordinate on where
266 * the scheduler instance is created or owned.
267 *
268 * Returns: (transfer none): a [[email protected]]
269 */
270 DexScheduler *
271 dex_thread_pool_scheduler_get_default (void)
272 {
273 if (g_once_init_enter (&default_thread_pool))
274 {
275 DexScheduler *instance = dex_thread_pool_scheduler_new ();
276 g_once_init_leave (&default_thread_pool, instance);
277 }
278
279 return default_thread_pool;
280 }