heavy-wq.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. extern struct thread_master *master;
  37. static struct work_queue *heavy_wq;
  38. struct heavy_wq_node
  39. {
  40. char *str;
  41. int i;
  42. };
  43. enum
  44. {
  45. ITERS_FIRST = 0,
  46. ITERS_ERR = 100,
  47. ITERS_LATER = 400,
  48. ITERS_PRINT = 10,
  49. ITERS_MAX = 1000,
  50. };
  51. static void
  52. heavy_wq_add (struct vty *vty, const char *str, int i)
  53. {
  54. struct heavy_wq_node *hn;
  55. if ((hn = XCALLOC (MTYPE_PREFIX_LIST, sizeof(struct heavy_wq_node))) == NULL)
  56. {
  57. zlog_err ("%s: unable to allocate hn", __func__);
  58. return;
  59. }
  60. hn->i = i;
  61. if (!(hn->str = XSTRDUP (MTYPE_PREFIX_LIST_STR, str)))
  62. {
  63. zlog_err ("%s: unable to xstrdup", __func__);
  64. XFREE (MTYPE_PREFIX_LIST, hn);
  65. return;
  66. }
  67. work_queue_add (heavy_wq, hn);
  68. return;
  69. }
  70. static void
  71. slow_func_err (struct work_queue *wq, struct work_queue_item *item)
  72. {
  73. printf ("%s: running error function\n", __func__);
  74. }
  75. static void
  76. slow_func_del (struct work_queue *wq, void *data)
  77. {
  78. struct heavy_wq_node *hn = data;
  79. assert (hn && hn->str);
  80. printf ("%s: %s\n", __func__, hn->str);
  81. XFREE (MTYPE_PREFIX_LIST_STR, hn->str);
  82. hn->str = NULL;
  83. XFREE(MTYPE_PREFIX_LIST, hn);
  84. }
  85. static wq_item_status
  86. slow_func (struct work_queue *wq, void *data)
  87. {
  88. struct heavy_wq_node *hn = data;
  89. double x = 1;
  90. int j;
  91. assert (hn && hn->str);
  92. for (j = 0; j < 300; j++)
  93. x += sin(x)*j;
  94. if ((hn->i % ITERS_LATER) == 0)
  95. return WQ_RETRY_LATER;
  96. if ((hn->i % ITERS_ERR) == 0)
  97. return WQ_RETRY_NOW;
  98. if ((hn->i % ITERS_PRINT) == 0)
  99. printf ("%s did %d, x = %g\n", hn->str, hn->i, x);
  100. return WQ_SUCCESS;
  101. }
  102. static void
  103. clear_something (struct vty *vty, const char *str)
  104. {
  105. int i;
  106. /* this could be like iterating through 150k of route_table
  107. * or worse, iterating through a list of peers, to bgp_stop them with
  108. * each having 150k route tables to process...
  109. */
  110. for (i = ITERS_FIRST; i < ITERS_MAX; i++)
  111. heavy_wq_add (vty, str, i);
  112. }
  113. DEFUN (clear_foo,
  114. clear_foo_cmd,
  115. "clear foo .LINE",
  116. "clear command\n"
  117. "arbitrary string\n")
  118. {
  119. char *str;
  120. if (!argc)
  121. {
  122. vty_out (vty, "%% string argument required%s", VTY_NEWLINE);
  123. return CMD_WARNING;
  124. }
  125. str = argv_concat (argv, argc, 0);
  126. clear_something (vty, str);
  127. XFREE (MTYPE_TMP, str);
  128. return CMD_SUCCESS;
  129. }
  130. static int
  131. heavy_wq_init ()
  132. {
  133. if (! (heavy_wq = work_queue_new (master, "heavy_work_queue")))
  134. {
  135. zlog_err ("%s: could not get new work queue!", __func__);
  136. return -1;
  137. }
  138. heavy_wq->spec.workfunc = &slow_func;
  139. heavy_wq->spec.errorfunc = &slow_func_err;
  140. heavy_wq->spec.del_item_data = &slow_func_del;
  141. heavy_wq->spec.max_retries = 3;
  142. heavy_wq->spec.hold = 1000;
  143. return 0;
  144. }
  145. void
  146. test_init()
  147. {
  148. install_element (VIEW_NODE, &clear_foo_cmd);
  149. heavy_wq_init();
  150. }