]>
Commit | Line | Data |
---|---|---|
354d119a | 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 | |
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 */ | |
33 | static struct list work_queues; | |
34 | ||
35 | #define WORK_QUEUE_MIN_GRANULARITY 1 | |
36 | ||
37 | static struct work_queue_item * | |
38 | work_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 | ||
49 | static void | |
50 | work_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 */ | |
57 | struct work_queue * | |
58 | work_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; | |
6ce80bdb | 69 | SET_FLAG (new->flags, WQ_UNPLUGGED); |
354d119a | 70 | |
71 | if ( (new->items = list_new ()) == NULL) | |
72 | { | |
354d119a | 73 | XFREE (MTYPE_WORK_QUEUE_NAME, new->name); |
74 | XFREE (MTYPE_WORK_QUEUE, new); | |
75 | ||
76 | return NULL; | |
77 | } | |
78 | ||
79 | new->items->del = (void (*)(void *)) work_queue_item_free; | |
80 | ||
81 | listnode_add (&work_queues, new); | |
82 | ||
83 | new->cycles.granularity = WORK_QUEUE_MIN_GRANULARITY; | |
190880dc | 84 | |
85 | /* Default values, can be overriden by caller */ | |
190880dc | 86 | new->spec.hold = WORK_QUEUE_DEFAULT_HOLD; |
50596be0 | 87 | new->spec.yield = THREAD_YIELD_TIME_SLOT; |
190880dc | 88 | |
354d119a | 89 | return new; |
90 | } | |
91 | ||
92 | void | |
93 | work_queue_free (struct work_queue *wq) | |
94 | { | |
acde4b86 SH |
95 | if (wq->thread != NULL) |
96 | thread_cancel(wq->thread); | |
97 | ||
354d119a | 98 | /* list_delete frees items via callback */ |
99 | list_delete (wq->items); | |
100 | listnode_delete (&work_queues, wq); | |
101 | ||
102 | XFREE (MTYPE_WORK_QUEUE_NAME, wq->name); | |
103 | XFREE (MTYPE_WORK_QUEUE, wq); | |
104 | return; | |
105 | } | |
106 | ||
f63f06da | 107 | static int |
269d74fd | 108 | work_queue_schedule (struct work_queue *wq, unsigned int delay) |
109 | { | |
110 | /* if appropriate, schedule work queue thread */ | |
6ce80bdb | 111 | if ( CHECK_FLAG (wq->flags, WQ_UNPLUGGED) |
269d74fd | 112 | && (wq->thread == NULL) |
113 | && (listcount (wq->items) > 0) ) | |
114 | { | |
115 | wq->thread = thread_add_background (wq->master, work_queue_run, | |
116 | wq, delay); | |
50596be0 DS |
117 | /* set thread yield time, if needed */ |
118 | if (wq->thread && wq->spec.yield != THREAD_YIELD_TIME_SLOT) | |
119 | thread_set_yield_time (wq->thread, wq->spec.yield); | |
269d74fd | 120 | return 1; |
121 | } | |
122 | else | |
123 | return 0; | |
124 | } | |
125 | ||
354d119a | 126 | void |
127 | work_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; | |
e96f9203 | 140 | listnode_add (wq->items, item); |
354d119a | 141 | |
306d8890 | 142 | work_queue_schedule (wq, wq->spec.hold); |
354d119a | 143 | |
144 | return; | |
145 | } | |
146 | ||
147 | static void | |
148 | work_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) | |
889e9311 | 156 | wq->spec.del_item_data (wq, item->data); |
354d119a | 157 | |
158 | list_delete_node (wq->items, ln); | |
159 | work_queue_item_free (item); | |
160 | ||
161 | return; | |
162 | } | |
163 | ||
164 | static void | |
165 | work_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 | ||
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; | |
354d119a | 179 | |
180 | vty_out (vty, | |
50596be0 DS |
181 | "%c %8s %5s %8s %8s %21s%s", |
182 | ' ', "List","(ms) ","Q. Runs","Yields","Cycle Counts ", | |
354d119a | 183 | VTY_NEWLINE); |
184 | vty_out (vty, | |
50596be0 | 185 | "%c %8s %5s %8s %8s %7s %6s %8s %6s %s%s", |
306d8890 | 186 | 'P', |
354d119a | 187 | "Items", |
306d8890 | 188 | "Hold", |
50596be0 DS |
189 | "Total","Total", |
190 | "Best","Gran.","Total","Avg.", | |
354d119a | 191 | "Name", |
192 | VTY_NEWLINE); | |
193 | ||
194 | for (ALL_LIST_ELEMENTS_RO ((&work_queues), node, wq)) | |
195 | { | |
50596be0 | 196 | vty_out (vty,"%c %8d %5d %8ld %8ld %7d %6d %8ld %6u %s%s", |
6ce80bdb | 197 | (CHECK_FLAG (wq->flags, WQ_UNPLUGGED) ? ' ' : 'P'), |
354d119a | 198 | listcount (wq->items), |
306d8890 | 199 | wq->spec.hold, |
50596be0 DS |
200 | wq->runs, wq->yields, |
201 | wq->cycles.best, wq->cycles.granularity, wq->cycles.total, | |
84369684 | 202 | (wq->runs) ? |
203 | (unsigned int) (wq->cycles.total / wq->runs) : 0, | |
354d119a | 204 | wq->name, |
205 | VTY_NEWLINE); | |
206 | } | |
207 | ||
208 | return CMD_SUCCESS; | |
209 | } | |
210 | ||
269d74fd | 211 | /* 'plug' a queue: Stop it from being scheduled, |
212 | * ie: prevent the queue from draining. | |
213 | */ | |
214 | void | |
215 | work_queue_plug (struct work_queue *wq) | |
216 | { | |
217 | if (wq->thread) | |
218 | thread_cancel (wq->thread); | |
219 | ||
220 | wq->thread = NULL; | |
221 | ||
6ce80bdb | 222 | UNSET_FLAG (wq->flags, WQ_UNPLUGGED); |
269d74fd | 223 | } |
224 | ||
225 | /* unplug queue, schedule it again, if appropriate | |
226 | * Ie: Allow the queue to be drained again | |
227 | */ | |
228 | void | |
229 | work_queue_unplug (struct work_queue *wq) | |
230 | { | |
6ce80bdb | 231 | SET_FLAG (wq->flags, WQ_UNPLUGGED); |
269d74fd | 232 | |
233 | /* if thread isnt already waiting, add one */ | |
306d8890 | 234 | work_queue_schedule (wq, wq->spec.hold); |
269d74fd | 235 | } |
236 | ||
354d119a | 237 | /* timer thread to process a work queue |
238 | * will reschedule itself if required, | |
239 | * otherwise work_queue_item_add | |
240 | */ | |
241 | int | |
242 | work_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: | |
50596be0 DS |
257 | * list iteration == 1 run |
258 | * listnode processing == 1 cycle | |
354d119a | 259 | * granularity == # cycles between checks whether we should yield. |
260 | * | |
261 | * granularity should be > 0, and can increase slowly after each run to | |
262 | * provide some hysteris, but not past cycles.best or 2*cycles. | |
263 | * | |
264 | * Best: starts low, can only increase | |
265 | * | |
213d8dad PJ |
266 | * Granularity: starts at WORK_QUEUE_MIN_GRANULARITY, can be decreased |
267 | * if we run to end of time slot, can increase otherwise | |
268 | * by a small factor. | |
354d119a | 269 | * |
270 | * We could use just the average and save some work, however we want to be | |
271 | * able to adjust quickly to CPU pressure. Average wont shift much if | |
272 | * daemon has been running a long time. | |
273 | */ | |
274 | if (wq->cycles.granularity == 0) | |
275 | wq->cycles.granularity = WORK_QUEUE_MIN_GRANULARITY; | |
276 | ||
277 | for (ALL_LIST_ELEMENTS (wq->items, node, nnode, item)) | |
278 | { | |
279 | assert (item && item->data); | |
280 | ||
281 | /* dont run items which are past their allowed retries */ | |
84369684 | 282 | if (item->ran > wq->spec.max_retries) |
354d119a | 283 | { |
284 | /* run error handler, if any */ | |
285 | if (wq->spec.errorfunc) | |
286 | wq->spec.errorfunc (wq, item->data); | |
287 | work_queue_item_remove (wq, node); | |
288 | continue; | |
289 | } | |
290 | ||
291 | /* run and take care of items that want to be retried immediately */ | |
292 | do | |
293 | { | |
889e9311 | 294 | ret = wq->spec.workfunc (wq, item->data); |
84369684 | 295 | item->ran++; |
354d119a | 296 | } |
297 | while ((ret == WQ_RETRY_NOW) | |
84369684 | 298 | && (item->ran < wq->spec.max_retries)); |
354d119a | 299 | |
300 | switch (ret) | |
301 | { | |
269d74fd | 302 | case WQ_QUEUE_BLOCKED: |
303 | { | |
304 | /* decrement item->ran again, cause this isn't an item | |
305 | * specific error, and fall through to WQ_RETRY_LATER | |
306 | */ | |
307 | item->ran--; | |
308 | } | |
354d119a | 309 | case WQ_RETRY_LATER: |
310 | { | |
354d119a | 311 | goto stats; |
312 | } | |
313 | case WQ_REQUEUE: | |
314 | { | |
e96f9203 | 315 | item->ran--; |
354d119a | 316 | work_queue_item_requeue (wq, node); |
50596be0 DS |
317 | /* If a single node is being used with a meta-queue (e.g., zebra), |
318 | * update the next node as we don't want to exit the thread and | |
319 | * reschedule it after every node. By definition, WQ_REQUEUE is | |
320 | * meant to continue the processing; the yield logic will kick in | |
321 | * to terminate the thread when time has exceeded. | |
322 | */ | |
323 | if (nnode == NULL) | |
324 | nnode = node; | |
354d119a | 325 | break; |
326 | } | |
327 | case WQ_RETRY_NOW: | |
269d74fd | 328 | /* a RETRY_NOW that gets here has exceeded max_tries, same as ERROR */ |
354d119a | 329 | case WQ_ERROR: |
330 | { | |
331 | if (wq->spec.errorfunc) | |
332 | wq->spec.errorfunc (wq, item); | |
333 | } | |
334 | /* fall through here is deliberate */ | |
335 | case WQ_SUCCESS: | |
336 | default: | |
337 | { | |
338 | work_queue_item_remove (wq, node); | |
339 | break; | |
340 | } | |
341 | } | |
342 | ||
343 | /* completed cycle */ | |
344 | cycles++; | |
345 | ||
346 | /* test if we should yield */ | |
347 | if ( !(cycles % wq->cycles.granularity) | |
348 | && thread_should_yield (thread)) | |
349 | { | |
350 | yielded = 1; | |
351 | goto stats; | |
352 | } | |
353 | } | |
354 | ||
355 | stats: | |
356 | ||
3322055b | 357 | #define WQ_HYSTERESIS_FACTOR 4 |
354d119a | 358 | |
359 | /* we yielded, check whether granularity should be reduced */ | |
360 | if (yielded && (cycles < wq->cycles.granularity)) | |
361 | { | |
50596be0 | 362 | wq->cycles.granularity = ((cycles > 0) ? cycles |
354d119a | 363 | : WORK_QUEUE_MIN_GRANULARITY); |
364 | } | |
3322055b PJ |
365 | /* otherwise, should granularity increase? */ |
366 | else if (cycles >= (wq->cycles.granularity)) | |
354d119a | 367 | { |
368 | if (cycles > wq->cycles.best) | |
369 | wq->cycles.best = cycles; | |
50596be0 | 370 | |
3322055b PJ |
371 | /* along with yielded check, provides hysteresis for granularity */ |
372 | if (cycles > (wq->cycles.granularity * WQ_HYSTERESIS_FACTOR | |
373 | * WQ_HYSTERESIS_FACTOR)) | |
374 | wq->cycles.granularity *= WQ_HYSTERESIS_FACTOR; /* quick ramp-up */ | |
375 | else if (cycles > (wq->cycles.granularity * WQ_HYSTERESIS_FACTOR)) | |
376 | wq->cycles.granularity += WQ_HYSTERESIS_FACTOR; | |
354d119a | 377 | } |
378 | #undef WQ_HYSTERIS_FACTOR | |
379 | ||
380 | wq->runs++; | |
381 | wq->cycles.total += cycles; | |
50596be0 DS |
382 | if (yielded) |
383 | wq->yields++; | |
354d119a | 384 | |
385 | #if 0 | |
386 | printf ("%s: cycles %d, new: best %d, worst %d\n", | |
387 | __func__, cycles, wq->cycles.best, wq->cycles.granularity); | |
388 | #endif | |
389 | ||
269d74fd | 390 | /* Is the queue done yet? If it is, call the completion callback. */ |
354d119a | 391 | if (listcount (wq->items) > 0) |
306d8890 | 392 | work_queue_schedule (wq, 0); |
393 | else if (wq->spec.completion_func) | |
394 | wq->spec.completion_func (wq); | |
269d74fd | 395 | |
354d119a | 396 | return 0; |
397 | } |