]> git.proxmox.com Git - mirror_frr.git/blame - tests/heavy-wq.c
*: split & distribute memtypes and stop (re|ab)using lib/ MTYPEs
[mirror_frr.git] / tests / heavy-wq.c
CommitLineData
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 *
14 * You should have received a copy of the GNU General Public License
15 * along with Quagga; see the file COPYING. If not, write to the Free
16 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 * 02111-1307, USA.
18 */
19
20/* This programme shows the effects of 'heavy' long-running functions
21 * on the cooperative threading model.
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 the vty interface is unresponsive
26 * for quite a period of time, due to the clear_something command
27 * taking a very long time to complete.
28 */
29#include <zebra.h>
30
31#include "thread.h"
32#include "vty.h"
33#include "command.h"
34#include "memory.h"
35#include "log.h"
36#include "workqueue.h"
37#include <math.h>
38
9fc3f9b3
DL
39#include "tests.h"
40
4a1ab8e4
DL
41DEFINE_MGROUP(TEST_HEAVYWQ, "heavy-wq test")
42DEFINE_MTYPE_STATIC(TEST_HEAVYWQ, WQ_NODE, "heavy_wq_node")
43DEFINE_MTYPE_STATIC(TEST_HEAVYWQ, WQ_NODE_STR, "heavy_wq_node->str")
44
9f3f7a11 45extern struct thread_master *master;
46static struct work_queue *heavy_wq;
47
48struct heavy_wq_node
49{
50 char *str;
51 int i;
52};
53
54enum
55{
56 ITERS_FIRST = 0,
57 ITERS_ERR = 100,
58 ITERS_LATER = 400,
59 ITERS_PRINT = 10,
60 ITERS_MAX = 1000,
61};
62
63static void
64heavy_wq_add (struct vty *vty, const char *str, int i)
65{
66 struct heavy_wq_node *hn;
67
4a1ab8e4 68 if ((hn = XCALLOC (MTYPE_WQ_NODE, sizeof(struct heavy_wq_node))) == NULL)
9f3f7a11 69 {
70 zlog_err ("%s: unable to allocate hn", __func__);
71 return;
72 }
73
74 hn->i = i;
4a1ab8e4 75 if (!(hn->str = XSTRDUP (MTYPE_WQ_NODE_STR, str)))
9f3f7a11 76 {
77 zlog_err ("%s: unable to xstrdup", __func__);
4a1ab8e4 78 XFREE (MTYPE_WQ_NODE, hn);
9f3f7a11 79 return;
80 }
81
82 work_queue_add (heavy_wq, hn);
83
84 return;
85}
86
87static void
88slow_func_err (struct work_queue *wq, struct work_queue_item *item)
89{
90 printf ("%s: running error function\n", __func__);
91}
92
93static void
3414bf25 94slow_func_del (struct work_queue *wq, void *data)
9f3f7a11 95{
6379b961 96 struct heavy_wq_node *hn = data;
9f3f7a11 97 assert (hn && hn->str);
3414bf25 98 printf ("%s: %s\n", __func__, hn->str);
4a1ab8e4 99 XFREE (MTYPE_WQ_NODE_STR, hn->str);
9f3f7a11 100 hn->str = NULL;
4a1ab8e4 101 XFREE(MTYPE_WQ_NODE, hn);
9f3f7a11 102}
103
104static wq_item_status
3414bf25 105slow_func (struct work_queue *wq, void *data)
9f3f7a11 106{
6379b961 107 struct heavy_wq_node *hn = data;
9f3f7a11 108 double x = 1;
109 int j;
110
111 assert (hn && hn->str);
112
113 for (j = 0; j < 300; j++)
114 x += sin(x)*j;
115
116 if ((hn->i % ITERS_LATER) == 0)
117 return WQ_RETRY_LATER;
118
119 if ((hn->i % ITERS_ERR) == 0)
120 return WQ_RETRY_NOW;
121
122 if ((hn->i % ITERS_PRINT) == 0)
123 printf ("%s did %d, x = %g\n", hn->str, hn->i, x);
124
125 return WQ_SUCCESS;
126}
127
128static void
129clear_something (struct vty *vty, const char *str)
130{
131 int i;
132
133 /* this could be like iterating through 150k of route_table
134 * or worse, iterating through a list of peers, to bgp_stop them with
135 * each having 150k route tables to process...
136 */
137 for (i = ITERS_FIRST; i < ITERS_MAX; i++)
138 heavy_wq_add (vty, str, i);
139}
140
141DEFUN (clear_foo,
142 clear_foo_cmd,
143 "clear foo .LINE",
144 "clear command\n"
145 "arbitrary string\n")
146{
147 char *str;
148 if (!argc)
149 {
150 vty_out (vty, "%% string argument required%s", VTY_NEWLINE);
151 return CMD_WARNING;
152 }
153
154 str = argv_concat (argv, argc, 0);
155
156 clear_something (vty, str);
157 XFREE (MTYPE_TMP, str);
158 return CMD_SUCCESS;
159}
160
161static int
162heavy_wq_init ()
163{
164 if (! (heavy_wq = work_queue_new (master, "heavy_work_queue")))
165 {
166 zlog_err ("%s: could not get new work queue!", __func__);
167 return -1;
168 }
169
170 heavy_wq->spec.workfunc = &slow_func;
171 heavy_wq->spec.errorfunc = &slow_func_err;
172 heavy_wq->spec.del_item_data = &slow_func_del;
173 heavy_wq->spec.max_retries = 3;
9f3f7a11 174 heavy_wq->spec.hold = 1000;
175
176 return 0;
177}
178
179void
180test_init()
181{
182 install_element (VIEW_NODE, &clear_foo_cmd);
183 heavy_wq_init();
184}