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