home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / CPUMET.ZIP / CPUMETER.C next >
C/C++ Source or Header  |  1990-12-21  |  5KB  |  202 lines

  1. #define INCL_BASE
  2. #define INCL_WIN
  3. #include <os2.h>
  4. #include <process.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include "cpumeter.h"
  8.  
  9. #define  THREADSTACKSIZE 4096
  10.  
  11. void TimingThread(void);
  12. void CountingThread(void);
  13. void CalibrationThread(void);
  14. int  nearest_10_percent(LONG lAmount);
  15.  
  16. TID    tidTiming;
  17. TID    tidCounting;
  18. TID    tidCalibration;
  19.  
  20. HWND    hwndClient;
  21. HWND    hwndFrame;
  22.  
  23. HSYSSEM hSem;
  24.  
  25. LONG    lCount       = 0L;
  26. LONG    lCountMax  = 0L;
  27. LONG    lTenth       = 0L;
  28. LONG    lTwentieth = 0L;
  29.  
  30. UCHAR    iTimingThreadStack    [THREADSTACKSIZE];
  31. UCHAR    iCalibrationThreadStack [THREADSTACKSIZE];
  32. UCHAR    iCountingThreadStack    [THREADSTACKSIZE];
  33.  
  34. HPOINTER hIcon[11];
  35.  
  36. //
  37. // Main function of programm
  38. //
  39. int main (int argc, char *argv[])
  40. {
  41.     static CHAR  szClientClass [] = "CPUMETER";
  42.     static ULONG flFrameFlags = FCF_TITLEBAR  | FCF_SYSMENU  |
  43.                 FCF_MINBUTTON | FCF_TASKLIST |
  44.                 FCF_ICON;
  45.     HAB      hab;
  46.     HMQ      hmq;
  47.     QMSG     qmsg ;
  48.  
  49.     //
  50.     // only one instance
  51.     //
  52.     if (DosCreateSem(CSEM_PUBLIC, &hSem, "\\sem\\cpumeter.sem"))
  53.     DosExit(EXIT_PROCESS, 0);
  54.  
  55.     //
  56.     // Initialize PM
  57.     //
  58.     hab = WinInitialize (0) ;
  59.     hmq = WinCreateMsgQueue (hab, 0) ;
  60.  
  61.     WinRegisterClass (
  62.                    hab,                // Anchor block handle
  63.                    szClientClass,      // Name of class being registered
  64.                    ClientWndProc,      // Window procedure for class
  65.                    CS_SIZEREDRAW,      // Class style
  66.                    0) ;                // Extra bytes to reserve
  67.  
  68.  
  69.     hwndFrame = WinCreateStdWindow (
  70.                    HWND_DESKTOP,       // Parent window handle
  71.            WS_VISIBLE,           // Style of frame window
  72.                    &flFrameFlags,      // Pointer to control data
  73.                    szClientClass,      // Client window class name
  74.            NULL,           // Title bar text
  75.                    0L,                 // Style of client window
  76.                    (HMODULE)NULL,      // Module handle for resources
  77.            ID_ICON00,           // ID of resources
  78.                    &hwndClient) ;      // Pointer to client window handle
  79.  
  80.     WinSetWindowPos (hwndFrame, HWND_TOP,
  81.              0, 0, WinQuerySysValue (HWND_DESKTOP, SV_CXSCREEN)/3,
  82.              WinQuerySysValue (HWND_DESKTOP, SV_CYTITLEBAR),
  83.              SWP_SHOW|SWP_MOVE|SWP_SIZE);
  84.  
  85.     WinSetWindowPos (hwndFrame, HWND_TOP,
  86.              0, 0, 0, 0,
  87.              SWP_SHOW|SWP_MINIMIZE);
  88.  
  89.     DosSetPrty (PRTYS_THREAD, PRTYC_TIMECRITICAL, 31, 1);
  90.     tidCalibration =_beginthread (CalibrationThread, iCalibrationThreadStack,
  91.                   THREADSTACKSIZE, NULL);
  92.     DosSleep (1000L);
  93.     DosSuspendThread (tidCalibration);
  94.     DosSetPrty (PRTYS_THREAD, PRTYC_REGULAR, 0, 1);
  95.  
  96.     lTenth     = lCountMax / 10;
  97.     lTwentieth = lCountMax / 20;
  98.  
  99.     tidTiming  = _beginthread (TimingThread, iTimingThreadStack,
  100.                    THREADSTACKSIZE, NULL);
  101.     tidCounting    = _beginthread (CountingThread, iCountingThreadStack,
  102.                    THREADSTACKSIZE, NULL);
  103.  
  104.     //
  105.     // Main message loop
  106.     //
  107.     while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  108.          WinDispatchMsg (hab, &qmsg) ;
  109.  
  110.     //
  111.     // End the thread & term PM
  112.     //
  113.     WinDestroyWindow (hwndFrame) ;
  114.     WinDestroyMsgQueue (hmq) ;
  115.     WinTerminate (hab) ;
  116.     return 0 ;
  117. }
  118.  
  119. //
  120. // Window Procedure
  121. //
  122. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  123. {
  124.     int i;
  125.  
  126.     switch (msg)
  127.     {
  128.         case WM_CREATE:
  129.         for (i = 0; i < 11; i++)
  130.         hIcon[i] = WinLoadPointer (HWND_DESKTOP, (HMODULE)NULL,
  131.                        100+i);
  132.         return 0;
  133.  
  134.     case WM_SEM1:
  135.     {
  136.         static char szBuffer[60];
  137.         static int    iOld = -1;
  138.  
  139.         i = nearest_10_percent (LONGFROMMP(mp1));
  140.         if (i != iOld)
  141.         {
  142.         iOld = i;
  143. //        sprintf(szBuffer, "%ld/%ld -> %d0%%",
  144. //            LONGFROMMP(mp1), lCountMax, i);
  145.         sprintf(szBuffer, "%d0%%", i);
  146.         WinSetWindowText (hwndFrame, szBuffer);
  147.         WinSendMsg (hwndFrame, WM_SETICON, hIcon[i], NULL);
  148.         WinInvalidateRect (hwndFrame, NULL, FALSE);
  149.         WinUpdateWindow (hwndFrame);
  150.         }
  151.         return 0;
  152.     }
  153.  
  154.     case WM_DESTROY:
  155.     {
  156.         for (i = 0; i < 11; i++)
  157.         WinDestroyPointer (hIcon[i]);
  158.         DosCloseSem (hSem);
  159.         return 0;
  160.     }
  161.     }
  162.     return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  163. }
  164.  
  165. void TimingThread ()
  166. {
  167.     DosSetPrty (PRTYS_THREAD, PRTYC_TIMECRITICAL, PRTYD_MAXIMUM, tidTiming);
  168.     while (TRUE)
  169.     {
  170.     DosSleep (1000L);
  171.     WinPostMsg (hwndClient, WM_SEM1, MPFROMLONG(lCount), NULL);
  172.     lCount = 0L;
  173.     }
  174. }
  175.  
  176. void CountingThread ()
  177. {
  178.     DosSetPrty (PRTYS_THREAD, PRTYC_IDLETIME, 0, tidCounting);
  179.     while (TRUE) lCount++;
  180. }
  181.  
  182. void CalibrationThread ()
  183. {
  184.     DosSetPrty (PRTYS_THREAD, PRTYC_TIMECRITICAL, 30, tidCalibration);
  185.     while (TRUE) lCountMax++;
  186. }
  187.  
  188. INT  nearest_10_percent (LONG lAmount)
  189. {
  190.     ldiv_t struct_ldiv;
  191.  
  192.     if (lAmount == 0)
  193.     return 10;
  194.     if (lAmount >= lCountMax)
  195.     return 0;
  196.  
  197.     struct_ldiv = ldiv (lAmount, lTenth);
  198.     if (struct_ldiv.rem >= lTwentieth)
  199.     struct_ldiv.quot++;
  200.     return (int)(10-struct_ldiv.quot);
  201. }
  202.