float GetCPUUsageWin32() { CPerfCounters<LONGLONG> PerfCounters; DWORD dwObjectIndex = PROCESS_OBJECT_INDEX; DWORD dwCpuUsageIndex = PROCESSOR_TIME_COUNTER_INDEX; int CpuUsage = 0; LONGLONG lnNewValue = 0; PPERF_DATA_BLOCK pPerfData = NULL; LARGE_INTEGER NewPerfTime100nSec = {0}; lnNewValue = PerfCounters.GetCounterValueForProcessID(&pPerfData, dwObjectIndex, dwCpuUsageIndex, GetCurrentProcessId()); NewPerfTime100nSec = pPerfData->PerfTime100nSec; if (m_bFirstTime) { m_bFirstTime = false; m_lnOldValue = lnNewValue; m_OldPerfTime100nSec = NewPerfTime100nSec; return 0; } LONGLONG lnValueDelta = lnNewValue - m_lnOldValue; double DeltaPerfTime100nSec = (double)NewPerfTime100nSec.QuadPart - (double)m_OldPerfTime100nSec.QuadPart; m_lnOldValue = lnNewValue; m_OldPerfTime100nSec = NewPerfTime100nSec; double a = (double)lnValueDelta / DeltaPerfTime100nSec; a /= double(number_of_cpus); return (a * 100); }
DWORD CSystemCounters::GetServerSessions() { CPerfCounters<LONGLONG> PerfCounters; DWORD dwObjectIndex = SERVER_OBJECT_INDEX; DWORD dwServerSessionsCounter = SERVER_SESSIONS_OBJECT_INDEX; PPERF_DATA_BLOCK pPerfData = NULL; PPERF_OBJECT_TYPE pPerfObj = NULL; LONGLONG x = PerfCounters.GetCounterValue(&pPerfData, &pPerfObj, dwObjectIndex, dwServerSessionsCounter, NULL); return (DWORD)x; }
double CCpuUsage::GetCpuUsage(DWORD dwProcessID) { static PLATFORM Platform = GetPlatform(); if (m_bFirstTime) EnablePerformaceCounters(); // Cpu usage counter is 8 byte length. CPerfCounters<LONGLONG> PerfCounters; DWORD dwObjectIndex = PROCESS_OBJECT_INDEX; DWORD dwCpuUsageIndex = PROCESSOR_TIME_COUNTER_INDEX; double CpuUsage = 0; LONGLONG lnNewValue = 0; PPERF_DATA_BLOCK pPerfData = NULL; LARGE_INTEGER NewPerfTime100nSec = {0}; lnNewValue = PerfCounters.GetCounterValueForProcessID(&pPerfData, dwObjectIndex, dwCpuUsageIndex, dwProcessID); NewPerfTime100nSec = pPerfData->PerfTime100nSec; if (m_bFirstTime) { m_bFirstTime = false; m_lnOldValue = lnNewValue; m_OldPerfTime100nSec = NewPerfTime100nSec; return 0; } LONGLONG lnValueDelta = lnNewValue - m_lnOldValue; double DeltaPerfTime100nSec = (double)NewPerfTime100nSec.QuadPart - (double)m_OldPerfTime100nSec.QuadPart; m_lnOldValue = lnNewValue; m_OldPerfTime100nSec = NewPerfTime100nSec; if (DeltaPerfTime100nSec == 0) return 0; double a = (double)lnValueDelta / DeltaPerfTime100nSec; CpuUsage = (double) (a*100); if (CpuUsage < 0) return 0.00; return CpuUsage; }
int CCpuUsage::GetCpuUsage(LPCTSTR pProcessName) { if (m_bFirstTime) EnablePerformaceCounters(); // Cpu usage counter is 8 byte length. CPerfCounters PerfCounters; char szInstance[256] = {0}; DWORD dwObjectIndex = PROCESS_OBJECT_INDEX; DWORD dwCpuUsageIndex = PROCESSOR_TIME_COUNTER_INDEX; strcpy_s(szInstance,256,pProcessName); int CpuUsage = 0; LONGLONG lnNewValue = 0; PPERF_DATA_BLOCK pPerfData = NULL; LARGE_INTEGER NewPerfTime100nSec = {0}; lnNewValue = PerfCounters.GetCounterValue(&pPerfData, dwObjectIndex, dwCpuUsageIndex, szInstance); NewPerfTime100nSec = pPerfData->PerfTime100nSec; if (m_bFirstTime) { m_bFirstTime = false; m_lnOldValue = lnNewValue; m_OldPerfTime100nSec = NewPerfTime100nSec; return 0; } LONGLONG lnValueDelta = lnNewValue - m_lnOldValue; double DeltaPerfTime100nSec = (double)NewPerfTime100nSec.QuadPart - (double)m_OldPerfTime100nSec.QuadPart; m_lnOldValue = lnNewValue; m_OldPerfTime100nSec = NewPerfTime100nSec; double a = (double)lnValueDelta / DeltaPerfTime100nSec; CpuUsage = (int) (a*100); if (CpuUsage < 0) return 0; return CpuUsage; }
/////////////////////////////////////////////////////////////////// // // GetSystemUpTime uses the performance counters to retrieve the // system up time. // The system up time counter is of type PERF_ELAPSED_TIME // which as the following calculation: // // Element Value // ======= =========== // X CounterData // Y PerfTime // Data Size 8 Bytes // Time base PerfFreq // Calculation (Y-X)/TB // /////////////////////////////////////////////////////////////////// DWORD CSystemCounters::GetSystemUpTime() { CPerfCounters<LONGLONG> PerfCounters; DWORD dwObjectIndex = SYSTEM_OBJECT_INDEX; DWORD dwSystemUpTimeCounter = SYSTEM_UPTIME_COUNTER_INDEX; PPERF_DATA_BLOCK pPerfData = NULL; PPERF_OBJECT_TYPE pPerfObj = NULL; LONGLONG x = PerfCounters.GetCounterValue(&pPerfData, &pPerfObj, dwObjectIndex, dwSystemUpTimeCounter, NULL); LONGLONG y = pPerfObj->PerfTime.QuadPart; LONGLONG tb = pPerfObj->PerfFreq.QuadPart; if (tb == 0) return 0; DWORD UpTime = (DWORD)((y-x) / tb); if (UpTime < 0) return 0; return UpTime; }
// // GetCpuUsage returns the system-wide cpu usage. // Since we calculate the cpu usage by two samplings, the first // call to GetCpuUsage() returns 0 and keeps the values for the next // sampling. // Read the comment at the beginning of this file for the formula. // int CCpuUsage::GetCpuUsage() { if (m_bFirstTime) EnablePerformaceCounters(); // Cpu usage counter is 8 byte length. CPerfCounters PerfCounters; char szInstance[256] = {0}; // Note: // ==== // On windows NT, cpu usage counter is '% Total processor time' // under 'System' object. However, in Win2K/XP Microsoft moved // that counter to '% processor time' under '_Total' instance // of 'Processor' object. // Read 'INFO: Percent Total Performance Counter Changes on Windows 2000' // Q259390 in MSDN. DWORD dwObjectIndex; DWORD dwCpuUsageIndex; switch (m_nPlatform) { case WINNT: dwObjectIndex = SYSTEM_OBJECT_INDEX; dwCpuUsageIndex = TOTAL_PROCESSOR_TIME_COUNTER_INDEX; break; case WIN2K_XP: dwObjectIndex = PROCESSOR_OBJECT_INDEX; dwCpuUsageIndex = PROCESSOR_TIME_COUNTER_INDEX; strcpy_s(szInstance,256,"_Total"); break; default: return -1; } int CpuUsage = 0; LONGLONG lnNewValue = 0; PPERF_DATA_BLOCK pPerfData = NULL; LARGE_INTEGER NewPerfTime100nSec = {0}; lnNewValue = PerfCounters.GetCounterValue(&pPerfData, dwObjectIndex, dwCpuUsageIndex, szInstance); NewPerfTime100nSec = pPerfData->PerfTime100nSec; if (m_bFirstTime) { m_bFirstTime = false; m_lnOldValue = lnNewValue; m_OldPerfTime100nSec = NewPerfTime100nSec; return 0; } LONGLONG lnValueDelta = lnNewValue - m_lnOldValue; double DeltaPerfTime100nSec = (double)NewPerfTime100nSec.QuadPart - (double)m_OldPerfTime100nSec.QuadPart; m_lnOldValue = lnNewValue; m_OldPerfTime100nSec = NewPerfTime100nSec; double a = (double)lnValueDelta / DeltaPerfTime100nSec; double f = (1.0 - a) * 100.0; CpuUsage = (int)(f ); // rounding the result if (CpuUsage < 0) return 0; return CpuUsage; }
// // GetCpuUsage returns the system-wide cpu usage. // Since we calculate the cpu usage by two samplings, the first // call to GetCpuUsage() returns 0 and keeps the values for the next // sampling. // Read the comment at the beginning of this file for the formula. // double CCpuUsage::GetCpuUsage() { static PLATFORM Platform = GetPlatform(); if (m_bFirstTime) EnablePerformaceCounters(); // Cpu usage counter is 8 byte length. CPerfCounters<LONGLONG> PerfCounters; wstring szInstance; // Note: // ==== // On windows NT, cpu usage counter is '% Total processor time' // under 'System' object. However, in Win2K/XP Microsoft moved // that counter to '% processor time' under '_Total' instance // of 'Processor' object. // Read 'INFO: Percent Total Performance Counter Changes on Windows 2000' // Q259390 in MSDN. DWORD dwObjectIndex; DWORD dwCpuUsageIndex; switch (Platform) { case WINNT: dwObjectIndex = SYSTEM_OBJECT_INDEX; dwCpuUsageIndex = TOTAL_PROCESSOR_TIME_COUNTER_INDEX; break; case WIN2K_XP: dwObjectIndex = PROCESSOR_OBJECT_INDEX; dwCpuUsageIndex = PROCESSOR_TIME_COUNTER_INDEX; if (m_proc == TOTAL_CPU_USAGE_PROC) szInstance = L"_Total"; else { std::wostringstream oss; oss << m_proc; szInstance = oss.str(); } break; default: return -1; } double CpuUsage = 0; LONGLONG lnNewValue = 0; PPERF_DATA_BLOCK pPerfData = NULL; LARGE_INTEGER NewPerfTime100nSec = {0}; lnNewValue = PerfCounters.GetCounterValue(&pPerfData, dwObjectIndex, dwCpuUsageIndex, (const LPWSTR)szInstance.c_str()); NewPerfTime100nSec = pPerfData->PerfTime100nSec; if (m_bFirstTime) { m_bFirstTime = false; m_lnOldValue = lnNewValue; m_OldPerfTime100nSec = NewPerfTime100nSec; return 0; } LONGLONG lnValueDelta = lnNewValue - m_lnOldValue; double DeltaPerfTime100nSec = (double)NewPerfTime100nSec.QuadPart - (double)m_OldPerfTime100nSec.QuadPart; m_lnOldValue = lnNewValue; m_OldPerfTime100nSec = NewPerfTime100nSec; if (DeltaPerfTime100nSec == 0) return 0; double a = (double)lnValueDelta / DeltaPerfTime100nSec; double f = (1.0 - a) * 100.0; CpuUsage = (double)(f + 0.5); // rounding the result if (CpuUsage < 0) return 0; return CpuUsage; }