]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_heavy_thread.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
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.
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
25 extern struct thread_master
*master
;
27 enum { ITERS_FIRST
= 0,
40 static void slow_func(struct vty
*vty
, const char *str
, const int i
)
45 for (j
= 0; j
< 300; j
++)
48 if ((i
% ITERS_LATER
) == 0)
49 printf("%s: %d, temporary error, save this somehow and do it later..\n",
52 if ((i
% ITERS_ERR
) == 0)
53 printf("%s: hard error\n", __func__
);
55 if ((i
% ITERS_PRINT
) == 0)
56 printf("%s did %d, x = %g\n", str
, i
, x
);
59 static void clear_something(struct thread
*thread
)
61 struct work_state
*ws
= THREAD_ARG(thread
);
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...
67 while (ws
->i
< ITERS_MAX
) {
68 slow_func(ws
->vty
, ws
->str
, ws
->i
);
70 if (thread_should_yield(thread
)) {
71 thread_add_timer_msec(master
, clear_something
, ws
, 0,
78 XFREE(MTYPE_TMP
, ws
->str
);
89 struct work_state
*ws
;
92 vty_out(vty
, "%% string argument required\n");
96 str
= argv_concat(argv
, argc
, 0);
98 ws
= XMALLOC(MTYPE_TMP
, sizeof(*ws
));
100 ws
->str
= XSTRDUP(MTYPE_TMP
, str
);
105 thread_add_timer_msec(master
, clear_something
, ws
, 0, NULL
);
112 install_element(VIEW_NODE
, &clear_foo_cmd
);