home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: SysTools / SysTools.zip / sysba021.zip / SRC.ZIP / sysbar2 / Piper / cpuload / pscoll.cpp < prev    next >
C/C++ Source or Header  |  1998-07-22  |  6KB  |  230 lines

  1. /*
  2. ** Module   :PSCOLL.CPP
  3. ** Abstract :Process collection
  4. **
  5. ** Copyright (C) Sergey I. Yevtushenko
  6. ** Log: Thu  03/04/97   Designed from two different source trees
  7. **      Fri  05/09/97   Last cleanup before sending it.
  8. */
  9.  
  10. #define INCL_WIN
  11. #define INCL_NLS
  12. #define INCL_DOS
  13. #define INCL_GPI
  14. #include <os2.h>
  15. #include "pscoll.h"
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include "dosqss.h"
  19. #include <process.h>
  20.  
  21. #define PQ_BUFSIZE  0x10000
  22. #define MQ_BUFSIZE  0x8000
  23.  
  24. PQTOPLEVEL pqData = 0;
  25.  
  26. APIRET QuerySysInfo(void)
  27. {
  28.     APIRET RC;
  29.     if(!pqData)
  30.         pqData = (PQTOPLEVEL) new char[PQ_BUFSIZE];
  31.     memset(pqData, 0, PQ_BUFSIZE);
  32.     RC = DosQuerySysState(0x1F, 0, 0, 0, pqData, PQ_BUFSIZE);
  33.     return RC;
  34. }
  35.  
  36. void chtab(char *i)
  37. {
  38.     char *tmp = strchr(i, '\x0A');
  39.     
  40.     while (tmp)
  41.     {
  42.         *tmp = ' ';
  43.         tmp = strchr(i, '\x0A');
  44.     }
  45.     tmp = strchr(i, '\x0D');
  46.     while (tmp)
  47.     {
  48.         *tmp = ' ';
  49.         tmp = strchr(i, '\x0D');
  50.     }
  51. }
  52.  
  53. //-------------------------------------------------------
  54.  
  55. ProcInfo * ProcessCollection::LocatePID(int pid)
  56. {
  57.     for(int i = 0; i < Count(); i++)
  58.     {
  59.         ProcInfo * pInfo = (ProcInfo*)Get(i);
  60.         if(pInfo && pInfo->iPid == pid)
  61.             return pInfo;
  62.     }
  63.     return 0;
  64. }
  65.  
  66. ProcInfo * ProcessCollection::CheckAndInsert(ProcInfo *pInfo, int& flag)
  67. {
  68.     DWord index = Look(pInfo);
  69.     if(index < Count())
  70.     {
  71.         if(!Compare(pInfo, Get(index))) //------ Process exist ?
  72.         {
  73.             //------ Process already here
  74.             delete pInfo;
  75.             pInfo = (ProcInfo*)Get(index);
  76.             flag = 0;
  77.         }
  78.         else //------ No process don't exist
  79.         {
  80.             Add(pInfo);
  81.             flag = 1; //Additional info should be filled
  82.         }
  83.     }
  84.     else
  85.     {
  86.         Add(pInfo);
  87.         flag = 1;
  88.     }
  89.     return pInfo;
  90. }
  91.  
  92. void ProcessCollection::CollectData()
  93. {
  94.     APIRET RC;
  95.  
  96.     RC = QuerySysInfo();
  97.     if (!RC)
  98.     {
  99.         PQPROCESS pProcRecord = pqData->procdata;
  100.  
  101.         while ( pProcRecord->rectype != 3 )
  102.         {
  103.             PQTHREAD pThread = pProcRecord->threads;
  104.             ProcInfo * pInfo = new ProcInfo;
  105.             int iNeedFill = 0;
  106.  
  107.             pInfo->iPid = pProcRecord->pid;
  108.  
  109. //---- Check: if there any record found for that process
  110.  
  111.             pInfo = CheckAndInsert(pInfo, iNeedFill);
  112.  
  113.             if(iNeedFill)
  114.             {
  115.                 //char *pszType;
  116.                 //char Title[256];
  117.                 pInfo->cProxName[0] = 0;
  118.                 pInfo->iSid = pProcRecord->sessid;
  119.                 pInfo->iType = pProcRecord->type;
  120.                 pInfo->lOldUser   =
  121.                 pInfo->lOldSystem =
  122.                 pInfo->lDeltaUser =
  123.                 pInfo->lDeltaSystem = 0;
  124.  
  125.                 DosQueryModuleName(pProcRecord->hndmod, sizeof(pInfo->cProxName), pInfo->cProxName);
  126.                 if(!pInfo->cProxName[0])
  127.                     strcpy(pInfo->cProxName, "<zombie>");
  128.  
  129.                 //pszType = ppszType[(pInfo->iType <= 4 ) ? pInfo->iType:5];
  130.                 //if(pInfo->iType != 4 && !WinQueryTaskTitle(pInfo->iSid, Title, sizeof(Title)))
  131.                 //{
  132.                 //    chtab(Title);
  133.                 //    strcat(pInfo->cProxName, "(");
  134.                 //    strcat(pInfo->cProxName, Title);
  135.                 //    strcat(pInfo->cProxName, ")");
  136.                 //}
  137.                 //else
  138.                 //    if(!pInfo->cProxName[0])
  139.                 //        strcpy(pInfo->cProxName, "<zombie>");
  140.  
  141.             }
  142.             if(pThread)
  143.             {
  144.                 int j;
  145.                 unsigned long ulSys = 0;
  146.                 unsigned long ulUsr = 0;
  147.  
  148.                 for(j = 0; j < pProcRecord->threadcnt; j++)
  149.                 {
  150.                     ulSys += pThread->systime;
  151.                     ulUsr += pThread->usertime;
  152.                     pThread++;
  153.                 }
  154.                 pInfo->lDeltaUser   = ulUsr - pInfo->lOldUser;
  155.                 pInfo->lDeltaSystem = ulSys - pInfo->lOldSystem;
  156.                 pInfo->lOldUser     = ulUsr;
  157.                 pInfo->lOldSystem   = ulSys;
  158.             }
  159.  
  160. //--- Anyway, process are here, touch it
  161.  
  162.             pInfo->iTouched = 1;
  163.  
  164.             pProcRecord = (PQPROCESS) (pThread) ;
  165.         }
  166.     }
  167.  
  168. //------- Ok, data filled. Cleanup database
  169.     ulTotalSys = 0;
  170.     ulTotalUsr = 0;
  171.     for(int i = 0; i < Count(); i++)
  172.     {
  173.         ProcInfo * pInfo = (ProcInfo *)Get(i);
  174.         if(pInfo->iTouched == 0)        // Process not touched last time
  175.         {
  176.             Free(Remove(i));
  177.             i--;
  178.         }
  179.         else
  180.         {
  181.             ulTotalSys += pInfo->lDeltaSystem;
  182.             ulTotalUsr += pInfo->lDeltaUser;
  183.             pInfo->iTouched = 0;
  184.         }
  185.     }
  186.     if((ulTotalSys+ulTotalUsr) <= 0)
  187.     {
  188.         ulTotalUsr = 0;
  189.         ulTotalSys = 1;
  190.     }
  191. }
  192.  
  193. //void ProcessCollection::Print( char *pszBuf )
  194. int ProcessCollection::GetCPULoad( void )
  195. {
  196. /*
  197.     char cFiller[11];
  198.     int usedLen;
  199.     int freeLen;
  200.  
  201.     usedLen = (ulTotalUsr * 10)/(ulTotalUsr + ulTotalSys);
  202.     if(usedLen < 0)
  203.         usedLen = 0;
  204.  
  205.     if((((ulTotalUsr * 100)/(ulTotalUsr + ulTotalSys))%10) > 5)
  206.         usedLen++;
  207.  
  208.     freeLen = 10 - usedLen;
  209.     if(usedLen)
  210.         memset(cFiller, '▓', usedLen);
  211.     if(freeLen)
  212.         memset(&cFiller[usedLen], '░', freeLen);
  213.  
  214.     cFiller[10] = 0;
  215.  
  216.     printf("--------------\x1B[K\n"
  217.            "[%s] %d%%\x1B[K\n"
  218.            "--------------\x1B[K\n",
  219.             cFiller,
  220.            (ulTotalUsr*100)/(ulTotalUsr + ulTotalSys)
  221.            );
  222. */
  223.     return ( ulTotalUsr * 100 ) / ( ulTotalUsr + ulTotalSys );
  224. //    if ( iLoad > 99 ) iLoad = 99;
  225. //    if ( iLoad < 0 ) iLoad = 0;
  226. //    sprintf( pszBuf, "%02d%%", iLoad );
  227. //    fflush( stdout );
  228. }
  229.  
  230.