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", |
| 166 | "List","(ms) ","Q. Runs","Cycle Counts ", |
| 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 | { |
| 179 | vty_out (vty,"%8d %5d %5d %8ld %7d %6d %6u %s%s", |
| 180 | listcount (wq->items), |
| 181 | wq->spec.delay, wq->spec.hold, |
| 182 | wq->runs, |
| 183 | wq->cycles.best, wq->cycles.granularity, |
| 184 | (unsigned int)(wq->cycles.total / wq->runs), |
| 185 | wq->name, |
| 186 | VTY_NEWLINE); |
| 187 | } |
| 188 | |
| 189 | return CMD_SUCCESS; |
| 190 | } |
| 191 | |
| 192 | /* timer thread to process a work queue |
| 193 | * will reschedule itself if required, |
| 194 | * otherwise work_queue_item_add |
| 195 | */ |
| 196 | int |
| 197 | work_queue_run (struct thread *thread) |
| 198 | { |
| 199 | struct work_queue *wq; |
| 200 | struct work_queue_item *item; |
| 201 | wq_item_status ret; |
| 202 | unsigned int cycles = 0; |
| 203 | struct listnode *node, *nnode; |
| 204 | char yielded = 0; |
| 205 | |
| 206 | wq = THREAD_ARG (thread); |
| 207 | wq->thread = NULL; |
| 208 | |
| 209 | assert (wq && wq->items); |
| 210 | |
| 211 | /* calculate cycle granularity: |
| 212 | * list iteration == 1 cycle |
| 213 | * granularity == # cycles between checks whether we should yield. |
| 214 | * |
| 215 | * granularity should be > 0, and can increase slowly after each run to |
| 216 | * provide some hysteris, but not past cycles.best or 2*cycles. |
| 217 | * |
| 218 | * Best: starts low, can only increase |
| 219 | * |
| 220 | * Granularity: starts at WORK_QUEUE_MIN_GRANULARITY, can be decreased if we run to end of time |
| 221 | * slot, can increase otherwise by a small factor. |
| 222 | * |
| 223 | * We could use just the average and save some work, however we want to be |
| 224 | * able to adjust quickly to CPU pressure. Average wont shift much if |
| 225 | * daemon has been running a long time. |
| 226 | */ |
| 227 | if (wq->cycles.granularity == 0) |
| 228 | wq->cycles.granularity = WORK_QUEUE_MIN_GRANULARITY; |
| 229 | |
| 230 | for (ALL_LIST_ELEMENTS (wq->items, node, nnode, item)) |
| 231 | { |
| 232 | assert (item && item->data); |
| 233 | |
| 234 | /* dont run items which are past their allowed retries */ |
| 235 | if (item->retry_count >= wq->spec.max_retries) |
| 236 | { |
| 237 | /* run error handler, if any */ |
| 238 | if (wq->spec.errorfunc) |
| 239 | wq->spec.errorfunc (wq, item->data); |
| 240 | work_queue_item_remove (wq, node); |
| 241 | continue; |
| 242 | } |
| 243 | |
| 244 | /* run and take care of items that want to be retried immediately */ |
| 245 | do |
| 246 | { |
| 247 | ret = wq->spec.workfunc (item->data); |
| 248 | item->retry_count++; |
| 249 | } |
| 250 | while ((ret == WQ_RETRY_NOW) |
| 251 | && (item->retry_count < wq->spec.max_retries)); |
| 252 | |
| 253 | switch (ret) |
| 254 | { |
| 255 | case WQ_RETRY_LATER: |
| 256 | { |
| 257 | item->retry_count++; |
| 258 | goto stats; |
| 259 | } |
| 260 | case WQ_REQUEUE: |
| 261 | { |
| 262 | item->retry_count++; |
| 263 | work_queue_item_requeue (wq, node); |
| 264 | break; |
| 265 | } |
| 266 | case WQ_RETRY_NOW: |
| 267 | case WQ_ERROR: |
| 268 | { |
| 269 | if (wq->spec.errorfunc) |
| 270 | wq->spec.errorfunc (wq, item); |
| 271 | } |
| 272 | /* fall through here is deliberate */ |
| 273 | case WQ_SUCCESS: |
| 274 | default: |
| 275 | { |
| 276 | work_queue_item_remove (wq, node); |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* completed cycle */ |
| 282 | cycles++; |
| 283 | |
| 284 | /* test if we should yield */ |
| 285 | if ( !(cycles % wq->cycles.granularity) |
| 286 | && thread_should_yield (thread)) |
| 287 | { |
| 288 | yielded = 1; |
| 289 | goto stats; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | stats: |
| 294 | |
| 295 | #define WQ_HYSTERIS_FACTOR 2 |
| 296 | |
| 297 | /* we yielded, check whether granularity should be reduced */ |
| 298 | if (yielded && (cycles < wq->cycles.granularity)) |
| 299 | { |
| 300 | wq->cycles.granularity = ((cycles > 0) ? cycles |
| 301 | : WORK_QUEUE_MIN_GRANULARITY); |
| 302 | } |
| 303 | |
| 304 | if (cycles > (wq->cycles.granularity)) |
| 305 | { |
| 306 | if (cycles > wq->cycles.best) |
| 307 | wq->cycles.best = cycles; |
| 308 | |
| 309 | /* along with yielded check, provides hysteris for granularity */ |
| 310 | if (cycles > (wq->cycles.granularity * WQ_HYSTERIS_FACTOR)) |
| 311 | wq->cycles.granularity += WQ_HYSTERIS_FACTOR; |
| 312 | } |
| 313 | #undef WQ_HYSTERIS_FACTOR |
| 314 | |
| 315 | wq->runs++; |
| 316 | wq->cycles.total += cycles; |
| 317 | |
| 318 | #if 0 |
| 319 | printf ("%s: cycles %d, new: best %d, worst %d\n", |
| 320 | __func__, cycles, wq->cycles.best, wq->cycles.granularity); |
| 321 | #endif |
| 322 | |
| 323 | /* Is the queue done yet? */ |
| 324 | if (listcount (wq->items) > 0) |
| 325 | wq->thread = thread_add_background (wq->master, work_queue_run, wq, |
| 326 | wq->spec.delay); |
| 327 | |
| 328 | return 0; |
| 329 | } |