2 * This file is part of Quagga.
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
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.
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
20 /* This programme shows the effects of 'heavy' long-running functions
21 * on the cooperative threading model.
23 * Run it with a config file containing 'password whatever', telnet to it
24 * (it defaults to port 4000) and enter the 'clear foo string' command.
25 * then type whatever and observe that the vty interface is unresponsive
26 * for quite a period of time, due to the clear_something command
27 * taking a very long time to complete.
36 #include "workqueue.h"
41 extern struct thread_master *master;
42 static struct work_queue *heavy_wq;
60 heavy_wq_add (struct vty *vty, const char *str, int i)
62 struct heavy_wq_node *hn;
64 if ((hn = XCALLOC (MTYPE_PREFIX_LIST, sizeof(struct heavy_wq_node))) == NULL)
66 zlog_err ("%s: unable to allocate hn", __func__);
71 if (!(hn->str = XSTRDUP (MTYPE_PREFIX_LIST_STR, str)))
73 zlog_err ("%s: unable to xstrdup", __func__);
74 XFREE (MTYPE_PREFIX_LIST, hn);
78 work_queue_add (heavy_wq, hn);
84 slow_func_err (struct work_queue *wq, struct work_queue_item *item)
86 printf ("%s: running error function\n", __func__);
90 slow_func_del (struct work_queue *wq, void *data)
92 struct heavy_wq_node *hn = data;
93 assert (hn && hn->str);
94 printf ("%s: %s\n", __func__, hn->str);
95 XFREE (MTYPE_PREFIX_LIST_STR, hn->str);
97 XFREE(MTYPE_PREFIX_LIST, hn);
100 static wq_item_status
101 slow_func (struct work_queue *wq, void *data)
103 struct heavy_wq_node *hn = data;
107 assert (hn && hn->str);
109 for (j = 0; j < 300; j++)
112 if ((hn->i % ITERS_LATER) == 0)
113 return WQ_RETRY_LATER;
115 if ((hn->i % ITERS_ERR) == 0)
118 if ((hn->i % ITERS_PRINT) == 0)
119 printf ("%s did %d, x = %g\n", hn->str, hn->i, x);
125 clear_something (struct vty *vty, const char *str)
129 /* this could be like iterating through 150k of route_table
130 * or worse, iterating through a list of peers, to bgp_stop them with
131 * each having 150k route tables to process...
133 for (i = ITERS_FIRST; i < ITERS_MAX; i++)
134 heavy_wq_add (vty, str, i);
141 "arbitrary string\n")
146 vty_out (vty, "%% string argument required%s", VTY_NEWLINE);
150 str = argv_concat (argv, argc, 0);
152 clear_something (vty, str);
153 XFREE (MTYPE_TMP, str);
160 if (! (heavy_wq = work_queue_new (master, "heavy_work_queue")))
162 zlog_err ("%s: could not get new work queue!", __func__);
166 heavy_wq->spec.workfunc = &slow_func;
167 heavy_wq->spec.errorfunc = &slow_func_err;
168 heavy_wq->spec.del_item_data = &slow_func_del;
169 heavy_wq->spec.max_retries = 3;
170 heavy_wq->spec.hold = 1000;
178 install_element (VIEW_NODE, &clear_foo_cmd);