]> git.proxmox.com Git - mirror_frr.git/blame - tests/lib/test_heavy_wq.c
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / tests / lib / test_heavy_wq.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
9f3f7a11 2/*
9f3f7a11 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
24a58196 16#include "frrevent.h"
9f3f7a11 17#include "vty.h"
18#include "command.h"
19#include "memory.h"
20#include "log.h"
21#include "workqueue.h"
22#include <math.h>
23
9fc3f9b3
DL
24#include "tests.h"
25
bf8d3d6a
DL
26DEFINE_MGROUP(TEST_HEAVYWQ, "heavy-wq test");
27DEFINE_MTYPE_STATIC(TEST_HEAVYWQ, WQ_NODE, "heavy_wq_node");
28DEFINE_MTYPE_STATIC(TEST_HEAVYWQ, WQ_NODE_STR, "heavy_wq_node->str");
4a1ab8e4 29
cd9d0537 30extern struct event_loop *master;
9f3f7a11 31static struct work_queue *heavy_wq;
32
d62a17ae 33struct heavy_wq_node {
34 char *str;
35 int i;
9f3f7a11 36};
37
d62a17ae 38enum { ITERS_FIRST = 0,
39 ITERS_ERR = 100,
40 ITERS_LATER = 400,
41 ITERS_PRINT = 10,
42 ITERS_MAX = 1000,
9f3f7a11 43};
44
d62a17ae 45static void heavy_wq_add(struct vty *vty, const char *str, int i)
9f3f7a11 46{
d62a17ae 47 struct heavy_wq_node *hn;
48
13143701 49 hn = XCALLOC(MTYPE_WQ_NODE, sizeof(struct heavy_wq_node));
d62a17ae 50
51 hn->i = i;
13143701 52 hn->str = XSTRDUP(MTYPE_WQ_NODE_STR, str);
d62a17ae 53
54 work_queue_add(heavy_wq, hn);
55
56 return;
9f3f7a11 57}
58
d62a17ae 59static void slow_func_del(struct work_queue *wq, void *data)
9f3f7a11 60{
d62a17ae 61 struct heavy_wq_node *hn = data;
62 assert(hn && hn->str);
63 printf("%s: %s\n", __func__, hn->str);
64 XFREE(MTYPE_WQ_NODE_STR, hn->str);
d62a17ae 65 XFREE(MTYPE_WQ_NODE, hn);
9f3f7a11 66}
67
d62a17ae 68static wq_item_status slow_func(struct work_queue *wq, void *data)
9f3f7a11 69{
d62a17ae 70 struct heavy_wq_node *hn = data;
71 double x = 1;
72 int j;
73
74 assert(hn && hn->str);
75
76 for (j = 0; j < 300; j++)
77 x += sin(x) * j;
78
79 if ((hn->i % ITERS_LATER) == 0)
80 return WQ_RETRY_LATER;
81
82 if ((hn->i % ITERS_ERR) == 0)
83 return WQ_RETRY_NOW;
84
85 if ((hn->i % ITERS_PRINT) == 0)
86 printf("%s did %d, x = %g\n", hn->str, hn->i, x);
87
88 return WQ_SUCCESS;
9f3f7a11 89}
90
d62a17ae 91static void clear_something(struct vty *vty, const char *str)
9f3f7a11 92{
d62a17ae 93 int i;
94
95 /* this could be like iterating through 150k of route_table
96 * or worse, iterating through a list of peers, to bgp_stop them with
97 * each having 150k route tables to process...
98 */
99 for (i = ITERS_FIRST; i < ITERS_MAX; i++)
100 heavy_wq_add(vty, str, i);
9f3f7a11 101}
102
103DEFUN (clear_foo,
104 clear_foo_cmd,
e961923c 105 "clear foo LINE...",
9f3f7a11 106 "clear command\n"
107 "arbitrary string\n")
108{
d62a17ae 109 char *str;
110 if (!argc) {
111 vty_out(vty, "%% string argument required\n");
112 return CMD_WARNING;
113 }
114
115 str = argv_concat(argv, argc, 0);
116
117 clear_something(vty, str);
118 XFREE(MTYPE_TMP, str);
119 return CMD_SUCCESS;
9f3f7a11 120}
121
4d762f26 122static int heavy_wq_init(void)
9f3f7a11 123{
13143701 124 heavy_wq = work_queue_new(master, "heavy_work_queue");
d62a17ae 125
126 heavy_wq->spec.workfunc = &slow_func;
d62a17ae 127 heavy_wq->spec.del_item_data = &slow_func_del;
128 heavy_wq->spec.max_retries = 3;
129 heavy_wq->spec.hold = 1000;
130
131 return 0;
9f3f7a11 132}
133
4d762f26 134void test_init(void)
9f3f7a11 135{
d62a17ae 136 install_element(VIEW_NODE, &clear_foo_cmd);
137 heavy_wq_init();
9f3f7a11 138}