paul | 43313d0 | 2005-04-19 21:28:36 +0000 | [diff] [blame] | 1 | /* |
paul | 9f3f7a1 | 2005-04-25 16:42:24 +0000 | [diff] [blame] | 2 | * $Id: heavy.c,v 1.3 2005/04/25 16:42:24 paul Exp $ |
paul | 43313d0 | 2005-04-19 21:28:36 +0000 | [diff] [blame] | 3 | * |
| 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 | |
paul | 43313d0 | 2005-04-19 21:28:36 +0000 | [diff] [blame] | 33 | #include "thread.h" |
| 34 | #include "vty.h" |
| 35 | #include "command.h" |
| 36 | #include "memory.h" |
paul | f4d062f | 2005-04-21 16:58:44 +0000 | [diff] [blame] | 37 | #include <math.h> |
paul | 43313d0 | 2005-04-19 21:28:36 +0000 | [diff] [blame] | 38 | |
David Lamparter | c313895 | 2015-04-21 10:02:23 +0200 | [diff] [blame^] | 39 | #include "tests.h" |
| 40 | |
paul | 43313d0 | 2005-04-19 21:28:36 +0000 | [diff] [blame] | 41 | enum |
| 42 | { |
| 43 | ITERS_FIRST = 0, |
paul | f4d062f | 2005-04-21 16:58:44 +0000 | [diff] [blame] | 44 | ITERS_ERR = 100, |
| 45 | ITERS_LATER = 400, |
paul | 43313d0 | 2005-04-19 21:28:36 +0000 | [diff] [blame] | 46 | ITERS_PRINT = 10, |
| 47 | ITERS_MAX = 1000, |
| 48 | }; |
| 49 | |
| 50 | static void |
paul | f4d062f | 2005-04-21 16:58:44 +0000 | [diff] [blame] | 51 | slow_func (struct vty *vty, const char *str, const int i) |
paul | 43313d0 | 2005-04-19 21:28:36 +0000 | [diff] [blame] | 52 | { |
paul | f4d062f | 2005-04-21 16:58:44 +0000 | [diff] [blame] | 53 | double x = 1; |
| 54 | int j; |
| 55 | |
paul | 9f3f7a1 | 2005-04-25 16:42:24 +0000 | [diff] [blame] | 56 | for (j = 0; j < 300; j++) |
paul | f4d062f | 2005-04-21 16:58:44 +0000 | [diff] [blame] | 57 | x += sin(x)*j; |
| 58 | |
| 59 | if ((i % ITERS_LATER) == 0) |
| 60 | printf ("%s: %d, temporary error, save this somehow and do it later..\n", |
| 61 | __func__, i); |
| 62 | |
| 63 | if ((i % ITERS_ERR) == 0) |
| 64 | printf ("%s: hard error\n", __func__); |
| 65 | |
paul | 43313d0 | 2005-04-19 21:28:36 +0000 | [diff] [blame] | 66 | if ((i % ITERS_PRINT) == 0) |
paul | f4d062f | 2005-04-21 16:58:44 +0000 | [diff] [blame] | 67 | printf ("%s did %d, x = %g%s", str, i, x, VTY_NEWLINE); |
paul | 43313d0 | 2005-04-19 21:28:36 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static void |
paul | f4d062f | 2005-04-21 16:58:44 +0000 | [diff] [blame] | 71 | clear_something (struct vty *vty, const char *str) |
paul | 43313d0 | 2005-04-19 21:28:36 +0000 | [diff] [blame] | 72 | { |
| 73 | int i; |
| 74 | |
| 75 | /* this could be like iterating through 150k of route_table |
paul | f4d062f | 2005-04-21 16:58:44 +0000 | [diff] [blame] | 76 | * or worse, iterating through a list of peers, to bgp_stop them with |
| 77 | * each having 150k route tables to process... |
paul | 43313d0 | 2005-04-19 21:28:36 +0000 | [diff] [blame] | 78 | */ |
| 79 | for (i = ITERS_FIRST; i < ITERS_MAX; i++) |
| 80 | slow_func (vty, str, i); |
paul | f4d062f | 2005-04-21 16:58:44 +0000 | [diff] [blame] | 81 | } |
| 82 | |
paul | 43313d0 | 2005-04-19 21:28:36 +0000 | [diff] [blame] | 83 | DEFUN (clear_foo, |
| 84 | clear_foo_cmd, |
| 85 | "clear foo .LINE", |
| 86 | "clear command\n" |
| 87 | "arbitrary string\n") |
| 88 | { |
| 89 | char *str; |
| 90 | if (!argc) |
| 91 | { |
| 92 | vty_out (vty, "%% string argument required%s", VTY_NEWLINE); |
| 93 | return CMD_WARNING; |
| 94 | } |
| 95 | |
| 96 | str = argv_concat (argv, argc, 0); |
| 97 | |
| 98 | clear_something (vty, str); |
paul | f4d062f | 2005-04-21 16:58:44 +0000 | [diff] [blame] | 99 | XFREE (MTYPE_TMP, str); |
paul | 43313d0 | 2005-04-19 21:28:36 +0000 | [diff] [blame] | 100 | return CMD_SUCCESS; |
| 101 | } |
| 102 | |
| 103 | static void |
| 104 | slow_vty_init() |
| 105 | { |
paul | f4d062f | 2005-04-21 16:58:44 +0000 | [diff] [blame] | 106 | install_element (VIEW_NODE, &clear_foo_cmd); |
paul | 43313d0 | 2005-04-19 21:28:36 +0000 | [diff] [blame] | 107 | } |
| 108 | |
paul | 9f3f7a1 | 2005-04-25 16:42:24 +0000 | [diff] [blame] | 109 | void |
| 110 | test_init() |
paul | 43313d0 | 2005-04-19 21:28:36 +0000 | [diff] [blame] | 111 | { |
paul | 9f3f7a1 | 2005-04-25 16:42:24 +0000 | [diff] [blame] | 112 | slow_vty_init(); |
paul | 43313d0 | 2005-04-19 21:28:36 +0000 | [diff] [blame] | 113 | } |