heavy.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * $Id: heavy.c,v 1.3 2005/04/25 16:42:24 paul Exp $
  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. /* This programme shows the effects of 'heavy' long-running functions
  22. * on the cooperative threading model.
  23. *
  24. * Run it with a config file containing 'password whatever', telnet to it
  25. * (it defaults to port 4000) and enter the 'clear foo string' command.
  26. * then type whatever and observe that the vty interface is unresponsive
  27. * for quite a period of time, due to the clear_something command
  28. * taking a very long time to complete.
  29. */
  30. #include <zebra.h>
  31. #include "thread.h"
  32. #include "vty.h"
  33. #include "command.h"
  34. #include "memory.h"
  35. #include <math.h>
  36. #include "tests.h"
  37. enum
  38. {
  39. ITERS_FIRST = 0,
  40. ITERS_ERR = 100,
  41. ITERS_LATER = 400,
  42. ITERS_PRINT = 10,
  43. ITERS_MAX = 1000,
  44. };
  45. static void
  46. slow_func (struct vty *vty, const char *str, const int i)
  47. {
  48. double x = 1;
  49. int j;
  50. for (j = 0; j < 300; j++)
  51. x += sin(x)*j;
  52. if ((i % ITERS_LATER) == 0)
  53. printf ("%s: %d, temporary error, save this somehow and do it later..\n",
  54. __func__, i);
  55. if ((i % ITERS_ERR) == 0)
  56. printf ("%s: hard error\n", __func__);
  57. if ((i % ITERS_PRINT) == 0)
  58. printf ("%s did %d, x = %g%s", str, i, x, VTY_NEWLINE);
  59. }
  60. static void
  61. clear_something (struct vty *vty, const char *str)
  62. {
  63. int i;
  64. /* this could be like iterating through 150k of route_table
  65. * or worse, iterating through a list of peers, to bgp_stop them with
  66. * each having 150k route tables to process...
  67. */
  68. for (i = ITERS_FIRST; i < ITERS_MAX; i++)
  69. slow_func (vty, str, i);
  70. }
  71. DEFUN (clear_foo,
  72. clear_foo_cmd,
  73. "clear foo .LINE",
  74. "clear command\n"
  75. "arbitrary string\n")
  76. {
  77. char *str;
  78. if (!argc)
  79. {
  80. vty_out (vty, "%% string argument required%s", VTY_NEWLINE);
  81. return CMD_WARNING;
  82. }
  83. str = argv_concat (argv, argc, 0);
  84. clear_something (vty, str);
  85. XFREE (MTYPE_TMP, str);
  86. return CMD_SUCCESS;
  87. }
  88. static void
  89. slow_vty_init()
  90. {
  91. install_element (VIEW_NODE, &clear_foo_cmd);
  92. }
  93. void
  94. test_init()
  95. {
  96. slow_vty_init();
  97. }