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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
/* $Id$ */
/** @file
*
* VBox Windows-specific Performance Classes implementation.
*/
/*
* Copyright (C) 2008-2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#else /* !_WIN32_WINNT */
#if (_WIN32_WINNT < 0x0500)
#error Win XP or later required!
#endif /* _WIN32_WINNT < 0x0500 */
#endif /* !_WIN32_WINNT */
#include <windows.h>
#include <winternl.h>
#include <psapi.h>
extern "C" {
#include <powrprof.h>
}
#include <iprt/err.h>
#include <iprt/mp.h>
#include <iprt/mem.h>
#include <iprt/system.h>
#include <map>
#include "Logging.h"
#include "Performance.h"
#ifndef NT_ERROR
#define NT_ERROR(Status) ((ULONG)(Status) >> 30 == 3)
#endif
namespace pm {
class CollectorWin : public CollectorHAL
{
public:
CollectorWin();
virtual ~CollectorWin();
virtual int preCollect(const CollectorHints& hints, uint64_t /* iTick */);
virtual int getHostCpuLoad(ULONG *user, ULONG *kernel, ULONG *idle);
virtual int getHostCpuMHz(ULONG *mhz);
virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
virtual int getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel);
virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);
virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
private:
struct VMProcessStats
{
uint64_t cpuUser;
uint64_t cpuKernel;
uint64_t cpuTotal;
uint64_t ramUsed;
};
typedef std::map<RTPROCESS, VMProcessStats> VMProcessMap;
VMProcessMap mProcessStats;
typedef BOOL (WINAPI *PFNGST)(
LPFILETIME lpIdleTime,
LPFILETIME lpKernelTime,
LPFILETIME lpUserTime);
typedef NTSTATUS (WINAPI *PFNNQSI)(
SYSTEM_INFORMATION_CLASS SystemInformationClass,
PVOID SystemInformation,
ULONG SystemInformationLength,
PULONG ReturnLength);
PFNGST mpfnGetSystemTimes;
PFNNQSI mpfnNtQuerySystemInformation;
HMODULE mhNtDll;
};
CollectorHAL *createHAL()
{
return new CollectorWin();
}
CollectorWin::CollectorWin() : CollectorHAL(), mhNtDll(0)
{
mpfnGetSystemTimes = (PFNGST)GetProcAddress(
GetModuleHandle(TEXT("kernel32.dll")),
"GetSystemTimes");
if (!mpfnGetSystemTimes)
{
/* Fall back to deprecated NtQuerySystemInformation */
if (!(mhNtDll = LoadLibrary(TEXT("ntdll.dll"))))
{
LogRel(("Failed to load NTDLL.DLL with error 0x%x. GetSystemTimes() is"
" not available either. CPU and VM metrics will not be collected.\n",
GetLastError()));
mpfnNtQuerySystemInformation = 0;
}
else if (!(mpfnNtQuerySystemInformation = (PFNNQSI)GetProcAddress(mhNtDll,
"NtQuerySystemInformation")))
{
LogRel(("Neither GetSystemTimes() nor NtQuerySystemInformation() is"
" not available. CPU and VM metrics will not be collected.\n"));
mpfnNtQuerySystemInformation = 0;
}
}
}
CollectorWin::~CollectorWin()
{
if (mhNtDll)
FreeLibrary(mhNtDll);
}
#define FILETTIME_TO_100NS(ft) (((uint64_t)ft.dwHighDateTime << 32) + ft.dwLowDateTime)
int CollectorWin::preCollect(const CollectorHints& hints, uint64_t /* iTick */)
{
LogFlowThisFuncEnter();
uint64_t user, kernel, idle, total;
int rc = getRawHostCpuLoad(&user, &kernel, &idle);
if (RT_FAILURE(rc))
return rc;
total = user + kernel + idle;
DWORD dwError;
const CollectorHints::ProcessList& processes = hints.getProcessFlags();
CollectorHints::ProcessList::const_iterator it;
mProcessStats.clear();
for (it = processes.begin(); it != processes.end() && RT_SUCCESS(rc); it++)
{
RTPROCESS process = it->first;
HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, process);
if (!h)
{
dwError = GetLastError();
Log (("OpenProcess() -> 0x%x\n", dwError));
rc = RTErrConvertFromWin32(dwError);
break;
}
VMProcessStats vmStats;
if ((it->second & COLLECT_CPU_LOAD) != 0)
{
FILETIME ftCreate, ftExit, ftKernel, ftUser;
if (!GetProcessTimes(h, &ftCreate, &ftExit, &ftKernel, &ftUser))
{
dwError = GetLastError();
Log (("GetProcessTimes() -> 0x%x\n", dwError));
rc = RTErrConvertFromWin32(dwError);
}
else
{
vmStats.cpuKernel = FILETTIME_TO_100NS(ftKernel);
vmStats.cpuUser = FILETTIME_TO_100NS(ftUser);
vmStats.cpuTotal = total;
}
}
if (RT_SUCCESS(rc) && (it->second & COLLECT_RAM_USAGE) != 0)
{
PROCESS_MEMORY_COUNTERS pmc;
if (!GetProcessMemoryInfo(h, &pmc, sizeof(pmc)))
{
dwError = GetLastError();
Log (("GetProcessMemoryInfo() -> 0x%x\n", dwError));
rc = RTErrConvertFromWin32(dwError);
}
else
vmStats.ramUsed = pmc.WorkingSetSize;
}
CloseHandle(h);
mProcessStats[process] = vmStats;
}
LogFlowThisFuncLeave();
return rc;
}
int CollectorWin::getHostCpuLoad(ULONG *user, ULONG *kernel, ULONG *idle)
{
return VERR_NOT_IMPLEMENTED;
}
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;
int CollectorWin::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
{
LogFlowThisFuncEnter();
FILETIME ftIdle, ftKernel, ftUser;
if (mpfnGetSystemTimes)
{
if (!mpfnGetSystemTimes(&ftIdle, &ftKernel, &ftUser))
{
DWORD dwError = GetLastError();
Log (("GetSystemTimes() -> 0x%x\n", dwError));
return RTErrConvertFromWin32(dwError);
}
*user = FILETTIME_TO_100NS(ftUser);
*idle = FILETTIME_TO_100NS(ftIdle);
*kernel = FILETTIME_TO_100NS(ftKernel) - *idle;
}
else
{
/* GetSystemTimes is not available, fall back to NtQuerySystemInformation */
if (!mpfnNtQuerySystemInformation)
return VERR_NOT_IMPLEMENTED;
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION sppi[MAXIMUM_PROCESSORS];
ULONG ulReturned;
NTSTATUS status = mpfnNtQuerySystemInformation(
SystemProcessorPerformanceInformation, &sppi, sizeof(sppi), &ulReturned);
if (NT_ERROR(status))
{
Log(("NtQuerySystemInformation() -> 0x%x\n", status));
return RTErrConvertFromNtStatus(status);
}
/* Sum up values across all processors */
*user = *kernel = *idle = 0;
for (unsigned i = 0; i < ulReturned / sizeof(sppi[0]); ++i)
{
*idle += sppi[i].IdleTime.QuadPart;
*kernel += sppi[i].KernelTime.QuadPart - sppi[i].IdleTime.QuadPart;
*user += sppi[i].UserTime.QuadPart;
}
}
LogFlowThisFunc(("user=%lu kernel=%lu idle=%lu\n", *user, *kernel, *idle));
LogFlowThisFuncLeave();
return VINF_SUCCESS;
}
typedef struct _PROCESSOR_POWER_INFORMATION {
ULONG Number;
ULONG MaxMhz;
ULONG CurrentMhz;
ULONG MhzLimit;
ULONG MaxIdleState;
ULONG CurrentIdleState;
} PROCESSOR_POWER_INFORMATION , *PPROCESSOR_POWER_INFORMATION;
int CollectorWin::getHostCpuMHz(ULONG *mhz)
{
uint64_t uTotalMhz = 0;
RTCPUID nProcessors = RTMpGetCount();
PPROCESSOR_POWER_INFORMATION ppi = (PPROCESSOR_POWER_INFORMATION)RTMemAllocZ(nProcessors * sizeof(PROCESSOR_POWER_INFORMATION));
if (!ppi)
return VERR_NO_MEMORY;
LONG ns = CallNtPowerInformation(ProcessorInformation, NULL, 0, ppi,
nProcessors * sizeof(PROCESSOR_POWER_INFORMATION));
if (ns)
{
Log(("CallNtPowerInformation() -> %x\n", ns));
RTMemFree(ppi);
return VERR_INTERNAL_ERROR;
}
/* Compute an average over all CPUs */
for (unsigned i = 0; i < nProcessors; i++)
uTotalMhz += ppi[i].CurrentMhz;
*mhz = (ULONG)(uTotalMhz / nProcessors);
RTMemFree(ppi);
LogFlowThisFunc(("mhz=%u\n", *mhz));
LogFlowThisFuncLeave();
return VINF_SUCCESS;
}
int CollectorWin::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
{
uint64_t cb;
int rc = RTSystemQueryTotalRam(&cb);
if (RT_SUCCESS(rc))
{
*total = (ULONG)(cb / 1024);
rc = RTSystemQueryAvailableRam(&cb);
if (RT_SUCCESS(rc))
{
*available = (ULONG)(cb / 1024);
*used = *total - *available;
}
}
return rc;
}
int CollectorWin::getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel)
{
return VERR_NOT_IMPLEMENTED;
}
int CollectorWin::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
{
VMProcessMap::const_iterator it = mProcessStats.find(process);
if (it == mProcessStats.end())
{
Log (("No stats pre-collected for process %x\n", process));
return VERR_INTERNAL_ERROR;
}
*user = it->second.cpuUser;
*kernel = it->second.cpuKernel;
*total = it->second.cpuTotal;
return VINF_SUCCESS;
}
int CollectorWin::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
{
VMProcessMap::const_iterator it = mProcessStats.find(process);
if (it == mProcessStats.end())
{
Log (("No stats pre-collected for process %x\n", process));
return VERR_INTERNAL_ERROR;
}
*used = (ULONG)(it->second.ramUsed / 1024);
return VINF_SUCCESS;
}
}
|