heavy-thread.c 3.4 KB

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