blob: e09d009f17e70bf5753491bbb902733c1a840504 [file] [log] [blame]
paul354d1192005-04-25 16:26:42 +00001/*
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
19 * along with Quagga; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * 02111-1307, USA.
22 */
23
24#include <lib/zebra.h>
25#include "thread.h"
26#include "memory.h"
27#include "workqueue.h"
28#include "linklist.h"
29#include "command.h"
30#include "log.h"
31
32/* master list of work_queues */
Paul Jakma7aa9dce2014-09-19 14:42:23 +010033static struct list _work_queues;
34/* pointer primarily to avoid an otherwise harmless warning on
35 * ALL_LIST_ELEMENTS_RO
36 */
37static struct list *work_queues = &_work_queues;
paul354d1192005-04-25 16:26:42 +000038
39#define WORK_QUEUE_MIN_GRANULARITY 1
40
41static struct work_queue_item *
42work_queue_item_new (struct work_queue *wq)
43{
44 struct work_queue_item *item;
45 assert (wq);
46
47 item = XCALLOC (MTYPE_WORK_QUEUE_ITEM,
48 sizeof (struct work_queue_item));
49
50 return item;
51}
52
53static void
54work_queue_item_free (struct work_queue_item *item)
55{
56 XFREE (MTYPE_WORK_QUEUE_ITEM, item);
57 return;
58}
59
60/* create new work queue */
61struct work_queue *
62work_queue_new (struct thread_master *m, const char *queue_name)
63{
64 struct work_queue *new;
65
66 new = XCALLOC (MTYPE_WORK_QUEUE, sizeof (struct work_queue));
67
68 if (new == NULL)
69 return new;
70
71 new->name = XSTRDUP (MTYPE_WORK_QUEUE_NAME, queue_name);
72 new->master = m;
Denis Ovsienko6ce80bd2007-11-12 14:55:01 +000073 SET_FLAG (new->flags, WQ_UNPLUGGED);
paul354d1192005-04-25 16:26:42 +000074
75 if ( (new->items = list_new ()) == NULL)
76 {
paul354d1192005-04-25 16:26:42 +000077 XFREE (MTYPE_WORK_QUEUE_NAME, new->name);
78 XFREE (MTYPE_WORK_QUEUE, new);
79
80 return NULL;
81 }
82
83 new->items->del = (void (*)(void *)) work_queue_item_free;
84
Paul Jakma7aa9dce2014-09-19 14:42:23 +010085 listnode_add (work_queues, new);
paul354d1192005-04-25 16:26:42 +000086
87 new->cycles.granularity = WORK_QUEUE_MIN_GRANULARITY;
paul190880d2005-11-14 12:07:47 +000088
89 /* Default values, can be overriden by caller */
paul190880d2005-11-14 12:07:47 +000090 new->spec.hold = WORK_QUEUE_DEFAULT_HOLD;
paul190880d2005-11-14 12:07:47 +000091
paul354d1192005-04-25 16:26:42 +000092 return new;
93}
94
95void
96work_queue_free (struct work_queue *wq)
97{
Steve Hillacde4b82009-06-02 14:28:16 +010098 if (wq->thread != NULL)
99 thread_cancel(wq->thread);
100
paul354d1192005-04-25 16:26:42 +0000101 /* list_delete frees items via callback */
102 list_delete (wq->items);
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100103 listnode_delete (work_queues, wq);
paul354d1192005-04-25 16:26:42 +0000104
105 XFREE (MTYPE_WORK_QUEUE_NAME, wq->name);
106 XFREE (MTYPE_WORK_QUEUE, wq);
107 return;
108}
109
Paul Jakmaf63f06d2011-04-08 12:44:43 +0100110static int
paul269d74f2005-05-23 13:42:46 +0000111work_queue_schedule (struct work_queue *wq, unsigned int delay)
112{
113 /* if appropriate, schedule work queue thread */
Denis Ovsienko6ce80bd2007-11-12 14:55:01 +0000114 if ( CHECK_FLAG (wq->flags, WQ_UNPLUGGED)
paul269d74f2005-05-23 13:42:46 +0000115 && (wq->thread == NULL)
116 && (listcount (wq->items) > 0) )
117 {
118 wq->thread = thread_add_background (wq->master, work_queue_run,
119 wq, delay);
120 return 1;
121 }
122 else
123 return 0;
124}
125
paul354d1192005-04-25 16:26:42 +0000126void
127work_queue_add (struct work_queue *wq, void *data)
128{
129 struct work_queue_item *item;
130
131 assert (wq);
132
133 if (!(item = work_queue_item_new (wq)))
134 {
135 zlog_err ("%s: unable to get new queue item", __func__);
136 return;
137 }
138
139 item->data = data;
Denis Ovsienkoe96f9202008-06-02 12:03:22 +0000140 listnode_add (wq->items, item);
paul354d1192005-04-25 16:26:42 +0000141
paul306d8892006-02-02 17:50:19 +0000142 work_queue_schedule (wq, wq->spec.hold);
paul354d1192005-04-25 16:26:42 +0000143
144 return;
145}
146
147static void
148work_queue_item_remove (struct work_queue *wq, struct listnode *ln)
149{
150 struct work_queue_item *item = listgetdata (ln);
151
152 assert (item && item->data);
153
154 /* call private data deletion callback if needed */
155 if (wq->spec.del_item_data)
paul889e9312005-11-14 14:46:35 +0000156 wq->spec.del_item_data (wq, item->data);
paul354d1192005-04-25 16:26:42 +0000157
158 list_delete_node (wq->items, ln);
159 work_queue_item_free (item);
160
161 return;
162}
163
164static void
165work_queue_item_requeue (struct work_queue *wq, struct listnode *ln)
166{
167 LISTNODE_DETACH (wq->items, ln);
168 LISTNODE_ATTACH (wq->items, ln); /* attach to end of list */
169}
170
171DEFUN(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;
paul354d1192005-04-25 16:26:42 +0000179
180 vty_out (vty,
paul306d8892006-02-02 17:50:19 +0000181 "%c %8s %5s %8s %21s%s",
182 ' ', "List","(ms) ","Q. Runs","Cycle Counts ",
paul354d1192005-04-25 16:26:42 +0000183 VTY_NEWLINE);
184 vty_out (vty,
paul306d8892006-02-02 17:50:19 +0000185 "%c %8s %5s %8s %7s %6s %6s %s%s",
186 'P',
paul354d1192005-04-25 16:26:42 +0000187 "Items",
paul306d8892006-02-02 17:50:19 +0000188 "Hold",
paul354d1192005-04-25 16:26:42 +0000189 "Total",
190 "Best","Gran.","Avg.",
191 "Name",
192 VTY_NEWLINE);
193
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100194 for (ALL_LIST_ELEMENTS_RO (work_queues, node, wq))
paul354d1192005-04-25 16:26:42 +0000195 {
paul306d8892006-02-02 17:50:19 +0000196 vty_out (vty,"%c %8d %5d %8ld %7d %6d %6u %s%s",
Denis Ovsienko6ce80bd2007-11-12 14:55:01 +0000197 (CHECK_FLAG (wq->flags, WQ_UNPLUGGED) ? ' ' : 'P'),
paul354d1192005-04-25 16:26:42 +0000198 listcount (wq->items),
paul306d8892006-02-02 17:50:19 +0000199 wq->spec.hold,
paul354d1192005-04-25 16:26:42 +0000200 wq->runs,
paul84369682005-04-27 12:39:27 +0000201 wq->cycles.best, wq->cycles.granularity,
202 (wq->runs) ?
203 (unsigned int) (wq->cycles.total / wq->runs) : 0,
paul354d1192005-04-25 16:26:42 +0000204 wq->name,
205 VTY_NEWLINE);
206 }
207
208 return CMD_SUCCESS;
209}
210
paul269d74f2005-05-23 13:42:46 +0000211/* 'plug' a queue: Stop it from being scheduled,
212 * ie: prevent the queue from draining.
213 */
214void
215work_queue_plug (struct work_queue *wq)
216{
217 if (wq->thread)
218 thread_cancel (wq->thread);
219
220 wq->thread = NULL;
221
Denis Ovsienko6ce80bd2007-11-12 14:55:01 +0000222 UNSET_FLAG (wq->flags, WQ_UNPLUGGED);
paul269d74f2005-05-23 13:42:46 +0000223}
224
225/* unplug queue, schedule it again, if appropriate
226 * Ie: Allow the queue to be drained again
227 */
228void
229work_queue_unplug (struct work_queue *wq)
230{
Denis Ovsienko6ce80bd2007-11-12 14:55:01 +0000231 SET_FLAG (wq->flags, WQ_UNPLUGGED);
paul269d74f2005-05-23 13:42:46 +0000232
233 /* if thread isnt already waiting, add one */
paul306d8892006-02-02 17:50:19 +0000234 work_queue_schedule (wq, wq->spec.hold);
paul269d74f2005-05-23 13:42:46 +0000235}
236
paul354d1192005-04-25 16:26:42 +0000237/* timer thread to process a work queue
238 * will reschedule itself if required,
239 * otherwise work_queue_item_add
240 */
241int
242work_queue_run (struct thread *thread)
243{
244 struct work_queue *wq;
245 struct work_queue_item *item;
246 wq_item_status ret;
247 unsigned int cycles = 0;
248 struct listnode *node, *nnode;
249 char yielded = 0;
250
251 wq = THREAD_ARG (thread);
252 wq->thread = NULL;
253
254 assert (wq && wq->items);
255
256 /* calculate cycle granularity:
257 * list iteration == 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 *
Paul Jakma213d8da2006-03-30 14:45:47 +0000265 * 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.
paul354d1192005-04-25 16:26:42 +0000268 *
269 * We could use just the average and save some work, however we want to be
270 * able to adjust quickly to CPU pressure. Average wont shift much if
271 * daemon has been running a long time.
272 */
273 if (wq->cycles.granularity == 0)
274 wq->cycles.granularity = WORK_QUEUE_MIN_GRANULARITY;
275
276 for (ALL_LIST_ELEMENTS (wq->items, node, nnode, item))
277 {
278 assert (item && item->data);
279
280 /* dont run items which are past their allowed retries */
paul84369682005-04-27 12:39:27 +0000281 if (item->ran > wq->spec.max_retries)
paul354d1192005-04-25 16:26:42 +0000282 {
283 /* run error handler, if any */
284 if (wq->spec.errorfunc)
285 wq->spec.errorfunc (wq, item->data);
286 work_queue_item_remove (wq, node);
287 continue;
288 }
289
290 /* run and take care of items that want to be retried immediately */
291 do
292 {
paul889e9312005-11-14 14:46:35 +0000293 ret = wq->spec.workfunc (wq, item->data);
paul84369682005-04-27 12:39:27 +0000294 item->ran++;
paul354d1192005-04-25 16:26:42 +0000295 }
296 while ((ret == WQ_RETRY_NOW)
paul84369682005-04-27 12:39:27 +0000297 && (item->ran < wq->spec.max_retries));
paul354d1192005-04-25 16:26:42 +0000298
299 switch (ret)
300 {
paul269d74f2005-05-23 13:42:46 +0000301 case WQ_QUEUE_BLOCKED:
302 {
303 /* decrement item->ran again, cause this isn't an item
304 * specific error, and fall through to WQ_RETRY_LATER
305 */
306 item->ran--;
307 }
paul354d1192005-04-25 16:26:42 +0000308 case WQ_RETRY_LATER:
309 {
paul354d1192005-04-25 16:26:42 +0000310 goto stats;
311 }
312 case WQ_REQUEUE:
313 {
Denis Ovsienkoe96f9202008-06-02 12:03:22 +0000314 item->ran--;
paul354d1192005-04-25 16:26:42 +0000315 work_queue_item_requeue (wq, node);
316 break;
317 }
318 case WQ_RETRY_NOW:
paul269d74f2005-05-23 13:42:46 +0000319 /* a RETRY_NOW that gets here has exceeded max_tries, same as ERROR */
paul354d1192005-04-25 16:26:42 +0000320 case WQ_ERROR:
321 {
322 if (wq->spec.errorfunc)
323 wq->spec.errorfunc (wq, item);
324 }
325 /* fall through here is deliberate */
326 case WQ_SUCCESS:
327 default:
328 {
329 work_queue_item_remove (wq, node);
330 break;
331 }
332 }
333
334 /* completed cycle */
335 cycles++;
336
337 /* test if we should yield */
338 if ( !(cycles % wq->cycles.granularity)
339 && thread_should_yield (thread))
340 {
341 yielded = 1;
342 goto stats;
343 }
344 }
345
346stats:
347
Paul Jakma33220552010-01-11 13:55:01 +0000348#define WQ_HYSTERESIS_FACTOR 4
paul354d1192005-04-25 16:26:42 +0000349
350 /* we yielded, check whether granularity should be reduced */
351 if (yielded && (cycles < wq->cycles.granularity))
352 {
353 wq->cycles.granularity = ((cycles > 0) ? cycles
354 : WORK_QUEUE_MIN_GRANULARITY);
355 }
Paul Jakma33220552010-01-11 13:55:01 +0000356 /* otherwise, should granularity increase? */
357 else if (cycles >= (wq->cycles.granularity))
paul354d1192005-04-25 16:26:42 +0000358 {
359 if (cycles > wq->cycles.best)
360 wq->cycles.best = cycles;
361
Paul Jakma33220552010-01-11 13:55:01 +0000362 /* along with yielded check, provides hysteresis for granularity */
363 if (cycles > (wq->cycles.granularity * WQ_HYSTERESIS_FACTOR
364 * WQ_HYSTERESIS_FACTOR))
365 wq->cycles.granularity *= WQ_HYSTERESIS_FACTOR; /* quick ramp-up */
366 else if (cycles > (wq->cycles.granularity * WQ_HYSTERESIS_FACTOR))
367 wq->cycles.granularity += WQ_HYSTERESIS_FACTOR;
paul354d1192005-04-25 16:26:42 +0000368 }
369#undef WQ_HYSTERIS_FACTOR
370
371 wq->runs++;
372 wq->cycles.total += cycles;
373
374#if 0
375 printf ("%s: cycles %d, new: best %d, worst %d\n",
376 __func__, cycles, wq->cycles.best, wq->cycles.granularity);
377#endif
378
paul269d74f2005-05-23 13:42:46 +0000379 /* Is the queue done yet? If it is, call the completion callback. */
paul354d1192005-04-25 16:26:42 +0000380 if (listcount (wq->items) > 0)
paul306d8892006-02-02 17:50:19 +0000381 work_queue_schedule (wq, 0);
382 else if (wq->spec.completion_func)
383 wq->spec.completion_func (wq);
paul269d74f2005-05-23 13:42:46 +0000384
paul354d1192005-04-25 16:26:42 +0000385 return 0;
386}