]> git.proxmox.com Git - mirror_frr.git/blame - tests/lib/test_heavy_thread.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / tests / lib / test_heavy_thread.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
864c364d 2/*
864c364d 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
864c364d 17#include "thread.h"
18#include "vty.h"
19#include "command.h"
20#include "memory.h"
21#include "log.h"
22
9fc3f9b3
DL
23#include "tests.h"
24
9f3f7a11 25extern struct thread_master *master;
864c364d 26
d62a17ae 27enum { ITERS_FIRST = 0,
28 ITERS_ERR = 100,
29 ITERS_LATER = 400,
30 ITERS_PRINT = 10,
31 ITERS_MAX = 1000,
864c364d 32};
33
34struct work_state {
d62a17ae 35 struct vty *vty;
36 char *str;
37 int i;
864c364d 38};
39
d62a17ae 40static void slow_func(struct vty *vty, const char *str, const int i)
864c364d 41{
d62a17ae 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);
864c364d 57}
58
cc9f21da 59static void clear_something(struct thread *thread)
864c364d 60{
d62a17ae 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);
cc9f21da 73 return;
d62a17ae 74 }
75 }
76
77 /* All done! */
78 XFREE(MTYPE_TMP, ws->str);
79 XFREE(MTYPE_TMP, ws);
864c364d 80}
81
864c364d 82DEFUN (clear_foo,
83 clear_foo_cmd,
e961923c 84 "clear foo LINE...",
864c364d 85 "clear command\n"
86 "arbitrary string\n")
87{
d62a17ae 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
13143701 98 ws = XMALLOC(MTYPE_TMP, sizeof(*ws));
d62a17ae 99
13143701 100 ws->str = XSTRDUP(MTYPE_TMP, str);
d62a17ae 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;
864c364d 108}
109
4d762f26 110void test_init(void)
864c364d 111{
d62a17ae 112 install_element(VIEW_NODE, &clear_foo_cmd);
864c364d 113}