]>
Commit | Line | Data |
---|---|---|
864c364d | 1 | /* |
864c364d | 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 | |
864c364d | 17 | */ |
18 | ||
19 | /* This programme shows the effects of 'heavy' long-running functions | |
20 | * on the cooperative threading model, as demonstrated by heavy.c, and how | |
21 | * they can be mitigated using a background thread. | |
22 | * | |
23 | * Run it with a config file containing 'password whatever', telnet to it | |
24 | * (it defaults to port 4000) and enter the 'clear foo string' command. | |
25 | * then type whatever and observe that, unlike heavy.c, the vty interface | |
26 | * remains responsive. | |
27 | */ | |
28 | #include <zebra.h> | |
29 | #include <math.h> | |
30 | ||
864c364d | 31 | #include "thread.h" |
32 | #include "vty.h" | |
33 | #include "command.h" | |
34 | #include "memory.h" | |
35 | #include "log.h" | |
36 | ||
9fc3f9b3 DL |
37 | #include "tests.h" |
38 | ||
9f3f7a11 | 39 | extern struct thread_master *master; |
864c364d | 40 | |
d62a17ae | 41 | enum { ITERS_FIRST = 0, |
42 | ITERS_ERR = 100, | |
43 | ITERS_LATER = 400, | |
44 | ITERS_PRINT = 10, | |
45 | ITERS_MAX = 1000, | |
864c364d | 46 | }; |
47 | ||
48 | struct work_state { | |
d62a17ae | 49 | struct vty *vty; |
50 | char *str; | |
51 | int i; | |
864c364d | 52 | }; |
53 | ||
d62a17ae | 54 | static void slow_func(struct vty *vty, const char *str, const int i) |
864c364d | 55 | { |
d62a17ae | 56 | double x = 1; |
57 | int j; | |
58 | ||
59 | for (j = 0; j < 300; j++) | |
60 | x += sin(x) * j; | |
61 | ||
62 | if ((i % ITERS_LATER) == 0) | |
63 | printf("%s: %d, temporary error, save this somehow and do it later..\n", | |
64 | __func__, i); | |
65 | ||
66 | if ((i % ITERS_ERR) == 0) | |
67 | printf("%s: hard error\n", __func__); | |
68 | ||
69 | if ((i % ITERS_PRINT) == 0) | |
70 | printf("%s did %d, x = %g\n", str, i, x); | |
864c364d | 71 | } |
72 | ||
d62a17ae | 73 | static int clear_something(struct thread *thread) |
864c364d | 74 | { |
d62a17ae | 75 | struct work_state *ws = THREAD_ARG(thread); |
76 | ||
77 | /* this could be like iterating through 150k of route_table | |
78 | * or worse, iterating through a list of peers, to bgp_stop them with | |
79 | * each having 150k route tables to process... | |
80 | */ | |
81 | while (ws->i < ITERS_MAX) { | |
82 | slow_func(ws->vty, ws->str, ws->i); | |
83 | ws->i++; | |
84 | if (thread_should_yield(thread)) { | |
85 | thread_add_timer_msec(master, clear_something, ws, 0, | |
86 | NULL); | |
87 | return 0; | |
88 | } | |
89 | } | |
90 | ||
91 | /* All done! */ | |
92 | XFREE(MTYPE_TMP, ws->str); | |
93 | XFREE(MTYPE_TMP, ws); | |
94 | return 0; | |
864c364d | 95 | } |
96 | ||
864c364d | 97 | DEFUN (clear_foo, |
98 | clear_foo_cmd, | |
e961923c | 99 | "clear foo LINE...", |
864c364d | 100 | "clear command\n" |
101 | "arbitrary string\n") | |
102 | { | |
d62a17ae | 103 | char *str; |
104 | struct work_state *ws; | |
105 | ||
106 | if (!argc) { | |
107 | vty_out(vty, "%% string argument required\n"); | |
108 | return CMD_WARNING; | |
109 | } | |
110 | ||
111 | str = argv_concat(argv, argc, 0); | |
112 | ||
13143701 | 113 | ws = XMALLOC(MTYPE_TMP, sizeof(*ws)); |
d62a17ae | 114 | |
13143701 | 115 | ws->str = XSTRDUP(MTYPE_TMP, str); |
d62a17ae | 116 | |
117 | ws->vty = vty; | |
118 | ws->i = ITERS_FIRST; | |
119 | ||
120 | thread_add_timer_msec(master, clear_something, ws, 0, NULL); | |
121 | ||
122 | return CMD_SUCCESS; | |
864c364d | 123 | } |
124 | ||
4d762f26 | 125 | void test_init(void) |
864c364d | 126 | { |
d62a17ae | 127 | install_element(VIEW_NODE, &clear_foo_cmd); |
864c364d | 128 | } |