home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo3.zoo / demo / misc / stat2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-08  |  10.9 KB  |  465 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: stat.c,v 4.2 88/06/22 14:38:11 bianchi Exp $
  9.     $Source: /tmp/mgrsrc/demo/misc/RCS/stat.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/demo/misc/RCS/stat.c,v $$Revision: 4.2 $";
  12.  
  13. /* strip_chart vmstat output version II */
  14.  
  15. /*
  16. #define NEWESC    'E' 
  17. */
  18.  
  19. #include "term.h"
  20. #include <signal.h>
  21. #include <sgtty.h>
  22.  
  23. #define MAXTRY    5        /* max number of tries for window parameters */
  24.  
  25. FILE *popen(), *file;
  26.  
  27. #define MAX_COLORS    20
  28. int colors[MAX_COLORS] = {
  29.     0,    /* unused */
  30.     3,    /* background */
  31.     6, /* title */
  32.     4, /* lavbels */
  33.     5, /* grid lines */
  34.     2, /* exeeded max's */
  35.     1, /* lines for plot 1 */
  36.     1, /* lines for plot 2 */
  37.     1, /* lines for plot 3 */
  38.     1, /* lines for plot 4 */
  39.     1, /* lines for plot 5 */
  40.     };
  41.  
  42. int color=0;        /* true iff color mgr */
  43.  
  44. struct data {
  45.    char *s_title;    /* short label */
  46.    char *l_title;    /* long label */
  47.    int index;        /* which item in vmstat */
  48.    int max;        /* maximum value */
  49.    };
  50.  
  51. struct data data[] = {
  52.    "r",        "jobs in run q",    0,    5,
  53.    "b",        "jobs blocked",        1,    5,
  54.    "w",        "jobs waiting",        2,    5,
  55.    "fre",    "free memory",        4,    2000,
  56.    "fr",    "freed pages",        9,    10,
  57.    "d1",    "disk 1 accesses",    12,    5,
  58.    "d2",    "disk 2 accesses",    13,    5,
  59.    "d3",    "disk 3 accesses",    14,    5,
  60.    "d4",    "disk 4 accesses",    15,    5,
  61.    "in",    "interrupts",        16,    60,
  62.    "sy",    "system calls",        17,    60,
  63.    "cs",    "context switches",    18,    30,
  64.    "us",    "% user time",        19,    100,
  65.    "kn",    "% system time",    20,    100,
  66.    "id",    "% idle time",        21,    100,
  67.    "",        "",            -1,    -1
  68.    };
  69.  
  70. #define INTERVAL    60    /* bar interval (in secs) */
  71. #define SCROLL        4    /* # of scrolls per window */
  72. #define MAX        10    /* max number of plots */
  73. #define FREQ        3    /* update frequency (secs)*/
  74. #define DELTA        4    /* pixels/update */
  75.  
  76. #define Min(x,y)    ((x)>(y)?(y):(x))
  77. #define Max(x,y)    ((x)>(y)?(x):(y))
  78. #define dprintf        if (debug) fprintf
  79.  
  80. main(argc,argv)
  81. int argc;
  82. char **argv;
  83.    {
  84.    register int i;
  85.    register int x;    /* current plot position */
  86.    int bar=0;        /* hash mark counter */
  87.    int back=0;        /* enable background writes */
  88.    int solid=1;        /* make solid lines */
  89.    int freq = FREQ;    /* update frequency (secs) */
  90.    int size;        /* size of label region */
  91.    int interval;    /* hash mark interval (secs) */
  92.     int warn;        /* line exeeded range */
  93.  
  94.    char host[16];    /* hostname */
  95.    char title[255];    /* chart title */
  96.    char line[255];    /* vmstat input buffer */
  97.    char *fields[25];    /* pntrs to vmstat fields */
  98.    char labels[MAX][100];    /* place for labels */
  99.  
  100.    int current[MAX];    /* current data value */
  101.    int old[MAX];    /* previous data value */
  102.    int field[MAX];    /* index into 'data' */
  103.    int first =1;    /* first time through */ 
  104.    int clean();
  105.  
  106.    int f_high, f_wide;        /* font size */
  107.    int high,wide;        /* window size */
  108.    int x1;            /* left margin */
  109.    int x2;            /* title */
  110.    int y1;            /* title line */
  111.    int y2;            /* first bar */
  112.    int scroll;            /* scroll amount */
  113.    int count;            /* number of plots */
  114.    int delta=DELTA;        /* pixels/line */
  115.    int item;
  116.    int max = 0;
  117.    int dummy;
  118.    int debug=0;
  119.  
  120.    ckmgrterm( *argv );
  121.  
  122.    /* process arguments */
  123.  
  124.    if (argc<2) {
  125.       fprintf(stderr,"usage: %s -[i<incr>sbf<freq>] [-max] arg ...\n",argv[0]);
  126.       fprintf(stderr,"args:\n");
  127.       for(i=0;data[i].max >=0;i++)
  128.          fprintf(stderr,"  %s    (%s)\n",data[i].s_title,data[i].l_title);
  129.       exit(1);
  130.       }
  131.  
  132.    for(count=0,i=1;i<argc;i++) {
  133.       if (strcmp(argv[i],"-d")==0) {
  134.          debug++;
  135.          continue;
  136.          }
  137.       if (strcmp(argv[i],"-b")==0) {
  138.          back++;
  139.          continue;
  140.          }
  141.       if (strcmp(argv[i],"-s")==0) {
  142.          solid=0;
  143.          continue;
  144.          }
  145.       if (strncmp(argv[i],"-c",2)==0) {
  146.          set_colors(argv[i]+2);
  147.          continue;
  148.          }
  149.       if (strncmp(argv[i],"-f",2)==0) {
  150.          freq = atoi(argv[i]+2);
  151.          if (freq < 1) freq = 1;
  152.          if (freq > 120) freq = 120;
  153.          continue;
  154.          }
  155.       if (strncmp(argv[i],"-i",2)==0) {
  156.          delta = atoi(argv[i]+2);
  157.          if (delta < 1) delta = 1;
  158.          if (freq > 10) delta = 10;
  159.          continue;
  160.          }
  161.       if (*argv[i] == '-') {
  162.          max = atoi(argv[i]+1);
  163.          continue;
  164.          }
  165.       if ((item = get_item(argv[i],data)) < 0) {
  166.          fprintf(stderr,"%s:    %s is an invalid item\n",argv[0],argv[i]);
  167.          continue;
  168.          }
  169.       if (max > 0) {
  170.          data[item].max = max;
  171.          max = 0;
  172.          }
  173.       field[count] = item;
  174.       sprintf(labels[count],"%s",data[item].l_title);
  175.       if (++count == MAX)
  176.          break;
  177.       }
  178.  
  179.    if (count < 1) {
  180.       fprintf(stderr,"%s:    not enough fields\n,argv[0]");
  181.       exit(5); 
  182.       }
  183.  
  184.    sprintf(line,"vmstat %d",freq);
  185.    if ((file = popen(line,"r")) == NULL)
  186.       exit(1);
  187.  
  188.    m_setup(0);
  189.    m_ttyset();
  190.     color = is_color();
  191.    m_push(P_EVENT|P_FLAGS);
  192.    m_setmode(M_ABS);
  193.    if (!back)
  194.       m_setmode(M_BACKGROUND);
  195.    else
  196.       m_clearmode(M_BACKGROUND);
  197.  
  198.    signal(SIGINT,clean);
  199.    signal(SIGTERM,clean);
  200.  
  201.    system("stty -ctlecho");
  202.    m_setevent(RESHAPE,"R\fRedrawing...\n");
  203.    m_setevent(REDRAW,"R\fRedrawing...\n");
  204.    first = 1;
  205.  
  206.    while (1) {
  207.  
  208.       for(size=0,i=0;i<count;i++)
  209.          size = Max(size,strlen(labels[i]));
  210.  
  211.       /* clear the screen, flush pending input */
  212.  
  213.       read_it(fileno(m_termin),line);
  214.  
  215.       /* get font size */
  216.  
  217.       for(i=0;i<MAXTRY && get_font(&f_wide,&f_high) < 0;i++);
  218.  
  219.       /* get window size */
  220.  
  221.       for(i=0;i<MAXTRY && get_size(0,0,&wide,&high) <= 0;i++);
  222.    
  223.       if (wide==0 || high==0 || f_wide==0 || f_high==0) {
  224.          fprintf(stderr,"Can't get window info\n");
  225.          clean();
  226.          }
  227.  
  228.       /* get the title */
  229.  
  230.       gethostname(host,sizeof(host));
  231.  
  232.       sprintf(title,"Statistics for %s (%d second intervals)",host,freq);
  233.  
  234.       if (strlen(title)*f_wide > wide)
  235.          sprintf(title,"%s (%d sec.)",host,freq);
  236.  
  237.       /* make sure window is big enough */
  238.  
  239.       if (f_high * (count+1) > high) {
  240.          fprintf(stderr,"\fWindow isn't tall enough\n");
  241.          m_gets(line);
  242.          continue;
  243.          }
  244.  
  245.       if (strlen(title)*f_wide > wide || 3*size*f_wide > wide*2) {
  246.          fprintf(stderr,"\fWindow isn't\nwide enough\n");
  247.          m_gets(line);
  248.          continue;
  249.          }
  250.  
  251.       /* calculate key positions */
  252.  
  253.       x1 = (size*f_wide+1);
  254.       x2 = (wide-strlen(title)*f_wide)/2;
  255.       y1 = f_high +1;
  256.       y2 = (high - y1) /count;
  257.       high--;
  258.  
  259.       m_func(B_SET);
  260.         bg_color(1);
  261.       m_clear();
  262.       x = x1;
  263.       scroll = Max((wide-x1)/SCROLL,10);
  264.       scroll += scroll%DELTA;
  265.  
  266.       if (freq >15)
  267.          interval = INTERVAL * 10 /freq;
  268.       else 
  269.          interval = INTERVAL / freq;
  270.  
  271.       /* draw form */
  272.  
  273.         fg_color(2);
  274.       m_moveprint(x2,y1,title);
  275.  
  276.         fg_color(3);
  277.       line_color(4);
  278.       for(i=0;i<count;i++) {
  279.          char tmp[10];
  280.          if (f_high * (3*count+1) <= high) {
  281.             sprintf(tmp,"%d",data[field[i]].max);
  282.             m_moveprint(x1-f_wide*strlen(tmp),high-(i+1)*y2+f_wide*2+1,tmp);
  283.             m_moveprint(x1-f_wide,high-i*y2,"0");
  284.             m_moveprint(1,high-i*y2-f_high,labels[i]);
  285.             }
  286.          else
  287.             m_moveprint(1,high-i*y2-1,labels[i]);
  288.          m_line(x1,high-i*y2,wide,high-i*y2);
  289.          }
  290.    
  291.       m_line(0,y1,wide,y1);
  292.       m_line(x1,y1,x1,high);
  293.       m_movecursor(x1,0);
  294.       m_flush();
  295.  
  296.       /* read the data */
  297.  
  298.       while (fgets(line,sizeof(line),file) != NULL) {
  299.          i = parse(line,fields);
  300.          if (strcmp(*fields,"procs")==0) {
  301.             fgets(line,sizeof(line),file);
  302.             fgets(line,sizeof(line),file);
  303.             continue;
  304.             }
  305.           if (i < 22) continue;
  306.  
  307.          /* calculate new line position */
  308.  
  309.          for(i=0;i<count;i++) {
  310.                 line_color(6+i);
  311.             current[i] = atoi(fields[data[field[i]].index]) *
  312.                          (y2-3)/data[field[i]].max;
  313.                 warn = (current[i] > y2-3);
  314.             current[i] = Min(current[i],y2-3) + y2*i + 1;
  315.  
  316.             if (!first) {
  317.                m_line(x,high-old[i],x+delta,high-current[i]);
  318.                if (solid) {
  319.                         if (warn) line_color(5);
  320.                   m_line(x+delta,high-y2*i,x+delta,high-current[i]);
  321.                         }
  322.                }
  323.  
  324.             dprintf(stderr,"%s %d->%d, ",data[field[i]].s_title,
  325.                             high-old[i],high-current[i]);
  326.             old[i] = current[i];
  327.             }
  328.          dprintf(stderr," [%d]\n",high);
  329.    
  330.          if (++bar  == interval) {
  331.               line_color(4);
  332.             m_line(x,y1,x,high);
  333.             bar = 0;
  334.             dprintf(stderr,"---------\n");
  335.             }
  336.  
  337.          if (first)
  338.             first = 0;
  339.          else
  340.             x += delta;
  341.  
  342.          if (x > wide-delta) {
  343.  
  344.             /* scroll the display */
  345.  
  346.             x -= scroll;
  347.             m_func(B_COPY);
  348.             m_bitcopy(x1+1,y1+1,wide-x1-1,high-y1-1,x1+scroll+1,y1+1);
  349.                 if (color) {
  350.                     line_color(1);
  351.                 m_func(B_SRC);
  352.                     }
  353.                 else
  354.                 m_func(B_CLEAR);
  355.             m_bitwrite(wide-scroll,y1+1,scroll,high);
  356.             m_func(B_SET);
  357.          
  358.             dprintf(stderr,"scroll to %d,%d from %d,%d\n",
  359.                     x1+1,y1+1,x1+scroll+1,y1+1);
  360.                 line_color(4);
  361.             for(i=0;i<count;i++) 
  362.                m_line(wide-scroll,high-i*y2,wide,high-i*y2);
  363.             }
  364.          m_flush();
  365.          if (read_it(fileno(m_termin),line) && *line == 'R')
  366.             break;
  367.          }
  368.       }
  369.    }
  370.  
  371. int
  372. get_item(s,data)    /* look up an parameter */
  373. char *s;
  374. struct data data[];
  375.    {
  376.    register int i;
  377.  
  378.    for(i=0;data[i].index>=0;i++)
  379.       if (strcmp(s,data[i].s_title)==0)
  380.         return(i);
  381.    return(-1);
  382.    }
  383.  
  384. clean()            /* clean up on SIGINT */
  385.    {
  386.    m_pop();
  387.    pclose(file);
  388.    m_clear();
  389.    m_flush();
  390.    m_ttyreset();
  391.    exit(1);
  392.    }
  393.  
  394. int read_it(fd,line)    /* non blocking read */
  395. int fd;
  396. char *line;
  397.    {
  398.    long rd;
  399.  
  400.    ioctl(fd,FIONREAD,&rd);
  401.    line[rd] = '\0';
  402.    if (rd > 0)  {
  403.       return(read(fd,line,rd));
  404.       }
  405.    else
  406.       return(0);
  407.    }
  408.  
  409. /* see if MGR is color */
  410.  
  411. int
  412. is_color()
  413.     {
  414.     int bits = 0;
  415.     m_getinfo(G_SYSTEM);
  416.     sscanf(m_get(),"%*s %*d %*d %*d %d",&bits);
  417.     return(bits>1);
  418.     }
  419.  
  420. /* set the bg color */
  421.  
  422. int
  423. bg_color(n)
  424. int n;
  425.     {
  426.     if (color) m_bcolor(colors[n]);
  427.     }
  428.  
  429. /* set the FG color */
  430.  
  431. int
  432. fg_color(n)
  433. int n;
  434.     {
  435.     if (color) m_fcolor(colors[n]);
  436.     }
  437.  
  438. /* set the line color */
  439.  
  440. int
  441. line_color(n)
  442. int n;
  443.     {
  444.     if (color) m_linecolor(B_SRC,colors[n]);
  445.     }
  446.  
  447. /* get colors from the command line */
  448.  
  449. int
  450. set_colors(s)
  451. register char *s;
  452.     {
  453.     register int i=0;
  454.     register unsigned char c;
  455.  
  456.     while((c = *s++) && i<MAX_COLORS) {
  457.         if (c>='0' && c <= '9')
  458.             colors[i++] = c - '0';
  459.         else if (c >= 'a' && c <= 'z')
  460.             colors[i++] = 16 + c - 'a';
  461.         else if (c >= 'A' && c <= 'Z')
  462.             colors[i++] = 16 + c - 'A';
  463.         }
  464.     }
  465.