File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / quagga / tests / heavy-thread.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Feb 21 17:26:11 2012 UTC (12 years, 4 months ago) by misho
Branches: quagga, MAIN
CVS tags: v0_99_22p0, v0_99_22, v0_99_21, v0_99_20_1, v0_99_20, HEAD
quagga

    1: /*
    2:  * $Id: heavy-thread.c,v 1.1.1.1 2012/02/21 17:26:11 misho Exp $
    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: 
   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.
   25:  *
   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
   29:  * remains responsive.
   30:  */
   31: #include <zebra.h>
   32: #include <math.h>
   33: 
   34: #include "thread.h"
   35: #include "vty.h"
   36: #include "command.h"
   37: #include "memory.h"
   38: #include "log.h"
   39: 
   40: extern struct thread_master *master;
   41: 
   42: enum
   43: {
   44:   ITERS_FIRST = 0,
   45:   ITERS_ERR = 100,
   46:   ITERS_LATER = 400,
   47:   ITERS_PRINT = 10,
   48:   ITERS_MAX = 1000,
   49: };
   50: 
   51: struct work_state {
   52:   struct vty *vty;
   53:   char *str;
   54:   int i;
   55: };
   56: 
   57: static void
   58: slow_func (struct vty *vty, const char *str, const int i)
   59: {
   60:   double x = 1;
   61:   int j;
   62:   
   63:   for (j = 0; j < 300; j++)
   64:     x += sin(x)*j;
   65:   
   66:   if ((i % ITERS_LATER) == 0)
   67:     printf ("%s: %d, temporary error, save this somehow and do it later..\n",
   68:             __func__, i);
   69:   
   70:   if ((i % ITERS_ERR) == 0)
   71:     printf ("%s: hard error\n", __func__);
   72:   
   73:   if ((i % ITERS_PRINT) == 0)
   74:     printf ("%s did %d, x = %g\n", str, i, x);
   75: }
   76: 
   77: static int
   78: clear_something (struct thread *thread)
   79: {
   80:   struct work_state *ws = THREAD_ARG(thread);
   81:   
   82:   /* this could be like iterating through 150k of route_table 
   83:    * or worse, iterating through a list of peers, to bgp_stop them with
   84:    * each having 150k route tables to process...
   85:    */
   86:   while (ws->i < ITERS_MAX)
   87:     {
   88:       slow_func(ws->vty, ws->str, ws->i);
   89:       ws->i++;
   90:       if (thread_should_yield(thread))
   91:         {
   92: 	  thread_add_background(master, clear_something, ws, 0);
   93: 	  return 0;
   94:         }
   95:     }
   96:   
   97:   /* All done! */
   98:   XFREE (MTYPE_TMP, ws->str);
   99:   XFREE (MTYPE_TMP, ws);
  100:   return 0;
  101: }
  102: 
  103: DEFUN (clear_foo,
  104:        clear_foo_cmd,
  105:        "clear foo .LINE",
  106:        "clear command\n"
  107:        "arbitrary string\n")
  108: {
  109:   char *str;
  110:   struct work_state *ws;
  111: 
  112:   if (!argc)
  113:     {
  114:       vty_out (vty, "%% string argument required%s", VTY_NEWLINE);
  115:       return CMD_WARNING;
  116:     }
  117:   
  118:   str = argv_concat (argv, argc, 0);
  119:   
  120:   if ((ws = XMALLOC(MTYPE_TMP, sizeof(*ws))) == NULL)
  121:     {
  122:       zlog_err ("%s: unable to allocate work_state", __func__);
  123:       return CMD_WARNING;
  124:     }
  125:   
  126:   if (!(ws->str = XSTRDUP (MTYPE_TMP, str)))
  127:     {
  128:       zlog_err ("%s: unable to xstrdup", __func__);
  129:       XFREE (MTYPE_TMP, ws);
  130:       return CMD_WARNING;
  131:     }
  132:   
  133:   ws->vty = vty;
  134:   ws->i = ITERS_FIRST;
  135: 
  136:   thread_add_background(master, clear_something, ws, 0);
  137: 
  138:   return CMD_SUCCESS;
  139: }
  140: 
  141: void
  142: test_init()
  143: {
  144:   install_element (VIEW_NODE, &clear_foo_cmd);
  145: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>