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 / stat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-24  |  9.3 KB  |  370 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. struct data {
  28.    char *s_title;    /* short label */
  29.    char *l_title;    /* long label */
  30.    int index;        /* which item in vmstat */
  31.    int max;        /* maximum value */
  32.    };
  33.  
  34. struct data data[] = {
  35.    "r",        "jobs in run q",    0,    5,
  36.    "b",        "jobs blocked",        1,    5,
  37.    "w",        "jobs waiting",        2,    5,
  38.    "fre",    "free memory",        4,    2000,
  39.    "fr",    "freed pages",        9,    10,
  40.    "d1",    "disk 1 accesses",    12,    5,
  41.    "d2",    "disk 2 accesses",    13,    5,
  42.    "d3",    "disk 3 accesses",    14,    5,
  43.    "d4",    "disk 4 accesses",    15,    5,
  44.    "in",    "interrupts",        16,    60,
  45.    "sy",    "system calls",        17,    60,
  46.    "cs",    "context switches",    18,    30,
  47.    "us",    "% user time",        19,    100,
  48.    "kn",    "% system time",    20,    100,
  49.    "id",    "% idle time",        21,    100,
  50.    "",        "",            -1,    -1
  51.    };
  52.  
  53. #define INTERVAL    60    /* bar interval (in secs) */
  54. #define SCROLL        4    /* # of scrolls per window */
  55. #define MAX        10    /* max number of plots */
  56. #define FREQ        3    /* update frequency (secs)*/
  57. #define DELTA        4    /* pixels/update */
  58.  
  59. #define Min(x,y)    ((x)>(y)?(y):(x))
  60. #define Max(x,y)    ((x)>(y)?(x):(y))
  61. #define dprintf        if (debug) fprintf
  62.  
  63. main(argc,argv)
  64. int argc;
  65. char **argv;
  66.    {
  67.    register int i;
  68.    register int x;    /* current plot position */
  69.    int bar=0;        /* hash mark counter */
  70.    int back=0;        /* enable background writes */
  71.    int solid=1;        /* make solid lines */
  72.    int freq = FREQ;    /* update frequency (secs) */
  73.    int size;        /* size of label region */
  74.    int interval;    /* hash mark interval (secs) */
  75.  
  76.    char host[16];    /* hostname */
  77.    char title[255];    /* chart title */
  78.    char line[255];    /* vmstat input buffer */
  79.    char *fields[25];    /* pntrs to vmstat fields */
  80.    char labels[MAX][100];    /* place for labels */
  81.  
  82.    int current[MAX];    /* current data value */
  83.    int old[MAX];    /* previous data value */
  84.    int field[MAX];    /* index into 'data' */
  85.    int first =1;    /* first time through */ 
  86.    int clean();
  87.  
  88.    int f_high, f_wide;        /* font size */
  89.    int high,wide;        /* window size */
  90.    int x1;            /* left margin */
  91.    int x2;            /* title */
  92.    int y1;            /* title line */
  93.    int y2;            /* first bar */
  94.    int scroll;            /* scroll amount */
  95.    int count;            /* number of plots */
  96.    int delta=DELTA;        /* pixels/line */
  97.    int item;
  98.    int max = 0;
  99.    int dummy;
  100.    int debug=0;
  101.  
  102.    ckmgrterm( *argv );
  103.  
  104.    /* process arguments */
  105.  
  106.    if (argc<2) {
  107.       fprintf(stderr,"usage: %s -[i<incr>sbf<freq>] [-max] arg ...\n",argv[0]);
  108.       fprintf(stderr,"args:\n");
  109.       for(i=0;data[i].max >=0;i++)
  110.          fprintf(stderr,"  %s    (%s)\n",data[i].s_title,data[i].l_title);
  111.       exit(1);
  112.       }
  113.  
  114.    for(count=0,i=1;i<argc;i++) {
  115.       if (strcmp(argv[i],"-d")==0) {
  116.          debug++;
  117.          continue;
  118.          }
  119.       if (strcmp(argv[i],"-b")==0) {
  120.          back++;
  121.          continue;
  122.          }
  123.       if (strcmp(argv[i],"-s")==0) {
  124.          solid=0;
  125.          continue;
  126.          }
  127.       if (strncmp(argv[i],"-f",2)==0) {
  128.          freq = atoi(argv[i]+2);
  129.          if (freq < 1) freq = 1;
  130.          if (freq > 120) freq = 120;
  131.          continue;
  132.          }
  133.       if (strncmp(argv[i],"-i",2)==0) {
  134.          delta = atoi(argv[i]+2);
  135.          if (delta < 1) delta = 1;
  136.          if (freq > 10) delta = 10;
  137.          continue;
  138.          }
  139.       if (*argv[i] == '-') {
  140.          max = atoi(argv[i]+1);
  141.          continue;
  142.          }
  143.       if ((item = get_item(argv[i],data)) < 0) {
  144.          fprintf(stderr,"%s:    %s is an invalid item\n",argv[0],argv[i]);
  145.          continue;
  146.          }
  147.       if (max > 0) {
  148.          data[item].max = max;
  149.          max = 0;
  150.          }
  151.       field[count] = item;
  152.       sprintf(labels[count],"%s",data[item].l_title);
  153.       if (++count == MAX)
  154.          break;
  155.       }
  156.  
  157.    if (count < 1) {
  158.       fprintf(stderr,"%s:    not enough fields\n,argv[0]");
  159.       exit(5); 
  160.       }
  161.  
  162.    sprintf(line,"vmstat %d",freq);
  163.    if ((file = popen(line,"r")) == NULL)
  164.       exit(1);
  165.  
  166.    m_setup(0);
  167.    m_ttyset();
  168.    m_push(P_EVENT|P_FLAGS);
  169.    m_setmode(M_ABS);
  170.    if (!back)
  171.       m_setmode(M_BACKGROUND);
  172.    else
  173.       m_clearmode(M_BACKGROUND);
  174.  
  175.    signal(SIGINT,clean);
  176.    signal(SIGTERM,clean);
  177.  
  178.    system("stty -ctlecho");
  179.    m_setevent(RESHAPE,"R\fRedrawing...\n");
  180.    m_setevent(REDRAW,"R\fRedrawing...\n");
  181.    first = 1;
  182.  
  183.    while (1) {
  184.  
  185.       for(size=0,i=0;i<count;i++)
  186.          size = Max(size,strlen(labels[i]));
  187.  
  188.       /* clear the screen, flush pending input */
  189.  
  190.       read_it(fileno(m_termin),line);
  191.  
  192.       /* get font size */
  193.  
  194.       for(i=0;i<MAXTRY && get_font(&f_wide,&f_high) < 0;i++);
  195.  
  196.       /* get window size */
  197.  
  198.       for(i=0;i<MAXTRY && get_size(0,0,&wide,&high) <= 0;i++);
  199.    
  200.       if (wide==0 || high==0 || f_wide==0 || f_high==0) {
  201.          fprintf(stderr,"Can't get window info\n");
  202.          clean();
  203.          }
  204.  
  205.       /* get the title */
  206.  
  207.       gethostname(host,sizeof(host));
  208.  
  209.       sprintf(title,"Statistics for %s (%d second intervals)",host,freq);
  210.  
  211.       if (strlen(title)*f_wide > wide)
  212.          sprintf(title,"%s (%d sec.)",host,freq);
  213.  
  214.       /* make sure window is big enough */
  215.  
  216.       if (f_high * (count+1) > high) {
  217.          fprintf(stderr,"\fWindow isn't tall enough\n");
  218.          m_gets(line);
  219.          continue;
  220.          }
  221.  
  222.       if (strlen(title)*f_wide > wide || 3*size*f_wide > wide*2) {
  223.          fprintf(stderr,"\fWindow isn't\nwide enough\n");
  224.          m_gets(line);
  225.          continue;
  226.          }
  227.  
  228.       /* calculate key positions */
  229.  
  230.       x1 = (size*f_wide+1);
  231.       x2 = (wide-strlen(title)*f_wide)/2;
  232.       y1 = f_high +1;
  233.       y2 = (high - y1) /count;
  234.       high--;
  235.  
  236.       m_func(B_SET);
  237.       m_clear();
  238.       x = x1;
  239.       scroll = Max((wide-x1)/SCROLL,10);
  240.       scroll += scroll%DELTA;
  241.  
  242.       if (freq >15)
  243.          interval = INTERVAL * 10 /freq;
  244.       else 
  245.          interval = INTERVAL / freq;
  246.  
  247.       /* draw form */
  248.  
  249.       m_moveprint(x2,y1,title);
  250.  
  251.       for(i=0;i<count;i++) {
  252.          char tmp[10];
  253.          if (f_high * (3*count+1) <= high) {
  254.             sprintf(tmp,"%d",data[field[i]].max);
  255.             m_moveprint(x1-f_wide*strlen(tmp),high-(i+1)*y2+f_wide*2+1,tmp);
  256.             m_moveprint(x1-f_wide,high-i*y2,"0");
  257.             m_moveprint(1,high-i*y2-f_high,labels[i]);
  258.             }
  259.          else
  260.             m_moveprint(1,high-i*y2-1,labels[i]);
  261.          m_line(x1,high-i*y2,wide,high-i*y2);
  262.          }
  263.    
  264.       m_line(0,y1,wide,y1);
  265.       m_line(x1,y1,x1,high);
  266.       m_movecursor(x1,0);
  267.       m_flush();
  268.  
  269.       /* read the data */
  270.  
  271.       while (fgets(line,sizeof(line),file) != NULL) {
  272.          i = parse(line,fields);
  273.          if (strcmp(*fields,"procs")==0) {
  274.             fgets(line,sizeof(line),file);
  275.             fgets(line,sizeof(line),file);
  276.             continue;
  277.             }
  278.           if (i < 22) continue;
  279.  
  280.          /* calculate new line position */
  281.  
  282.          for(i=0;i<count;i++) {
  283.             current[i] = atoi(fields[data[field[i]].index]) *
  284.                          (y2-3)/data[field[i]].max;
  285.             current[i] = Min(current[i],y2-3) + y2*i + 1;
  286.  
  287.             if (!first) {
  288.                m_line(x,high-old[i],x+delta,high-current[i]);
  289.                if (solid)
  290.                   m_line(x+delta,high-y2*i,x+delta,high-current[i]);
  291.                }
  292.  
  293.             dprintf(stderr,"%s %d->%d, ",data[field[i]].s_title,
  294.                             high-old[i],high-current[i]);
  295.             old[i] = current[i];
  296.             }
  297.          dprintf(stderr," [%d]\n",high);
  298.    
  299.          if (++bar  == interval) {
  300.             m_line(x,y1,x,high);
  301.             bar = 0;
  302.             dprintf(stderr,"---------\n");
  303.             }
  304.  
  305.          if (first)
  306.             first = 0;
  307.          else
  308.             x += delta;
  309.  
  310.          if (x > wide-delta) {
  311.  
  312.             /* scroll the display */
  313.  
  314.             x -= scroll;
  315.             m_func(B_COPY);
  316.             m_bitcopy(x1+1,y1+1,wide-x1-1,high-y1-1,x1+scroll+1,y1+1);
  317.             m_func(B_CLEAR);
  318.             m_bitwrite(wide-scroll,y1+1,scroll,high);
  319.             m_func(B_SET);
  320.          
  321.             dprintf(stderr,"scroll to %d,%d from %d,%d\n",
  322.                     x1+1,y1+1,x1+scroll+1,y1+1);
  323.             for(i=0;i<count;i++) 
  324.                m_line(wide-scroll,high-i*y2,wide,high-i*y2);
  325.             }
  326.          m_flush();
  327.          if (read_it(fileno(m_termin),line) && *line == 'R')
  328.             break;
  329.          }
  330.       }
  331.    }
  332.  
  333. int
  334. get_item(s,data)    /* look up an parameter */
  335. char *s;
  336. struct data data[];
  337.    {
  338.    register int i;
  339.  
  340.    for(i=0;data[i].index>=0;i++)
  341.       if (strcmp(s,data[i].s_title)==0)
  342.         return(i);
  343.    return(-1);
  344.    }
  345.  
  346. clean()            /* clean up on SIGINT */
  347.    {
  348.    m_pop();
  349.    pclose(file);
  350.    m_clear();
  351.    m_flush();
  352.    m_ttyreset();
  353.    exit(1);
  354.    }
  355.  
  356. int read_it(fd,line)    /* non blocking read */
  357. int fd;
  358. char *line;
  359.    {
  360.    long rd;
  361.  
  362.    ioctl(fd,FIONREAD,&rd);
  363.    line[rd] = '\0';
  364.    if (rd > 0)  {
  365.       return(read(fd,line,rd));
  366.       }
  367.    else
  368.       return(0);
  369.    }
  370.