paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 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; |
Denis Ovsienko | 6ce80bd | 2007-11-12 14:55:01 +0000 | [diff] [blame] | 69 | SET_FLAG (new->flags, WQ_UNPLUGGED); |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 70 | |
| 71 | if ( (new->items = list_new ()) == NULL) |
| 72 | { |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 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; |
paul | 190880d | 2005-11-14 12:07:47 +0000 | [diff] [blame] | 84 | |
| 85 | /* Default values, can be overriden by caller */ |
paul | 190880d | 2005-11-14 12:07:47 +0000 | [diff] [blame] | 86 | new->spec.hold = WORK_QUEUE_DEFAULT_HOLD; |
paul | 190880d | 2005-11-14 12:07:47 +0000 | [diff] [blame] | 87 | |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 88 | return new; |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | work_queue_free (struct work_queue *wq) |
| 93 | { |
| 94 | /* list_delete frees items via callback */ |
| 95 | list_delete (wq->items); |
| 96 | listnode_delete (&work_queues, wq); |
| 97 | |
| 98 | XFREE (MTYPE_WORK_QUEUE_NAME, wq->name); |
| 99 | XFREE (MTYPE_WORK_QUEUE, wq); |
| 100 | return; |
| 101 | } |
| 102 | |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 103 | static inline int |
| 104 | work_queue_schedule (struct work_queue *wq, unsigned int delay) |
| 105 | { |
| 106 | /* if appropriate, schedule work queue thread */ |
Denis Ovsienko | 6ce80bd | 2007-11-12 14:55:01 +0000 | [diff] [blame] | 107 | if ( CHECK_FLAG (wq->flags, WQ_UNPLUGGED) |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 108 | && (wq->thread == NULL) |
| 109 | && (listcount (wq->items) > 0) ) |
| 110 | { |
| 111 | wq->thread = thread_add_background (wq->master, work_queue_run, |
| 112 | wq, delay); |
| 113 | return 1; |
| 114 | } |
| 115 | else |
| 116 | return 0; |
| 117 | } |
| 118 | |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 119 | void |
| 120 | work_queue_add (struct work_queue *wq, void *data) |
| 121 | { |
| 122 | struct work_queue_item *item; |
| 123 | |
| 124 | assert (wq); |
| 125 | |
| 126 | if (!(item = work_queue_item_new (wq))) |
| 127 | { |
| 128 | zlog_err ("%s: unable to get new queue item", __func__); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | item->data = data; |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 133 | listnode_add (wq->items, item); |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 134 | |
paul | 306d889 | 2006-02-02 17:50:19 +0000 | [diff] [blame] | 135 | work_queue_schedule (wq, wq->spec.hold); |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 136 | |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | static void |
| 141 | work_queue_item_remove (struct work_queue *wq, struct listnode *ln) |
| 142 | { |
| 143 | struct work_queue_item *item = listgetdata (ln); |
| 144 | |
| 145 | assert (item && item->data); |
| 146 | |
| 147 | /* call private data deletion callback if needed */ |
| 148 | if (wq->spec.del_item_data) |
paul | 889e931 | 2005-11-14 14:46:35 +0000 | [diff] [blame] | 149 | wq->spec.del_item_data (wq, item->data); |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 150 | |
| 151 | list_delete_node (wq->items, ln); |
| 152 | work_queue_item_free (item); |
| 153 | |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | static void |
| 158 | work_queue_item_requeue (struct work_queue *wq, struct listnode *ln) |
| 159 | { |
| 160 | LISTNODE_DETACH (wq->items, ln); |
| 161 | LISTNODE_ATTACH (wq->items, ln); /* attach to end of list */ |
| 162 | } |
| 163 | |
| 164 | DEFUN(show_work_queues, |
| 165 | show_work_queues_cmd, |
| 166 | "show work-queues", |
| 167 | SHOW_STR |
| 168 | "Work Queue information\n") |
| 169 | { |
| 170 | struct listnode *node; |
| 171 | struct work_queue *wq; |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 172 | |
| 173 | vty_out (vty, |
paul | 306d889 | 2006-02-02 17:50:19 +0000 | [diff] [blame] | 174 | "%c %8s %5s %8s %21s%s", |
| 175 | ' ', "List","(ms) ","Q. Runs","Cycle Counts ", |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 176 | VTY_NEWLINE); |
| 177 | vty_out (vty, |
paul | 306d889 | 2006-02-02 17:50:19 +0000 | [diff] [blame] | 178 | "%c %8s %5s %8s %7s %6s %6s %s%s", |
| 179 | 'P', |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 180 | "Items", |
paul | 306d889 | 2006-02-02 17:50:19 +0000 | [diff] [blame] | 181 | "Hold", |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 182 | "Total", |
| 183 | "Best","Gran.","Avg.", |
| 184 | "Name", |
| 185 | VTY_NEWLINE); |
| 186 | |
| 187 | for (ALL_LIST_ELEMENTS_RO ((&work_queues), node, wq)) |
| 188 | { |
paul | 306d889 | 2006-02-02 17:50:19 +0000 | [diff] [blame] | 189 | vty_out (vty,"%c %8d %5d %8ld %7d %6d %6u %s%s", |
Denis Ovsienko | 6ce80bd | 2007-11-12 14:55:01 +0000 | [diff] [blame] | 190 | (CHECK_FLAG (wq->flags, WQ_UNPLUGGED) ? ' ' : 'P'), |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 191 | listcount (wq->items), |
paul | 306d889 | 2006-02-02 17:50:19 +0000 | [diff] [blame] | 192 | wq->spec.hold, |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 193 | wq->runs, |
paul | 8436968 | 2005-04-27 12:39:27 +0000 | [diff] [blame] | 194 | wq->cycles.best, wq->cycles.granularity, |
| 195 | (wq->runs) ? |
| 196 | (unsigned int) (wq->cycles.total / wq->runs) : 0, |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 197 | wq->name, |
| 198 | VTY_NEWLINE); |
| 199 | } |
| 200 | |
| 201 | return CMD_SUCCESS; |
| 202 | } |
| 203 | |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 204 | /* 'plug' a queue: Stop it from being scheduled, |
| 205 | * ie: prevent the queue from draining. |
| 206 | */ |
| 207 | void |
| 208 | work_queue_plug (struct work_queue *wq) |
| 209 | { |
| 210 | if (wq->thread) |
| 211 | thread_cancel (wq->thread); |
| 212 | |
| 213 | wq->thread = NULL; |
| 214 | |
Denis Ovsienko | 6ce80bd | 2007-11-12 14:55:01 +0000 | [diff] [blame] | 215 | UNSET_FLAG (wq->flags, WQ_UNPLUGGED); |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* unplug queue, schedule it again, if appropriate |
| 219 | * Ie: Allow the queue to be drained again |
| 220 | */ |
| 221 | void |
| 222 | work_queue_unplug (struct work_queue *wq) |
| 223 | { |
Denis Ovsienko | 6ce80bd | 2007-11-12 14:55:01 +0000 | [diff] [blame] | 224 | SET_FLAG (wq->flags, WQ_UNPLUGGED); |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 225 | |
| 226 | /* if thread isnt already waiting, add one */ |
paul | 306d889 | 2006-02-02 17:50:19 +0000 | [diff] [blame] | 227 | work_queue_schedule (wq, wq->spec.hold); |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 228 | } |
| 229 | |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 230 | /* timer thread to process a work queue |
| 231 | * will reschedule itself if required, |
| 232 | * otherwise work_queue_item_add |
| 233 | */ |
| 234 | int |
| 235 | work_queue_run (struct thread *thread) |
| 236 | { |
| 237 | struct work_queue *wq; |
| 238 | struct work_queue_item *item; |
| 239 | wq_item_status ret; |
| 240 | unsigned int cycles = 0; |
| 241 | struct listnode *node, *nnode; |
| 242 | char yielded = 0; |
| 243 | |
| 244 | wq = THREAD_ARG (thread); |
| 245 | wq->thread = NULL; |
| 246 | |
| 247 | assert (wq && wq->items); |
| 248 | |
| 249 | /* calculate cycle granularity: |
| 250 | * list iteration == 1 cycle |
| 251 | * granularity == # cycles between checks whether we should yield. |
| 252 | * |
| 253 | * granularity should be > 0, and can increase slowly after each run to |
| 254 | * provide some hysteris, but not past cycles.best or 2*cycles. |
| 255 | * |
| 256 | * Best: starts low, can only increase |
| 257 | * |
Paul Jakma | 213d8da | 2006-03-30 14:45:47 +0000 | [diff] [blame] | 258 | * Granularity: starts at WORK_QUEUE_MIN_GRANULARITY, can be decreased |
| 259 | * if we run to end of time slot, can increase otherwise |
| 260 | * by a small factor. |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 261 | * |
| 262 | * We could use just the average and save some work, however we want to be |
| 263 | * able to adjust quickly to CPU pressure. Average wont shift much if |
| 264 | * daemon has been running a long time. |
| 265 | */ |
| 266 | if (wq->cycles.granularity == 0) |
| 267 | wq->cycles.granularity = WORK_QUEUE_MIN_GRANULARITY; |
| 268 | |
| 269 | for (ALL_LIST_ELEMENTS (wq->items, node, nnode, item)) |
| 270 | { |
| 271 | assert (item && item->data); |
| 272 | |
| 273 | /* dont run items which are past their allowed retries */ |
paul | 8436968 | 2005-04-27 12:39:27 +0000 | [diff] [blame] | 274 | if (item->ran > wq->spec.max_retries) |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 275 | { |
| 276 | /* run error handler, if any */ |
| 277 | if (wq->spec.errorfunc) |
| 278 | wq->spec.errorfunc (wq, item->data); |
| 279 | work_queue_item_remove (wq, node); |
| 280 | continue; |
| 281 | } |
| 282 | |
| 283 | /* run and take care of items that want to be retried immediately */ |
| 284 | do |
| 285 | { |
paul | 889e931 | 2005-11-14 14:46:35 +0000 | [diff] [blame] | 286 | ret = wq->spec.workfunc (wq, item->data); |
paul | 8436968 | 2005-04-27 12:39:27 +0000 | [diff] [blame] | 287 | item->ran++; |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 288 | } |
| 289 | while ((ret == WQ_RETRY_NOW) |
paul | 8436968 | 2005-04-27 12:39:27 +0000 | [diff] [blame] | 290 | && (item->ran < wq->spec.max_retries)); |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 291 | |
| 292 | switch (ret) |
| 293 | { |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 294 | case WQ_QUEUE_BLOCKED: |
| 295 | { |
| 296 | /* decrement item->ran again, cause this isn't an item |
| 297 | * specific error, and fall through to WQ_RETRY_LATER |
| 298 | */ |
| 299 | item->ran--; |
| 300 | } |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 301 | case WQ_RETRY_LATER: |
| 302 | { |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 303 | goto stats; |
| 304 | } |
| 305 | case WQ_REQUEUE: |
| 306 | { |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 307 | item->ran--; |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 308 | work_queue_item_requeue (wq, node); |
| 309 | break; |
| 310 | } |
| 311 | case WQ_RETRY_NOW: |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 312 | /* a RETRY_NOW that gets here has exceeded max_tries, same as ERROR */ |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 313 | case WQ_ERROR: |
| 314 | { |
| 315 | if (wq->spec.errorfunc) |
| 316 | wq->spec.errorfunc (wq, item); |
| 317 | } |
| 318 | /* fall through here is deliberate */ |
| 319 | case WQ_SUCCESS: |
| 320 | default: |
| 321 | { |
| 322 | work_queue_item_remove (wq, node); |
| 323 | break; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /* completed cycle */ |
| 328 | cycles++; |
| 329 | |
| 330 | /* test if we should yield */ |
| 331 | if ( !(cycles % wq->cycles.granularity) |
| 332 | && thread_should_yield (thread)) |
| 333 | { |
| 334 | yielded = 1; |
| 335 | goto stats; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | stats: |
| 340 | |
| 341 | #define WQ_HYSTERIS_FACTOR 2 |
| 342 | |
| 343 | /* we yielded, check whether granularity should be reduced */ |
| 344 | if (yielded && (cycles < wq->cycles.granularity)) |
| 345 | { |
| 346 | wq->cycles.granularity = ((cycles > 0) ? cycles |
| 347 | : WORK_QUEUE_MIN_GRANULARITY); |
| 348 | } |
| 349 | |
paul | 8436968 | 2005-04-27 12:39:27 +0000 | [diff] [blame] | 350 | if (cycles >= (wq->cycles.granularity)) |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 351 | { |
| 352 | if (cycles > wq->cycles.best) |
| 353 | wq->cycles.best = cycles; |
| 354 | |
| 355 | /* along with yielded check, provides hysteris for granularity */ |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 356 | if (cycles > (wq->cycles.granularity * WQ_HYSTERIS_FACTOR * 2)) |
| 357 | wq->cycles.granularity *= WQ_HYSTERIS_FACTOR; /* quick ramp-up */ |
| 358 | else if (cycles > (wq->cycles.granularity * WQ_HYSTERIS_FACTOR)) |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 359 | wq->cycles.granularity += WQ_HYSTERIS_FACTOR; |
| 360 | } |
| 361 | #undef WQ_HYSTERIS_FACTOR |
| 362 | |
| 363 | wq->runs++; |
| 364 | wq->cycles.total += cycles; |
| 365 | |
| 366 | #if 0 |
| 367 | printf ("%s: cycles %d, new: best %d, worst %d\n", |
| 368 | __func__, cycles, wq->cycles.best, wq->cycles.granularity); |
| 369 | #endif |
| 370 | |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 371 | /* Is the queue done yet? If it is, call the completion callback. */ |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 372 | if (listcount (wq->items) > 0) |
paul | 306d889 | 2006-02-02 17:50:19 +0000 | [diff] [blame] | 373 | work_queue_schedule (wq, 0); |
| 374 | else if (wq->spec.completion_func) |
| 375 | wq->spec.completion_func (wq); |
paul | 269d74f | 2005-05-23 13:42:46 +0000 | [diff] [blame] | 376 | |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 377 | return 0; |
| 378 | } |