heavy-wq.c 4.1 KB

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