home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.lbl.gov / 2014.05.ftp.ee.lbl.gov.tar / ftp.ee.lbl.gov / cbq2.0g.tar.Z / cbq2.0g.tar / usr / src / cmd / cbqd / cbqd.c < prev    next >
C/C++ Source or Header  |  1998-07-06  |  5KB  |  218 lines

  1. /*
  2.  * Copyright (c) Sun Microsystems, Inc. 1995-1998 All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  *
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in the
  13.  *    documentation and/or other materials provided with the distribution.
  14.  *
  15.  * 3. All advertising materials mentioning features or use of this software
  16.  *    must display the following acknowledgement:
  17.  *      This product includes software developed by the SMCC Technology
  18.  *      Development Group at Sun Microsystems, Inc.
  19.  *
  20.  * 4. The name of the Sun Microsystems, Inc nor may not be used to endorse or
  21.  *      promote products derived from this software without specific prior
  22.  *      written permission.
  23.  *
  24.  * SUN MICROSYSTEMS MERCHANTABILITY OF THIS SOFTWARE OR THE SUITABILITY OF THIS
  25.  * SOFTWARE FOR ANY PARTICULAR PURPOSE.  The software is provided "as is" 
  26.  * without express or implied warranty of any kind. 
  27.  *  
  28.  * These notices must be retained in any copies of any part of this software.
  29.  */
  30.  
  31. #pragma ident "@(#)cbqd.c  1.30     98/05/11 SMI"
  32.  
  33. #include <stdio.h>
  34. #include <signal.h>
  35. #include <thread.h>
  36. #include <cbqif.h>
  37.  
  38. /*
  39.  * Local macros.
  40.  */
  41.  
  42. #define CMD_LINE_OPTS    "d:f:c:pi:o"
  43.  
  44. /*
  45.  * Local variables.
  46.  */
  47. static int    debug = 0;
  48. static char    *config = "cbqd.conf";
  49. static int    done = 0;
  50. static char    *interface = "ipdptp0";
  51.  
  52. /*
  53.  * Local Functions.
  54.  */
  55. static void *sighandler(void *);
  56.  
  57. /*
  58.  * Global Data.
  59.  */
  60. int        count = 0, nclasses = 0;
  61. class_stats_t    *clstats = NULL;
  62. class_stats_t    *pclstats = NULL;
  63. float        *pstats = NULL;
  64. int        performance = 0;
  65. int        is_offtime = 1;
  66.  
  67. int
  68. main(int argc, char **argv)
  69. {
  70.     extern int    optind;
  71.     extern char    *optarg;
  72.  
  73.     int        i, c, sig;
  74.     int        maxcount = 10000;
  75.     sigset_t    set;
  76.     cbq_ifinfo_t    *ifinfo;
  77.     thread_t    sigthreadid;
  78.     int        domaxcount = 0;
  79.     int        inited = 0;
  80.     int        diff;
  81.     float        bytes_per_sec, bytes_per_ns;
  82. #ifdef CBQPERF
  83.     struct cbq_perf    perf, *gp = &perf;
  84.     int        nperf = 0;
  85. #endif
  86.  
  87.     hrtime_t    last, now;
  88.  
  89.     while ((c = getopt(argc, argv, CMD_LINE_OPTS)) != EOF) {
  90.         switch (c) {
  91.         case 'o':
  92.             is_offtime = 0;
  93.             break;
  94.         case 'p':
  95.             performance++;
  96.             break;
  97.         case 'i':
  98.             interface = optarg;
  99.             break;
  100.         case 'd':
  101.             debug = atoi(optarg);
  102.             break;
  103.         case 'f':
  104.             config = optarg;
  105.             break;
  106.         case 'c':
  107.             domaxcount = 1;
  108.             maxcount = atoi(optarg);
  109.             break;
  110.         }
  111.     }
  112.  
  113.     if (thr_create((void *)NULL, 0, sighandler, (void *)NULL,
  114.             THR_NEW_LWP | THR_BOUND, &sigthreadid)) {
  115.         perror("sigthread");
  116.         exit(1);
  117.     }
  118.  
  119.     if (cbq_init(debug, is_offtime))
  120.         goto err_exit;
  121.  
  122.     if ((nclasses = cbq_do_config(config)) == 0)
  123.         goto err_exit;
  124.  
  125.     clstats = (class_stats_t *)malloc(sizeof(class_stats_t) * nclasses);
  126.     pclstats = (class_stats_t *)malloc(sizeof(class_stats_t) * nclasses);
  127.     pstats = (float *)malloc(sizeof (float) * nclasses);
  128.     if (!clstats || !pclstats || !pstats)
  129.         goto err_exit;
  130.     
  131.     if (cbq_enableall())
  132.         goto err_exit;
  133.  
  134.     memset(pstats, 0, sizeof (float) * nclasses);
  135.     memset(pclstats, 0, sizeof (class_stats_t) * nclasses);
  136.     memset(clstats, 0, sizeof (class_stats_t) * nclasses);
  137.  
  138.     ifinfo = cbq_getifinfo(interface);
  139.     bytes_per_sec = (float)ifinfo->bandwidth / 8.0;
  140.     bytes_per_ns = bytes_per_sec / 1000000000;
  141.  
  142.     last = gethrtime();
  143.     poll(0, 0, 1000);
  144.     while (!done) {
  145.         if (cbq_getstats(interface, clstats, nclasses))
  146.             done++;
  147.         else {
  148.             now = gethrtime();
  149.             diff = (now - last);
  150.             last = now;
  151.  
  152.             /*
  153.              * nbytes / t = x bytes / sec;
  154.              * x bytes/sec / link bytes second = % of link bw;
  155.              */
  156.             for (i = 0; i < nclasses; i++) {
  157.                 pstats[i] = clstats[i].nbytes -
  158.                     pclstats[i].nbytes;
  159.                 pstats[i] = ((pstats[i] / diff) * 1000000000.0)
  160.                     / bytes_per_sec;
  161.             }
  162.  
  163.             cbq_report_stats(interface, clstats, pstats, nclasses);
  164.             memcpy((void *)pclstats, (void *)clstats, 
  165.                 sizeof(class_stats_t) * nclasses);
  166.  
  167.             if (domaxcount) {
  168.                 if (++count >= maxcount)
  169.                     done++;
  170.             }
  171.  
  172.             poll(0, 0, 1000);
  173.         }
  174.     }
  175.  
  176. #ifdef CBQPERF    
  177.     if (performance) {
  178.         if (!cbq_getnperf(interface, &nperf)) {
  179.             for (i = 0; i < nperf; i++) {
  180.                 if (!cbq_getperf(interface, i, gp)) {
  181.                     printf("%s:class.%x, %d, %lld, %lld\n",
  182.                         interface,
  183.                         gp->class_id,
  184.                         i,
  185.                         gp->schedtime,
  186.                         gp->departtime);
  187.                 }
  188.             }
  189.         } else
  190.             fprintf(stderr, "cbq_getnperf failed\n");
  191.     }
  192. #endif
  193.  
  194. err_exit:
  195.     cbq_destroyall();
  196.     exit(1);
  197. }
  198.  
  199. static void *
  200. sighandler(void *arg)
  201. {
  202.         sigset_t        set;
  203.  
  204.     sigemptyset(&set);
  205.     sigaddset(&set, SIGTERM);
  206.     sigaddset(&set, SIGHUP);
  207.  
  208.     switch (sigwait(&set)) {
  209.     case SIGINT:
  210.     case SIGTERM:
  211.     case SIGHUP:
  212.         done++;
  213.         break;
  214.     }
  215.  
  216.     thr_exit(0);
  217. }
  218.