paul | 9f3f7a1 | 2005-04-25 16:42:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * $Id: heavy-wq.c,v 1.1 2005/04/25 16:42:24 paul Exp $ |
| 3 | * |
| 4 | * This file is part of Quagga. |
| 5 | * |
| 6 | * Quagga is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; either version 2, or (at your option) any |
| 9 | * later version. |
| 10 | * |
| 11 | * Quagga is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with Quagga; see the file COPYING. If not, write to the Free |
| 18 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | * 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | /* This programme shows the effects of 'heavy' long-running functions |
| 23 | * on the cooperative threading model. |
| 24 | * |
| 25 | * Run it with a config file containing 'password whatever', telnet to it |
| 26 | * (it defaults to port 4000) and enter the 'clear foo string' command. |
| 27 | * then type whatever and observe that the vty interface is unresponsive |
| 28 | * for quite a period of time, due to the clear_something command |
| 29 | * taking a very long time to complete. |
| 30 | */ |
| 31 | #include <zebra.h> |
| 32 | |
| 33 | #include "thread.h" |
| 34 | #include "vty.h" |
| 35 | #include "command.h" |
| 36 | #include "memory.h" |
| 37 | #include "log.h" |
| 38 | #include "workqueue.h" |
| 39 | #include <math.h> |
| 40 | |
| 41 | extern struct thread_master *master; |
| 42 | static struct work_queue *heavy_wq; |
| 43 | |
| 44 | struct heavy_wq_node |
| 45 | { |
| 46 | char *str; |
| 47 | int i; |
| 48 | }; |
| 49 | |
| 50 | enum |
| 51 | { |
| 52 | ITERS_FIRST = 0, |
| 53 | ITERS_ERR = 100, |
| 54 | ITERS_LATER = 400, |
| 55 | ITERS_PRINT = 10, |
| 56 | ITERS_MAX = 1000, |
| 57 | }; |
| 58 | |
| 59 | static void |
| 60 | heavy_wq_add (struct vty *vty, const char *str, int i) |
| 61 | { |
| 62 | struct heavy_wq_node *hn; |
| 63 | |
| 64 | if ((hn = XCALLOC (MTYPE_PREFIX_LIST, sizeof(struct heavy_wq_node))) == NULL) |
| 65 | { |
| 66 | zlog_err ("%s: unable to allocate hn", __func__); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | hn->i = i; |
| 71 | if (!(hn->str = XSTRDUP (MTYPE_PREFIX_LIST_STR, str))) |
| 72 | { |
| 73 | zlog_err ("%s: unable to xstrdup", __func__); |
| 74 | XFREE (MTYPE_PREFIX_LIST, hn); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | work_queue_add (heavy_wq, hn); |
| 79 | |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | slow_func_err (struct work_queue *wq, struct work_queue_item *item) |
| 85 | { |
| 86 | printf ("%s: running error function\n", __func__); |
| 87 | } |
| 88 | |
| 89 | static void |
| 90 | slow_func_del (struct heavy_wq_node *hn) |
| 91 | { |
| 92 | assert (hn && hn->str); |
| 93 | XFREE (MTYPE_PREFIX_LIST_STR, hn->str); |
| 94 | hn->str = NULL; |
| 95 | XFREE(MTYPE_PREFIX_LIST, hn); |
| 96 | } |
| 97 | |
| 98 | static wq_item_status |
| 99 | slow_func (struct heavy_wq_node *hn) |
| 100 | { |
| 101 | double x = 1; |
| 102 | int j; |
| 103 | |
| 104 | assert (hn && hn->str); |
| 105 | |
| 106 | for (j = 0; j < 300; j++) |
| 107 | x += sin(x)*j; |
| 108 | |
| 109 | if ((hn->i % ITERS_LATER) == 0) |
| 110 | return WQ_RETRY_LATER; |
| 111 | |
| 112 | if ((hn->i % ITERS_ERR) == 0) |
| 113 | return WQ_RETRY_NOW; |
| 114 | |
| 115 | if ((hn->i % ITERS_PRINT) == 0) |
| 116 | printf ("%s did %d, x = %g\n", hn->str, hn->i, x); |
| 117 | |
| 118 | return WQ_SUCCESS; |
| 119 | } |
| 120 | |
| 121 | static void |
| 122 | clear_something (struct vty *vty, const char *str) |
| 123 | { |
| 124 | int i; |
| 125 | |
| 126 | /* this could be like iterating through 150k of route_table |
| 127 | * or worse, iterating through a list of peers, to bgp_stop them with |
| 128 | * each having 150k route tables to process... |
| 129 | */ |
| 130 | for (i = ITERS_FIRST; i < ITERS_MAX; i++) |
| 131 | heavy_wq_add (vty, str, i); |
| 132 | } |
| 133 | |
| 134 | DEFUN (clear_foo, |
| 135 | clear_foo_cmd, |
| 136 | "clear foo .LINE", |
| 137 | "clear command\n" |
| 138 | "arbitrary string\n") |
| 139 | { |
| 140 | char *str; |
| 141 | if (!argc) |
| 142 | { |
| 143 | vty_out (vty, "%% string argument required%s", VTY_NEWLINE); |
| 144 | return CMD_WARNING; |
| 145 | } |
| 146 | |
| 147 | str = argv_concat (argv, argc, 0); |
| 148 | |
| 149 | clear_something (vty, str); |
| 150 | XFREE (MTYPE_TMP, str); |
| 151 | return CMD_SUCCESS; |
| 152 | } |
| 153 | |
| 154 | static int |
| 155 | heavy_wq_init () |
| 156 | { |
| 157 | if (! (heavy_wq = work_queue_new (master, "heavy_work_queue"))) |
| 158 | { |
| 159 | zlog_err ("%s: could not get new work queue!", __func__); |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | heavy_wq->spec.workfunc = &slow_func; |
| 164 | heavy_wq->spec.errorfunc = &slow_func_err; |
| 165 | heavy_wq->spec.del_item_data = &slow_func_del; |
| 166 | heavy_wq->spec.max_retries = 3; |
| 167 | heavy_wq->spec.delay = 10; |
| 168 | heavy_wq->spec.hold = 1000; |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | test_init() |
| 175 | { |
| 176 | install_element (VIEW_NODE, &clear_foo_cmd); |
| 177 | heavy_wq_init(); |
| 178 | } |