blob: 0c61f5c4f6a1524aa89029a7011958bef29dcb15 [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 */
33static struct list work_queues;
34
35#define WORK_QUEUE_MIN_GRANULARITY 1
36
37static struct work_queue_item *
38work_queue_item_new (struct work_queue *wq)
39{
40 struct work_queue_item *item;
41 assert (wq);
42
43 item = XCALLOC (MTYPE_WORK_QUEUE_ITEM,
44 sizeof (struct work_queue_item));
45
46 return item;
47}
48
49static void
50work_queue_item_free (struct work_queue_item *item)
51{
52 XFREE (MTYPE_WORK_QUEUE_ITEM, item);
53 return;
54}
55
56/* create new work queue */
57struct work_queue *
58work_queue_new (struct thread_master *m, const char *queue_name)
59{
60 struct work_queue *new;
61
62 new = XCALLOC (MTYPE_WORK_QUEUE, sizeof (struct work_queue));
63
64 if (new == NULL)
65 return new;
66
67 new->name = XSTRDUP (MTYPE_WORK_QUEUE_NAME, queue_name);
68 new->master = m;
69
70 if ( (new->items = list_new ()) == NULL)
71 {
paul354d1192005-04-25 16:26:42 +000072 XFREE (MTYPE_WORK_QUEUE_NAME, new->name);
73 XFREE (MTYPE_WORK_QUEUE, new);
74
75 return NULL;
76 }
77
78 new->items->del = (void (*)(void *)) work_queue_item_free;
79
80 listnode_add (&work_queues, new);
81
82 new->cycles.granularity = WORK_QUEUE_MIN_GRANULARITY;
paul190880d2005-11-14 12:07:47 +000083
84 /* Default values, can be overriden by caller */
85 new->spec.delay = WORK_QUEUE_DEFAULT_DELAY;
86 new->spec.hold = WORK_QUEUE_DEFAULT_HOLD;
87 new->spec.flood = WORK_QUEUE_DEFAULT_FLOOD;
88
paul354d1192005-04-25 16:26:42 +000089 return new;
90}
91
92void
93work_queue_free (struct work_queue *wq)
94{
95 /* list_delete frees items via callback */
96 list_delete (wq->items);
97 listnode_delete (&work_queues, wq);
98
99 XFREE (MTYPE_WORK_QUEUE_NAME, wq->name);
100 XFREE (MTYPE_WORK_QUEUE, wq);
101 return;
102}
103
paul269d74f2005-05-23 13:42:46 +0000104static inline int
105work_queue_schedule (struct work_queue *wq, unsigned int delay)
106{
107 /* if appropriate, schedule work queue thread */
108 if ( (wq->flags == WQ_UNPLUGGED)
109 && (wq->thread == NULL)
110 && (listcount (wq->items) > 0) )
111 {
112 wq->thread = thread_add_background (wq->master, work_queue_run,
113 wq, delay);
114 return 1;
115 }
116 else
117 return 0;
118}
119
paul354d1192005-04-25 16:26:42 +0000120void
121work_queue_add (struct work_queue *wq, void *data)
122{
123 struct work_queue_item *item;
124
125 assert (wq);
126
127 if (!(item = work_queue_item_new (wq)))
128 {
129 zlog_err ("%s: unable to get new queue item", __func__);
130 return;
131 }
132
133 item->data = data;
134 listnode_add (wq->items, item);
135
paul190880d2005-11-14 12:07:47 +0000136 work_queue_schedule (wq, wq->spec.delay);
paul354d1192005-04-25 16:26:42 +0000137
138 return;
139}
140
141static void
142work_queue_item_remove (struct work_queue *wq, struct listnode *ln)
143{
144 struct work_queue_item *item = listgetdata (ln);
145
146 assert (item && item->data);
147
148 /* call private data deletion callback if needed */
149 if (wq->spec.del_item_data)
paul889e9312005-11-14 14:46:35 +0000150 wq->spec.del_item_data (wq, item->data);
paul354d1192005-04-25 16:26:42 +0000151
152 list_delete_node (wq->items, ln);
153 work_queue_item_free (item);
154
155 return;
156}
157
158static void
159work_queue_item_requeue (struct work_queue *wq, struct listnode *ln)
160{
161 LISTNODE_DETACH (wq->items, ln);
162 LISTNODE_ATTACH (wq->items, ln); /* attach to end of list */
163}
164
165DEFUN(show_work_queues,
166 show_work_queues_cmd,
167 "show work-queues",
168 SHOW_STR
169 "Work Queue information\n")
170{
171 struct listnode *node;
172 struct work_queue *wq;
paul354d1192005-04-25 16:26:42 +0000173
174 vty_out (vty,
paul190880d2005-11-14 12:07:47 +0000175 "%c%c %8s %11s %8s %21s%s",
176 ' ', ' ', "List","(ms) ","Q. Runs","Cycle Counts ",
paul354d1192005-04-25 16:26:42 +0000177 VTY_NEWLINE);
178 vty_out (vty,
paul190880d2005-11-14 12:07:47 +0000179 "%c%c %8s %5s %5s %8s %7s %6s %6s %s%s",
180 'P', 'F',
paul354d1192005-04-25 16:26:42 +0000181 "Items",
182 "Delay","Hold",
183 "Total",
184 "Best","Gran.","Avg.",
185 "Name",
186 VTY_NEWLINE);
187
188 for (ALL_LIST_ELEMENTS_RO ((&work_queues), node, wq))
189 {
paul190880d2005-11-14 12:07:47 +0000190 vty_out (vty,"%c%c %8d %5d %5d %8ld %7d %6d %6u %s%s",
paul269d74f2005-05-23 13:42:46 +0000191 (wq->flags == WQ_PLUGGED ? 'P' : ' '),
paul190880d2005-11-14 12:07:47 +0000192 (wq->runs_since_clear >= wq->spec.flood ? 'F' : ' '),
paul354d1192005-04-25 16:26:42 +0000193 listcount (wq->items),
194 wq->spec.delay, wq->spec.hold,
195 wq->runs,
paul84369682005-04-27 12:39:27 +0000196 wq->cycles.best, wq->cycles.granularity,
197 (wq->runs) ?
198 (unsigned int) (wq->cycles.total / wq->runs) : 0,
paul354d1192005-04-25 16:26:42 +0000199 wq->name,
200 VTY_NEWLINE);
201 }
202
203 return CMD_SUCCESS;
204}
205
paul269d74f2005-05-23 13:42:46 +0000206/* 'plug' a queue: Stop it from being scheduled,
207 * ie: prevent the queue from draining.
208 */
209void
210work_queue_plug (struct work_queue *wq)
211{
212 if (wq->thread)
213 thread_cancel (wq->thread);
214
215 wq->thread = NULL;
216
217 wq->flags = WQ_PLUGGED;
218}
219
220/* unplug queue, schedule it again, if appropriate
221 * Ie: Allow the queue to be drained again
222 */
223void
224work_queue_unplug (struct work_queue *wq)
225{
226 wq->flags = WQ_UNPLUGGED;
227
228 /* if thread isnt already waiting, add one */
paul190880d2005-11-14 12:07:47 +0000229 work_queue_schedule (wq, wq->spec.delay);
paul269d74f2005-05-23 13:42:46 +0000230}
231
paul354d1192005-04-25 16:26:42 +0000232/* timer thread to process a work queue
233 * will reschedule itself if required,
234 * otherwise work_queue_item_add
235 */
236int
237work_queue_run (struct thread *thread)
238{
239 struct work_queue *wq;
240 struct work_queue_item *item;
241 wq_item_status ret;
242 unsigned int cycles = 0;
243 struct listnode *node, *nnode;
244 char yielded = 0;
245
246 wq = THREAD_ARG (thread);
247 wq->thread = NULL;
248
249 assert (wq && wq->items);
250
251 /* calculate cycle granularity:
252 * list iteration == 1 cycle
253 * granularity == # cycles between checks whether we should yield.
254 *
255 * granularity should be > 0, and can increase slowly after each run to
256 * provide some hysteris, but not past cycles.best or 2*cycles.
257 *
258 * Best: starts low, can only increase
259 *
260 * Granularity: starts at WORK_QUEUE_MIN_GRANULARITY, can be decreased if we run to end of time
261 * slot, can increase otherwise by a small factor.
262 *
263 * We could use just the average and save some work, however we want to 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
270 for (ALL_LIST_ELEMENTS (wq->items, node, nnode, item))
271 {
272 assert (item && item->data);
273
274 /* dont run items which are past their allowed retries */
paul84369682005-04-27 12:39:27 +0000275 if (item->ran > wq->spec.max_retries)
paul354d1192005-04-25 16:26:42 +0000276 {
277 /* run error handler, if any */
278 if (wq->spec.errorfunc)
279 wq->spec.errorfunc (wq, item->data);
280 work_queue_item_remove (wq, node);
281 continue;
282 }
283
284 /* run and take care of items that want to be retried immediately */
285 do
286 {
paul889e9312005-11-14 14:46:35 +0000287 ret = wq->spec.workfunc (wq, item->data);
paul84369682005-04-27 12:39:27 +0000288 item->ran++;
paul354d1192005-04-25 16:26:42 +0000289 }
290 while ((ret == WQ_RETRY_NOW)
paul84369682005-04-27 12:39:27 +0000291 && (item->ran < wq->spec.max_retries));
paul354d1192005-04-25 16:26:42 +0000292
293 switch (ret)
294 {
paul269d74f2005-05-23 13:42:46 +0000295 case WQ_QUEUE_BLOCKED:
296 {
297 /* decrement item->ran again, cause this isn't an item
298 * specific error, and fall through to WQ_RETRY_LATER
299 */
300 item->ran--;
301 }
paul354d1192005-04-25 16:26:42 +0000302 case WQ_RETRY_LATER:
303 {
paul354d1192005-04-25 16:26:42 +0000304 goto stats;
305 }
306 case WQ_REQUEUE:
307 {
paul354d1192005-04-25 16:26:42 +0000308 work_queue_item_requeue (wq, node);
309 break;
310 }
311 case WQ_RETRY_NOW:
paul269d74f2005-05-23 13:42:46 +0000312 /* a RETRY_NOW that gets here has exceeded max_tries, same as ERROR */
paul354d1192005-04-25 16:26:42 +0000313 case WQ_ERROR:
314 {
315 if (wq->spec.errorfunc)
316 wq->spec.errorfunc (wq, item);
317 }
318 /* fall through here is deliberate */
319 case WQ_SUCCESS:
320 default:
321 {
322 work_queue_item_remove (wq, node);
323 break;
324 }
325 }
326
327 /* completed cycle */
328 cycles++;
329
330 /* test if we should yield */
331 if ( !(cycles % wq->cycles.granularity)
332 && thread_should_yield (thread))
333 {
334 yielded = 1;
335 goto stats;
336 }
337 }
338
339stats:
340
341#define WQ_HYSTERIS_FACTOR 2
342
343 /* we yielded, check whether granularity should be reduced */
344 if (yielded && (cycles < wq->cycles.granularity))
345 {
346 wq->cycles.granularity = ((cycles > 0) ? cycles
347 : WORK_QUEUE_MIN_GRANULARITY);
348 }
349
paul84369682005-04-27 12:39:27 +0000350 if (cycles >= (wq->cycles.granularity))
paul354d1192005-04-25 16:26:42 +0000351 {
352 if (cycles > wq->cycles.best)
353 wq->cycles.best = cycles;
354
355 /* along with yielded check, provides hysteris for granularity */
paul269d74f2005-05-23 13:42:46 +0000356 if (cycles > (wq->cycles.granularity * WQ_HYSTERIS_FACTOR * 2))
357 wq->cycles.granularity *= WQ_HYSTERIS_FACTOR; /* quick ramp-up */
358 else if (cycles > (wq->cycles.granularity * WQ_HYSTERIS_FACTOR))
paul354d1192005-04-25 16:26:42 +0000359 wq->cycles.granularity += WQ_HYSTERIS_FACTOR;
360 }
361#undef WQ_HYSTERIS_FACTOR
362
363 wq->runs++;
364 wq->cycles.total += cycles;
365
366#if 0
367 printf ("%s: cycles %d, new: best %d, worst %d\n",
368 __func__, cycles, wq->cycles.best, wq->cycles.granularity);
369#endif
370
paul269d74f2005-05-23 13:42:46 +0000371 /* Is the queue done yet? If it is, call the completion callback. */
paul354d1192005-04-25 16:26:42 +0000372 if (listcount (wq->items) > 0)
paul190880d2005-11-14 12:07:47 +0000373 {
374 if (++(wq->runs_since_clear) < wq->spec.flood)
375 work_queue_schedule (wq, wq->spec.hold);
376 else
377 work_queue_schedule (wq, 0); /* queue flooded, go into overdrive */
378 }
379 else
380 {
381 wq->runs_since_clear = 0;
382
383 if (wq->spec.completion_func)
384 wq->spec.completion_func (wq);
385 }
paul269d74f2005-05-23 13:42:46 +0000386
paul354d1192005-04-25 16:26:42 +0000387 return 0;
388}