home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 145.lha / lav.c < prev    next >
C/C++ Source or Header  |  1986-11-21  |  4KB  |  159 lines

  1. /*
  2.  *    lav.c - Amiga load average
  3.  *
  4.  *    Written by:
  5.  *    William J. Rucklidge
  6.  *    wjr@unicus.UUCP        (soon to be wjr@unicus.com)
  7.  *
  8.  *    You may take this program and skywrite it and sell tickets for all
  9.  *    I care. (i.e this program is placed in the public domain).
  10.  *    However, if you have any improvements, suggestions or comments, mail
  11.  *    a note to me.
  12.  */
  13.  
  14. #include    <intuition/intuition.h>
  15. #include    <exec/exec.h>
  16. #include    <exec/execbase.h>
  17.  
  18. #define    TIME15    900    /* Number of samples in 15 minutes */
  19. #define    TIME5    300    /* Number of samples in 5 minutes */
  20. #define    TIME1    60    /* Number of samples in 1 minute */
  21. #define    TPS    50    /* Ticks per sample, for Delay() */
  22. #define    CLOSEWIDTH    30    /* Width of the close gadget in pixels */
  23.  
  24. struct NewWindow windef = {
  25.     289, 0, 300, 10,
  26.     -1, -1,
  27.     CLOSEWINDOW,
  28.     WINDOWCLOSE | WINDOWDEPTH | WINDOWDRAG,
  29.     NULL, NULL,
  30.     (UBYTE *) "Load",
  31.     NULL, NULL, 0, 0, 0, 0,
  32.     WBENCHSCREEN
  33. };
  34.  
  35. struct IntuiText wintext = {
  36.     0, 1, JAM2,
  37.     0, 0,
  38.     NULL, NULL, NULL
  39.     };
  40.  
  41. extern void *OpenLibrary(), *OpenWindow(), *FindTask();
  42.  
  43. struct IntuitionBase *IntuitionBase;
  44.  
  45. void
  46. convertTime(sum, count, position)
  47. long sum;
  48. int count;
  49. register char *position;
  50. {
  51.     register long temp;
  52.  
  53.     temp = (sum * 100L + 50L) / count;
  54.     *(position + 4) = '0' + (int)(temp % 10);
  55.     temp /= 10;
  56.     *(position + 3) = '0' + (int)(temp % 10);
  57.     temp /= 10;
  58.     *(position + 1) = '0' + (int)(temp % 10);
  59.     temp /= 10;
  60.     if (temp != 0) {
  61.     *position = '0' + (int)(temp % 10);
  62.     }
  63.     else {
  64.     *position = ' ';
  65.     }
  66.     }
  67.  
  68. #define    FORMAT    "Load: ##.## ##.## ##.##"
  69.  
  70. main()
  71. {
  72.     register int length;    /* Length of the current run queue */
  73.     register struct Node *mynode; /* Node to walk through the queue */
  74.     register int i;    /* General counter */
  75.     int last[TIME15];    /* Length of the run queue in the last TIME seconds */
  76.     long sum15 = 0;    /* Sum of the last 15 minutes */
  77.     long sum5 = 0;    /* Sum of the last 5 minutes */
  78.     long sum1 = 0;    /* Sum of the last minute */
  79.     int pos = 0;    /* Current position in last for new value */
  80.     int pos15 = 0;    /* Current position in last for sum15 */
  81.     int pos5 = TIME15-TIME5;    /* Current position in last for sum5 */
  82.     int pos1 = TIME15-TIME1;    /* Current position in last for sum1 */
  83.     char uptimestr[26];
  84.     struct ExecBase *ExecBase;
  85.     struct Window *win;
  86.  
  87.     SetTaskPri(FindTask(0L), 5L);
  88.  
  89.     (void) strcpy(uptimestr, FORMAT);
  90.  
  91.     if (!(ExecBase = (struct ExecBase *)OpenLibrary("exec.library", 0L))) {
  92.     printf("Couldn't open exec... the sky is falling!\n");
  93.     exit(999);
  94.     }
  95.  
  96.     if (!(IntuitionBase =
  97.         (struct IntuitionBase *)OpenLibrary("intuition.library", 0L))) {
  98.     printf("Couldn't open intuition - must be dumb\n");
  99.     CloseLibrary(ExecBase);
  100.     exit(998);
  101.     }
  102.  
  103.     if (!(win = OpenWindow(&windef))) {
  104.     printf("Couldn't open window\n");
  105.     CloseLibrary(IntuitionBase);
  106.     CloseLibrary(ExecBase);
  107.     exit(997);
  108.     }
  109.  
  110.     wintext.IText = (UBYTE *)uptimestr;
  111.  
  112.     for (i = 0; i < TIME15 ; i++) {
  113.     last[i] = 0;
  114.     }
  115.  
  116.     for (;;) {
  117.     Delay((long)TPS);
  118.     if (GetMsg(win -> UserPort)) {
  119.         break;
  120.         }
  121.  
  122.     length = 0;
  123.     Disable();
  124.     for (mynode = (ExecBase -> TaskReady).lh_Head;
  125.         mynode = mynode -> ln_Succ; ) {
  126.         length++;
  127.         }
  128.     Enable();
  129.  
  130.     sum15 += (length - last[pos15]);
  131.     sum5 += (length - last[pos5]);
  132.     sum1 += (length - last[pos1]);
  133.     last[pos] = length;
  134.     if (++pos15 == TIME15) {
  135.         pos15 = 0;
  136.         }
  137.     if (++pos5 == TIME15) {
  138.         pos5 = 0;
  139.         }
  140.     if (++pos1 == TIME15) {
  141.         pos1 = 0;
  142.         }
  143.     if (++pos == TIME15) {
  144.         pos = 0;
  145.         }
  146.  
  147.     convertTime(sum1, TIME1, uptimestr + 6);
  148.     convertTime(sum5, TIME5, uptimestr + 12);
  149.     convertTime(sum15, TIME15, uptimestr + 18);
  150.     PrintIText(win -> RPort, &wintext, (long)CLOSEWIDTH, 1L);
  151.     }
  152.  
  153.     CloseWindow(win);
  154.     CloseLibrary(IntuitionBase);
  155.     CloseLibrary(ExecBase);
  156.     }
  157.  
  158.  
  159.