]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
43313d05 | 2 | /* |
43313d05 | 3 | */ |
4 | ||
5 | /* This programme shows the effects of 'heavy' long-running functions | |
6 | * on the cooperative threading model. | |
7 | * | |
8 | * Run it with a config file containing 'password whatever', telnet to it | |
9 | * (it defaults to port 4000) and enter the 'clear foo string' command. | |
10 | * then type whatever and observe that the vty interface is unresponsive | |
11 | * for quite a period of time, due to the clear_something command | |
12 | * taking a very long time to complete. | |
13 | */ | |
14 | #include <zebra.h> | |
15 | ||
43313d05 | 16 | #include "thread.h" |
17 | #include "vty.h" | |
18 | #include "command.h" | |
19 | #include "memory.h" | |
f4d062f8 | 20 | #include <math.h> |
43313d05 | 21 | |
9fc3f9b3 DL |
22 | #include "tests.h" |
23 | ||
d62a17ae | 24 | enum { ITERS_FIRST = 0, |
25 | ITERS_ERR = 100, | |
26 | ITERS_LATER = 400, | |
27 | ITERS_PRINT = 10, | |
28 | ITERS_MAX = 1000, | |
43313d05 | 29 | }; |
30 | ||
d62a17ae | 31 | static void slow_func(struct vty *vty, const char *str, const int i) |
43313d05 | 32 | { |
d62a17ae | 33 | double x = 1; |
34 | int j; | |
35 | ||
36 | for (j = 0; j < 300; j++) | |
37 | x += sin(x) * j; | |
38 | ||
39 | if ((i % ITERS_LATER) == 0) | |
40 | printf("%s: %d, temporary error, save this somehow and do it later..\n", | |
41 | __func__, i); | |
42 | ||
43 | if ((i % ITERS_ERR) == 0) | |
44 | printf("%s: hard error\n", __func__); | |
45 | ||
46 | if ((i % ITERS_PRINT) == 0) | |
47 | printf("%s did %d, x = %g\n", str, i, x); | |
43313d05 | 48 | } |
49 | ||
d62a17ae | 50 | static void clear_something(struct vty *vty, const char *str) |
43313d05 | 51 | { |
d62a17ae | 52 | int i; |
53 | ||
54 | /* this could be like iterating through 150k of route_table | |
55 | * or worse, iterating through a list of peers, to bgp_stop them with | |
56 | * each having 150k route tables to process... | |
57 | */ | |
58 | for (i = ITERS_FIRST; i < ITERS_MAX; i++) | |
59 | slow_func(vty, str, i); | |
f4d062f8 | 60 | } |
61 | ||
43313d05 | 62 | DEFUN (clear_foo, |
63 | clear_foo_cmd, | |
e961923c | 64 | "clear foo LINE...", |
43313d05 | 65 | "clear command\n" |
66 | "arbitrary string\n") | |
67 | { | |
d62a17ae | 68 | char *str; |
69 | if (!argc) { | |
70 | vty_out(vty, "%% string argument required\n"); | |
71 | return CMD_WARNING; | |
72 | } | |
73 | ||
74 | str = argv_concat(argv, argc, 0); | |
75 | ||
76 | clear_something(vty, str); | |
77 | XFREE(MTYPE_TMP, str); | |
78 | return CMD_SUCCESS; | |
43313d05 | 79 | } |
80 | ||
4d762f26 | 81 | static void slow_vty_init(void) |
43313d05 | 82 | { |
d62a17ae | 83 | install_element(VIEW_NODE, &clear_foo_cmd); |
43313d05 | 84 | } |
85 | ||
4d762f26 | 86 | void test_init(void) |
43313d05 | 87 | { |
d62a17ae | 88 | slow_vty_init(); |
43313d05 | 89 | } |