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