heavy-thread.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * $Id: heavy-thread.c,v 1.2 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, as demonstrated by heavy.c, and how
  23. * they can be mitigated using a background thread.
  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, unlike heavy.c, the vty interface
  28. * remains responsive.
  29. */
  30. #include <zebra.h>
  31. #include <math.h>
  32. #include "thread.h"
  33. #include "vty.h"
  34. #include "command.h"
  35. #include "memory.h"
  36. #include "log.h"
  37. #include "tests.h"
  38. extern struct thread_master *master;
  39. enum
  40. {
  41. ITERS_FIRST = 0,
  42. ITERS_ERR = 100,
  43. ITERS_LATER = 400,
  44. ITERS_PRINT = 10,
  45. ITERS_MAX = 1000,
  46. };
  47. struct work_state {
  48. struct vty *vty;
  49. char *str;
  50. int i;
  51. };
  52. static void
  53. slow_func (struct vty *vty, const char *str, const int i)
  54. {
  55. double x = 1;
  56. int j;
  57. for (j = 0; j < 300; j++)
  58. x += sin(x)*j;
  59. if ((i % ITERS_LATER) == 0)
  60. printf ("%s: %d, temporary error, save this somehow and do it later..\n",
  61. __func__, i);
  62. if ((i % ITERS_ERR) == 0)
  63. printf ("%s: hard error\n", __func__);
  64. if ((i % ITERS_PRINT) == 0)
  65. printf ("%s did %d, x = %g\n", str, i, x);
  66. }
  67. static int
  68. clear_something (struct thread *thread)
  69. {
  70. struct work_state *ws = THREAD_ARG(thread);
  71. /* this could be like iterating through 150k of route_table
  72. * or worse, iterating through a list of peers, to bgp_stop them with
  73. * each having 150k route tables to process...
  74. */
  75. while (ws->i < ITERS_MAX)
  76. {
  77. slow_func(ws->vty, ws->str, ws->i);
  78. ws->i++;
  79. if (thread_should_yield(thread))
  80. {
  81. thread_add_background(master, clear_something, ws, 0);
  82. return 0;
  83. }
  84. }
  85. /* All done! */
  86. XFREE (MTYPE_TMP, ws->str);
  87. XFREE (MTYPE_TMP, ws);
  88. return 0;
  89. }
  90. DEFUN (clear_foo,
  91. clear_foo_cmd,
  92. "clear foo .LINE",
  93. "clear command\n"
  94. "arbitrary string\n")
  95. {
  96. char *str;
  97. struct work_state *ws;
  98. if (!argc)
  99. {
  100. vty_out (vty, "%% string argument required%s", VTY_NEWLINE);
  101. return CMD_WARNING;
  102. }
  103. str = argv_concat (argv, argc, 0);
  104. if ((ws = XMALLOC(MTYPE_TMP, sizeof(*ws))) == NULL)
  105. {
  106. zlog_err ("%s: unable to allocate work_state", __func__);
  107. return CMD_WARNING;
  108. }
  109. if (!(ws->str = XSTRDUP (MTYPE_TMP, str)))
  110. {
  111. zlog_err ("%s: unable to xstrdup", __func__);
  112. XFREE (MTYPE_TMP, ws);
  113. return CMD_WARNING;
  114. }
  115. ws->vty = vty;
  116. ws->i = ITERS_FIRST;
  117. thread_add_background(master, clear_something, ws, 0);
  118. return CMD_SUCCESS;
  119. }
  120. void
  121. test_init()
  122. {
  123. install_element (VIEW_NODE, &clear_foo_cmd);
  124. }