summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests/ChangeLog12
-rw-r--r--tests/Makefile.am10
-rw-r--r--tests/heavy-thread.c171
-rw-r--r--tests/heavy-wq.c178
-rw-r--r--tests/heavy.c167
-rw-r--r--tests/main.c199
6 files changed, 405 insertions, 332 deletions
diff --git a/tests/ChangeLog b/tests/ChangeLog
index f3ec6cf9..923a3ca6 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,15 @@
+2004-05-25 Paul Jakma <paul@dishone.st>
+
+ * main.c: new file, common 'main' portion which then calls
+ test_init.
+ * heavy.c: remove common portions now in 'main.
+ * heavy-wq.c: ditto
+ * heavy-thread.c: ditto
+ * heavy*.c: Set the slow_function iteration number lower, to 300, it
+ was several orders of magnitude too 'slow' to be useful. :)
+ * Makefile.am: main is a source of the tests. heavy should link
+ to the math library.
+
2004-05-22 Paul Jakma <paul@dishone.st>
* heavy-thread.c: Andrew Schorr's adaptation of heavy.c to use a
diff --git a/tests/Makefile.am b/tests/Makefile.am
index e5d8739e..67ea37ee 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,15 +1,17 @@
INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib
DEFS = @DEFS@ $(LOCAL_OPTS) -DSYSCONFDIR=\"$(sysconfdir)/\"
-noinst_PROGRAMS = testsig testbuffer testmemory heavy heavythread
+noinst_PROGRAMS = testsig testbuffer testmemory heavy heavywq heavythread
testsig_SOURCES = test-sig.c
testbuffer_SOURCES = test-buffer.c
testmemory_SOURCES = test-memory.c
-heavy_SOURCES = heavy.c
-heavythread_SOURCES = heavy-thread.c
+heavy_SOURCES = heavy.c main.c
+heavywq_SOURCES = heavy-wq.c main.c
+heavythread_SOURCES = heavy-thread.c main.c
testsig_LDADD = ../lib/libzebra.la @LIBCAP@
testbuffer_LDADD = ../lib/libzebra.la @LIBCAP@
testmemory_LDADD = ../lib/libzebra.la @LIBCAP@
-heavy_LDADD = ../lib/libzebra.la @LIBCAP@
+heavy_LDADD = ../lib/libzebra.la @LIBCAP@ -lm
+heavywq_LDADD = ../lib/libzebra.la @LIBCAP@ -lm
heavythread_LDADD = ../lib/libzebra.la @LIBCAP@ -lm
diff --git a/tests/heavy-thread.c b/tests/heavy-thread.c
index 252c2960..cd3a3b9d 100644
--- a/tests/heavy-thread.c
+++ b/tests/heavy-thread.c
@@ -1,5 +1,5 @@
/*
- * $Id: heavy-thread.c,v 1.1 2005/04/22 00:50:08 paul Exp $
+ * $Id: heavy-thread.c,v 1.2 2005/04/25 16:42:24 paul Exp $
*
* This file is part of Quagga.
*
@@ -31,26 +31,13 @@
#include <zebra.h>
#include <math.h>
-#include <lib/version.h>
-#include "getopt.h"
#include "thread.h"
#include "vty.h"
#include "command.h"
#include "memory.h"
#include "log.h"
-struct thread_master *master;
-
-struct option longopts[] =
-{
- { "daemon", no_argument, NULL, 'd'},
- { "config_file", required_argument, NULL, 'f'},
- { "help", no_argument, NULL, 'h'},
- { "vty_addr", required_argument, NULL, 'A'},
- { "vty_port", required_argument, NULL, 'P'},
- { "version", no_argument, NULL, 'v'},
- { 0 }
-};
+extern struct thread_master *master;
enum
{
@@ -73,7 +60,7 @@ slow_func (struct vty *vty, const char *str, const int i)
double x = 1;
int j;
- for (j = 0; j < 30000; j++)
+ for (j = 0; j < 300; j++)
x += sin(x)*j;
if ((i % ITERS_LATER) == 0)
@@ -113,14 +100,6 @@ clear_something (struct thread *thread)
return 0;
}
-DEFUN (daemon_exit,
- daemon_exit_cmd,
- "daemon-exit",
- "Make the daemon exit\n")
-{
- exit(0);
-}
-
DEFUN (clear_foo,
clear_foo_cmd,
"clear foo .LINE",
@@ -159,148 +138,8 @@ DEFUN (clear_foo,
return CMD_SUCCESS;
}
-static int timer_count;
-int
-heavy_timer (struct thread *thread)
-{
- int *count = THREAD_ARG(thread);
-
- printf ("run %d of timer\n", (*count)++);
- thread_add_timer (master, heavy_timer, count, 5);
- return 0;
-}
-
-static void
-heavy_timer_init()
-{
- thread_add_timer (master, heavy_timer, &timer_count, 5);
-}
-
-static void
-slow_vty_init()
+void
+test_init()
{
install_element (VIEW_NODE, &clear_foo_cmd);
- install_element (VIEW_NODE, &daemon_exit_cmd);
-}
-
-/* Help information display. */
-static void
-usage (char *progname, int status)
-{
- if (status != 0)
- fprintf (stderr, "Try `%s --help' for more information.\n", progname);
- else
- {
- printf ("Usage : %s [OPTION...]\n\
-Daemon which does 'slow' things.\n\n\
--d, --daemon Runs in daemon mode\n\
--f, --config_file Set configuration file name\n\
--A, --vty_addr Set vty's bind address\n\
--P, --vty_port Set vty's port number\n\
--v, --version Print program version\n\
--h, --help Display this help and exit\n\
-\n\
-Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
- }
- exit (status);
}
-
-
-/* main routine. */
-int
-main (int argc, char **argv)
-{
- char *p;
- char *vty_addr = NULL;
- int vty_port = 4000;
- int daemon_mode = 0;
- char *progname;
- struct thread thread;
- char *config_file = NULL;
-
- /* Set umask before anything for security */
- umask (0027);
-
- /* get program name */
- progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
-
- /* master init. */
- master = thread_master_create ();
-
- while (1)
- {
- int opt;
-
- opt = getopt_long (argc, argv, "dhf:A:P:v", longopts, 0);
-
- if (opt == EOF)
- break;
-
- switch (opt)
- {
- case 0:
- break;
- case 'f':
- config_file = optarg;
- break;
- case 'd':
- daemon_mode = 1;
- break;
- case 'A':
- vty_addr = optarg;
- break;
- case 'P':
- /* Deal with atoi() returning 0 on failure */
- if (strcmp(optarg, "0") == 0)
- {
- vty_port = 0;
- break;
- }
- vty_port = atoi (optarg);
- vty_port = (vty_port ? vty_port : 4000);
- break;
- case 'v':
- print_version (progname);
- exit (0);
- break;
- case 'h':
- usage (progname, 0);
- break;
- default:
- usage (progname, 1);
- break;
- }
- }
-
- /* Library inits. */
- cmd_init (1);
- vty_init (master);
- memory_init ();
-
- /* OSPF vty inits. */
- slow_vty_init ();
-
- sort_node ();
-
- /* Change to the daemon program. */
- if (daemon_mode)
- daemon (0, 0);
-
- /* Create VTY socket */
- vty_serv_sock (vty_addr, vty_port, "/tmp/.heavy.sock");
-
- /* Configuration file read*/
- if (!config_file)
- usage (progname, 1);
- vty_read_config (config_file, NULL);
-
- heavy_timer_init();
-
- /* Fetch next active thread. */
- while (thread_fetch (master, &thread))
- thread_call (&thread);
-
- /* Not reached. */
- exit (0);
-}
-
diff --git a/tests/heavy-wq.c b/tests/heavy-wq.c
new file mode 100644
index 00000000..093fbd41
--- /dev/null
+++ b/tests/heavy-wq.c
@@ -0,0 +1,178 @@
+/*
+ * $Id: heavy-wq.c,v 1.1 2005/04/25 16:42:24 paul Exp $
+ *
+ * This file is part of Quagga.
+ *
+ * Quagga is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * Quagga is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Quagga; see the file COPYING. If not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
+
+/* This programme shows the effects of 'heavy' long-running functions
+ * on the cooperative threading model.
+ *
+ * Run it with a config file containing 'password whatever', telnet to it
+ * (it defaults to port 4000) and enter the 'clear foo string' command.
+ * then type whatever and observe that the vty interface is unresponsive
+ * for quite a period of time, due to the clear_something command
+ * taking a very long time to complete.
+ */
+#include <zebra.h>
+
+#include "thread.h"
+#include "vty.h"
+#include "command.h"
+#include "memory.h"
+#include "log.h"
+#include "workqueue.h"
+#include <math.h>
+
+extern struct thread_master *master;
+static struct work_queue *heavy_wq;
+
+struct heavy_wq_node
+{
+ char *str;
+ int i;
+};
+
+enum
+{
+ ITERS_FIRST = 0,
+ ITERS_ERR = 100,
+ ITERS_LATER = 400,
+ ITERS_PRINT = 10,
+ ITERS_MAX = 1000,
+};
+
+static void
+heavy_wq_add (struct vty *vty, const char *str, int i)
+{
+ struct heavy_wq_node *hn;
+
+ if ((hn = XCALLOC (MTYPE_PREFIX_LIST, sizeof(struct heavy_wq_node))) == NULL)
+ {
+ zlog_err ("%s: unable to allocate hn", __func__);
+ return;
+ }
+
+ hn->i = i;
+ if (!(hn->str = XSTRDUP (MTYPE_PREFIX_LIST_STR, str)))
+ {
+ zlog_err ("%s: unable to xstrdup", __func__);
+ XFREE (MTYPE_PREFIX_LIST, hn);
+ return;
+ }
+
+ work_queue_add (heavy_wq, hn);
+
+ return;
+}
+
+static void
+slow_func_err (struct work_queue *wq, struct work_queue_item *item)
+{
+ printf ("%s: running error function\n", __func__);
+}
+
+static void
+slow_func_del (struct heavy_wq_node *hn)
+{
+ assert (hn && hn->str);
+ XFREE (MTYPE_PREFIX_LIST_STR, hn->str);
+ hn->str = NULL;
+ XFREE(MTYPE_PREFIX_LIST, hn);
+}
+
+static wq_item_status
+slow_func (struct heavy_wq_node *hn)
+{
+ double x = 1;
+ int j;
+
+ assert (hn && hn->str);
+
+ for (j = 0; j < 300; j++)
+ x += sin(x)*j;
+
+ if ((hn->i % ITERS_LATER) == 0)
+ return WQ_RETRY_LATER;
+
+ if ((hn->i % ITERS_ERR) == 0)
+ return WQ_RETRY_NOW;
+
+ if ((hn->i % ITERS_PRINT) == 0)
+ printf ("%s did %d, x = %g\n", hn->str, hn->i, x);
+
+ return WQ_SUCCESS;
+}
+
+static void
+clear_something (struct vty *vty, const char *str)
+{
+ int i;
+
+ /* this could be like iterating through 150k of route_table
+ * or worse, iterating through a list of peers, to bgp_stop them with
+ * each having 150k route tables to process...
+ */
+ for (i = ITERS_FIRST; i < ITERS_MAX; i++)
+ heavy_wq_add (vty, str, i);
+}
+
+DEFUN (clear_foo,
+ clear_foo_cmd,
+ "clear foo .LINE",
+ "clear command\n"
+ "arbitrary string\n")
+{
+ char *str;
+ if (!argc)
+ {
+ vty_out (vty, "%% string argument required%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+
+ str = argv_concat (argv, argc, 0);
+
+ clear_something (vty, str);
+ XFREE (MTYPE_TMP, str);
+ return CMD_SUCCESS;
+}
+
+static int
+heavy_wq_init ()
+{
+ if (! (heavy_wq = work_queue_new (master, "heavy_work_queue")))
+ {
+ zlog_err ("%s: could not get new work queue!", __func__);
+ return -1;
+ }
+
+ heavy_wq->spec.workfunc = &slow_func;
+ heavy_wq->spec.errorfunc = &slow_func_err;
+ heavy_wq->spec.del_item_data = &slow_func_del;
+ heavy_wq->spec.max_retries = 3;
+ heavy_wq->spec.delay = 10;
+ heavy_wq->spec.hold = 1000;
+
+ return 0;
+}
+
+void
+test_init()
+{
+ install_element (VIEW_NODE, &clear_foo_cmd);
+ heavy_wq_init();
+}
diff --git a/tests/heavy.c b/tests/heavy.c
index 4b0fc732..577a4816 100644
--- a/tests/heavy.c
+++ b/tests/heavy.c
@@ -1,5 +1,5 @@
/*
- * $Id: heavy.c,v 1.2 2005/04/21 16:58:44 paul Exp $
+ * $Id: heavy.c,v 1.3 2005/04/25 16:42:24 paul Exp $
*
* This file is part of Quagga.
*
@@ -30,27 +30,12 @@
*/
#include <zebra.h>
-#include <lib/version.h>
-#include "getopt.h"
#include "thread.h"
#include "vty.h"
#include "command.h"
#include "memory.h"
#include <math.h>
-struct thread_master *master;
-
-struct option longopts[] =
-{
- { "daemon", no_argument, NULL, 'd'},
- { "config_file", required_argument, NULL, 'f'},
- { "help", no_argument, NULL, 'h'},
- { "vty_addr", required_argument, NULL, 'A'},
- { "vty_port", required_argument, NULL, 'P'},
- { "version", no_argument, NULL, 'v'},
- { 0 }
-};
-
enum
{
ITERS_FIRST = 0,
@@ -66,7 +51,7 @@ slow_func (struct vty *vty, const char *str, const int i)
double x = 1;
int j;
- for (j = 0; j < 300000; j++)
+ for (j = 0; j < 300; j++)
x += sin(x)*j;
if ((i % ITERS_LATER) == 0)
@@ -93,14 +78,6 @@ clear_something (struct vty *vty, const char *str)
slow_func (vty, str, i);
}
-DEFUN (daemon_exit,
- daemon_exit_cmd,
- "daemon-exit",
- "Make the daemon exit\n")
-{
- exit(0);
-}
-
DEFUN (clear_foo,
clear_foo_cmd,
"clear foo .LINE",
@@ -121,148 +98,14 @@ DEFUN (clear_foo,
return CMD_SUCCESS;
}
-static int timer_count;
-int
-heavy_timer (struct thread *thread)
-{
- int *count = THREAD_ARG(thread);
-
- printf ("run %d of timer\n", (*count)++);
- thread_add_timer (master, heavy_timer, count, 5);
- return 0;
-}
-
-static void
-heavy_timer_init()
-{
- thread_add_timer (master, heavy_timer, &timer_count, 5);
-}
-
static void
slow_vty_init()
{
install_element (VIEW_NODE, &clear_foo_cmd);
- install_element (VIEW_NODE, &daemon_exit_cmd);
}
-/* Help information display. */
-static void
-usage (char *progname, int status)
+void
+test_init()
{
- if (status != 0)
- fprintf (stderr, "Try `%s --help' for more information.\n", progname);
- else
- {
- printf ("Usage : %s [OPTION...]\n\
-Daemon which does 'slow' things.\n\n\
--d, --daemon Runs in daemon mode\n\
--f, --config_file Set configuration file name\n\
--A, --vty_addr Set vty's bind address\n\
--P, --vty_port Set vty's port number\n\
--v, --version Print program version\n\
--h, --help Display this help and exit\n\
-\n\
-Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
- }
- exit (status);
+ slow_vty_init();
}
-
-
-/* main routine. */
-int
-main (int argc, char **argv)
-{
- char *p;
- char *vty_addr = NULL;
- int vty_port = 4000;
- int daemon_mode = 0;
- char *progname;
- struct thread thread;
- char *config_file = NULL;
-
- /* Set umask before anything for security */
- umask (0027);
-
- /* get program name */
- progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
-
- /* master init. */
- master = thread_master_create ();
-
- while (1)
- {
- int opt;
-
- opt = getopt_long (argc, argv, "dhf:A:P:v", longopts, 0);
-
- if (opt == EOF)
- break;
-
- switch (opt)
- {
- case 0:
- break;
- case 'f':
- config_file = optarg;
- break;
- case 'd':
- daemon_mode = 1;
- break;
- case 'A':
- vty_addr = optarg;
- break;
- case 'P':
- /* Deal with atoi() returning 0 on failure */
- if (strcmp(optarg, "0") == 0)
- {
- vty_port = 0;
- break;
- }
- vty_port = atoi (optarg);
- vty_port = (vty_port ? vty_port : 4000);
- break;
- case 'v':
- print_version (progname);
- exit (0);
- break;
- case 'h':
- usage (progname, 0);
- break;
- default:
- usage (progname, 1);
- break;
- }
- }
-
- /* Library inits. */
- cmd_init (1);
- vty_init (master);
- memory_init ();
-
- /* OSPF vty inits. */
- slow_vty_init ();
-
- sort_node ();
-
- /* Change to the daemon program. */
- if (daemon_mode)
- daemon (0, 0);
-
- /* Create VTY socket */
- vty_serv_sock (vty_addr, vty_port, "/tmp/.heavy.sock");
-
- /* Configuration file read*/
- if (!config_file)
- usage (progname, 1);
- vty_read_config (config_file, NULL);
-
- heavy_timer_init();
-
- /* Fetch next active thread. */
- while (thread_fetch (master, &thread))
- thread_call (&thread);
-
- /* Not reached. */
- exit (0);
-}
-
diff --git a/tests/main.c b/tests/main.c
new file mode 100644
index 00000000..edc3b2de
--- /dev/null
+++ b/tests/main.c
@@ -0,0 +1,199 @@
+/*
+ * $Id: main.c,v 1.1 2005/04/25 16:42:24 paul Exp $
+ *
+ * This file is part of Quagga.
+ *
+ * Quagga is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * Quagga is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Quagga; see the file COPYING. If not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
+
+#include <zebra.h>
+
+#include <lib/version.h>
+#include "getopt.h"
+#include "thread.h"
+#include "vty.h"
+#include "command.h"
+#include "memory.h"
+
+extern void test_init();
+
+struct thread_master *master;
+
+struct option longopts[] =
+{
+ { "daemon", no_argument, NULL, 'd'},
+ { "config_file", required_argument, NULL, 'f'},
+ { "help", no_argument, NULL, 'h'},
+ { "vty_addr", required_argument, NULL, 'A'},
+ { "vty_port", required_argument, NULL, 'P'},
+ { "version", no_argument, NULL, 'v'},
+ { 0 }
+};
+
+DEFUN (daemon_exit,
+ daemon_exit_cmd,
+ "daemon-exit",
+ "Make the daemon exit\n")
+{
+ exit(0);
+}
+
+static int timer_count;
+int
+test_timer (struct thread *thread)
+{
+ int *count = THREAD_ARG(thread);
+
+ printf ("run %d of timer\n", (*count)++);
+ thread_add_timer (master, test_timer, count, 5);
+ return 0;
+}
+
+static void
+test_timer_init()
+{
+ thread_add_timer (master, test_timer, &timer_count, 10);
+}
+
+static void
+test_vty_init()
+{
+ install_element (VIEW_NODE, &daemon_exit_cmd);
+}
+
+/* Help information display. */
+static void
+usage (char *progname, int status)
+{
+ if (status != 0)
+ fprintf (stderr, "Try `%s --help' for more information.\n", progname);
+ else
+ {
+ printf ("Usage : %s [OPTION...]\n\
+Daemon which does 'slow' things.\n\n\
+-d, --daemon Runs in daemon mode\n\
+-f, --config_file Set configuration file name\n\
+-A, --vty_addr Set vty's bind address\n\
+-P, --vty_port Set vty's port number\n\
+-v, --version Print program version\n\
+-h, --help Display this help and exit\n\
+\n\
+Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
+ }
+ exit (status);
+}
+
+
+/* main routine. */
+int
+main (int argc, char **argv)
+{
+ char *p;
+ char *vty_addr = NULL;
+ int vty_port = 4000;
+ int daemon_mode = 0;
+ char *progname;
+ struct thread thread;
+ char *config_file = NULL;
+
+ /* Set umask before anything for security */
+ umask (0027);
+
+ /* get program name */
+ progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
+
+ /* master init. */
+ master = thread_master_create ();
+
+ while (1)
+ {
+ int opt;
+
+ opt = getopt_long (argc, argv, "dhf:A:P:v", longopts, 0);
+
+ if (opt == EOF)
+ break;
+
+ switch (opt)
+ {
+ case 0:
+ break;
+ case 'f':
+ config_file = optarg;
+ break;
+ case 'd':
+ daemon_mode = 1;
+ break;
+ case 'A':
+ vty_addr = optarg;
+ break;
+ case 'P':
+ /* Deal with atoi() returning 0 on failure */
+ if (strcmp(optarg, "0") == 0)
+ {
+ vty_port = 0;
+ break;
+ }
+ vty_port = atoi (optarg);
+ vty_port = (vty_port ? vty_port : 4000);
+ break;
+ case 'v':
+ print_version (progname);
+ exit (0);
+ break;
+ case 'h':
+ usage (progname, 0);
+ break;
+ default:
+ usage (progname, 1);
+ break;
+ }
+ }
+
+ /* Library inits. */
+ cmd_init (1);
+ vty_init (master);
+ memory_init ();
+
+ /* OSPF vty inits. */
+ test_vty_init ();
+
+ sort_node ();
+
+ /* Change to the daemon program. */
+ if (daemon_mode)
+ daemon (0, 0);
+
+ /* Create VTY socket */
+ vty_serv_sock (vty_addr, vty_port, "/tmp/.heavy.sock");
+
+ /* Configuration file read*/
+ if (!config_file)
+ usage (progname, 1);
+ vty_read_config (config_file, NULL);
+
+ test_timer_init();
+
+ test_init();
+
+ /* Fetch next active thread. */
+ while (thread_fetch (master, &thread))
+ thread_call (&thread);
+
+ /* Not reached. */
+ exit (0);
+}
+