1 | /*
|
---|
2 | * Copyright (c) 2012 Adam Hraska
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup generic
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * @brief Work queue/thread pool that automatically adjusts its size
|
---|
36 | * depending on the current load. Queued work functions may sleep..
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include <assert.h>
|
---|
40 | #include <errno.h>
|
---|
41 | #include <synch/workqueue.h>
|
---|
42 | #include <synch/spinlock.h>
|
---|
43 | #include <synch/condvar.h>
|
---|
44 | #include <synch/mutex.h>
|
---|
45 | #include <proc/thread.h>
|
---|
46 | #include <config.h>
|
---|
47 | #include <arch.h>
|
---|
48 | #include <cpu.h>
|
---|
49 | #include <macros.h>
|
---|
50 |
|
---|
51 | #define WORKQ_MAGIC 0xf00c1333U
|
---|
52 | #define WORK_ITEM_MAGIC 0xfeec1777U
|
---|
53 |
|
---|
54 |
|
---|
55 | struct work_queue {
|
---|
56 | /*
|
---|
57 | * Protects everything except activate_worker.
|
---|
58 | * Must be acquired after any thread->locks.
|
---|
59 | */
|
---|
60 | IRQ_SPINLOCK_DECLARE(lock);
|
---|
61 |
|
---|
62 | /* Activates a worker if new work arrives or if shutting down the queue. */
|
---|
63 | condvar_t activate_worker;
|
---|
64 |
|
---|
65 | /* Queue of work_items ready to be dispatched. */
|
---|
66 | list_t queue;
|
---|
67 |
|
---|
68 | /* List of worker threads. */
|
---|
69 | list_t workers;
|
---|
70 |
|
---|
71 | /* Number of work items queued. */
|
---|
72 | size_t item_cnt;
|
---|
73 |
|
---|
74 | /* Indicates the work queue is shutting down. */
|
---|
75 | bool stopping;
|
---|
76 | const char *name;
|
---|
77 |
|
---|
78 | /* Total number of created worker threads. */
|
---|
79 | size_t cur_worker_cnt;
|
---|
80 | /* Number of workers waiting for work to arrive. */
|
---|
81 | size_t idle_worker_cnt;
|
---|
82 | /* Number of idle workers signaled that have not yet been woken up. */
|
---|
83 | size_t activate_pending;
|
---|
84 | /* Number of blocked workers sleeping in work func() (ie not idle). */
|
---|
85 | size_t blocked_worker_cnt;
|
---|
86 |
|
---|
87 | /* Number of pending signal_worker_op() operations. */
|
---|
88 | size_t pending_op_cnt;
|
---|
89 |
|
---|
90 | link_t nb_link;
|
---|
91 |
|
---|
92 | #ifdef CONFIG_DEBUG
|
---|
93 | /* Magic cookie for integrity checks. Immutable. Accessed without lock. */
|
---|
94 | uint32_t cookie;
|
---|
95 | #endif
|
---|
96 | };
|
---|
97 |
|
---|
98 |
|
---|
99 | /** Min number of idle workers to keep. */
|
---|
100 | static size_t min_worker_cnt;
|
---|
101 | /** Max total number of workers - be it blocked, idle, or active. */
|
---|
102 | static size_t max_worker_cnt;
|
---|
103 | /** Max number of concurrently running active workers, ie not blocked nor idle. */
|
---|
104 | static size_t max_concurrent_workers;
|
---|
105 | /** Max number of work items per active worker before a new worker is activated.*/
|
---|
106 | static const size_t max_items_per_worker = 8;
|
---|
107 |
|
---|
108 | /** System wide work queue. */
|
---|
109 | static struct work_queue g_work_queue;
|
---|
110 |
|
---|
111 | static int booting = true;
|
---|
112 |
|
---|
113 |
|
---|
114 | typedef struct {
|
---|
115 | IRQ_SPINLOCK_DECLARE(lock);
|
---|
116 | condvar_t req_cv;
|
---|
117 | thread_t *thread;
|
---|
118 | list_t work_queues;
|
---|
119 | } nonblock_adder_t;
|
---|
120 |
|
---|
121 | static nonblock_adder_t nonblock_adder;
|
---|
122 |
|
---|
123 |
|
---|
124 |
|
---|
125 | /** Typedef a worker thread signaling operation prototype. */
|
---|
126 | typedef void (*signal_op_t)(struct work_queue *workq);
|
---|
127 |
|
---|
128 |
|
---|
129 | /* Fwd decl. */
|
---|
130 | static void workq_preinit(struct work_queue *workq, const char *name);
|
---|
131 | static bool add_worker(struct work_queue *workq);
|
---|
132 | static void interrupt_workers(struct work_queue *workq);
|
---|
133 | static void wait_for_workers(struct work_queue *workq);
|
---|
134 | static int _workq_enqueue(struct work_queue *workq, work_t *work_item,
|
---|
135 | work_func_t func, bool can_block);
|
---|
136 | static void init_work_item(work_t *work_item, work_func_t func);
|
---|
137 | static signal_op_t signal_worker_logic(struct work_queue *workq, bool can_block);
|
---|
138 | static void worker_thread(void *arg);
|
---|
139 | static bool dequeue_work(struct work_queue *workq, work_t **pwork_item);
|
---|
140 | static bool worker_unnecessary(struct work_queue *workq);
|
---|
141 | static void cv_wait(struct work_queue *workq);
|
---|
142 | static void nonblock_init(void);
|
---|
143 |
|
---|
144 | #ifdef CONFIG_DEBUG
|
---|
145 | static bool workq_corrupted(struct work_queue *workq);
|
---|
146 | static bool work_item_corrupted(work_t *work_item);
|
---|
147 | #endif
|
---|
148 |
|
---|
149 | /** Creates worker thread for the system-wide worker queue. */
|
---|
150 | void workq_global_worker_init(void)
|
---|
151 | {
|
---|
152 | /*
|
---|
153 | * No need for additional synchronization. Stores to word-sized
|
---|
154 | * variables are atomic and the change will eventually propagate.
|
---|
155 | * Moreover add_worker() includes the necessary memory barriers
|
---|
156 | * in spinlock lock/unlock().
|
---|
157 | */
|
---|
158 | booting = false;
|
---|
159 |
|
---|
160 | nonblock_init();
|
---|
161 |
|
---|
162 | if (!add_worker(&g_work_queue))
|
---|
163 | panic("Could not create a single global work queue worker!\n");
|
---|
164 |
|
---|
165 | }
|
---|
166 |
|
---|
167 | /** Initializes the system wide work queue and support for other work queues. */
|
---|
168 | void workq_global_init(void)
|
---|
169 | {
|
---|
170 | /* Keep idle workers on 1/4-th of cpus, but at least 2 threads. */
|
---|
171 | min_worker_cnt = max(2, config.cpu_count / 4);
|
---|
172 | /* Allow max 8 sleeping work items per cpu. */
|
---|
173 | max_worker_cnt = max(32, 8 * config.cpu_count);
|
---|
174 | /* Maximum concurrency without slowing down the system. */
|
---|
175 | max_concurrent_workers = max(2, config.cpu_count);
|
---|
176 |
|
---|
177 | workq_preinit(&g_work_queue, "kworkq");
|
---|
178 | }
|
---|
179 |
|
---|
180 | /** Stops the system global work queue and waits for all work items to complete.*/
|
---|
181 | void workq_global_stop(void)
|
---|
182 | {
|
---|
183 | workq_stop(&g_work_queue);
|
---|
184 | }
|
---|
185 |
|
---|
186 | /** Creates and initializes a work queue. Returns NULL upon failure. */
|
---|
187 | struct work_queue *workq_create(const char *name)
|
---|
188 | {
|
---|
189 | struct work_queue *workq = malloc(sizeof(struct work_queue));
|
---|
190 | if (!workq)
|
---|
191 | return NULL;
|
---|
192 |
|
---|
193 | if (workq) {
|
---|
194 | if (workq_init(workq, name)) {
|
---|
195 | assert(!workq_corrupted(workq));
|
---|
196 | return workq;
|
---|
197 | }
|
---|
198 |
|
---|
199 | free(workq);
|
---|
200 | }
|
---|
201 |
|
---|
202 | return NULL;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /** Frees work queue resources and stops it if it had not been done so already.*/
|
---|
206 | void workq_destroy(struct work_queue *workq)
|
---|
207 | {
|
---|
208 | assert(!workq_corrupted(workq));
|
---|
209 |
|
---|
210 | irq_spinlock_lock(&workq->lock, true);
|
---|
211 | bool stopped = workq->stopping;
|
---|
212 | #ifdef CONFIG_DEBUG
|
---|
213 | size_t running_workers = workq->cur_worker_cnt;
|
---|
214 | #endif
|
---|
215 | irq_spinlock_unlock(&workq->lock, true);
|
---|
216 |
|
---|
217 | if (!stopped) {
|
---|
218 | workq_stop(workq);
|
---|
219 | } else {
|
---|
220 | assert(0 == running_workers);
|
---|
221 | }
|
---|
222 |
|
---|
223 | #ifdef CONFIG_DEBUG
|
---|
224 | workq->cookie = 0;
|
---|
225 | #endif
|
---|
226 |
|
---|
227 | free(workq);
|
---|
228 | }
|
---|
229 |
|
---|
230 | /** Initializes workq structure without creating any workers. */
|
---|
231 | static void workq_preinit(struct work_queue *workq, const char *name)
|
---|
232 | {
|
---|
233 | #ifdef CONFIG_DEBUG
|
---|
234 | workq->cookie = WORKQ_MAGIC;
|
---|
235 | #endif
|
---|
236 |
|
---|
237 | irq_spinlock_initialize(&workq->lock, name);
|
---|
238 | condvar_initialize(&workq->activate_worker);
|
---|
239 |
|
---|
240 | list_initialize(&workq->queue);
|
---|
241 | list_initialize(&workq->workers);
|
---|
242 |
|
---|
243 | workq->item_cnt = 0;
|
---|
244 | workq->stopping = false;
|
---|
245 | workq->name = name;
|
---|
246 |
|
---|
247 | workq->cur_worker_cnt = 1;
|
---|
248 | workq->idle_worker_cnt = 0;
|
---|
249 | workq->activate_pending = 0;
|
---|
250 | workq->blocked_worker_cnt = 0;
|
---|
251 |
|
---|
252 | workq->pending_op_cnt = 0;
|
---|
253 | link_initialize(&workq->nb_link);
|
---|
254 | }
|
---|
255 |
|
---|
256 | /** Initializes a work queue. Returns true if successful.
|
---|
257 | *
|
---|
258 | * Before destroying a work queue it must be stopped via
|
---|
259 | * workq_stop().
|
---|
260 | */
|
---|
261 | bool workq_init(struct work_queue *workq, const char *name)
|
---|
262 | {
|
---|
263 | workq_preinit(workq, name);
|
---|
264 | return add_worker(workq);
|
---|
265 | }
|
---|
266 |
|
---|
267 | /** Add a new worker thread. Returns false if the thread could not be created. */
|
---|
268 | static bool add_worker(struct work_queue *workq)
|
---|
269 | {
|
---|
270 | assert(!workq_corrupted(workq));
|
---|
271 |
|
---|
272 | thread_t *thread = thread_create(worker_thread, workq, TASK,
|
---|
273 | THREAD_FLAG_NONE, workq->name);
|
---|
274 |
|
---|
275 | if (!thread) {
|
---|
276 | irq_spinlock_lock(&workq->lock, true);
|
---|
277 |
|
---|
278 | /* cur_worker_cnt proactively increased in signal_worker_logic() .*/
|
---|
279 | assert(0 < workq->cur_worker_cnt);
|
---|
280 | --workq->cur_worker_cnt;
|
---|
281 |
|
---|
282 | irq_spinlock_unlock(&workq->lock, true);
|
---|
283 | return false;
|
---|
284 | }
|
---|
285 |
|
---|
286 | /* Respect lock ordering. */
|
---|
287 | irq_spinlock_lock(&thread->lock, true);
|
---|
288 | irq_spinlock_lock(&workq->lock, false);
|
---|
289 |
|
---|
290 | bool success;
|
---|
291 |
|
---|
292 | if (!workq->stopping) {
|
---|
293 | success = true;
|
---|
294 |
|
---|
295 | /* Try to distribute workers among cpus right away. */
|
---|
296 | unsigned int cpu_id = (workq->cur_worker_cnt) % config.cpu_active;
|
---|
297 |
|
---|
298 | if (!cpus[cpu_id].active)
|
---|
299 | cpu_id = CPU->id;
|
---|
300 |
|
---|
301 | thread->workq = workq;
|
---|
302 | thread->cpu = &cpus[cpu_id];
|
---|
303 | thread->workq_blocked = false;
|
---|
304 | thread->workq_idling = false;
|
---|
305 | link_initialize(&thread->workq_link);
|
---|
306 |
|
---|
307 | list_append(&thread->workq_link, &workq->workers);
|
---|
308 | } else {
|
---|
309 | /*
|
---|
310 | * Work queue is shutting down - we must not add the worker
|
---|
311 | * and we cannot destroy it without ready-ing it. Mark it
|
---|
312 | * interrupted so the worker exits right away without even
|
---|
313 | * touching workq.
|
---|
314 | */
|
---|
315 | success = false;
|
---|
316 |
|
---|
317 | /* cur_worker_cnt proactively increased in signal_worker() .*/
|
---|
318 | assert(0 < workq->cur_worker_cnt);
|
---|
319 | --workq->cur_worker_cnt;
|
---|
320 | }
|
---|
321 |
|
---|
322 | irq_spinlock_unlock(&workq->lock, false);
|
---|
323 | irq_spinlock_unlock(&thread->lock, true);
|
---|
324 |
|
---|
325 | if (!success) {
|
---|
326 | thread_interrupt(thread);
|
---|
327 | }
|
---|
328 |
|
---|
329 | thread_ready(thread);
|
---|
330 |
|
---|
331 | return success;
|
---|
332 | }
|
---|
333 |
|
---|
334 | /** Shuts down the work queue. Waits for all pending work items to complete.
|
---|
335 | *
|
---|
336 | * workq_stop() may only be run once.
|
---|
337 | */
|
---|
338 | void workq_stop(struct work_queue *workq)
|
---|
339 | {
|
---|
340 | assert(!workq_corrupted(workq));
|
---|
341 |
|
---|
342 | interrupt_workers(workq);
|
---|
343 | wait_for_workers(workq);
|
---|
344 | }
|
---|
345 |
|
---|
346 | /** Notifies worker threads the work queue is shutting down. */
|
---|
347 | static void interrupt_workers(struct work_queue *workq)
|
---|
348 | {
|
---|
349 | irq_spinlock_lock(&workq->lock, true);
|
---|
350 |
|
---|
351 | /* workq_stop() may only be called once. */
|
---|
352 | assert(!workq->stopping);
|
---|
353 | workq->stopping = true;
|
---|
354 |
|
---|
355 | /* Respect lock ordering - do not hold workq->lock during broadcast. */
|
---|
356 | irq_spinlock_unlock(&workq->lock, true);
|
---|
357 |
|
---|
358 | condvar_broadcast(&workq->activate_worker);
|
---|
359 | }
|
---|
360 |
|
---|
361 | /** Waits for all worker threads to exit. */
|
---|
362 | static void wait_for_workers(struct work_queue *workq)
|
---|
363 | {
|
---|
364 | assert(!PREEMPTION_DISABLED);
|
---|
365 |
|
---|
366 | irq_spinlock_lock(&workq->lock, true);
|
---|
367 |
|
---|
368 | list_foreach_safe(workq->workers, cur_worker, next_worker) {
|
---|
369 | thread_t *worker = list_get_instance(cur_worker, thread_t, workq_link);
|
---|
370 | list_remove(cur_worker);
|
---|
371 |
|
---|
372 | /* Wait without the lock. */
|
---|
373 | irq_spinlock_unlock(&workq->lock, true);
|
---|
374 |
|
---|
375 | thread_join(worker);
|
---|
376 | thread_detach(worker);
|
---|
377 |
|
---|
378 | irq_spinlock_lock(&workq->lock, true);
|
---|
379 | }
|
---|
380 |
|
---|
381 | assert(list_empty(&workq->workers));
|
---|
382 |
|
---|
383 | /* Wait for deferred add_worker_op(), signal_worker_op() to finish. */
|
---|
384 | while (0 < workq->cur_worker_cnt || 0 < workq->pending_op_cnt) {
|
---|
385 | irq_spinlock_unlock(&workq->lock, true);
|
---|
386 |
|
---|
387 | scheduler();
|
---|
388 |
|
---|
389 | irq_spinlock_lock(&workq->lock, true);
|
---|
390 | }
|
---|
391 |
|
---|
392 | irq_spinlock_unlock(&workq->lock, true);
|
---|
393 | }
|
---|
394 |
|
---|
395 | /** Queues a function into the global wait queue without blocking.
|
---|
396 | *
|
---|
397 | * See workq_enqueue_noblock() for more details.
|
---|
398 | */
|
---|
399 | bool workq_global_enqueue_noblock(work_t *work_item, work_func_t func)
|
---|
400 | {
|
---|
401 | return workq_enqueue_noblock(&g_work_queue, work_item, func);
|
---|
402 | }
|
---|
403 |
|
---|
404 | /** Queues a function into the global wait queue; may block.
|
---|
405 | *
|
---|
406 | * See workq_enqueue() for more details.
|
---|
407 | */
|
---|
408 | bool workq_global_enqueue(work_t *work_item, work_func_t func)
|
---|
409 | {
|
---|
410 | return workq_enqueue(&g_work_queue, work_item, func);
|
---|
411 | }
|
---|
412 |
|
---|
413 | /** Adds a function to be invoked in a separate thread without blocking.
|
---|
414 | *
|
---|
415 | * workq_enqueue_noblock() is guaranteed not to block. It is safe
|
---|
416 | * to invoke from interrupt handlers.
|
---|
417 | *
|
---|
418 | * Consider using workq_enqueue() instead if at all possible. Otherwise,
|
---|
419 | * your work item may have to wait for previously enqueued sleeping
|
---|
420 | * work items to complete if you are unlucky.
|
---|
421 | *
|
---|
422 | * @param workq Work queue where to queue the work item.
|
---|
423 | * @param work_item Work item bookkeeping structure. Must be valid
|
---|
424 | * until func() is entered.
|
---|
425 | * @param func User supplied function to invoke in a worker thread.
|
---|
426 | *
|
---|
427 | * @return false if work queue is shutting down; function is not
|
---|
428 | * queued for further processing.
|
---|
429 | * @return true Otherwise. func() will be invoked in a separate thread.
|
---|
430 | */
|
---|
431 | bool workq_enqueue_noblock(struct work_queue *workq, work_t *work_item,
|
---|
432 | work_func_t func)
|
---|
433 | {
|
---|
434 | return _workq_enqueue(workq, work_item, func, false);
|
---|
435 | }
|
---|
436 |
|
---|
437 | /** Adds a function to be invoked in a separate thread; may block.
|
---|
438 | *
|
---|
439 | * While the workq_enqueue() is unlikely to block, it may do so if too
|
---|
440 | * many previous work items blocked sleeping.
|
---|
441 | *
|
---|
442 | * @param workq Work queue where to queue the work item.
|
---|
443 | * @param work_item Work item bookkeeping structure. Must be valid
|
---|
444 | * until func() is entered.
|
---|
445 | * @param func User supplied function to invoke in a worker thread.
|
---|
446 | *
|
---|
447 | * @return false if work queue is shutting down; function is not
|
---|
448 | * queued for further processing.
|
---|
449 | * @return true Otherwise. func() will be invoked in a separate thread.
|
---|
450 | */
|
---|
451 | bool workq_enqueue(struct work_queue *workq, work_t *work_item, work_func_t func)
|
---|
452 | {
|
---|
453 | return _workq_enqueue(workq, work_item, func, true);
|
---|
454 | }
|
---|
455 |
|
---|
456 | /** Adds a work item that will be processed by a separate worker thread.
|
---|
457 | *
|
---|
458 | * func() will be invoked in another kernel thread and may block.
|
---|
459 | *
|
---|
460 | * Prefer to call _workq_enqueue() with can_block set. Otherwise
|
---|
461 | * your work item may have to wait for sleeping work items to complete.
|
---|
462 | * If all worker threads are blocked/sleeping a new worker thread cannot
|
---|
463 | * be create without can_block set because creating a thread might
|
---|
464 | * block due to low memory conditions.
|
---|
465 | *
|
---|
466 | * @param workq Work queue where to queue the work item.
|
---|
467 | * @param work_item Work item bookkeeping structure. Must be valid
|
---|
468 | * until func() is entered.
|
---|
469 | * @param func User supplied function to invoke in a worker thread.
|
---|
470 | * @param can_block May adding this work item block?
|
---|
471 | *
|
---|
472 | * @return false if work queue is shutting down; function is not
|
---|
473 | * queued for further processing.
|
---|
474 | * @return true Otherwise.
|
---|
475 | */
|
---|
476 | static int _workq_enqueue(struct work_queue *workq, work_t *work_item,
|
---|
477 | work_func_t func, bool can_block)
|
---|
478 | {
|
---|
479 | assert(!workq_corrupted(workq));
|
---|
480 |
|
---|
481 | bool success = true;
|
---|
482 | signal_op_t signal_op = NULL;
|
---|
483 |
|
---|
484 | irq_spinlock_lock(&workq->lock, true);
|
---|
485 |
|
---|
486 | if (workq->stopping) {
|
---|
487 | success = false;
|
---|
488 | } else {
|
---|
489 | init_work_item(work_item, func);
|
---|
490 | list_append(&work_item->queue_link, &workq->queue);
|
---|
491 | ++workq->item_cnt;
|
---|
492 | success = true;
|
---|
493 |
|
---|
494 | if (!booting) {
|
---|
495 | signal_op = signal_worker_logic(workq, can_block);
|
---|
496 | } else {
|
---|
497 | /*
|
---|
498 | * During boot there are no workers to signal. Just queue
|
---|
499 | * the work and let future workers take care of it.
|
---|
500 | */
|
---|
501 | }
|
---|
502 | }
|
---|
503 |
|
---|
504 | irq_spinlock_unlock(&workq->lock, true);
|
---|
505 |
|
---|
506 | if (signal_op) {
|
---|
507 | signal_op(workq);
|
---|
508 | }
|
---|
509 |
|
---|
510 | return success;
|
---|
511 | }
|
---|
512 |
|
---|
513 | /** Prepare an item to be added to the work item queue. */
|
---|
514 | static void init_work_item(work_t *work_item, work_func_t func)
|
---|
515 | {
|
---|
516 | #ifdef CONFIG_DEBUG
|
---|
517 | work_item->cookie = WORK_ITEM_MAGIC;
|
---|
518 | #endif
|
---|
519 |
|
---|
520 | link_initialize(&work_item->queue_link);
|
---|
521 | work_item->func = func;
|
---|
522 | }
|
---|
523 |
|
---|
524 | /** Returns the number of workers running work func() that are not blocked. */
|
---|
525 | static size_t active_workers_now(struct work_queue *workq)
|
---|
526 | {
|
---|
527 | assert(irq_spinlock_locked(&workq->lock));
|
---|
528 |
|
---|
529 | /* Workers blocked are sleeping in the work function (ie not idle). */
|
---|
530 | assert(workq->blocked_worker_cnt <= workq->cur_worker_cnt);
|
---|
531 | /* Idle workers are waiting for more work to arrive in condvar_wait. */
|
---|
532 | assert(workq->idle_worker_cnt <= workq->cur_worker_cnt);
|
---|
533 |
|
---|
534 | /* Idle + blocked workers == sleeping worker threads. */
|
---|
535 | size_t sleeping_workers = workq->blocked_worker_cnt + workq->idle_worker_cnt;
|
---|
536 |
|
---|
537 | assert(sleeping_workers <= workq->cur_worker_cnt);
|
---|
538 | /* Workers pending activation are idle workers not yet given a time slice. */
|
---|
539 | assert(workq->activate_pending <= workq->idle_worker_cnt);
|
---|
540 |
|
---|
541 | /*
|
---|
542 | * Workers actively running the work func() this very moment and
|
---|
543 | * are neither blocked nor idle. Exclude ->activate_pending workers
|
---|
544 | * since they will run their work func() once they get a time slice
|
---|
545 | * and are not running it right now.
|
---|
546 | */
|
---|
547 | return workq->cur_worker_cnt - sleeping_workers;
|
---|
548 | }
|
---|
549 |
|
---|
550 | /**
|
---|
551 | * Returns the number of workers that are running or are about to run work
|
---|
552 | * func() and that are not blocked.
|
---|
553 | */
|
---|
554 | static size_t active_workers(struct work_queue *workq)
|
---|
555 | {
|
---|
556 | assert(irq_spinlock_locked(&workq->lock));
|
---|
557 |
|
---|
558 | /*
|
---|
559 | * Workers actively running the work func() and are neither blocked nor
|
---|
560 | * idle. ->activate_pending workers will run their work func() once they
|
---|
561 | * get a time slice after waking from a condvar wait, so count them
|
---|
562 | * as well.
|
---|
563 | */
|
---|
564 | return active_workers_now(workq) + workq->activate_pending;
|
---|
565 | }
|
---|
566 |
|
---|
567 | static void add_worker_noblock_op(struct work_queue *workq)
|
---|
568 | {
|
---|
569 | condvar_signal(&nonblock_adder.req_cv);
|
---|
570 | }
|
---|
571 |
|
---|
572 | static void add_worker_op(struct work_queue *workq)
|
---|
573 | {
|
---|
574 | add_worker(workq);
|
---|
575 | }
|
---|
576 |
|
---|
577 | static void signal_worker_op(struct work_queue *workq)
|
---|
578 | {
|
---|
579 | assert(!workq_corrupted(workq));
|
---|
580 |
|
---|
581 | condvar_signal(&workq->activate_worker);
|
---|
582 |
|
---|
583 | irq_spinlock_lock(&workq->lock, true);
|
---|
584 | assert(0 < workq->pending_op_cnt);
|
---|
585 | --workq->pending_op_cnt;
|
---|
586 | irq_spinlock_unlock(&workq->lock, true);
|
---|
587 | }
|
---|
588 |
|
---|
589 | /** Determines how to signal workers if at all.
|
---|
590 | *
|
---|
591 | * @param workq Work queue where a new work item was queued.
|
---|
592 | * @param can_block True if we may block while signaling a worker or creating
|
---|
593 | * a new worker.
|
---|
594 | *
|
---|
595 | * @return Function that will notify workers or NULL if no action is needed.
|
---|
596 | */
|
---|
597 | static signal_op_t signal_worker_logic(struct work_queue *workq, bool can_block)
|
---|
598 | {
|
---|
599 | assert(!workq_corrupted(workq));
|
---|
600 | assert(irq_spinlock_locked(&workq->lock));
|
---|
601 |
|
---|
602 | /* Only signal workers if really necessary. */
|
---|
603 | signal_op_t signal_op = NULL;
|
---|
604 |
|
---|
605 | /*
|
---|
606 | * Workers actively running the work func() and neither blocked nor idle.
|
---|
607 | * Including ->activate_pending workers that will run their work func()
|
---|
608 | * once they get a time slice.
|
---|
609 | */
|
---|
610 | size_t active = active_workers(workq);
|
---|
611 | /* Max total allowed number of work items queued for active workers. */
|
---|
612 | size_t max_load = active * max_items_per_worker;
|
---|
613 |
|
---|
614 | /* Active workers are getting overwhelmed - activate another. */
|
---|
615 | if (max_load < workq->item_cnt) {
|
---|
616 |
|
---|
617 | size_t remaining_idle =
|
---|
618 | workq->idle_worker_cnt - workq->activate_pending;
|
---|
619 |
|
---|
620 | /* Idle workers still exist - activate one. */
|
---|
621 | if (remaining_idle > 0) {
|
---|
622 | /*
|
---|
623 | * Directly changing idle_worker_cnt here would not allow
|
---|
624 | * workers to recognize spurious wake-ups. Change
|
---|
625 | * activate_pending instead.
|
---|
626 | */
|
---|
627 | ++workq->activate_pending;
|
---|
628 | ++workq->pending_op_cnt;
|
---|
629 | signal_op = signal_worker_op;
|
---|
630 | } else {
|
---|
631 | /* No idle workers remain. Request that a new one be created. */
|
---|
632 | bool need_worker = (active < max_concurrent_workers) &&
|
---|
633 | (workq->cur_worker_cnt < max_worker_cnt);
|
---|
634 |
|
---|
635 | if (need_worker && can_block) {
|
---|
636 | signal_op = add_worker_op;
|
---|
637 | /*
|
---|
638 | * It may take some time to actually create the worker.
|
---|
639 | * We don't want to swamp the thread pool with superfluous
|
---|
640 | * worker creation requests so pretend it was already
|
---|
641 | * created and proactively increase the worker count.
|
---|
642 | */
|
---|
643 | ++workq->cur_worker_cnt;
|
---|
644 | }
|
---|
645 |
|
---|
646 | /*
|
---|
647 | * We cannot create a new worker but we need one desperately
|
---|
648 | * because all workers are blocked in their work functions.
|
---|
649 | */
|
---|
650 | if (need_worker && !can_block && 0 == active) {
|
---|
651 | assert(0 == workq->idle_worker_cnt);
|
---|
652 |
|
---|
653 | irq_spinlock_lock(&nonblock_adder.lock, true);
|
---|
654 |
|
---|
655 | if (nonblock_adder.thread && !link_used(&workq->nb_link)) {
|
---|
656 | signal_op = add_worker_noblock_op;
|
---|
657 | ++workq->cur_worker_cnt;
|
---|
658 | list_append(&workq->nb_link, &nonblock_adder.work_queues);
|
---|
659 | }
|
---|
660 |
|
---|
661 | irq_spinlock_unlock(&nonblock_adder.lock, true);
|
---|
662 | }
|
---|
663 | }
|
---|
664 | } else {
|
---|
665 | /*
|
---|
666 | * There are enough active/running workers to process the queue.
|
---|
667 | * No need to signal/activate any new workers.
|
---|
668 | */
|
---|
669 | signal_op = NULL;
|
---|
670 | }
|
---|
671 |
|
---|
672 | return signal_op;
|
---|
673 | }
|
---|
674 |
|
---|
675 | /** Executes queued work items. */
|
---|
676 | static void worker_thread(void *arg)
|
---|
677 | {
|
---|
678 | /*
|
---|
679 | * The thread has been created after the work queue was ordered to stop.
|
---|
680 | * Do not access the work queue and return immediately.
|
---|
681 | */
|
---|
682 | if (thread_interrupted(THREAD)) {
|
---|
683 | thread_detach(THREAD);
|
---|
684 | return;
|
---|
685 | }
|
---|
686 |
|
---|
687 | assert(arg != NULL);
|
---|
688 |
|
---|
689 | struct work_queue *workq = arg;
|
---|
690 | work_t *work_item;
|
---|
691 |
|
---|
692 | while (dequeue_work(workq, &work_item)) {
|
---|
693 | /* Copy the func field so func() can safely free work_item. */
|
---|
694 | work_func_t func = work_item->func;
|
---|
695 |
|
---|
696 | func(work_item);
|
---|
697 | }
|
---|
698 | }
|
---|
699 |
|
---|
700 | /** Waits and retrieves a work item. Returns false if the worker should exit. */
|
---|
701 | static bool dequeue_work(struct work_queue *workq, work_t **pwork_item)
|
---|
702 | {
|
---|
703 | assert(!workq_corrupted(workq));
|
---|
704 |
|
---|
705 | irq_spinlock_lock(&workq->lock, true);
|
---|
706 |
|
---|
707 | /* Check if we should exit if load is low. */
|
---|
708 | if (!workq->stopping && worker_unnecessary(workq)) {
|
---|
709 | /* There are too many workers for this load. Exit. */
|
---|
710 | assert(0 < workq->cur_worker_cnt);
|
---|
711 | --workq->cur_worker_cnt;
|
---|
712 | list_remove(&THREAD->workq_link);
|
---|
713 | irq_spinlock_unlock(&workq->lock, true);
|
---|
714 |
|
---|
715 | thread_detach(THREAD);
|
---|
716 | return false;
|
---|
717 | }
|
---|
718 |
|
---|
719 | bool stop = false;
|
---|
720 |
|
---|
721 | /* Wait for work to arrive. */
|
---|
722 | while (list_empty(&workq->queue) && !workq->stopping) {
|
---|
723 | cv_wait(workq);
|
---|
724 |
|
---|
725 | if (0 < workq->activate_pending)
|
---|
726 | --workq->activate_pending;
|
---|
727 | }
|
---|
728 |
|
---|
729 | /* Process remaining work even if requested to stop. */
|
---|
730 | if (!list_empty(&workq->queue)) {
|
---|
731 | link_t *work_link = list_first(&workq->queue);
|
---|
732 | *pwork_item = list_get_instance(work_link, work_t, queue_link);
|
---|
733 |
|
---|
734 | #ifdef CONFIG_DEBUG
|
---|
735 | assert(!work_item_corrupted(*pwork_item));
|
---|
736 | (*pwork_item)->cookie = 0;
|
---|
737 | #endif
|
---|
738 | list_remove(work_link);
|
---|
739 | --workq->item_cnt;
|
---|
740 |
|
---|
741 | stop = false;
|
---|
742 | } else {
|
---|
743 | /* Requested to stop and no more work queued. */
|
---|
744 | assert(workq->stopping);
|
---|
745 | --workq->cur_worker_cnt;
|
---|
746 | stop = true;
|
---|
747 | }
|
---|
748 |
|
---|
749 | irq_spinlock_unlock(&workq->lock, true);
|
---|
750 |
|
---|
751 | return !stop;
|
---|
752 | }
|
---|
753 |
|
---|
754 | /** Returns true if for the given load there are too many workers. */
|
---|
755 | static bool worker_unnecessary(struct work_queue *workq)
|
---|
756 | {
|
---|
757 | assert(irq_spinlock_locked(&workq->lock));
|
---|
758 |
|
---|
759 | /* No work is pending. We don't need too many idle threads. */
|
---|
760 | if (list_empty(&workq->queue)) {
|
---|
761 | /* There are too many idle workers. Exit. */
|
---|
762 | return (min_worker_cnt <= workq->idle_worker_cnt);
|
---|
763 | } else {
|
---|
764 | /*
|
---|
765 | * There is work but we are swamped with too many active workers
|
---|
766 | * that were woken up from sleep at around the same time. We
|
---|
767 | * don't need another worker fighting for cpu time.
|
---|
768 | */
|
---|
769 | size_t active = active_workers_now(workq);
|
---|
770 | return (max_concurrent_workers < active);
|
---|
771 | }
|
---|
772 | }
|
---|
773 |
|
---|
774 | /** Waits for a signal to activate_worker. Thread marked idle while waiting. */
|
---|
775 | static void cv_wait(struct work_queue *workq)
|
---|
776 | {
|
---|
777 | ++workq->idle_worker_cnt;
|
---|
778 | THREAD->workq_idling = true;
|
---|
779 |
|
---|
780 | /* Ignore lock ordering just here. */
|
---|
781 | assert(irq_spinlock_locked(&workq->lock));
|
---|
782 |
|
---|
783 | _condvar_wait_timeout_irq_spinlock(&workq->activate_worker,
|
---|
784 | &workq->lock, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
|
---|
785 |
|
---|
786 | assert(!workq_corrupted(workq));
|
---|
787 | assert(irq_spinlock_locked(&workq->lock));
|
---|
788 |
|
---|
789 | THREAD->workq_idling = false;
|
---|
790 | --workq->idle_worker_cnt;
|
---|
791 | }
|
---|
792 |
|
---|
793 |
|
---|
794 | /** Invoked from thread_ready() right before the thread is woken up. */
|
---|
795 | void workq_before_thread_is_ready(thread_t *thread)
|
---|
796 | {
|
---|
797 | assert(thread);
|
---|
798 | assert(irq_spinlock_locked(&thread->lock));
|
---|
799 |
|
---|
800 | /* Worker's work func() is about to wake up from sleeping. */
|
---|
801 | if (thread->workq && thread->workq_blocked) {
|
---|
802 | /* Must be blocked in user work func() and not be waiting for work. */
|
---|
803 | assert(!thread->workq_idling);
|
---|
804 | assert(thread->state == Sleeping);
|
---|
805 | assert(THREAD != thread);
|
---|
806 | assert(!workq_corrupted(thread->workq));
|
---|
807 |
|
---|
808 | /* Protected by thread->lock */
|
---|
809 | thread->workq_blocked = false;
|
---|
810 |
|
---|
811 | irq_spinlock_lock(&thread->workq->lock, true);
|
---|
812 | --thread->workq->blocked_worker_cnt;
|
---|
813 | irq_spinlock_unlock(&thread->workq->lock, true);
|
---|
814 | }
|
---|
815 | }
|
---|
816 |
|
---|
817 | /** Invoked from scheduler() before switching away from a thread. */
|
---|
818 | void workq_after_thread_ran(void)
|
---|
819 | {
|
---|
820 | assert(THREAD);
|
---|
821 | assert(irq_spinlock_locked(&THREAD->lock));
|
---|
822 |
|
---|
823 | /* Worker's work func() is about to sleep/block. */
|
---|
824 | if (THREAD->workq && THREAD->state == Sleeping && !THREAD->workq_idling) {
|
---|
825 | assert(!THREAD->workq_blocked);
|
---|
826 | assert(!workq_corrupted(THREAD->workq));
|
---|
827 |
|
---|
828 | THREAD->workq_blocked = true;
|
---|
829 |
|
---|
830 | irq_spinlock_lock(&THREAD->workq->lock, false);
|
---|
831 |
|
---|
832 | ++THREAD->workq->blocked_worker_cnt;
|
---|
833 |
|
---|
834 | bool can_block = false;
|
---|
835 | signal_op_t op = signal_worker_logic(THREAD->workq, can_block);
|
---|
836 |
|
---|
837 | irq_spinlock_unlock(&THREAD->workq->lock, false);
|
---|
838 |
|
---|
839 | if (op) {
|
---|
840 | assert(add_worker_noblock_op == op || signal_worker_op == op);
|
---|
841 | op(THREAD->workq);
|
---|
842 | }
|
---|
843 | }
|
---|
844 | }
|
---|
845 |
|
---|
846 | /** Prints stats of the work queue to the kernel console. */
|
---|
847 | void workq_print_info(struct work_queue *workq)
|
---|
848 | {
|
---|
849 | irq_spinlock_lock(&workq->lock, true);
|
---|
850 |
|
---|
851 | size_t total = workq->cur_worker_cnt;
|
---|
852 | size_t blocked = workq->blocked_worker_cnt;
|
---|
853 | size_t idle = workq->idle_worker_cnt;
|
---|
854 | size_t active = active_workers(workq);
|
---|
855 | size_t items = workq->item_cnt;
|
---|
856 | bool stopping = workq->stopping;
|
---|
857 | bool worker_surplus = worker_unnecessary(workq);
|
---|
858 | const char *load_str = worker_surplus ? "decreasing" :
|
---|
859 | (0 < workq->activate_pending) ? "increasing" : "stable";
|
---|
860 |
|
---|
861 | irq_spinlock_unlock(&workq->lock, true);
|
---|
862 |
|
---|
863 | printf(
|
---|
864 | "Configuration: max_worker_cnt=%zu, min_worker_cnt=%zu,\n"
|
---|
865 | " max_concurrent_workers=%zu, max_items_per_worker=%zu\n"
|
---|
866 | "Workers: %zu\n"
|
---|
867 | "Active: %zu (workers currently processing work)\n"
|
---|
868 | "Blocked: %zu (work functions sleeping/blocked)\n"
|
---|
869 | "Idle: %zu (idle workers waiting for more work)\n"
|
---|
870 | "Items: %zu (queued not yet dispatched work)\n"
|
---|
871 | "Stopping: %d\n"
|
---|
872 | "Load: %s\n",
|
---|
873 | max_worker_cnt, min_worker_cnt,
|
---|
874 | max_concurrent_workers, max_items_per_worker,
|
---|
875 | total,
|
---|
876 | active,
|
---|
877 | blocked,
|
---|
878 | idle,
|
---|
879 | items,
|
---|
880 | stopping,
|
---|
881 | load_str);
|
---|
882 | }
|
---|
883 |
|
---|
884 | /** Prints stats of the global work queue. */
|
---|
885 | void workq_global_print_info(void)
|
---|
886 | {
|
---|
887 | workq_print_info(&g_work_queue);
|
---|
888 | }
|
---|
889 |
|
---|
890 |
|
---|
891 | static bool dequeue_add_req(nonblock_adder_t *info, struct work_queue **pworkq)
|
---|
892 | {
|
---|
893 | bool stop = false;
|
---|
894 |
|
---|
895 | irq_spinlock_lock(&info->lock, true);
|
---|
896 |
|
---|
897 | while (list_empty(&info->work_queues) && !stop) {
|
---|
898 | errno_t ret = _condvar_wait_timeout_irq_spinlock(&info->req_cv,
|
---|
899 | &info->lock, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_INTERRUPTIBLE);
|
---|
900 |
|
---|
901 | stop = (ret == EINTR);
|
---|
902 | }
|
---|
903 |
|
---|
904 | if (!stop) {
|
---|
905 | *pworkq = list_get_instance(list_first(&info->work_queues),
|
---|
906 | struct work_queue, nb_link);
|
---|
907 |
|
---|
908 | assert(!workq_corrupted(*pworkq));
|
---|
909 |
|
---|
910 | list_remove(&(*pworkq)->nb_link);
|
---|
911 | }
|
---|
912 |
|
---|
913 | irq_spinlock_unlock(&info->lock, true);
|
---|
914 |
|
---|
915 | return !stop;
|
---|
916 | }
|
---|
917 |
|
---|
918 | static void thr_nonblock_add_worker(void *arg)
|
---|
919 | {
|
---|
920 | nonblock_adder_t *info = arg;
|
---|
921 | struct work_queue *workq = NULL;
|
---|
922 |
|
---|
923 | while (dequeue_add_req(info, &workq)) {
|
---|
924 | add_worker(workq);
|
---|
925 | }
|
---|
926 | }
|
---|
927 |
|
---|
928 |
|
---|
929 | static void nonblock_init(void)
|
---|
930 | {
|
---|
931 | irq_spinlock_initialize(&nonblock_adder.lock, "kworkq-nb.lock");
|
---|
932 | condvar_initialize(&nonblock_adder.req_cv);
|
---|
933 | list_initialize(&nonblock_adder.work_queues);
|
---|
934 |
|
---|
935 | nonblock_adder.thread = thread_create(thr_nonblock_add_worker,
|
---|
936 | &nonblock_adder, TASK, THREAD_FLAG_NONE, "kworkq-nb");
|
---|
937 |
|
---|
938 | if (nonblock_adder.thread) {
|
---|
939 | thread_ready(nonblock_adder.thread);
|
---|
940 | } else {
|
---|
941 | /*
|
---|
942 | * We won't be able to add workers without blocking if all workers
|
---|
943 | * sleep, but at least boot the system.
|
---|
944 | */
|
---|
945 | printf("Failed to create kworkq-nb. Sleeping work may stall the workq.\n");
|
---|
946 | }
|
---|
947 | }
|
---|
948 |
|
---|
949 | #ifdef CONFIG_DEBUG
|
---|
950 | /** Returns true if the workq is definitely corrupted; false if not sure.
|
---|
951 | *
|
---|
952 | * Can be used outside of any locks.
|
---|
953 | */
|
---|
954 | static bool workq_corrupted(struct work_queue *workq)
|
---|
955 | {
|
---|
956 | /*
|
---|
957 | * Needed to make the most current cookie value set by workq_preinit()
|
---|
958 | * visible even if we access the workq right after it is created but
|
---|
959 | * on a different cpu. Otherwise, workq_corrupted() would not work
|
---|
960 | * outside a lock.
|
---|
961 | */
|
---|
962 | memory_barrier();
|
---|
963 | return NULL == workq || workq->cookie != WORKQ_MAGIC;
|
---|
964 | }
|
---|
965 |
|
---|
966 | /** Returns true if the work_item is definitely corrupted; false if not sure.
|
---|
967 | *
|
---|
968 | * Must be used with the work queue protecting spinlock locked.
|
---|
969 | */
|
---|
970 | static bool work_item_corrupted(work_t *work_item)
|
---|
971 | {
|
---|
972 | return NULL == work_item || work_item->cookie != WORK_ITEM_MAGIC;
|
---|
973 | }
|
---|
974 | #endif
|
---|
975 |
|
---|
976 | /** @}
|
---|
977 | */
|
---|