forked from rainmeter/rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeasureCPU.cpp
207 lines (176 loc) · 5.21 KB
/
MeasureCPU.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* Copyright (C) 2001 Rainmeter Project Developers
*
* This Source Code Form is subject to the terms of the GNU General Public
* License; either version 2 of the License, or (at your option) any later
* version. If a copy of the GPL was not distributed with this file, You can
* obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */
#include "StdAfx.h"
#include "MeasureCPU.h"
#include "Rainmeter.h"
#include "System.h"
#define STATUS_SUCCESS 0
#define STATUS_INFO_LENGTH_MISMATCH 0xC0000004
#define SystemProcessorPerformanceInformation 8
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;
#define Li2Double(x) ((double)((x).QuadPart))
#define Ft2Double(x) ((double)((x).dwHighDateTime) * 4.294967296E9 + (double)((x).dwLowDateTime))
FPNTQSI MeasureCPU::c_NtQuerySystemInformation = nullptr;
int MeasureCPU::c_NumOfProcessors = 0;
ULONG MeasureCPU::c_BufferSize = 0;
// 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
// // written to the buffer
// );
MeasureCPU::MeasureCPU(Skin* skin, const WCHAR* name) : Measure(skin, name),
m_Processor(),
m_OldTime()
{
m_MaxValue = 100.0;
}
MeasureCPU::~MeasureCPU()
{
}
/*
** Read the options specified in the ini file.
**
*/
void MeasureCPU::ReadOptions(ConfigParser& parser, const WCHAR* section)
{
Measure::ReadOptions(parser, section);
int processor = parser.ReadInt(section, L"Processor", 0);
if (processor < 0 || processor > c_NumOfProcessors)
{
LogWarningF(this, L"CPU: Processor=%i is not valid", processor);
processor = 0;
}
if (processor != m_Processor)
{
m_Processor = processor;
m_OldTime[0] = m_OldTime[1] = 0.0;
}
}
/*
** Updates the current CPU utilization value.
**
*/
void MeasureCPU::UpdateValue()
{
if (m_Processor == 0)
{
BOOL status;
FILETIME ftIdleTime, ftKernelTime, ftUserTime;
// get new CPU's idle/kernel/user time
status = GetSystemTimes(&ftIdleTime, &ftKernelTime, &ftUserTime);
if (status == 0) return;
CalcUsage(Ft2Double(ftIdleTime), Ft2Double(ftKernelTime) + Ft2Double(ftUserTime));
}
else if (c_NtQuerySystemInformation)
{
LONG status = 0L;
ULONG bufSize = c_BufferSize;
BYTE* buf = (bufSize > 0) ? new BYTE[bufSize] : nullptr;
int loop = 0;
do
{
ULONG size = 0UL;
status = c_NtQuerySystemInformation(SystemProcessorPerformanceInformation, buf, bufSize, &size);
if (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) * c_NumOfProcessors;
}
else
{
bufSize += sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
}
}
else
{
if (size != bufSize)
{
bufSize = size;
}
else // ??
{
bufSize += sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
}
}
delete [] buf;
buf = new BYTE[bufSize];
}
else
{
break;
}
++loop;
}
while (loop < 5);
if (status == STATUS_SUCCESS)
{
if (bufSize != c_BufferSize)
{
// Store the new buffer size
c_BufferSize = bufSize;
}
PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION systemPerfInfo = (PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)buf;
if (systemPerfInfo)
{
int processor = m_Processor - 1;
CalcUsage(Li2Double(systemPerfInfo[processor].IdleTime),
Li2Double(systemPerfInfo[processor].KernelTime) + Li2Double(systemPerfInfo[processor].UserTime));
}
}
delete [] buf;
buf = nullptr;
}
}
/*
** Calculates the current CPU utilization value.
**
*/
void MeasureCPU::CalcUsage(double idleTime, double systemTime)
{
// CurrentCpuUsage% = 100 - ((IdleTime / SystemTime) * 100)
double dbCpuUsage = 100.0 - ((idleTime - m_OldTime[0]) / (systemTime - m_OldTime[1])) * 100.0;
dbCpuUsage = min(dbCpuUsage, 100.0);
m_Value = max(dbCpuUsage, 0.0);
// store new CPU's idle and system time
m_OldTime[0] = idleTime;
m_OldTime[1] = systemTime;
}
void MeasureCPU::InitializeStatic()
{
HMODULE hmod = GetModuleHandle(L"ntdll");
if (hmod)
{
c_NtQuerySystemInformation = (FPNTQSI)GetProcAddress(hmod, "NtQuerySystemInformation");
}
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
c_NumOfProcessors = (int)systemInfo.dwNumberOfProcessors;
}
void MeasureCPU::FinalizeStatic()
{
}