blob: 577a4816f5a7022d346cb3bdcb2f8dc147a6309c [file] [log] [blame]
paul43313d02005-04-19 21:28:36 +00001/*
paul9f3f7a12005-04-25 16:42:24 +00002 * $Id: heavy.c,v 1.3 2005/04/25 16:42:24 paul Exp $
paul43313d02005-04-19 21:28:36 +00003 *
4 * This file is part of Quagga.
5 *
6 * Quagga is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * Quagga is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Quagga; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22/* This programme shows the effects of 'heavy' long-running functions
23 * on the cooperative threading model.
24 *
25 * Run it with a config file containing 'password whatever', telnet to it
26 * (it defaults to port 4000) and enter the 'clear foo string' command.
27 * then type whatever and observe that the vty interface is unresponsive
28 * for quite a period of time, due to the clear_something command
29 * taking a very long time to complete.
30 */
31#include <zebra.h>
32
paul43313d02005-04-19 21:28:36 +000033#include "thread.h"
34#include "vty.h"
35#include "command.h"
36#include "memory.h"
paulf4d062f2005-04-21 16:58:44 +000037#include <math.h>
paul43313d02005-04-19 21:28:36 +000038
paul43313d02005-04-19 21:28:36 +000039enum
40{
41 ITERS_FIRST = 0,
paulf4d062f2005-04-21 16:58:44 +000042 ITERS_ERR = 100,
43 ITERS_LATER = 400,
paul43313d02005-04-19 21:28:36 +000044 ITERS_PRINT = 10,
45 ITERS_MAX = 1000,
46};
47
48static void
paulf4d062f2005-04-21 16:58:44 +000049slow_func (struct vty *vty, const char *str, const int i)
paul43313d02005-04-19 21:28:36 +000050{
paulf4d062f2005-04-21 16:58:44 +000051 double x = 1;
52 int j;
53
paul9f3f7a12005-04-25 16:42:24 +000054 for (j = 0; j < 300; j++)
paulf4d062f2005-04-21 16:58:44 +000055 x += sin(x)*j;
56
57 if ((i % ITERS_LATER) == 0)
58 printf ("%s: %d, temporary error, save this somehow and do it later..\n",
59 __func__, i);
60
61 if ((i % ITERS_ERR) == 0)
62 printf ("%s: hard error\n", __func__);
63
paul43313d02005-04-19 21:28:36 +000064 if ((i % ITERS_PRINT) == 0)
paulf4d062f2005-04-21 16:58:44 +000065 printf ("%s did %d, x = %g%s", str, i, x, VTY_NEWLINE);
paul43313d02005-04-19 21:28:36 +000066}
67
68static void
paulf4d062f2005-04-21 16:58:44 +000069clear_something (struct vty *vty, const char *str)
paul43313d02005-04-19 21:28:36 +000070{
71 int i;
72
73 /* this could be like iterating through 150k of route_table
paulf4d062f2005-04-21 16:58:44 +000074 * or worse, iterating through a list of peers, to bgp_stop them with
75 * each having 150k route tables to process...
paul43313d02005-04-19 21:28:36 +000076 */
77 for (i = ITERS_FIRST; i < ITERS_MAX; i++)
78 slow_func (vty, str, i);
paulf4d062f2005-04-21 16:58:44 +000079}
80
paul43313d02005-04-19 21:28:36 +000081DEFUN (clear_foo,
82 clear_foo_cmd,
83 "clear foo .LINE",
84 "clear command\n"
85 "arbitrary string\n")
86{
87 char *str;
88 if (!argc)
89 {
90 vty_out (vty, "%% string argument required%s", VTY_NEWLINE);
91 return CMD_WARNING;
92 }
93
94 str = argv_concat (argv, argc, 0);
95
96 clear_something (vty, str);
paulf4d062f2005-04-21 16:58:44 +000097 XFREE (MTYPE_TMP, str);
paul43313d02005-04-19 21:28:36 +000098 return CMD_SUCCESS;
99}
100
101static void
102slow_vty_init()
103{
paulf4d062f2005-04-21 16:58:44 +0000104 install_element (VIEW_NODE, &clear_foo_cmd);
paul43313d02005-04-19 21:28:36 +0000105}
106
paul9f3f7a12005-04-25 16:42:24 +0000107void
108test_init()
paul43313d02005-04-19 21:28:36 +0000109{
paul9f3f7a12005-04-25 16:42:24 +0000110 slow_vty_init();
paul43313d02005-04-19 21:28:36 +0000111}