2 * Quagga Work Queue Support.
4 * Copyright (C) 2005 Sun Microsystems, Inc.
6 * This file is part of GNU Zebra.
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
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.
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
24 #include <lib/zebra.h>
27 #include "workqueue.h"
32 /* master list of work_queues */
33 static struct list work_queues
;
35 #define WORK_QUEUE_MIN_GRANULARITY 1
37 static struct work_queue_item
*
38 work_queue_item_new (struct work_queue
*wq
)
40 struct work_queue_item
*item
;
43 item
= XCALLOC (MTYPE_WORK_QUEUE_ITEM
,
44 sizeof (struct work_queue_item
));
50 work_queue_item_free (struct work_queue_item
*item
)
52 XFREE (MTYPE_WORK_QUEUE_ITEM
, item
);
56 /* create new work queue */
58 work_queue_new (struct thread_master
*m
, const char *queue_name
)
60 struct work_queue
*new;
62 new = XCALLOC (MTYPE_WORK_QUEUE
, sizeof (struct work_queue
));
67 new->name
= XSTRDUP (MTYPE_WORK_QUEUE_NAME
, queue_name
);
70 if ( (new->items
= list_new ()) == NULL
)
72 XFREE (MTYPE_WORK_QUEUE_NAME
, new->name
);
73 XFREE (MTYPE_WORK_QUEUE
, new);
78 new->items
->del
= (void (*)(void *)) work_queue_item_free
;
80 listnode_add (&work_queues
, new);
82 new->cycles
.granularity
= WORK_QUEUE_MIN_GRANULARITY
;
88 work_queue_free (struct work_queue
*wq
)
90 /* list_delete frees items via callback */
91 list_delete (wq
->items
);
92 listnode_delete (&work_queues
, wq
);
94 XFREE (MTYPE_WORK_QUEUE_NAME
, wq
->name
);
95 XFREE (MTYPE_WORK_QUEUE
, wq
);
100 work_queue_schedule (struct work_queue
*wq
, unsigned int delay
)
102 /* if appropriate, schedule work queue thread */
103 if ( (wq
->flags
== WQ_UNPLUGGED
)
104 && (wq
->thread
== NULL
)
105 && (listcount (wq
->items
) > 0) )
107 wq
->thread
= thread_add_background (wq
->master
, work_queue_run
,
116 work_queue_add (struct work_queue
*wq
, void *data
)
118 struct work_queue_item
*item
;
122 if (!(item
= work_queue_item_new (wq
)))
124 zlog_err ("%s: unable to get new queue item", __func__
);
129 listnode_add (wq
->items
, item
);
131 work_queue_schedule (wq
, wq
->spec
.hold
);
137 work_queue_item_remove (struct work_queue
*wq
, struct listnode
*ln
)
139 struct work_queue_item
*item
= listgetdata (ln
);
141 assert (item
&& item
->data
);
143 /* call private data deletion callback if needed */
144 if (wq
->spec
.del_item_data
)
145 wq
->spec
.del_item_data (item
->data
);
147 list_delete_node (wq
->items
, ln
);
148 work_queue_item_free (item
);
154 work_queue_item_requeue (struct work_queue
*wq
, struct listnode
*ln
)
156 LISTNODE_DETACH (wq
->items
, ln
);
157 LISTNODE_ATTACH (wq
->items
, ln
); /* attach to end of list */
160 DEFUN(show_work_queues
,
161 show_work_queues_cmd
,
164 "Work Queue information\n")
166 struct listnode
*node
;
167 struct work_queue
*wq
;
170 "%c %8s %11s %8s %21s%s",
171 ' ', "List","(ms) ","Q. Runs","Cycle Counts ",
174 "%c %8s %5s %5s %8s %7s %6s %6s %s%s",
179 "Best","Gran.","Avg.",
183 for (ALL_LIST_ELEMENTS_RO ((&work_queues
), node
, wq
))
185 vty_out (vty
,"%c %8d %5d %5d %8ld %7d %6d %6u %s%s",
186 (wq
->flags
== WQ_PLUGGED
? 'P' : ' '),
187 listcount (wq
->items
),
188 wq
->spec
.delay
, wq
->spec
.hold
,
190 wq
->cycles
.best
, wq
->cycles
.granularity
,
192 (unsigned int) (wq
->cycles
.total
/ wq
->runs
) : 0,
200 /* 'plug' a queue: Stop it from being scheduled,
201 * ie: prevent the queue from draining.
204 work_queue_plug (struct work_queue
*wq
)
207 thread_cancel (wq
->thread
);
211 wq
->flags
= WQ_PLUGGED
;
214 /* unplug queue, schedule it again, if appropriate
215 * Ie: Allow the queue to be drained again
218 work_queue_unplug (struct work_queue
*wq
)
220 wq
->flags
= WQ_UNPLUGGED
;
222 /* if thread isnt already waiting, add one */
223 work_queue_schedule (wq
, wq
->spec
.hold
);
226 /* timer thread to process a work queue
227 * will reschedule itself if required,
228 * otherwise work_queue_item_add
231 work_queue_run (struct thread
*thread
)
233 struct work_queue
*wq
;
234 struct work_queue_item
*item
;
236 unsigned int cycles
= 0;
237 struct listnode
*node
, *nnode
;
240 wq
= THREAD_ARG (thread
);
243 assert (wq
&& wq
->items
);
245 /* calculate cycle granularity:
246 * list iteration == 1 cycle
247 * granularity == # cycles between checks whether we should yield.
249 * granularity should be > 0, and can increase slowly after each run to
250 * provide some hysteris, but not past cycles.best or 2*cycles.
252 * Best: starts low, can only increase
254 * Granularity: starts at WORK_QUEUE_MIN_GRANULARITY, can be decreased if we run to end of time
255 * slot, can increase otherwise by a small factor.
257 * We could use just the average and save some work, however we want to be
258 * able to adjust quickly to CPU pressure. Average wont shift much if
259 * daemon has been running a long time.
261 if (wq
->cycles
.granularity
== 0)
262 wq
->cycles
.granularity
= WORK_QUEUE_MIN_GRANULARITY
;
264 for (ALL_LIST_ELEMENTS (wq
->items
, node
, nnode
, item
))
266 assert (item
&& item
->data
);
268 /* dont run items which are past their allowed retries */
269 if (item
->ran
> wq
->spec
.max_retries
)
271 /* run error handler, if any */
272 if (wq
->spec
.errorfunc
)
273 wq
->spec
.errorfunc (wq
, item
->data
);
274 work_queue_item_remove (wq
, node
);
278 /* run and take care of items that want to be retried immediately */
281 ret
= wq
->spec
.workfunc (item
->data
);
284 while ((ret
== WQ_RETRY_NOW
)
285 && (item
->ran
< wq
->spec
.max_retries
));
289 case WQ_QUEUE_BLOCKED
:
291 /* decrement item->ran again, cause this isn't an item
292 * specific error, and fall through to WQ_RETRY_LATER
302 work_queue_item_requeue (wq
, node
);
306 /* a RETRY_NOW that gets here has exceeded max_tries, same as ERROR */
309 if (wq
->spec
.errorfunc
)
310 wq
->spec
.errorfunc (wq
, item
);
312 /* fall through here is deliberate */
316 work_queue_item_remove (wq
, node
);
321 /* completed cycle */
324 /* test if we should yield */
325 if ( !(cycles
% wq
->cycles
.granularity
)
326 && thread_should_yield (thread
))
335 #define WQ_HYSTERIS_FACTOR 2
337 /* we yielded, check whether granularity should be reduced */
338 if (yielded
&& (cycles
< wq
->cycles
.granularity
))
340 wq
->cycles
.granularity
= ((cycles
> 0) ? cycles
341 : WORK_QUEUE_MIN_GRANULARITY
);
344 if (cycles
>= (wq
->cycles
.granularity
))
346 if (cycles
> wq
->cycles
.best
)
347 wq
->cycles
.best
= cycles
;
349 /* along with yielded check, provides hysteris for granularity */
350 if (cycles
> (wq
->cycles
.granularity
* WQ_HYSTERIS_FACTOR
* 2))
351 wq
->cycles
.granularity
*= WQ_HYSTERIS_FACTOR
; /* quick ramp-up */
352 else if (cycles
> (wq
->cycles
.granularity
* WQ_HYSTERIS_FACTOR
))
353 wq
->cycles
.granularity
+= WQ_HYSTERIS_FACTOR
;
355 #undef WQ_HYSTERIS_FACTOR
358 wq
->cycles
.total
+= cycles
;
361 printf ("%s: cycles %d, new: best %d, worst %d\n",
362 __func__
, cycles
, wq
->cycles
.best
, wq
->cycles
.granularity
);
365 /* Is the queue done yet? If it is, call the completion callback. */
366 if (listcount (wq
->items
) > 0)
367 work_queue_schedule (wq
, wq
->spec
.delay
);
368 else if (wq
->spec
.completion_func
)
369 wq
->spec
.completion_func (wq
);