]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_heavy_thread.c
2 * $Id: heavy-thread.c,v 1.2 2005/04/25 16:42:24 paul Exp $
4 * This file is part of Quagga.
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
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.
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
22 /* This programme shows the effects of 'heavy' long-running functions
23 * on the cooperative threading model, as demonstrated by heavy.c, and how
24 * they can be mitigated using a background thread.
26 * Run it with a config file containing 'password whatever', telnet to it
27 * (it defaults to port 4000) and enter the 'clear foo string' command.
28 * then type whatever and observe that, unlike heavy.c, the vty interface
42 extern struct thread_master
*master
;
44 enum { ITERS_FIRST
= 0,
57 static void slow_func(struct vty
*vty
, const char *str
, const int i
)
62 for (j
= 0; j
< 300; j
++)
65 if ((i
% ITERS_LATER
) == 0)
66 printf("%s: %d, temporary error, save this somehow and do it later..\n",
69 if ((i
% ITERS_ERR
) == 0)
70 printf("%s: hard error\n", __func__
);
72 if ((i
% ITERS_PRINT
) == 0)
73 printf("%s did %d, x = %g\n", str
, i
, x
);
76 static int clear_something(struct thread
*thread
)
78 struct work_state
*ws
= THREAD_ARG(thread
);
80 /* this could be like iterating through 150k of route_table
81 * or worse, iterating through a list of peers, to bgp_stop them with
82 * each having 150k route tables to process...
84 while (ws
->i
< ITERS_MAX
) {
85 slow_func(ws
->vty
, ws
->str
, ws
->i
);
87 if (thread_should_yield(thread
)) {
88 thread_add_background(master
, clear_something
, ws
, 0);
94 XFREE(MTYPE_TMP
, ws
->str
);
103 "arbitrary string\n")
106 struct work_state
*ws
;
109 vty_out(vty
, "%% string argument required%s", VTY_NEWLINE
);
113 str
= argv_concat(argv
, argc
, 0);
115 if ((ws
= XMALLOC(MTYPE_TMP
, sizeof(*ws
))) == NULL
) {
116 zlog_err("%s: unable to allocate work_state", __func__
);
120 if (!(ws
->str
= XSTRDUP(MTYPE_TMP
, str
))) {
121 zlog_err("%s: unable to xstrdup", __func__
);
122 XFREE(MTYPE_TMP
, ws
);
129 thread_add_background(master
, clear_something
, ws
, 0);
136 install_element(VIEW_NODE
, &clear_foo_cmd
);