]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_heavy_thread.c
*: reindent
[mirror_frr.git] / tests / lib / test_heavy_thread.c
1 /*
2 * $Id: heavy-thread.c,v 1.2 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 along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /* This programme shows the effects of 'heavy' long-running functions
22 * on the cooperative threading model, as demonstrated by heavy.c, and how
23 * they can be mitigated using a background thread.
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, unlike heavy.c, the vty interface
28 * remains responsive.
29 */
30 #include <zebra.h>
31 #include <math.h>
32
33 #include "thread.h"
34 #include "vty.h"
35 #include "command.h"
36 #include "memory.h"
37 #include "log.h"
38
39 #include "tests.h"
40
41 extern struct thread_master *master;
42
43 enum { ITERS_FIRST = 0,
44 ITERS_ERR = 100,
45 ITERS_LATER = 400,
46 ITERS_PRINT = 10,
47 ITERS_MAX = 1000,
48 };
49
50 struct work_state {
51 struct vty *vty;
52 char *str;
53 int i;
54 };
55
56 static void slow_func(struct vty *vty, const char *str, const int i)
57 {
58 double x = 1;
59 int j;
60
61 for (j = 0; j < 300; j++)
62 x += sin(x) * j;
63
64 if ((i % ITERS_LATER) == 0)
65 printf("%s: %d, temporary error, save this somehow and do it later..\n",
66 __func__, i);
67
68 if ((i % ITERS_ERR) == 0)
69 printf("%s: hard error\n", __func__);
70
71 if ((i % ITERS_PRINT) == 0)
72 printf("%s did %d, x = %g\n", str, i, x);
73 }
74
75 static int clear_something(struct thread *thread)
76 {
77 struct work_state *ws = THREAD_ARG(thread);
78
79 /* this could be like iterating through 150k of route_table
80 * or worse, iterating through a list of peers, to bgp_stop them with
81 * each having 150k route tables to process...
82 */
83 while (ws->i < ITERS_MAX) {
84 slow_func(ws->vty, ws->str, ws->i);
85 ws->i++;
86 if (thread_should_yield(thread)) {
87 thread_add_timer_msec(master, clear_something, ws, 0,
88 NULL);
89 return 0;
90 }
91 }
92
93 /* All done! */
94 XFREE(MTYPE_TMP, ws->str);
95 XFREE(MTYPE_TMP, ws);
96 return 0;
97 }
98
99 DEFUN (clear_foo,
100 clear_foo_cmd,
101 "clear foo LINE...",
102 "clear command\n"
103 "arbitrary string\n")
104 {
105 char *str;
106 struct work_state *ws;
107
108 if (!argc) {
109 vty_out(vty, "%% string argument required\n");
110 return CMD_WARNING;
111 }
112
113 str = argv_concat(argv, argc, 0);
114
115 if ((ws = XMALLOC(MTYPE_TMP, sizeof(*ws))) == NULL) {
116 zlog_err("%s: unable to allocate work_state", __func__);
117 return CMD_WARNING;
118 }
119
120 if (!(ws->str = XSTRDUP(MTYPE_TMP, str))) {
121 zlog_err("%s: unable to xstrdup", __func__);
122 XFREE(MTYPE_TMP, ws);
123 return CMD_WARNING;
124 }
125
126 ws->vty = vty;
127 ws->i = ITERS_FIRST;
128
129 thread_add_timer_msec(master, clear_something, ws, 0, NULL);
130
131 return CMD_SUCCESS;
132 }
133
134 void test_init()
135 {
136 install_element(VIEW_NODE, &clear_foo_cmd);
137 }