Reduced resource usage.

This commit is contained in:
spx
2011-02-07 09:38:27 +00:00
parent f505c30828
commit 1228e243cc
10 changed files with 165 additions and 133 deletions

View File

@ -31,6 +31,11 @@
#define Li2Double(x) ((double)((x).QuadPart))
#define Ft2Double(x) ((double)((x).dwHighDateTime) * 4.294967296E9 + (double)((x).dwLowDateTime))
PROCNTQSI CMeasureCPU::c_NtQuerySystemInformation = NULL;
PROCGST CMeasureCPU::c_GetSystemTimes = NULL;
int CMeasureCPU::c_NumOfProcessors = 0;
ULONG CMeasureCPU::c_BufferSize = 0;
// ntdll!NtQuerySystemInformation (NT specific!)
//
// The function copies the system information of the
@ -57,15 +62,24 @@
*/
CMeasureCPU::CMeasureCPU(CMeterWindow* meterWindow) : CMeasure(meterWindow),
m_FirstTime(true),
m_Processor(),
m_NtQuerySystemInformation((PROCNTQSI)GetProcAddress(GetModuleHandle(L"ntdll"), "NtQuerySystemInformation")),
m_GetSystemTimes((PROCGST)GetProcAddress(GetModuleHandle(L"kernel32"), "GetSystemTimes"))
m_Processor()
{
m_MaxValue = 100.0;
SYSTEM_INFO systemInfo = {0};
GetSystemInfo(&systemInfo);
m_NumOfProcessors = (int)systemInfo.dwNumberOfProcessors;
if (c_NtQuerySystemInformation == NULL)
{
c_NtQuerySystemInformation = (PROCNTQSI)GetProcAddress(GetModuleHandle(L"ntdll"), "NtQuerySystemInformation");
}
if (c_GetSystemTimes == NULL)
{
c_GetSystemTimes = (PROCGST)GetProcAddress(GetModuleHandle(L"kernel32"), "GetSystemTimes");
}
if (c_NumOfProcessors == 0)
{
SYSTEM_INFO systemInfo = {0};
GetSystemInfo(&systemInfo);
c_NumOfProcessors = (int)systemInfo.dwNumberOfProcessors;
}
}
/*
@ -90,7 +104,7 @@ void CMeasureCPU::ReadConfig(CConfigParser& parser, const WCHAR* section)
int processor = parser.ReadInt(section, L"Processor", 0);
if (processor < 0 || processor > m_NumOfProcessors)
if (processor < 0 || processor > c_NumOfProcessors)
{
LogWithArgs(LOG_WARNING, L"[%s] Invalid Processor: %i", section, processor);
@ -105,9 +119,9 @@ void CMeasureCPU::ReadConfig(CConfigParser& parser, const WCHAR* section)
if (m_FirstTime)
{
if (m_Processor == 0 && m_GetSystemTimes == NULL)
if (m_Processor == 0 && c_GetSystemTimes == NULL)
{
m_OldTime.assign(m_NumOfProcessors * 2, 0.0);
m_OldTime.assign(c_NumOfProcessors * 2, 0.0);
}
else
{
@ -126,23 +140,23 @@ bool CMeasureCPU::Update()
{
if (!CMeasure::PreUpdate()) return false;
if (m_Processor == 0 && m_GetSystemTimes)
if (m_Processor == 0 && c_GetSystemTimes)
{
BOOL status;
FILETIME ftIdleTime, ftKernelTime, ftUserTime;
// get new CPU's idle/kernel/user time
status = m_GetSystemTimes(&ftIdleTime, &ftKernelTime, &ftUserTime);
status = c_GetSystemTimes(&ftIdleTime, &ftKernelTime, &ftUserTime);
if (status == 0) return false;
CalcUsage(Ft2Double(ftIdleTime),
Ft2Double(ftKernelTime) + Ft2Double(ftUserTime));
}
else if (m_NtQuerySystemInformation)
else if (c_NtQuerySystemInformation)
{
LONG status;
BYTE* buf = NULL;
ULONG bufSize = 0;
ULONG bufSize = c_BufferSize;
BYTE* buf = (bufSize > 0) ? new BYTE[bufSize] : NULL;
int loop = 0;
@ -150,16 +164,16 @@ bool CMeasureCPU::Update()
{
ULONG size = 0;
status = m_NtQuerySystemInformation(SystemProcessorPerformanceInformation, buf, bufSize, &size);
if (status == STATUS_SUCCESS) break;
status = c_NtQuerySystemInformation(SystemProcessorPerformanceInformation, buf, bufSize, &size);
if (status == STATUS_SUCCESS || status != STATUS_INFO_LENGTH_MISMATCH) break;
if (status == STATUS_INFO_LENGTH_MISMATCH)
else // status == STATUS_INFO_LENGTH_MISMATCH
{
if (size == 0) // Returned required buffer size is always 0 on Windows 2000/XP.
{
if (bufSize == 0)
{
bufSize = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * m_NumOfProcessors;
bufSize = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * c_NumOfProcessors;
}
else
{
@ -178,24 +192,24 @@ bool CMeasureCPU::Update()
}
}
if (buf) delete [] buf;
delete [] buf;
buf = new BYTE[bufSize];
}
else // failed
{
if (buf) delete [] buf;
return false;
}
++loop;
} while (loop < 10);
} while (loop < 5);
if (status != STATUS_SUCCESS) // failed
{
if (buf) delete [] buf;
delete [] buf;
return false;
}
if (bufSize != c_BufferSize)
{
// Store the new buffer size
c_BufferSize = bufSize;
}
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* systemPerfInfo = (SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION*)buf;
if (m_Processor == 0)
@ -257,12 +271,12 @@ void CMeasureCPU::CalcUsage(double idleTime, double systemTime)
*/
void CMeasureCPU::CalcAverageUsage(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* systemPerfInfo)
{
if(!m_FirstTime)
if (!m_FirstTime)
{
double dbIdleTimeDiff = 0, dbSystemTimeDiff = 0;
double dbCpuUsage;
for (int i = 0; i < m_NumOfProcessors; ++i)
for (int i = 0; i < c_NumOfProcessors; ++i)
{
double dbIdleTime, dbSystemTime;
@ -286,7 +300,7 @@ void CMeasureCPU::CalcAverageUsage(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* sys
else
{
// store new CPU's idle and system time
for (int i = 0; i < m_NumOfProcessors; ++i)
for (int i = 0; i < c_NumOfProcessors; ++i)
{
m_OldTime[i * 2 + 0] = Li2Double(systemPerfInfo[i].IdleTime);
m_OldTime[i * 2 + 1] = Li2Double(systemPerfInfo[i].KernelTime) + Li2Double(systemPerfInfo[i].UserTime);