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 along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "workqueue.h"
31 DEFINE_MTYPE(LIB
, WORK_QUEUE
, "Work queue")
32 DEFINE_MTYPE_STATIC(LIB
, WORK_QUEUE_ITEM
, "Work queue item")
33 DEFINE_MTYPE_STATIC(LIB
, WORK_QUEUE_NAME
, "Work queue name string")
35 /* master list of work_queues */
36 static struct list _work_queues
;
37 /* pointer primarily to avoid an otherwise harmless warning on
38 * ALL_LIST_ELEMENTS_RO
40 static struct list
*work_queues
= &_work_queues
;
42 #define WORK_QUEUE_MIN_GRANULARITY 1
44 static struct work_queue_item
*work_queue_item_new(struct work_queue
*wq
)
46 struct work_queue_item
*item
;
49 item
= XCALLOC(MTYPE_WORK_QUEUE_ITEM
, sizeof(struct work_queue_item
));
54 static void work_queue_item_free(struct work_queue_item
*item
)
56 XFREE(MTYPE_WORK_QUEUE_ITEM
, item
);
60 static void work_queue_item_remove(struct work_queue
*wq
,
61 struct work_queue_item
*item
)
63 assert(item
&& item
->data
);
65 /* call private data deletion callback if needed */
66 if (wq
->spec
.del_item_data
)
67 wq
->spec
.del_item_data(wq
, item
->data
);
69 work_queue_item_dequeue(wq
, item
);
71 work_queue_item_free(item
);
76 /* create new work queue */
77 struct work_queue
*work_queue_new(struct thread_master
*m
,
78 const char *queue_name
)
80 struct work_queue
*new;
82 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct work_queue
));
84 new->name
= XSTRDUP(MTYPE_WORK_QUEUE_NAME
, queue_name
);
86 SET_FLAG(new->flags
, WQ_UNPLUGGED
);
88 STAILQ_INIT(&new->items
);
90 listnode_add(work_queues
, new);
92 new->cycles
.granularity
= WORK_QUEUE_MIN_GRANULARITY
;
94 /* Default values, can be overridden by caller */
95 new->spec
.hold
= WORK_QUEUE_DEFAULT_HOLD
;
96 new->spec
.yield
= THREAD_YIELD_TIME_SLOT
;
97 new->spec
.retry
= WORK_QUEUE_DEFAULT_RETRY
;
102 void work_queue_free_and_null(struct work_queue
**wqp
)
104 struct work_queue
*wq
= *wqp
;
106 if (wq
->thread
!= NULL
)
107 thread_cancel(wq
->thread
);
109 while (!work_queue_empty(wq
)) {
110 struct work_queue_item
*item
= work_queue_last_item(wq
);
112 work_queue_item_remove(wq
, item
);
115 listnode_delete(work_queues
, wq
);
117 XFREE(MTYPE_WORK_QUEUE_NAME
, wq
->name
);
118 XFREE(MTYPE_WORK_QUEUE
, wq
);
123 bool work_queue_is_scheduled(struct work_queue
*wq
)
125 return (wq
->thread
!= NULL
);
128 static int work_queue_schedule(struct work_queue
*wq
, unsigned int delay
)
130 /* if appropriate, schedule work queue thread */
131 if (CHECK_FLAG(wq
->flags
, WQ_UNPLUGGED
) && (wq
->thread
== NULL
)
132 && !work_queue_empty(wq
)) {
135 /* Schedule timer if there's a delay, otherwise just schedule
139 thread_add_timer_msec(wq
->master
, work_queue_run
, wq
,
142 thread_add_event(wq
->master
, work_queue_run
, wq
, 0,
145 /* set thread yield time, if needed */
146 if (wq
->thread
&& wq
->spec
.yield
!= THREAD_YIELD_TIME_SLOT
)
147 thread_set_yield_time(wq
->thread
, wq
->spec
.yield
);
153 void work_queue_add(struct work_queue
*wq
, void *data
)
155 struct work_queue_item
*item
;
159 item
= work_queue_item_new(wq
);
162 work_queue_item_enqueue(wq
, item
);
164 work_queue_schedule(wq
, wq
->spec
.hold
);
169 static void work_queue_item_requeue(struct work_queue
*wq
,
170 struct work_queue_item
*item
)
172 work_queue_item_dequeue(wq
, item
);
174 /* attach to end of list */
175 work_queue_item_enqueue(wq
, item
);
178 DEFUN (show_work_queues
,
179 show_work_queues_cmd
,
182 "Work Queue information\n")
184 struct listnode
*node
;
185 struct work_queue
*wq
;
187 vty_out(vty
, "%c %8s %5s %8s %8s %21s\n", ' ', "List", "(ms) ",
188 "Q. Runs", "Yields", "Cycle Counts ");
189 vty_out(vty
, "%c %8s %5s %8s %8s %7s %6s %8s %6s %s\n", 'P', "Items",
190 "Hold", "Total", "Total", "Best", "Gran.", "Total", "Avg.",
193 for (ALL_LIST_ELEMENTS_RO(work_queues
, node
, wq
)) {
194 vty_out(vty
, "%c %8d %5d %8ld %8ld %7d %6d %8ld %6u %s\n",
195 (CHECK_FLAG(wq
->flags
, WQ_UNPLUGGED
) ? ' ' : 'P'),
196 work_queue_item_count(wq
), wq
->spec
.hold
, wq
->runs
,
197 wq
->yields
, wq
->cycles
.best
, wq
->cycles
.granularity
,
199 (wq
->runs
) ? (unsigned int)(wq
->cycles
.total
/ wq
->runs
)
207 void workqueue_cmd_init(void)
209 install_element(VIEW_NODE
, &show_work_queues_cmd
);
212 /* 'plug' a queue: Stop it from being scheduled,
213 * ie: prevent the queue from draining.
215 void work_queue_plug(struct work_queue
*wq
)
218 thread_cancel(wq
->thread
);
222 UNSET_FLAG(wq
->flags
, WQ_UNPLUGGED
);
225 /* unplug queue, schedule it again, if appropriate
226 * Ie: Allow the queue to be drained again
228 void work_queue_unplug(struct work_queue
*wq
)
230 SET_FLAG(wq
->flags
, WQ_UNPLUGGED
);
232 /* if thread isnt already waiting, add one */
233 work_queue_schedule(wq
, wq
->spec
.hold
);
236 /* timer thread to process a work queue
237 * will reschedule itself if required,
238 * otherwise work_queue_item_add
240 int work_queue_run(struct thread
*thread
)
242 struct work_queue
*wq
;
243 struct work_queue_item
*item
, *titem
;
244 wq_item_status ret
= WQ_SUCCESS
;
245 unsigned int cycles
= 0;
248 wq
= THREAD_ARG(thread
);
254 /* calculate cycle granularity:
255 * list iteration == 1 run
256 * listnode processing == 1 cycle
257 * granularity == # cycles between checks whether we should yield.
259 * granularity should be > 0, and can increase slowly after each run to
260 * provide some hysteris, but not past cycles.best or 2*cycles.
262 * Best: starts low, can only increase
264 * Granularity: starts at WORK_QUEUE_MIN_GRANULARITY, can be decreased
265 * if we run to end of time slot, can increase otherwise
268 * We could use just the average and save some work, however we want to
270 * able to adjust quickly to CPU pressure. Average wont shift much if
271 * daemon has been running a long time.
273 if (wq
->cycles
.granularity
== 0)
274 wq
->cycles
.granularity
= WORK_QUEUE_MIN_GRANULARITY
;
276 STAILQ_FOREACH_SAFE (item
, &wq
->items
, wq
, titem
) {
279 /* dont run items which are past their allowed retries */
280 if (item
->ran
> wq
->spec
.max_retries
) {
281 /* run error handler, if any */
282 if (wq
->spec
.errorfunc
)
283 wq
->spec
.errorfunc(wq
, item
->data
);
284 work_queue_item_remove(wq
, item
);
288 /* run and take care of items that want to be retried
291 ret
= wq
->spec
.workfunc(wq
, item
->data
);
293 } while ((ret
== WQ_RETRY_NOW
)
294 && (item
->ran
< wq
->spec
.max_retries
));
297 case WQ_QUEUE_BLOCKED
: {
298 /* decrement item->ran again, cause this isn't an item
299 * specific error, and fall through to WQ_RETRY_LATER
303 case WQ_RETRY_LATER
: {
308 work_queue_item_requeue(wq
, item
);
309 /* If a single node is being used with a meta-queue
311 * update the next node as we don't want to exit the
313 * reschedule it after every node. By definition,
315 * meant to continue the processing; the yield logic
317 * to terminate the thread when time has exceeded.
324 /* a RETRY_NOW that gets here has exceeded max_tries, same as
327 if (wq
->spec
.errorfunc
)
328 wq
->spec
.errorfunc(wq
, item
);
333 work_queue_item_remove(wq
, item
);
338 /* completed cycle */
341 /* test if we should yield */
342 if (!(cycles
% wq
->cycles
.granularity
)
343 && thread_should_yield(thread
)) {
351 #define WQ_HYSTERESIS_FACTOR 4
353 /* we yielded, check whether granularity should be reduced */
354 if (yielded
&& (cycles
< wq
->cycles
.granularity
)) {
355 wq
->cycles
.granularity
=
356 ((cycles
> 0) ? cycles
: WORK_QUEUE_MIN_GRANULARITY
);
358 /* otherwise, should granularity increase? */
359 else if (cycles
>= (wq
->cycles
.granularity
)) {
360 if (cycles
> wq
->cycles
.best
)
361 wq
->cycles
.best
= cycles
;
363 /* along with yielded check, provides hysteresis for granularity
365 if (cycles
> (wq
->cycles
.granularity
* WQ_HYSTERESIS_FACTOR
366 * WQ_HYSTERESIS_FACTOR
))
367 wq
->cycles
.granularity
*=
368 WQ_HYSTERESIS_FACTOR
; /* quick ramp-up */
370 > (wq
->cycles
.granularity
* WQ_HYSTERESIS_FACTOR
))
371 wq
->cycles
.granularity
+= WQ_HYSTERESIS_FACTOR
;
373 #undef WQ_HYSTERIS_FACTOR
376 wq
->cycles
.total
+= cycles
;
381 printf ("%s: cycles %d, new: best %d, worst %d\n",
382 __func__
, cycles
, wq
->cycles
.best
, wq
->cycles
.granularity
);
385 /* Is the queue done yet? If it is, call the completion callback. */
386 if (!work_queue_empty(wq
)) {
387 if (ret
== WQ_RETRY_LATER
||
388 ret
== WQ_QUEUE_BLOCKED
)
389 work_queue_schedule(wq
, wq
->spec
.retry
);
391 work_queue_schedule(wq
, 0);
393 } else if (wq
->spec
.completion_func
)
394 wq
->spec
.completion_func(wq
);