heavy-wq.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * This file is part of Quagga.
  3. *
  4. * Quagga is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2, or (at your option) any
  7. * later version.
  8. *
  9. * Quagga is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with Quagga; see the file COPYING. If not, write to the Free
  16. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  17. * 02111-1307, USA.
  18. */
  19. /* This programme shows the effects of 'heavy' long-running functions
  20. * on the cooperative threading model.
  21. *
  22. * Run it with a config file containing 'password whatever', telnet to it
  23. * (it defaults to port 4000) and enter the 'clear foo string' command.
  24. * then type whatever and observe that the vty interface is unresponsive
  25. * for quite a period of time, due to the clear_something command
  26. * taking a very long time to complete.
  27. */
  28. #include <zebra.h>
  29. #include "thread.h"
  30. #include "vty.h"
  31. #include "command.h"
  32. #include "memory.h"
  33. #include "log.h"
  34. #include "workqueue.h"
  35. #include <math.h>
  36. #include "tests.h"
  37. extern struct thread_master *master;
  38. static struct work_queue *heavy_wq;
  39. struct heavy_wq_node
  40. {
  41. char *str;
  42. int i;
  43. };
  44. enum
  45. {
  46. ITERS_FIRST = 0,
  47. ITERS_ERR = 100,
  48. ITERS_LATER = 400,
  49. ITERS_PRINT = 10,
  50. ITERS_MAX = 1000,
  51. };
  52. static void
  53. heavy_wq_add (struct vty *vty, const char *str, int i)
  54. {
  55. struct heavy_wq_node *hn;
  56. if ((hn = XCALLOC (MTYPE_PREFIX_LIST, sizeof(struct heavy_wq_node))) == NULL)
  57. {
  58. zlog_err ("%s: unable to allocate hn", __func__);
  59. return;
  60. }
  61. hn->i = i;
  62. if (!(hn->str = XSTRDUP (MTYPE_PREFIX_LIST_STR, str)))
  63. {
  64. zlog_err ("%s: unable to xstrdup", __func__);
  65. XFREE (MTYPE_PREFIX_LIST, hn);
  66. return;
  67. }
  68. work_queue_add (heavy_wq, hn);
  69. return;
  70. }
  71. static void
  72. slow_func_err (struct work_queue *wq, struct work_queue_item *item)
  73. {
  74. printf ("%s: running error function\n", __func__);
  75. }
  76. static void
  77. slow_func_del (struct work_queue *wq, void *data)
  78. {
  79. struct heavy_wq_node *hn = data;
  80. assert (hn && hn->str);
  81. printf ("%s: %s\n", __func__, hn->str);
  82. XFREE (MTYPE_PREFIX_LIST_STR, hn->str);
  83. hn->str = NULL;
  84. XFREE(MTYPE_PREFIX_LIST, hn);
  85. }
  86. static wq_item_status
  87. slow_func (struct work_queue *wq, void *data)
  88. {
  89. struct heavy_wq_node *hn = data;
  90. double x = 1;
  91. int j;
  92. assert (hn && hn->str);
  93. for (j = 0; j < 300; j++)
  94. x += sin(x)*j;
  95. if ((hn->i % ITERS_LATER) == 0)
  96. return WQ_RETRY_LATER;
  97. if ((hn->i % ITERS_ERR) == 0)
  98. return WQ_RETRY_NOW;
  99. if ((hn->i % ITERS_PRINT) == 0)
  100. printf ("%s did %d, x = %g\n", hn->str, hn->i, x);
  101. return WQ_SUCCESS;
  102. }
  103. static void
  104. clear_something (struct vty *vty, const char *str)
  105. {
  106. int i;
  107. /* this could be like iterating through 150k of route_table
  108. * or worse, iterating through a list of peers, to bgp_stop them with
  109. * each having 150k route tables to process...
  110. */
  111. for (i = ITERS_FIRST; i < ITERS_MAX; i++)
  112. heavy_wq_add (vty, str, i);
  113. }
  114. DEFUN (clear_foo,
  115. clear_foo_cmd,
  116. "clear foo .LINE",
  117. "clear command\n"
  118. "arbitrary string\n")
  119. {
  120. char *str;
  121. if (!argc)
  122. {
  123. vty_out (vty, "%% string argument required%s", VTY_NEWLINE);
  124. return CMD_WARNING;
  125. }
  126. str = argv_concat (argv, argc, 0);
  127. clear_something (vty, str);
  128. XFREE (MTYPE_TMP, str);
  129. return CMD_SUCCESS;
  130. }
  131. static int
  132. heavy_wq_init ()
  133. {
  134. if (! (heavy_wq = work_queue_new (master, "heavy_work_queue")))
  135. {
  136. zlog_err ("%s: could not get new work queue!", __func__);
  137. return -1;
  138. }
  139. heavy_wq->spec.workfunc = &slow_func;
  140. heavy_wq->spec.errorfunc = &slow_func_err;
  141. heavy_wq->spec.del_item_data = &slow_func_del;
  142. heavy_wq->spec.max_retries = 3;
  143. heavy_wq->spec.hold = 1000;
  144. return 0;
  145. }
  146. void
  147. test_init()
  148. {
  149. install_element (VIEW_NODE, &clear_foo_cmd);
  150. heavy_wq_init();
  151. }