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
));
87 new->name
= XSTRDUP(MTYPE_WORK_QUEUE_NAME
, queue_name
);
89 SET_FLAG(new->flags
, WQ_UNPLUGGED
);
91 STAILQ_INIT(&new->items
);
93 listnode_add(work_queues
, new);
95 new->cycles
.granularity
= WORK_QUEUE_MIN_GRANULARITY
;
97 /* Default values, can be overriden by caller */
98 new->spec
.hold
= WORK_QUEUE_DEFAULT_HOLD
;
99 new->spec
.yield
= THREAD_YIELD_TIME_SLOT
;
104 void work_queue_free_original(struct work_queue
*wq
)
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
);
122 void work_queue_free_and_null(struct work_queue
**wq
)
124 work_queue_free_original(*wq
);
128 bool work_queue_is_scheduled(struct work_queue
*wq
)
130 return (wq
->thread
!= NULL
);
133 static int work_queue_schedule(struct work_queue
*wq
, unsigned int delay
)
135 /* if appropriate, schedule work queue thread */
136 if (CHECK_FLAG(wq
->flags
, WQ_UNPLUGGED
) && (wq
->thread
== NULL
)
137 && !work_queue_empty(wq
)) {
139 thread_add_timer_msec(wq
->master
, work_queue_run
, wq
, delay
,
141 /* set thread yield time, if needed */
142 if (wq
->thread
&& wq
->spec
.yield
!= THREAD_YIELD_TIME_SLOT
)
143 thread_set_yield_time(wq
->thread
, wq
->spec
.yield
);
149 void work_queue_add(struct work_queue
*wq
, void *data
)
151 struct work_queue_item
*item
;
155 if (!(item
= work_queue_item_new(wq
))) {
156 zlog_err("%s: unable to get new queue item", __func__
);
161 work_queue_item_enqueue(wq
, item
);
163 work_queue_schedule(wq
, wq
->spec
.hold
);
168 static void work_queue_item_requeue(struct work_queue
*wq
,
169 struct work_queue_item
*item
)
171 work_queue_item_dequeue(wq
, item
);
173 /* attach to end of list */
174 work_queue_item_enqueue(wq
, item
);
177 DEFUN (show_work_queues
,
178 show_work_queues_cmd
,
181 "Work Queue information\n")
183 struct listnode
*node
;
184 struct work_queue
*wq
;
186 vty_out(vty
, "%c %8s %5s %8s %8s %21s\n", ' ', "List", "(ms) ",
187 "Q. Runs", "Yields", "Cycle Counts ");
188 vty_out(vty
, "%c %8s %5s %8s %8s %7s %6s %8s %6s %s\n", 'P', "Items",
189 "Hold", "Total", "Total", "Best", "Gran.", "Total", "Avg.",
192 for (ALL_LIST_ELEMENTS_RO(work_queues
, node
, wq
)) {
193 vty_out(vty
, "%c %8d %5d %8ld %8ld %7d %6d %8ld %6u %s\n",
194 (CHECK_FLAG(wq
->flags
, WQ_UNPLUGGED
) ? ' ' : 'P'),
195 work_queue_item_count(wq
), wq
->spec
.hold
, wq
->runs
,
196 wq
->yields
, wq
->cycles
.best
, wq
->cycles
.granularity
,
198 (wq
->runs
) ? (unsigned int)(wq
->cycles
.total
/ wq
->runs
)
206 void workqueue_cmd_init(void)
208 install_element(VIEW_NODE
, &show_work_queues_cmd
);
211 /* 'plug' a queue: Stop it from being scheduled,
212 * ie: prevent the queue from draining.
214 void work_queue_plug(struct work_queue
*wq
)
217 thread_cancel(wq
->thread
);
221 UNSET_FLAG(wq
->flags
, WQ_UNPLUGGED
);
224 /* unplug queue, schedule it again, if appropriate
225 * Ie: Allow the queue to be drained again
227 void work_queue_unplug(struct work_queue
*wq
)
229 SET_FLAG(wq
->flags
, WQ_UNPLUGGED
);
231 /* if thread isnt already waiting, add one */
232 work_queue_schedule(wq
, wq
->spec
.hold
);
235 /* timer thread to process a work queue
236 * will reschedule itself if required,
237 * otherwise work_queue_item_add
239 int work_queue_run(struct thread
*thread
)
241 struct work_queue
*wq
;
242 struct work_queue_item
*item
, *titem
;
244 unsigned int cycles
= 0;
247 wq
= THREAD_ARG(thread
);
253 /* calculate cycle granularity:
254 * list iteration == 1 run
255 * listnode processing == 1 cycle
256 * granularity == # cycles between checks whether we should yield.
258 * granularity should be > 0, and can increase slowly after each run to
259 * provide some hysteris, but not past cycles.best or 2*cycles.
261 * Best: starts low, can only increase
263 * Granularity: starts at WORK_QUEUE_MIN_GRANULARITY, can be decreased
264 * if we run to end of time slot, can increase otherwise
267 * We could use just the average and save some work, however we want to
269 * able to adjust quickly to CPU pressure. Average wont shift much if
270 * daemon has been running a long time.
272 if (wq
->cycles
.granularity
== 0)
273 wq
->cycles
.granularity
= WORK_QUEUE_MIN_GRANULARITY
;
275 STAILQ_FOREACH_SAFE (item
, &wq
->items
, wq
, titem
) {
276 assert(item
&& item
->data
);
278 /* dont run items which are past their allowed retries */
279 if (item
->ran
> wq
->spec
.max_retries
) {
280 /* run error handler, if any */
281 if (wq
->spec
.errorfunc
)
282 wq
->spec
.errorfunc(wq
, item
->data
);
283 work_queue_item_remove(wq
, item
);
287 /* run and take care of items that want to be retried
290 ret
= wq
->spec
.workfunc(wq
, item
->data
);
292 } while ((ret
== WQ_RETRY_NOW
)
293 && (item
->ran
< wq
->spec
.max_retries
));
296 case WQ_QUEUE_BLOCKED
: {
297 /* decrement item->ran again, cause this isn't an item
298 * specific error, and fall through to WQ_RETRY_LATER
302 case WQ_RETRY_LATER
: {
307 work_queue_item_requeue(wq
, item
);
308 /* If a single node is being used with a meta-queue
310 * update the next node as we don't want to exit the
312 * reschedule it after every node. By definition,
314 * meant to continue the processing; the yield logic
316 * to terminate the thread when time has exceeded.
323 /* a RETRY_NOW that gets here has exceeded max_tries, same as
326 if (wq
->spec
.errorfunc
)
327 wq
->spec
.errorfunc(wq
, item
);
332 work_queue_item_remove(wq
, item
);
337 /* completed cycle */
340 /* test if we should yield */
341 if (!(cycles
% wq
->cycles
.granularity
)
342 && thread_should_yield(thread
)) {
350 #define WQ_HYSTERESIS_FACTOR 4
352 /* we yielded, check whether granularity should be reduced */
353 if (yielded
&& (cycles
< wq
->cycles
.granularity
)) {
354 wq
->cycles
.granularity
=
355 ((cycles
> 0) ? cycles
: WORK_QUEUE_MIN_GRANULARITY
);
357 /* otherwise, should granularity increase? */
358 else if (cycles
>= (wq
->cycles
.granularity
)) {
359 if (cycles
> wq
->cycles
.best
)
360 wq
->cycles
.best
= cycles
;
362 /* along with yielded check, provides hysteresis for granularity
364 if (cycles
> (wq
->cycles
.granularity
* WQ_HYSTERESIS_FACTOR
365 * WQ_HYSTERESIS_FACTOR
))
366 wq
->cycles
.granularity
*=
367 WQ_HYSTERESIS_FACTOR
; /* quick ramp-up */
369 > (wq
->cycles
.granularity
* WQ_HYSTERESIS_FACTOR
))
370 wq
->cycles
.granularity
+= WQ_HYSTERESIS_FACTOR
;
372 #undef WQ_HYSTERIS_FACTOR
375 wq
->cycles
.total
+= cycles
;
380 printf ("%s: cycles %d, new: best %d, worst %d\n",
381 __func__
, cycles
, wq
->cycles
.best
, wq
->cycles
.granularity
);
384 /* Is the queue done yet? If it is, call the completion callback. */
385 if (!work_queue_empty(wq
))
386 work_queue_schedule(wq
, 0);
387 else if (wq
->spec
.completion_func
)
388 wq
->spec
.completion_func(wq
);