blob: fc61d6800cf875fa76218796394ee72349193556 [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 {
72 if (new->items)
73 list_free (new->items);
74
75 XFREE (MTYPE_WORK_QUEUE_NAME, new->name);
76 XFREE (MTYPE_WORK_QUEUE, new);
77
78 return NULL;
79 }
80
81 new->items->del = (void (*)(void *)) work_queue_item_free;
82
83 listnode_add (&work_queues, new);
84
85 new->cycles.granularity = WORK_QUEUE_MIN_GRANULARITY;
86
87 return new;
88}
89
90void
91work_queue_free (struct work_queue *wq)
92{
93 /* list_delete frees items via callback */
94 list_delete (wq->items);
95 listnode_delete (&work_queues, wq);
96
97 XFREE (MTYPE_WORK_QUEUE_NAME, wq->name);
98 XFREE (MTYPE_WORK_QUEUE, wq);
99 return;
100}
101
102void
103work_queue_add (struct work_queue *wq, void *data)
104{
105 struct work_queue_item *item;
106
107 assert (wq);
108
109 if (!(item = work_queue_item_new (wq)))
110 {
111 zlog_err ("%s: unable to get new queue item", __func__);
112 return;
113 }
114
115 item->data = data;
116 listnode_add (wq->items, item);
117
118 /* if thread isnt already waiting, add one */
119 if (wq->thread == NULL)
120 wq->thread = thread_add_background (wq->master, work_queue_run,
121 wq, wq->spec.hold);
122
123 /* XXX: what if we didnt get a thread? try again? */
124
125 return;
126}
127
128static void
129work_queue_item_remove (struct work_queue *wq, struct listnode *ln)
130{
131 struct work_queue_item *item = listgetdata (ln);
132
133 assert (item && item->data);
134
135 /* call private data deletion callback if needed */
136 if (wq->spec.del_item_data)
137 wq->spec.del_item_data (item->data);
138
139 list_delete_node (wq->items, ln);
140 work_queue_item_free (item);
141
142 return;
143}
144
145static void
146work_queue_item_requeue (struct work_queue *wq, struct listnode *ln)
147{
148 LISTNODE_DETACH (wq->items, ln);
149 LISTNODE_ATTACH (wq->items, ln); /* attach to end of list */
150}
151
152DEFUN(show_work_queues,
153 show_work_queues_cmd,
154 "show work-queues",
155 SHOW_STR
156 "Work Queue information\n")
157{
158 struct listnode *node;
159 struct work_queue *wq;
paul354d1192005-04-25 16:26:42 +0000160
161 vty_out (vty,
162 "%8s %11s %8s %21s%s",
paul84369682005-04-27 12:39:27 +0000163 "List","(ms) ","Q. Runs","Cycle Counts ",
paul354d1192005-04-25 16:26:42 +0000164 VTY_NEWLINE);
165 vty_out (vty,
166 "%8s %5s %5s %8s %7s %6s %6s %s%s",
167 "Items",
168 "Delay","Hold",
169 "Total",
170 "Best","Gran.","Avg.",
171 "Name",
172 VTY_NEWLINE);
173
174 for (ALL_LIST_ELEMENTS_RO ((&work_queues), node, wq))
175 {
paul84369682005-04-27 12:39:27 +0000176 vty_out (vty,"%8d %5d %5d %8ld %7d %6d %6u %s%s",
paul354d1192005-04-25 16:26:42 +0000177 listcount (wq->items),
178 wq->spec.delay, wq->spec.hold,
179 wq->runs,
paul84369682005-04-27 12:39:27 +0000180 wq->cycles.best, wq->cycles.granularity,
181 (wq->runs) ?
182 (unsigned int) (wq->cycles.total / wq->runs) : 0,
paul354d1192005-04-25 16:26:42 +0000183 wq->name,
184 VTY_NEWLINE);
185 }
186
187 return CMD_SUCCESS;
188}
189
190/* timer thread to process a work queue
191 * will reschedule itself if required,
192 * otherwise work_queue_item_add
193 */
194int
195work_queue_run (struct thread *thread)
196{
197 struct work_queue *wq;
198 struct work_queue_item *item;
199 wq_item_status ret;
200 unsigned int cycles = 0;
201 struct listnode *node, *nnode;
202 char yielded = 0;
203
204 wq = THREAD_ARG (thread);
205 wq->thread = NULL;
206
207 assert (wq && wq->items);
208
209 /* calculate cycle granularity:
210 * list iteration == 1 cycle
211 * granularity == # cycles between checks whether we should yield.
212 *
213 * granularity should be > 0, and can increase slowly after each run to
214 * provide some hysteris, but not past cycles.best or 2*cycles.
215 *
216 * Best: starts low, can only increase
217 *
218 * Granularity: starts at WORK_QUEUE_MIN_GRANULARITY, can be decreased if we run to end of time
219 * slot, can increase otherwise by a small factor.
220 *
221 * We could use just the average and save some work, however we want to be
222 * able to adjust quickly to CPU pressure. Average wont shift much if
223 * daemon has been running a long time.
224 */
225 if (wq->cycles.granularity == 0)
226 wq->cycles.granularity = WORK_QUEUE_MIN_GRANULARITY;
227
228 for (ALL_LIST_ELEMENTS (wq->items, node, nnode, item))
229 {
230 assert (item && item->data);
231
232 /* dont run items which are past their allowed retries */
paul84369682005-04-27 12:39:27 +0000233 if (item->ran > wq->spec.max_retries)
paul354d1192005-04-25 16:26:42 +0000234 {
235 /* run error handler, if any */
236 if (wq->spec.errorfunc)
237 wq->spec.errorfunc (wq, item->data);
238 work_queue_item_remove (wq, node);
239 continue;
240 }
241
242 /* run and take care of items that want to be retried immediately */
243 do
244 {
245 ret = wq->spec.workfunc (item->data);
paul84369682005-04-27 12:39:27 +0000246 item->ran++;
paul354d1192005-04-25 16:26:42 +0000247 }
248 while ((ret == WQ_RETRY_NOW)
paul84369682005-04-27 12:39:27 +0000249 && (item->ran < wq->spec.max_retries));
paul354d1192005-04-25 16:26:42 +0000250
251 switch (ret)
252 {
253 case WQ_RETRY_LATER:
254 {
paul354d1192005-04-25 16:26:42 +0000255 goto stats;
256 }
257 case WQ_REQUEUE:
258 {
paul354d1192005-04-25 16:26:42 +0000259 work_queue_item_requeue (wq, node);
260 break;
261 }
262 case WQ_RETRY_NOW:
263 case WQ_ERROR:
264 {
265 if (wq->spec.errorfunc)
266 wq->spec.errorfunc (wq, item);
267 }
268 /* fall through here is deliberate */
269 case WQ_SUCCESS:
270 default:
271 {
272 work_queue_item_remove (wq, node);
273 break;
274 }
275 }
276
277 /* completed cycle */
278 cycles++;
279
280 /* test if we should yield */
281 if ( !(cycles % wq->cycles.granularity)
282 && thread_should_yield (thread))
283 {
284 yielded = 1;
285 goto stats;
286 }
287 }
288
289stats:
290
291#define WQ_HYSTERIS_FACTOR 2
292
293 /* we yielded, check whether granularity should be reduced */
294 if (yielded && (cycles < wq->cycles.granularity))
295 {
296 wq->cycles.granularity = ((cycles > 0) ? cycles
297 : WORK_QUEUE_MIN_GRANULARITY);
298 }
299
paul84369682005-04-27 12:39:27 +0000300 if (cycles >= (wq->cycles.granularity))
paul354d1192005-04-25 16:26:42 +0000301 {
302 if (cycles > wq->cycles.best)
303 wq->cycles.best = cycles;
304
305 /* along with yielded check, provides hysteris for granularity */
306 if (cycles > (wq->cycles.granularity * WQ_HYSTERIS_FACTOR))
307 wq->cycles.granularity += WQ_HYSTERIS_FACTOR;
308 }
309#undef WQ_HYSTERIS_FACTOR
310
311 wq->runs++;
312 wq->cycles.total += cycles;
313
314#if 0
315 printf ("%s: cycles %d, new: best %d, worst %d\n",
316 __func__, cycles, wq->cycles.best, wq->cycles.granularity);
317#endif
318
319 /* Is the queue done yet? */
320 if (listcount (wq->items) > 0)
321 wq->thread = thread_add_background (wq->master, work_queue_run, wq,
322 wq->spec.delay);
323
324 return 0;
325}