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