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; |
| 69 | |
| 70 | if ( (new->items = list_new ()) == NULL) |
| 71 | { |
| 72 | if (new->items) |
| 73 | list_free (new->items); |
| 74 | |
| 75 | XFREE (MTYPE_WORK_QUEUE_NAME, new->name); |
| 76 | XFREE (MTYPE_WORK_QUEUE, new); |
| 77 | |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | new->items->del = (void (*)(void *)) work_queue_item_free; |
| 82 | |
| 83 | listnode_add (&work_queues, new); |
| 84 | |
| 85 | new->cycles.granularity = WORK_QUEUE_MIN_GRANULARITY; |
| 86 | |
| 87 | return new; |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | work_queue_free (struct work_queue *wq) |
| 92 | { |
| 93 | /* list_delete frees items via callback */ |
| 94 | list_delete (wq->items); |
| 95 | listnode_delete (&work_queues, wq); |
| 96 | |
| 97 | XFREE (MTYPE_WORK_QUEUE_NAME, wq->name); |
| 98 | XFREE (MTYPE_WORK_QUEUE, wq); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | work_queue_add (struct work_queue *wq, void *data) |
| 104 | { |
| 105 | struct work_queue_item *item; |
| 106 | |
| 107 | assert (wq); |
| 108 | |
| 109 | if (!(item = work_queue_item_new (wq))) |
| 110 | { |
| 111 | zlog_err ("%s: unable to get new queue item", __func__); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | item->data = data; |
| 116 | listnode_add (wq->items, item); |
| 117 | |
| 118 | /* if thread isnt already waiting, add one */ |
| 119 | if (wq->thread == NULL) |
| 120 | wq->thread = thread_add_background (wq->master, work_queue_run, |
| 121 | wq, wq->spec.hold); |
| 122 | |
| 123 | /* XXX: what if we didnt get a thread? try again? */ |
| 124 | |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | static void |
| 129 | work_queue_item_remove (struct work_queue *wq, struct listnode *ln) |
| 130 | { |
| 131 | struct work_queue_item *item = listgetdata (ln); |
| 132 | |
| 133 | assert (item && item->data); |
| 134 | |
| 135 | /* call private data deletion callback if needed */ |
| 136 | if (wq->spec.del_item_data) |
| 137 | wq->spec.del_item_data (item->data); |
| 138 | |
| 139 | list_delete_node (wq->items, ln); |
| 140 | work_queue_item_free (item); |
| 141 | |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | static void |
| 146 | work_queue_item_requeue (struct work_queue *wq, struct listnode *ln) |
| 147 | { |
| 148 | LISTNODE_DETACH (wq->items, ln); |
| 149 | LISTNODE_ATTACH (wq->items, ln); /* attach to end of list */ |
| 150 | } |
| 151 | |
| 152 | DEFUN(show_work_queues, |
| 153 | show_work_queues_cmd, |
| 154 | "show work-queues", |
| 155 | SHOW_STR |
| 156 | "Work Queue information\n") |
| 157 | { |
| 158 | struct listnode *node; |
| 159 | struct work_queue *wq; |
| 160 | struct timeval tvnow; |
| 161 | |
| 162 | gettimeofday (&tvnow, NULL); |
| 163 | |
| 164 | vty_out (vty, |
| 165 | "%8s %11s %8s %21s%s", |
paul | 8436968 | 2005-04-27 12:39:27 +0000 | [diff] [blame^] | 166 | "List","(ms) ","Q. Runs","Cycle Counts ", |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 167 | VTY_NEWLINE); |
| 168 | vty_out (vty, |
| 169 | "%8s %5s %5s %8s %7s %6s %6s %s%s", |
| 170 | "Items", |
| 171 | "Delay","Hold", |
| 172 | "Total", |
| 173 | "Best","Gran.","Avg.", |
| 174 | "Name", |
| 175 | VTY_NEWLINE); |
| 176 | |
| 177 | for (ALL_LIST_ELEMENTS_RO ((&work_queues), node, wq)) |
| 178 | { |
paul | 8436968 | 2005-04-27 12:39:27 +0000 | [diff] [blame^] | 179 | vty_out (vty,"%8d %5d %5d %8ld %7d %6d %6u %s%s", |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 180 | listcount (wq->items), |
| 181 | wq->spec.delay, wq->spec.hold, |
| 182 | wq->runs, |
paul | 8436968 | 2005-04-27 12:39:27 +0000 | [diff] [blame^] | 183 | wq->cycles.best, wq->cycles.granularity, |
| 184 | (wq->runs) ? |
| 185 | (unsigned int) (wq->cycles.total / wq->runs) : 0, |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 186 | wq->name, |
| 187 | VTY_NEWLINE); |
| 188 | } |
| 189 | |
| 190 | return CMD_SUCCESS; |
| 191 | } |
| 192 | |
| 193 | /* timer thread to process a work queue |
| 194 | * will reschedule itself if required, |
| 195 | * otherwise work_queue_item_add |
| 196 | */ |
| 197 | int |
| 198 | work_queue_run (struct thread *thread) |
| 199 | { |
| 200 | struct work_queue *wq; |
| 201 | struct work_queue_item *item; |
| 202 | wq_item_status ret; |
| 203 | unsigned int cycles = 0; |
| 204 | struct listnode *node, *nnode; |
| 205 | char yielded = 0; |
| 206 | |
| 207 | wq = THREAD_ARG (thread); |
| 208 | wq->thread = NULL; |
| 209 | |
| 210 | assert (wq && wq->items); |
| 211 | |
| 212 | /* calculate cycle granularity: |
| 213 | * list iteration == 1 cycle |
| 214 | * granularity == # cycles between checks whether we should yield. |
| 215 | * |
| 216 | * granularity should be > 0, and can increase slowly after each run to |
| 217 | * provide some hysteris, but not past cycles.best or 2*cycles. |
| 218 | * |
| 219 | * Best: starts low, can only increase |
| 220 | * |
| 221 | * Granularity: starts at WORK_QUEUE_MIN_GRANULARITY, can be decreased if we run to end of time |
| 222 | * slot, can increase otherwise by a small factor. |
| 223 | * |
| 224 | * We could use just the average and save some work, however we want to be |
| 225 | * able to adjust quickly to CPU pressure. Average wont shift much if |
| 226 | * daemon has been running a long time. |
| 227 | */ |
| 228 | if (wq->cycles.granularity == 0) |
| 229 | wq->cycles.granularity = WORK_QUEUE_MIN_GRANULARITY; |
| 230 | |
| 231 | for (ALL_LIST_ELEMENTS (wq->items, node, nnode, item)) |
| 232 | { |
| 233 | assert (item && item->data); |
| 234 | |
| 235 | /* dont run items which are past their allowed retries */ |
paul | 8436968 | 2005-04-27 12:39:27 +0000 | [diff] [blame^] | 236 | if (item->ran > wq->spec.max_retries) |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 237 | { |
| 238 | /* run error handler, if any */ |
| 239 | if (wq->spec.errorfunc) |
| 240 | wq->spec.errorfunc (wq, item->data); |
| 241 | work_queue_item_remove (wq, node); |
| 242 | continue; |
| 243 | } |
| 244 | |
| 245 | /* run and take care of items that want to be retried immediately */ |
| 246 | do |
| 247 | { |
| 248 | ret = wq->spec.workfunc (item->data); |
paul | 8436968 | 2005-04-27 12:39:27 +0000 | [diff] [blame^] | 249 | item->ran++; |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 250 | } |
| 251 | while ((ret == WQ_RETRY_NOW) |
paul | 8436968 | 2005-04-27 12:39:27 +0000 | [diff] [blame^] | 252 | && (item->ran < wq->spec.max_retries)); |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 253 | |
| 254 | switch (ret) |
| 255 | { |
| 256 | case WQ_RETRY_LATER: |
| 257 | { |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 258 | goto stats; |
| 259 | } |
| 260 | case WQ_REQUEUE: |
| 261 | { |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 262 | work_queue_item_requeue (wq, node); |
| 263 | break; |
| 264 | } |
| 265 | case WQ_RETRY_NOW: |
| 266 | case WQ_ERROR: |
| 267 | { |
| 268 | if (wq->spec.errorfunc) |
| 269 | wq->spec.errorfunc (wq, item); |
| 270 | } |
| 271 | /* fall through here is deliberate */ |
| 272 | case WQ_SUCCESS: |
| 273 | default: |
| 274 | { |
| 275 | work_queue_item_remove (wq, node); |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /* completed cycle */ |
| 281 | cycles++; |
| 282 | |
| 283 | /* test if we should yield */ |
| 284 | if ( !(cycles % wq->cycles.granularity) |
| 285 | && thread_should_yield (thread)) |
| 286 | { |
| 287 | yielded = 1; |
| 288 | goto stats; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | stats: |
| 293 | |
| 294 | #define WQ_HYSTERIS_FACTOR 2 |
| 295 | |
| 296 | /* we yielded, check whether granularity should be reduced */ |
| 297 | if (yielded && (cycles < wq->cycles.granularity)) |
| 298 | { |
| 299 | wq->cycles.granularity = ((cycles > 0) ? cycles |
| 300 | : WORK_QUEUE_MIN_GRANULARITY); |
| 301 | } |
| 302 | |
paul | 8436968 | 2005-04-27 12:39:27 +0000 | [diff] [blame^] | 303 | if (cycles >= (wq->cycles.granularity)) |
paul | 354d119 | 2005-04-25 16:26:42 +0000 | [diff] [blame] | 304 | { |
| 305 | if (cycles > wq->cycles.best) |
| 306 | wq->cycles.best = cycles; |
| 307 | |
| 308 | /* along with yielded check, provides hysteris for granularity */ |
| 309 | if (cycles > (wq->cycles.granularity * WQ_HYSTERIS_FACTOR)) |
| 310 | wq->cycles.granularity += WQ_HYSTERIS_FACTOR; |
| 311 | } |
| 312 | #undef WQ_HYSTERIS_FACTOR |
| 313 | |
| 314 | wq->runs++; |
| 315 | wq->cycles.total += cycles; |
| 316 | |
| 317 | #if 0 |
| 318 | printf ("%s: cycles %d, new: best %d, worst %d\n", |
| 319 | __func__, cycles, wq->cycles.best, wq->cycles.granularity); |
| 320 | #endif |
| 321 | |
| 322 | /* Is the queue done yet? */ |
| 323 | if (listcount (wq->items) > 0) |
| 324 | wq->thread = thread_add_background (wq->master, work_queue_run, wq, |
| 325 | wq->spec.delay); |
| 326 | |
| 327 | return 0; |
| 328 | } |