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