home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Multimedia / AVStoDVD / AVStoDVD_280_Install.exe / AVSMeter / source / ProcessInfo.h < prev    next >
C/C++ Source or Header  |  2014-04-23  |  5KB  |  195 lines

  1. // This program is free software; you can redistribute it and/or modify
  2. // it under the terms of the GNU General Public License as published by
  3. // the Free Software Foundation; either version 2 of the License, or
  4. // (at your option) any later version.
  5.  
  6. #ifndef _PROCESSINFO_H
  7. #define _PROCESSINFO_H
  8.  
  9. #include "stdafx.h"
  10.  
  11. class CProcessInfo
  12. {
  13. public:
  14.     CProcessInfo();
  15.     virtual ~CProcessInfo();
  16.     void Update();
  17.     void CloseProcess();
  18.  
  19.     //public variables:
  20.     WORD  wCPUUsage;
  21.     WORD  wThreadCount;
  22.     DWORD dwPhysicalMemMB;
  23.     DWORD dwVirtualMemMB;
  24.  
  25. private:
  26.     WORD GetCurrentThreadCount();
  27.     void GetCPUUsage();
  28.     inline unsigned __int64 SubtractTimes(const FILETIME& ftA, const FILETIME& ftB);
  29.     BYTE btCPUUsage;
  30.     BOOL bFirstRun;
  31.     
  32.     FILETIME ftSysIdle, ftSysKernel, ftSysUser;
  33.     FILETIME ftProcCreation, ftProcExit, ftProcKernel, ftProcUser;
  34.  
  35.     //system total times
  36.     FILETIME ftPrevSysKernel;
  37.     FILETIME ftPrevSysUser;
  38.  
  39.     //process times
  40.     FILETIME ftPrevProcKernel;
  41.     FILETIME ftPrevProcUser;
  42.  
  43.     unsigned __int64 uiSysKernelDiff;
  44.     unsigned __int64 uiSysUserDiff;
  45.     unsigned __int64 uiProcKernelDiff;
  46.     unsigned __int64 uiProcUserDiff;
  47.     unsigned __int64 uiTotalSys;
  48.     unsigned __int64 uiTotalProc;
  49.  
  50.     DWORD dwLastTimeRun;
  51.     DWORD dwLastTimeRunThreadCount;
  52.     HANDLE hCurProcess;
  53.     PROCESS_MEMORY_COUNTERS pmc;
  54. };
  55.  
  56.  
  57. CProcessInfo::CProcessInfo()
  58. {
  59.     hCurProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, ::GetCurrentProcessId());
  60.  
  61.     dwPhysicalMemMB = 0;
  62.     dwVirtualMemMB = 0;
  63.  
  64.     ::ZeroMemory(&ftPrevSysKernel, sizeof(FILETIME));
  65.     ::ZeroMemory(&ftPrevSysUser, sizeof(FILETIME));
  66.     ::ZeroMemory(&ftPrevProcKernel, sizeof(FILETIME));
  67.     ::ZeroMemory(&ftPrevProcUser, sizeof(FILETIME));
  68.     
  69.     btCPUUsage = 0;
  70.     wThreadCount = GetCurrentThreadCount();
  71.     bFirstRun = TRUE;
  72.  
  73.     dwLastTimeRun = 0;
  74.     dwLastTimeRunThreadCount = 0;
  75. }
  76.  
  77. CProcessInfo::~CProcessInfo()
  78. {
  79. }
  80.  
  81.  
  82. void CProcessInfo::CloseProcess()
  83. {
  84.     if (hCurProcess)
  85.         ::CloseHandle(hCurProcess);
  86.  
  87.     return;
  88. }
  89.  
  90.  
  91. void CProcessInfo::Update()
  92. {
  93.     //Min. update interval 100ms
  94.     if ((::GetTickCount() - dwLastTimeRun) < 100)
  95.         return;
  96.  
  97.     GetCPUUsage();
  98.     bFirstRun = FALSE;
  99.  
  100.     if ((btCPUUsage < 1) || (btCPUUsage > 100))
  101.         wCPUUsage = 0;
  102.     else
  103.         wCPUUsage = (WORD)btCPUUsage;
  104.  
  105.     if ((::GetTickCount() - dwLastTimeRunThreadCount) > 1000)
  106.     {
  107.         wThreadCount = GetCurrentThreadCount();
  108.         dwLastTimeRunThreadCount = GetTickCount();
  109.     }
  110.  
  111.     ::GetProcessMemoryInfo(hCurProcess, &pmc, sizeof(pmc));
  112.     dwPhysicalMemMB = (DWORD)(((double)(pmc.WorkingSetSize) / 1048576.0) + 0.5);
  113.     dwVirtualMemMB = (DWORD)(((double)(pmc.PagefileUsage) / 1048576.0) + 0.5);
  114.  
  115.     dwLastTimeRun = GetTickCount();
  116.  
  117.     return;
  118. }
  119.  
  120.  
  121. void CProcessInfo::GetCPUUsage()
  122. {
  123.     if (!::GetSystemTimes(&ftSysIdle, &ftSysKernel, &ftSysUser) || !::GetProcessTimes(::GetCurrentProcess(), &ftProcCreation, &ftProcExit, &ftProcKernel, &ftProcUser))
  124.         return;
  125.  
  126.     if (!bFirstRun)
  127.     {
  128.         /*
  129.         CPU usage is calculated by getting the total amount of time the system
  130.         has operated since the last measurement (made up of kernel + user) and
  131.         the total amount of time the process has run (kernel + user).
  132.         */
  133.         uiSysKernelDiff = SubtractTimes(ftSysKernel, ftPrevSysKernel);
  134.         uiSysUserDiff = SubtractTimes(ftSysUser, ftPrevSysUser);
  135.         uiProcKernelDiff = SubtractTimes(ftProcKernel, ftPrevProcKernel);
  136.         uiProcUserDiff = SubtractTimes(ftProcUser, ftPrevProcUser);
  137.         uiTotalSys =  uiSysKernelDiff + uiSysUserDiff;
  138.         uiTotalProc = uiProcKernelDiff + uiProcUserDiff;
  139.  
  140.         if (uiTotalSys > 0)
  141.             btCPUUsage = (BYTE)((100.0 * (double)uiTotalProc) / (double)uiTotalSys);
  142.     }
  143.     
  144.     ftPrevSysKernel = ftSysKernel;
  145.     ftPrevSysUser = ftSysUser;
  146.     ftPrevProcKernel = ftProcKernel;
  147.     ftPrevProcUser = ftProcUser;
  148.  
  149.     return;
  150. }
  151.  
  152.  
  153. inline unsigned __int64 CProcessInfo::SubtractTimes(const FILETIME& ftA, const FILETIME& ftB)
  154. {
  155.     unsigned __int64 a = (((unsigned __int64)ftA.dwHighDateTime) << 32) + (unsigned __int64)ftA.dwLowDateTime;
  156.     unsigned __int64 b = (((unsigned __int64)ftB.dwHighDateTime) << 32) + (unsigned __int64)ftB.dwLowDateTime;
  157.  
  158.     return (a - b);
  159. }
  160.  
  161.  
  162. WORD CProcessInfo::GetCurrentThreadCount()
  163. {
  164.     DWORD dwPID = ::GetCurrentProcessId();
  165.     HANDLE hThreadSnapshot = INVALID_HANDLE_VALUE; 
  166.     THREADENTRY32 te32; 
  167.  
  168.     hThreadSnapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); 
  169.     if (hThreadSnapshot == INVALID_HANDLE_VALUE) 
  170.         return 0; 
  171.  
  172.     te32.dwSize = sizeof(THREADENTRY32); 
  173.  
  174.     if (!::Thread32First(hThreadSnapshot, &te32)) 
  175.     {
  176.         ::CloseHandle(hThreadSnapshot);
  177.         return 0;
  178.     }
  179.  
  180.     WORD wThreads = 0;
  181.     while (::Thread32Next(hThreadSnapshot, &te32))
  182.     {
  183.         if (te32.th32OwnerProcessID == dwPID)
  184.             ++wThreads;
  185.     }
  186.  
  187.     ::CloseHandle(hThreadSnapshot);
  188.  
  189.     return wThreads;
  190. }
  191.  
  192.  
  193. #endif //_PROCESSINFO_H
  194.  
  195.