2009-02-10 18:37:48 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2001 Kimmo Pekkola
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2012-01-23 06:36:15 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-02-10 18:37:48 +00:00
|
|
|
*/
|
|
|
|
|
2009-10-07 16:45:14 +00:00
|
|
|
#include "StdAfx.h"
|
2009-02-10 18:37:48 +00:00
|
|
|
#include "MeasureCPU.h"
|
|
|
|
#include "Rainmeter.h"
|
2010-09-11 19:39:45 +00:00
|
|
|
#include "System.h"
|
2010-02-13 03:07:34 +00:00
|
|
|
#include "Error.h"
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2010-02-13 03:07:34 +00:00
|
|
|
#define STATUS_SUCCESS 0
|
|
|
|
#define STATUS_INFO_LENGTH_MISMATCH 0xC0000004
|
|
|
|
|
|
|
|
#define SystemProcessorPerformanceInformation 8
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2011-12-09 03:28:19 +00:00
|
|
|
typedef struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION {
|
|
|
|
LARGE_INTEGER IdleTime;
|
|
|
|
LARGE_INTEGER KernelTime;
|
|
|
|
LARGE_INTEGER UserTime;
|
|
|
|
LARGE_INTEGER Reserved1[2];
|
|
|
|
ULONG Reserved2;
|
|
|
|
} SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION, *PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;
|
|
|
|
|
2010-11-11 20:24:59 +00:00
|
|
|
#define Li2Double(x) ((double)((x).QuadPart))
|
2010-02-13 03:07:34 +00:00
|
|
|
#define Ft2Double(x) ((double)((x).dwHighDateTime) * 4.294967296E9 + (double)((x).dwLowDateTime))
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2013-05-31 14:28:39 +00:00
|
|
|
FPNTQSI MeasureCPU::c_NtQuerySystemInformation = nullptr;
|
2013-05-31 14:18:52 +00:00
|
|
|
int MeasureCPU::c_NumOfProcessors = 0;
|
|
|
|
ULONG MeasureCPU::c_BufferSize = 0;
|
2011-02-07 09:38:27 +00:00
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
// ntdll!NtQuerySystemInformation (NT specific!)
|
|
|
|
//
|
|
|
|
// The function copies the system information of the
|
|
|
|
// specified type into a buffer
|
|
|
|
//
|
|
|
|
// NTSYSAPI
|
|
|
|
// NTSTATUS
|
|
|
|
// NTAPI
|
|
|
|
// NtQuerySystemInformation(
|
|
|
|
// IN UINT SystemInformationClass, // information type
|
|
|
|
// OUT PVOID SystemInformation, // pointer to buffer
|
|
|
|
// IN ULONG SystemInformationLength, // buffer size in bytes
|
|
|
|
// OUT PULONG ReturnLength OPTIONAL // pointer to a 32-bit
|
|
|
|
// // variable that receives
|
|
|
|
// // the number of bytes
|
2011-03-29 19:21:57 +00:00
|
|
|
// // written to the buffer
|
2009-02-10 18:37:48 +00:00
|
|
|
// );
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The constructor
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
MeasureCPU::MeasureCPU(MeterWindow* meterWindow, const WCHAR* name) : Measure(meterWindow, name),
|
2011-02-20 23:03:15 +00:00
|
|
|
m_Processor(),
|
|
|
|
m_OldTime()
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
m_MaxValue = 100.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The destructor
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
MeasureCPU::~MeasureCPU()
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-02-13 03:07:34 +00:00
|
|
|
/*
|
2012-06-01 13:06:36 +00:00
|
|
|
** Read the options specified in the ini file.
|
2010-02-13 03:07:34 +00:00
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void MeasureCPU::ReadOptions(ConfigParser& parser, const WCHAR* section)
|
2010-02-13 03:07:34 +00:00
|
|
|
{
|
2013-05-31 14:18:52 +00:00
|
|
|
Measure::ReadOptions(parser, section);
|
2010-02-13 03:07:34 +00:00
|
|
|
|
2012-04-09 17:53:07 +00:00
|
|
|
int processor = parser.ReadInt(section, L"Processor", 0);
|
2010-02-13 03:07:34 +00:00
|
|
|
|
2012-04-09 17:53:07 +00:00
|
|
|
if (processor < 0 || processor > c_NumOfProcessors)
|
2010-02-13 03:07:34 +00:00
|
|
|
{
|
2013-06-29 16:59:18 +00:00
|
|
|
LogWarningF(this, L"CPU: Processor=%i invalid in [%s]", processor, section);
|
2012-04-09 17:53:07 +00:00
|
|
|
processor = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (processor != m_Processor)
|
|
|
|
{
|
|
|
|
m_Processor = processor;
|
|
|
|
m_OldTime[0] = m_OldTime[1] = 0.0;
|
2010-02-13 03:07:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
/*
|
2010-11-11 20:24:59 +00:00
|
|
|
** Updates the current CPU utilization value.
|
2009-02-10 18:37:48 +00:00
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void MeasureCPU::UpdateValue()
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-02-20 23:03:15 +00:00
|
|
|
if (m_Processor == 0)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2010-11-11 20:24:59 +00:00
|
|
|
BOOL status;
|
|
|
|
FILETIME ftIdleTime, ftKernelTime, ftUserTime;
|
2010-02-13 03:07:34 +00:00
|
|
|
|
2010-11-11 20:24:59 +00:00
|
|
|
// get new CPU's idle/kernel/user time
|
2011-02-20 23:03:15 +00:00
|
|
|
status = GetSystemTimes(&ftIdleTime, &ftKernelTime, &ftUserTime);
|
2012-05-30 06:46:11 +00:00
|
|
|
if (status == 0) return;
|
2010-02-13 03:07:34 +00:00
|
|
|
|
2010-11-11 20:24:59 +00:00
|
|
|
CalcUsage(Ft2Double(ftIdleTime),
|
|
|
|
Ft2Double(ftKernelTime) + Ft2Double(ftUserTime));
|
|
|
|
}
|
2011-02-07 09:38:27 +00:00
|
|
|
else if (c_NtQuerySystemInformation)
|
2010-11-11 20:24:59 +00:00
|
|
|
{
|
|
|
|
LONG status;
|
2011-02-07 09:38:27 +00:00
|
|
|
ULONG bufSize = c_BufferSize;
|
2013-05-31 14:28:39 +00:00
|
|
|
BYTE* buf = (bufSize > 0) ? new BYTE[bufSize] : nullptr;
|
2010-02-13 03:07:34 +00:00
|
|
|
|
2010-11-11 20:24:59 +00:00
|
|
|
int loop = 0;
|
2010-02-13 03:07:34 +00:00
|
|
|
|
2010-11-11 20:24:59 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
ULONG size = 0;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2011-02-07 09:38:27 +00:00
|
|
|
status = c_NtQuerySystemInformation(SystemProcessorPerformanceInformation, buf, bufSize, &size);
|
2012-05-30 06:46:11 +00:00
|
|
|
if (status == STATUS_INFO_LENGTH_MISMATCH)
|
2010-11-11 20:24:59 +00:00
|
|
|
{
|
|
|
|
if (size == 0) // Returned required buffer size is always 0 on Windows 2000/XP.
|
2010-02-13 03:07:34 +00:00
|
|
|
{
|
2010-11-11 20:24:59 +00:00
|
|
|
if (bufSize == 0)
|
2010-02-13 03:07:34 +00:00
|
|
|
{
|
2011-02-07 09:38:27 +00:00
|
|
|
bufSize = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * c_NumOfProcessors;
|
2010-02-13 03:07:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-11-11 20:24:59 +00:00
|
|
|
bufSize += sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
|
2010-02-13 03:07:34 +00:00
|
|
|
}
|
|
|
|
}
|
2010-11-11 20:24:59 +00:00
|
|
|
else
|
2010-02-13 03:07:34 +00:00
|
|
|
{
|
2010-11-11 20:24:59 +00:00
|
|
|
if (size != bufSize)
|
|
|
|
{
|
|
|
|
bufSize = size;
|
|
|
|
}
|
|
|
|
else // ??
|
|
|
|
{
|
|
|
|
bufSize += sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
|
|
|
|
}
|
2010-02-13 03:07:34 +00:00
|
|
|
}
|
|
|
|
|
2011-02-07 09:38:27 +00:00
|
|
|
delete [] buf;
|
2010-11-11 20:24:59 +00:00
|
|
|
buf = new BYTE[bufSize];
|
|
|
|
}
|
2012-05-30 06:46:11 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-11-11 20:24:59 +00:00
|
|
|
++loop;
|
2011-11-08 10:32:57 +00:00
|
|
|
}
|
|
|
|
while (loop < 5);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-05-30 06:46:11 +00:00
|
|
|
if (status == STATUS_SUCCESS)
|
2011-02-07 09:38:27 +00:00
|
|
|
{
|
2012-05-30 06:46:11 +00:00
|
|
|
if (bufSize != c_BufferSize)
|
|
|
|
{
|
|
|
|
// Store the new buffer size
|
|
|
|
c_BufferSize = bufSize;
|
|
|
|
}
|
2011-02-07 09:38:27 +00:00
|
|
|
|
2012-11-12 02:10:40 +00:00
|
|
|
PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION systemPerfInfo = (PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)buf;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-05-30 06:46:11 +00:00
|
|
|
int processor = m_Processor - 1;
|
2010-11-11 20:24:59 +00:00
|
|
|
|
2012-05-30 06:46:11 +00:00
|
|
|
CalcUsage(Li2Double(systemPerfInfo[processor].IdleTime),
|
|
|
|
Li2Double(systemPerfInfo[processor].KernelTime) + Li2Double(systemPerfInfo[processor].UserTime));
|
|
|
|
}
|
2010-11-11 20:24:59 +00:00
|
|
|
|
|
|
|
delete [] buf;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-13 03:07:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Calculates the current CPU utilization value.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void MeasureCPU::CalcUsage(double idleTime, double systemTime)
|
2010-02-13 03:07:34 +00:00
|
|
|
{
|
2012-04-09 17:50:46 +00:00
|
|
|
// CurrentCpuUsage% = 100 - ((IdleTime / SystemTime) * 100)
|
|
|
|
double dbCpuUsage = 100.0 - ((idleTime - m_OldTime[0]) / (systemTime - m_OldTime[1])) * 100.0;
|
2010-02-13 03:07:34 +00:00
|
|
|
|
2012-04-09 17:50:46 +00:00
|
|
|
dbCpuUsage = min(dbCpuUsage, 100.0);
|
|
|
|
m_Value = max(dbCpuUsage, 0.0);
|
2010-02-13 03:07:34 +00:00
|
|
|
|
|
|
|
// store new CPU's idle and system time
|
|
|
|
m_OldTime[0] = idleTime;
|
|
|
|
m_OldTime[1] = systemTime;
|
|
|
|
}
|
2012-11-12 02:10:40 +00:00
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
void MeasureCPU::InitializeStatic()
|
2012-11-12 02:10:40 +00:00
|
|
|
{
|
|
|
|
c_NtQuerySystemInformation = (FPNTQSI)GetProcAddress(GetModuleHandle(L"ntdll"), "NtQuerySystemInformation");
|
|
|
|
|
|
|
|
SYSTEM_INFO systemInfo;
|
|
|
|
GetSystemInfo(&systemInfo);
|
|
|
|
c_NumOfProcessors = (int)systemInfo.dwNumberOfProcessors;
|
|
|
|
}
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
void MeasureCPU::FinalizeStatic()
|
2012-11-12 02:10:40 +00:00
|
|
|
{
|
|
|
|
}
|