]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_heavy_wq.c
2 * This file is part of Quagga.
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
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.
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
19 /* This programme shows the effects of 'heavy' long-running functions
20 * on the cooperative threading model.
22 * Run it with a config file containing 'password whatever', telnet to it
23 * (it defaults to port 4000) and enter the 'clear foo string' command.
24 * then type whatever and observe that the vty interface is unresponsive
25 * for quite a period of time, due to the clear_something command
26 * taking a very long time to complete.
35 #include "workqueue.h"
40 DEFINE_MGROUP(TEST_HEAVYWQ
, "heavy-wq test")
41 DEFINE_MTYPE_STATIC(TEST_HEAVYWQ
, WQ_NODE
, "heavy_wq_node")
42 DEFINE_MTYPE_STATIC(TEST_HEAVYWQ
, WQ_NODE_STR
, "heavy_wq_node->str")
44 extern struct thread_master
*master
;
45 static struct work_queue
*heavy_wq
;
47 struct heavy_wq_node
{
52 enum { ITERS_FIRST
= 0,
59 static void heavy_wq_add(struct vty
*vty
, const char *str
, int i
)
61 struct heavy_wq_node
*hn
;
63 hn
= XCALLOC(MTYPE_WQ_NODE
, sizeof(struct heavy_wq_node
));
66 hn
->str
= XSTRDUP(MTYPE_WQ_NODE_STR
, str
);
68 work_queue_add(heavy_wq
, hn
);
73 static void slow_func_err(struct work_queue
*wq
, struct work_queue_item
*item
)
75 printf("%s: running error function\n", __func__
);
78 static void slow_func_del(struct work_queue
*wq
, void *data
)
80 struct heavy_wq_node
*hn
= data
;
81 assert(hn
&& hn
->str
);
82 printf("%s: %s\n", __func__
, hn
->str
);
83 XFREE(MTYPE_WQ_NODE_STR
, hn
->str
);
85 XFREE(MTYPE_WQ_NODE
, hn
);
88 static wq_item_status
slow_func(struct work_queue
*wq
, void *data
)
90 struct heavy_wq_node
*hn
= data
;
94 assert(hn
&& hn
->str
);
96 for (j
= 0; j
< 300; j
++)
99 if ((hn
->i
% ITERS_LATER
) == 0)
100 return WQ_RETRY_LATER
;
102 if ((hn
->i
% ITERS_ERR
) == 0)
105 if ((hn
->i
% ITERS_PRINT
) == 0)
106 printf("%s did %d, x = %g\n", hn
->str
, hn
->i
, x
);
111 static void clear_something(struct vty
*vty
, const char *str
)
115 /* this could be like iterating through 150k of route_table
116 * or worse, iterating through a list of peers, to bgp_stop them with
117 * each having 150k route tables to process...
119 for (i
= ITERS_FIRST
; i
< ITERS_MAX
; i
++)
120 heavy_wq_add(vty
, str
, i
);
127 "arbitrary string\n")
131 vty_out(vty
, "%% string argument required\n");
135 str
= argv_concat(argv
, argc
, 0);
137 clear_something(vty
, str
);
138 XFREE(MTYPE_TMP
, str
);
142 static int heavy_wq_init(void)
144 heavy_wq
= work_queue_new(master
, "heavy_work_queue");
146 heavy_wq
->spec
.workfunc
= &slow_func
;
147 heavy_wq
->spec
.errorfunc
= &slow_func_err
;
148 heavy_wq
->spec
.del_item_data
= &slow_func_del
;
149 heavy_wq
->spec
.max_retries
= 3;
150 heavy_wq
->spec
.hold
= 1000;
157 install_element(VIEW_NODE
, &clear_foo_cmd
);