]> git.proxmox.com Git - mirror_frr.git/blame - lib/workqueue.c
Merge pull request #12708 from donaldsharp/no_notification
[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
d1c27668 106 THREAD_OFF(wq->thread);
d62a17ae 107
da7f979a
DS
108 while (!work_queue_empty(wq)) {
109 struct work_queue_item *item = work_queue_last_item(wq);
110
111 work_queue_item_remove(wq, item);
112 }
113
d62a17ae 114 listnode_delete(work_queues, wq);
115
116 XFREE(MTYPE_WORK_QUEUE_NAME, wq->name);
117 XFREE(MTYPE_WORK_QUEUE, wq);
354d119a 118
6b097e33 119 *wqp = NULL;
e208c8f9
DS
120}
121
d62a17ae 122bool work_queue_is_scheduled(struct work_queue *wq)
86582682 123{
d1c27668 124 return thread_is_scheduled(wq->thread);
86582682
PJ
125}
126
d62a17ae 127static int work_queue_schedule(struct work_queue *wq, unsigned int delay)
269d74fd 128{
d62a17ae 129 /* if appropriate, schedule work queue thread */
d1c27668
DS
130 if (CHECK_FLAG(wq->flags, WQ_UNPLUGGED) &&
131 !thread_is_scheduled(wq->thread) && !work_queue_empty(wq)) {
5418f988
MS
132 /* Schedule timer if there's a delay, otherwise just schedule
133 * as an 'event'
134 */
e8b3a2f7 135 if (delay > 0) {
5418f988
MS
136 thread_add_timer_msec(wq->master, work_queue_run, wq,
137 delay, &wq->thread);
e8b3a2f7
DS
138 thread_ignore_late_timer(wq->thread);
139 } else
5418f988
MS
140 thread_add_event(wq->master, work_queue_run, wq, 0,
141 &wq->thread);
142
d62a17ae 143 /* set thread yield time, if needed */
d1c27668
DS
144 if (thread_is_scheduled(wq->thread) &&
145 wq->spec.yield != THREAD_YIELD_TIME_SLOT)
d62a17ae 146 thread_set_yield_time(wq->thread, wq->spec.yield);
147 return 1;
148 } else
149 return 0;
269d74fd 150}
d62a17ae 151
152void work_queue_add(struct work_queue *wq, void *data)
354d119a 153{
d62a17ae 154 struct work_queue_item *item;
155
156 assert(wq);
157
0ce1ca80 158 item = work_queue_item_new(wq);
d62a17ae 159
160 item->data = data;
f104f6c1 161 work_queue_item_enqueue(wq, item);
d62a17ae 162
163 work_queue_schedule(wq, wq->spec.hold);
164
165 return;
354d119a 166}
167
996c9314
LB
168static void work_queue_item_requeue(struct work_queue *wq,
169 struct work_queue_item *item)
354d119a 170{
f104f6c1
JB
171 work_queue_item_dequeue(wq, item);
172
173 /* attach to end of list */
174 work_queue_item_enqueue(wq, item);
354d119a 175}
176
49d41a26
DS
177DEFUN (show_work_queues,
178 show_work_queues_cmd,
179 "show work-queues",
180 SHOW_STR
181 "Work Queue information\n")
354d119a 182{
d62a17ae 183 struct listnode *node;
184 struct work_queue *wq;
185
186 vty_out(vty, "%c %8s %5s %8s %8s %21s\n", ' ', "List", "(ms) ",
187 "Q. Runs", "Yields", "Cycle Counts ");
188 vty_out(vty, "%c %8s %5s %8s %8s %7s %6s %8s %6s %s\n", 'P', "Items",
189 "Hold", "Total", "Total", "Best", "Gran.", "Total", "Avg.",
190 "Name");
191
192 for (ALL_LIST_ELEMENTS_RO(work_queues, node, wq)) {
193 vty_out(vty, "%c %8d %5d %8ld %8ld %7d %6d %8ld %6u %s\n",
194 (CHECK_FLAG(wq->flags, WQ_UNPLUGGED) ? ' ' : 'P'),
f104f6c1 195 work_queue_item_count(wq), wq->spec.hold, wq->runs,
d62a17ae 196 wq->yields, wq->cycles.best, wq->cycles.granularity,
197 wq->cycles.total,
198 (wq->runs) ? (unsigned int)(wq->cycles.total / wq->runs)
199 : 0,
200 wq->name);
201 }
202
203 return CMD_SUCCESS;
354d119a 204}
205
d62a17ae 206void workqueue_cmd_init(void)
0b84f294 207{
d62a17ae 208 install_element(VIEW_NODE, &show_work_queues_cmd);
0b84f294
DL
209}
210
269d74fd 211/* 'plug' a queue: Stop it from being scheduled,
212 * ie: prevent the queue from draining.
213 */
d62a17ae 214void work_queue_plug(struct work_queue *wq)
269d74fd 215{
d1c27668 216 THREAD_OFF(wq->thread);
d62a17ae 217
218 UNSET_FLAG(wq->flags, WQ_UNPLUGGED);
269d74fd 219}
220
221/* unplug queue, schedule it again, if appropriate
222 * Ie: Allow the queue to be drained again
223 */
d62a17ae 224void work_queue_unplug(struct work_queue *wq)
269d74fd 225{
d62a17ae 226 SET_FLAG(wq->flags, WQ_UNPLUGGED);
269d74fd 227
d62a17ae 228 /* if thread isnt already waiting, add one */
229 work_queue_schedule(wq, wq->spec.hold);
269d74fd 230}
231
354d119a 232/* timer thread to process a work queue
233 * will reschedule itself if required,
d62a17ae 234 * otherwise work_queue_item_add
354d119a 235 */
cc9f21da 236void work_queue_run(struct thread *thread)
354d119a 237{
d62a17ae 238 struct work_queue *wq;
f104f6c1 239 struct work_queue_item *item, *titem;
5418f988 240 wq_item_status ret = WQ_SUCCESS;
d62a17ae 241 unsigned int cycles = 0;
d62a17ae 242 char yielded = 0;
243
244 wq = THREAD_ARG(thread);
d62a17ae 245
f104f6c1 246 assert(wq);
d62a17ae 247
248 /* calculate cycle granularity:
249 * list iteration == 1 run
250 * listnode processing == 1 cycle
251 * granularity == # cycles between checks whether we should yield.
252 *
253 * granularity should be > 0, and can increase slowly after each run to
254 * provide some hysteris, but not past cycles.best or 2*cycles.
255 *
256 * Best: starts low, can only increase
257 *
258 * Granularity: starts at WORK_QUEUE_MIN_GRANULARITY, can be decreased
259 * if we run to end of time slot, can increase otherwise
260 * by a small factor.
261 *
262 * We could use just the average and save some work, however we want to
263 * be
264 * able to adjust quickly to CPU pressure. Average wont shift much if
265 * daemon has been running a long time.
266 */
267 if (wq->cycles.granularity == 0)
268 wq->cycles.granularity = WORK_QUEUE_MIN_GRANULARITY;
269
a2addae8 270 STAILQ_FOREACH_SAFE (item, &wq->items, wq, titem) {
1383ff9c 271 assert(item->data);
d62a17ae 272
273 /* dont run items which are past their allowed retries */
274 if (item->ran > wq->spec.max_retries) {
f104f6c1 275 work_queue_item_remove(wq, item);
d62a17ae 276 continue;
277 }
278
279 /* run and take care of items that want to be retried
280 * immediately */
281 do {
282 ret = wq->spec.workfunc(wq, item->data);
283 item->ran++;
284 } while ((ret == WQ_RETRY_NOW)
285 && (item->ran < wq->spec.max_retries));
286
287 switch (ret) {
288 case WQ_QUEUE_BLOCKED: {
289 /* decrement item->ran again, cause this isn't an item
290 * specific error, and fall through to WQ_RETRY_LATER
291 */
292 item->ran--;
293 }
294 case WQ_RETRY_LATER: {
295 goto stats;
296 }
297 case WQ_REQUEUE: {
298 item->ran--;
f104f6c1 299 work_queue_item_requeue(wq, item);
d62a17ae 300 /* If a single node is being used with a meta-queue
301 * (e.g., zebra),
302 * update the next node as we don't want to exit the
303 * thread and
304 * reschedule it after every node. By definition,
305 * WQ_REQUEUE is
306 * meant to continue the processing; the yield logic
307 * will kick in
308 * to terminate the thread when time has exceeded.
309 */
f104f6c1
JB
310 if (titem == NULL)
311 titem = item;
d62a17ae 312 break;
313 }
314 case WQ_RETRY_NOW:
315 /* a RETRY_NOW that gets here has exceeded max_tries, same as
316 * ERROR */
d62a17ae 317 /* fallthru */
318 case WQ_SUCCESS:
319 default: {
f104f6c1 320 work_queue_item_remove(wq, item);
d62a17ae 321 break;
322 }
323 }
324
325 /* completed cycle */
326 cycles++;
327
328 /* test if we should yield */
329 if (!(cycles % wq->cycles.granularity)
330 && thread_should_yield(thread)) {
331 yielded = 1;
332 goto stats;
333 }
354d119a 334 }
354d119a 335
336stats:
337
3322055b 338#define WQ_HYSTERESIS_FACTOR 4
354d119a 339
d62a17ae 340 /* we yielded, check whether granularity should be reduced */
341 if (yielded && (cycles < wq->cycles.granularity)) {
342 wq->cycles.granularity =
343 ((cycles > 0) ? cycles : WORK_QUEUE_MIN_GRANULARITY);
344 }
345 /* otherwise, should granularity increase? */
346 else if (cycles >= (wq->cycles.granularity)) {
347 if (cycles > wq->cycles.best)
348 wq->cycles.best = cycles;
349
350 /* along with yielded check, provides hysteresis for granularity
351 */
352 if (cycles > (wq->cycles.granularity * WQ_HYSTERESIS_FACTOR
353 * WQ_HYSTERESIS_FACTOR))
354 wq->cycles.granularity *=
355 WQ_HYSTERESIS_FACTOR; /* quick ramp-up */
356 else if (cycles
357 > (wq->cycles.granularity * WQ_HYSTERESIS_FACTOR))
358 wq->cycles.granularity += WQ_HYSTERESIS_FACTOR;
359 }
354d119a 360#undef WQ_HYSTERIS_FACTOR
d62a17ae 361
362 wq->runs++;
363 wq->cycles.total += cycles;
364 if (yielded)
365 wq->yields++;
354d119a 366
d62a17ae 367 /* Is the queue done yet? If it is, call the completion callback. */
5418f988
MS
368 if (!work_queue_empty(wq)) {
369 if (ret == WQ_RETRY_LATER ||
370 ret == WQ_QUEUE_BLOCKED)
371 work_queue_schedule(wq, wq->spec.retry);
372 else
373 work_queue_schedule(wq, 0);
374
375 } else if (wq->spec.completion_func)
d62a17ae 376 wq->spec.completion_func(wq);
354d119a 377}