]> git.proxmox.com Git - mirror_frr.git/blame - lib/workqueue.c
*: auto-convert to SPDX License IDs
[mirror_frr.git] / lib / workqueue.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
d62a17ae 2/*
354d119a 3 * Quagga Work Queue Support.
4 *
5 * Copyright (C) 2005 Sun Microsystems, Inc.
354d119a 6 */
7
7a2fbbf0 8#include <zebra.h>
354d119a 9#include "thread.h"
10#include "memory.h"
11#include "workqueue.h"
12#include "linklist.h"
13#include "command.h"
14#include "log.h"
15
bf8d3d6a
DL
16DEFINE_MTYPE(LIB, WORK_QUEUE, "Work queue");
17DEFINE_MTYPE_STATIC(LIB, WORK_QUEUE_ITEM, "Work queue item");
18DEFINE_MTYPE_STATIC(LIB, WORK_QUEUE_NAME, "Work queue name string");
4a1ab8e4 19
354d119a 20/* master list of work_queues */
24873f0c 21static struct list _work_queues;
1f9a9fff 22/* pointer primarily to avoid an otherwise harmless warning on
24873f0c
DS
23 * ALL_LIST_ELEMENTS_RO
24 */
25static struct list *work_queues = &_work_queues;
354d119a 26
27#define WORK_QUEUE_MIN_GRANULARITY 1
28
d62a17ae 29static struct work_queue_item *work_queue_item_new(struct work_queue *wq)
354d119a 30{
d62a17ae 31 struct work_queue_item *item;
32 assert(wq);
354d119a 33
d62a17ae 34 item = XCALLOC(MTYPE_WORK_QUEUE_ITEM, sizeof(struct work_queue_item));
35
36 return item;
354d119a 37}
38
d62a17ae 39static void work_queue_item_free(struct work_queue_item *item)
354d119a 40{
d62a17ae 41 XFREE(MTYPE_WORK_QUEUE_ITEM, item);
42 return;
354d119a 43}
44
da7f979a
DS
45static void work_queue_item_remove(struct work_queue *wq,
46 struct work_queue_item *item)
47{
48 assert(item && item->data);
49
50 /* call private data deletion callback if needed */
51 if (wq->spec.del_item_data)
52 wq->spec.del_item_data(wq, item->data);
53
54 work_queue_item_dequeue(wq, item);
55
56 work_queue_item_free(item);
57
58 return;
59}
60
354d119a 61/* create new work queue */
d62a17ae 62struct work_queue *work_queue_new(struct thread_master *m,
63 const char *queue_name)
354d119a 64{
d62a17ae 65 struct work_queue *new;
66
67 new = XCALLOC(MTYPE_WORK_QUEUE, sizeof(struct work_queue));
68
d62a17ae 69 new->name = XSTRDUP(MTYPE_WORK_QUEUE_NAME, queue_name);
70 new->master = m;
71 SET_FLAG(new->flags, WQ_UNPLUGGED);
72
f104f6c1 73 STAILQ_INIT(&new->items);
d62a17ae 74
75 listnode_add(work_queues, new);
76
77 new->cycles.granularity = WORK_QUEUE_MIN_GRANULARITY;
78
5418f988 79 /* Default values, can be overridden by caller */
d62a17ae 80 new->spec.hold = WORK_QUEUE_DEFAULT_HOLD;
81 new->spec.yield = THREAD_YIELD_TIME_SLOT;
5418f988 82 new->spec.retry = WORK_QUEUE_DEFAULT_RETRY;
d62a17ae 83
84 return new;
354d119a 85}
86
6b097e33 87void work_queue_free_and_null(struct work_queue **wqp)
354d119a 88{
6b097e33
MS
89 struct work_queue *wq = *wqp;
90
d1c27668 91 THREAD_OFF(wq->thread);
d62a17ae 92
da7f979a
DS
93 while (!work_queue_empty(wq)) {
94 struct work_queue_item *item = work_queue_last_item(wq);
95
96 work_queue_item_remove(wq, item);
97 }
98
d62a17ae 99 listnode_delete(work_queues, wq);
100
101 XFREE(MTYPE_WORK_QUEUE_NAME, wq->name);
102 XFREE(MTYPE_WORK_QUEUE, wq);
354d119a 103
6b097e33 104 *wqp = NULL;
e208c8f9
DS
105}
106
d62a17ae 107bool work_queue_is_scheduled(struct work_queue *wq)
86582682 108{
d1c27668 109 return thread_is_scheduled(wq->thread);
86582682
PJ
110}
111
d62a17ae 112static int work_queue_schedule(struct work_queue *wq, unsigned int delay)
269d74fd 113{
d62a17ae 114 /* if appropriate, schedule work queue thread */
d1c27668
DS
115 if (CHECK_FLAG(wq->flags, WQ_UNPLUGGED) &&
116 !thread_is_scheduled(wq->thread) && !work_queue_empty(wq)) {
5418f988
MS
117 /* Schedule timer if there's a delay, otherwise just schedule
118 * as an 'event'
119 */
e8b3a2f7 120 if (delay > 0) {
5418f988
MS
121 thread_add_timer_msec(wq->master, work_queue_run, wq,
122 delay, &wq->thread);
e8b3a2f7
DS
123 thread_ignore_late_timer(wq->thread);
124 } else
5418f988
MS
125 thread_add_event(wq->master, work_queue_run, wq, 0,
126 &wq->thread);
127
d62a17ae 128 /* set thread yield time, if needed */
d1c27668
DS
129 if (thread_is_scheduled(wq->thread) &&
130 wq->spec.yield != THREAD_YIELD_TIME_SLOT)
d62a17ae 131 thread_set_yield_time(wq->thread, wq->spec.yield);
132 return 1;
133 } else
134 return 0;
269d74fd 135}
d62a17ae 136
137void work_queue_add(struct work_queue *wq, void *data)
354d119a 138{
d62a17ae 139 struct work_queue_item *item;
140
141 assert(wq);
142
0ce1ca80 143 item = work_queue_item_new(wq);
d62a17ae 144
145 item->data = data;
f104f6c1 146 work_queue_item_enqueue(wq, item);
d62a17ae 147
148 work_queue_schedule(wq, wq->spec.hold);
149
150 return;
354d119a 151}
152
996c9314
LB
153static void work_queue_item_requeue(struct work_queue *wq,
154 struct work_queue_item *item)
354d119a 155{
f104f6c1
JB
156 work_queue_item_dequeue(wq, item);
157
158 /* attach to end of list */
159 work_queue_item_enqueue(wq, item);
354d119a 160}
161
49d41a26
DS
162DEFUN (show_work_queues,
163 show_work_queues_cmd,
164 "show work-queues",
165 SHOW_STR
166 "Work Queue information\n")
354d119a 167{
d62a17ae 168 struct listnode *node;
169 struct work_queue *wq;
170
171 vty_out(vty, "%c %8s %5s %8s %8s %21s\n", ' ', "List", "(ms) ",
172 "Q. Runs", "Yields", "Cycle Counts ");
173 vty_out(vty, "%c %8s %5s %8s %8s %7s %6s %8s %6s %s\n", 'P', "Items",
174 "Hold", "Total", "Total", "Best", "Gran.", "Total", "Avg.",
175 "Name");
176
177 for (ALL_LIST_ELEMENTS_RO(work_queues, node, wq)) {
178 vty_out(vty, "%c %8d %5d %8ld %8ld %7d %6d %8ld %6u %s\n",
179 (CHECK_FLAG(wq->flags, WQ_UNPLUGGED) ? ' ' : 'P'),
f104f6c1 180 work_queue_item_count(wq), wq->spec.hold, wq->runs,
d62a17ae 181 wq->yields, wq->cycles.best, wq->cycles.granularity,
182 wq->cycles.total,
183 (wq->runs) ? (unsigned int)(wq->cycles.total / wq->runs)
184 : 0,
185 wq->name);
186 }
187
188 return CMD_SUCCESS;
354d119a 189}
190
d62a17ae 191void workqueue_cmd_init(void)
0b84f294 192{
d62a17ae 193 install_element(VIEW_NODE, &show_work_queues_cmd);
0b84f294
DL
194}
195
269d74fd 196/* 'plug' a queue: Stop it from being scheduled,
197 * ie: prevent the queue from draining.
198 */
d62a17ae 199void work_queue_plug(struct work_queue *wq)
269d74fd 200{
d1c27668 201 THREAD_OFF(wq->thread);
d62a17ae 202
203 UNSET_FLAG(wq->flags, WQ_UNPLUGGED);
269d74fd 204}
205
206/* unplug queue, schedule it again, if appropriate
207 * Ie: Allow the queue to be drained again
208 */
d62a17ae 209void work_queue_unplug(struct work_queue *wq)
269d74fd 210{
d62a17ae 211 SET_FLAG(wq->flags, WQ_UNPLUGGED);
269d74fd 212
d62a17ae 213 /* if thread isnt already waiting, add one */
214 work_queue_schedule(wq, wq->spec.hold);
269d74fd 215}
216
354d119a 217/* timer thread to process a work queue
218 * will reschedule itself if required,
d62a17ae 219 * otherwise work_queue_item_add
354d119a 220 */
cc9f21da 221void work_queue_run(struct thread *thread)
354d119a 222{
d62a17ae 223 struct work_queue *wq;
f104f6c1 224 struct work_queue_item *item, *titem;
5418f988 225 wq_item_status ret = WQ_SUCCESS;
d62a17ae 226 unsigned int cycles = 0;
d62a17ae 227 char yielded = 0;
228
229 wq = THREAD_ARG(thread);
d62a17ae 230
f104f6c1 231 assert(wq);
d62a17ae 232
233 /* calculate cycle granularity:
234 * list iteration == 1 run
235 * listnode processing == 1 cycle
236 * granularity == # cycles between checks whether we should yield.
237 *
238 * granularity should be > 0, and can increase slowly after each run to
239 * provide some hysteris, but not past cycles.best or 2*cycles.
240 *
241 * Best: starts low, can only increase
242 *
243 * Granularity: starts at WORK_QUEUE_MIN_GRANULARITY, can be decreased
244 * if we run to end of time slot, can increase otherwise
245 * by a small factor.
246 *
247 * We could use just the average and save some work, however we want to
248 * be
249 * able to adjust quickly to CPU pressure. Average wont shift much if
250 * daemon has been running a long time.
251 */
252 if (wq->cycles.granularity == 0)
253 wq->cycles.granularity = WORK_QUEUE_MIN_GRANULARITY;
254
a2addae8 255 STAILQ_FOREACH_SAFE (item, &wq->items, wq, titem) {
1383ff9c 256 assert(item->data);
d62a17ae 257
258 /* dont run items which are past their allowed retries */
259 if (item->ran > wq->spec.max_retries) {
f104f6c1 260 work_queue_item_remove(wq, item);
d62a17ae 261 continue;
262 }
263
264 /* run and take care of items that want to be retried
265 * immediately */
266 do {
267 ret = wq->spec.workfunc(wq, item->data);
268 item->ran++;
269 } while ((ret == WQ_RETRY_NOW)
270 && (item->ran < wq->spec.max_retries));
271
272 switch (ret) {
273 case WQ_QUEUE_BLOCKED: {
274 /* decrement item->ran again, cause this isn't an item
275 * specific error, and fall through to WQ_RETRY_LATER
276 */
277 item->ran--;
278 }
279 case WQ_RETRY_LATER: {
280 goto stats;
281 }
282 case WQ_REQUEUE: {
283 item->ran--;
f104f6c1 284 work_queue_item_requeue(wq, item);
d62a17ae 285 /* If a single node is being used with a meta-queue
286 * (e.g., zebra),
287 * update the next node as we don't want to exit the
288 * thread and
289 * reschedule it after every node. By definition,
290 * WQ_REQUEUE is
291 * meant to continue the processing; the yield logic
292 * will kick in
293 * to terminate the thread when time has exceeded.
294 */
f104f6c1
JB
295 if (titem == NULL)
296 titem = item;
d62a17ae 297 break;
298 }
299 case WQ_RETRY_NOW:
300 /* a RETRY_NOW that gets here has exceeded max_tries, same as
301 * ERROR */
d62a17ae 302 /* fallthru */
303 case WQ_SUCCESS:
304 default: {
f104f6c1 305 work_queue_item_remove(wq, item);
d62a17ae 306 break;
307 }
308 }
309
310 /* completed cycle */
311 cycles++;
312
313 /* test if we should yield */
314 if (!(cycles % wq->cycles.granularity)
315 && thread_should_yield(thread)) {
316 yielded = 1;
317 goto stats;
318 }
354d119a 319 }
354d119a 320
321stats:
322
3322055b 323#define WQ_HYSTERESIS_FACTOR 4
354d119a 324
d62a17ae 325 /* we yielded, check whether granularity should be reduced */
326 if (yielded && (cycles < wq->cycles.granularity)) {
327 wq->cycles.granularity =
328 ((cycles > 0) ? cycles : WORK_QUEUE_MIN_GRANULARITY);
329 }
330 /* otherwise, should granularity increase? */
331 else if (cycles >= (wq->cycles.granularity)) {
332 if (cycles > wq->cycles.best)
333 wq->cycles.best = cycles;
334
335 /* along with yielded check, provides hysteresis for granularity
336 */
337 if (cycles > (wq->cycles.granularity * WQ_HYSTERESIS_FACTOR
338 * WQ_HYSTERESIS_FACTOR))
339 wq->cycles.granularity *=
340 WQ_HYSTERESIS_FACTOR; /* quick ramp-up */
341 else if (cycles
342 > (wq->cycles.granularity * WQ_HYSTERESIS_FACTOR))
343 wq->cycles.granularity += WQ_HYSTERESIS_FACTOR;
344 }
354d119a 345#undef WQ_HYSTERIS_FACTOR
d62a17ae 346
347 wq->runs++;
348 wq->cycles.total += cycles;
349 if (yielded)
350 wq->yields++;
354d119a 351
d62a17ae 352 /* Is the queue done yet? If it is, call the completion callback. */
5418f988
MS
353 if (!work_queue_empty(wq)) {
354 if (ret == WQ_RETRY_LATER ||
355 ret == WQ_QUEUE_BLOCKED)
356 work_queue_schedule(wq, wq->spec.retry);
357 else
358 work_queue_schedule(wq, 0);
359
360 } else if (wq->spec.completion_func)
d62a17ae 361 wq->spec.completion_func(wq);
354d119a 362}