home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff314.lha / zc / zc.lzh / Examples / Amiga / Lav / lav.c < prev    next >
C/C++ Source or Header  |  1988-07-15  |  4KB  |  166 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. extern long Output(), Write();
  43.  
  44. struct IntuitionBase *IntuitionBase;
  45.  
  46. void errmsg( msg )
  47. char *msg;
  48. {
  49.     Write( Output(), msg, (long)strlen(msg));
  50. }
  51.  
  52. void
  53. convertTime(sum, count, position)
  54. long sum;
  55. int count;
  56. register char *position;
  57. {
  58.     register long temp;
  59.  
  60.     temp = (sum * 100L + 50L) / count;
  61.     *(position + 4) = '0' + (int)(temp % 10);
  62.     temp /= 10;
  63.     *(position + 3) = '0' + (int)(temp % 10);
  64.     temp /= 10;
  65.     *(position + 1) = '0' + (int)(temp % 10);
  66.     temp /= 10;
  67.     if (temp != 0) {
  68.     *position = '0' + (int)(temp % 10);
  69.     }
  70.     else {
  71.     *position = ' ';
  72.     }
  73.     }
  74.  
  75. #define    FORMAT    "Load: ##.## ##.## ##.##"
  76.  
  77. main()
  78. {
  79.     register int length;    /* Length of the current run queue */
  80.     register struct Node *mynode; /* Node to walk through the queue */
  81.     register int i;    /* General counter */
  82.     int last[TIME15];    /* Length of the run queue in the last TIME seconds */
  83.     long sum15 = 0;    /* Sum of the last 15 minutes */
  84.     long sum5 = 0;    /* Sum of the last 5 minutes */
  85.     long sum1 = 0;    /* Sum of the last minute */
  86.     int pos = 0;    /* Current position in last for new value */
  87.     int pos15 = 0;    /* Current position in last for sum15 */
  88.     int pos5 = TIME15-TIME5;    /* Current position in last for sum5 */
  89.     int pos1 = TIME15-TIME1;    /* Current position in last for sum1 */
  90.     char uptimestr[26];
  91.     struct ExecBase *ExecBase;
  92.     struct Window *win;
  93.  
  94.     SetTaskPri(FindTask(0L), 5L);
  95.  
  96.     (void) strcpy(uptimestr, FORMAT);
  97.  
  98.     if (!(ExecBase = (struct ExecBase *)OpenLibrary("exec.library", 0L))) {
  99.     errmsg("Couldn't open exec... the sky is falling!\n");
  100.     exit(999);
  101.     }
  102.  
  103.     if (!(IntuitionBase =
  104.         (struct IntuitionBase *)OpenLibrary("intuition.library", 0L))) {
  105.     errmsg("Couldn't open intuition - must be dumb\n");
  106.     CloseLibrary(ExecBase);
  107.     exit(998);
  108.     }
  109.  
  110.     if (!(win = OpenWindow(&windef))) {
  111.     errmsg("Couldn't open window\n");
  112.     CloseLibrary(IntuitionBase);
  113.     CloseLibrary(ExecBase);
  114.     exit(997);
  115.     }
  116.  
  117.     wintext.IText = (UBYTE *)uptimestr;
  118.  
  119.     for (i = 0; i < TIME15 ; i++) {
  120.     last[i] = 0;
  121.     }
  122.  
  123.     for (;;) {
  124.     Delay((long)TPS);
  125.     if (GetMsg(win -> UserPort)) {
  126.         break;
  127.         }
  128.  
  129.     length = 0;
  130.     Disable();
  131.     for (mynode = (ExecBase -> TaskReady).lh_Head;
  132.         mynode = mynode -> ln_Succ; ) {
  133.         length++;
  134.         }
  135.     Enable();
  136.  
  137.     sum15 += (length - last[pos15]);
  138.     sum5 += (length - last[pos5]);
  139.     sum1 += (length - last[pos1]);
  140.     last[pos] = length;
  141.     if (++pos15 == TIME15) {
  142.         pos15 = 0;
  143.         }
  144.     if (++pos5 == TIME15) {
  145.         pos5 = 0;
  146.         }
  147.     if (++pos1 == TIME15) {
  148.         pos1 = 0;
  149.         }
  150.     if (++pos == TIME15) {
  151.         pos = 0;
  152.         }
  153.  
  154.     convertTime(sum1, TIME1, uptimestr + 6);
  155.     convertTime(sum5, TIME5, uptimestr + 12);
  156.     convertTime(sum15, TIME15, uptimestr + 18);
  157.     PrintIText(win -> RPort, &wintext, (long)CLOSEWIDTH, 1L);
  158.     }
  159.  
  160.     CloseWindow(win);
  161.     CloseLibrary(IntuitionBase);
  162.     CloseLibrary(ExecBase);
  163.     }
  164.  
  165.  
  166.