1 | /* $Id: PerformanceWin.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Windows-specific Performance Classes implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #define LOG_GROUP LOG_GROUP_MAIN_PERFORMANCECOLLECTOR
|
---|
19 | #ifndef _WIN32_WINNT
|
---|
20 | # define _WIN32_WINNT 0x0500
|
---|
21 | #else /* !_WIN32_WINNT */
|
---|
22 | # if (_WIN32_WINNT < 0x0500)
|
---|
23 | # error Win XP or later required!
|
---|
24 | # endif /* _WIN32_WINNT < 0x0500 */
|
---|
25 | #endif /* !_WIN32_WINNT */
|
---|
26 |
|
---|
27 | #include <iprt/win/windows.h>
|
---|
28 | #include <winternl.h>
|
---|
29 | #include <psapi.h>
|
---|
30 | extern "C" {
|
---|
31 | #include <powrprof.h>
|
---|
32 | }
|
---|
33 |
|
---|
34 | #include <iprt/errcore.h>
|
---|
35 | #include <iprt/ldr.h>
|
---|
36 | #include <iprt/mp.h>
|
---|
37 | #include <iprt/mem.h>
|
---|
38 | #include <iprt/system.h>
|
---|
39 |
|
---|
40 | #include <map>
|
---|
41 |
|
---|
42 | #include "LoggingNew.h"
|
---|
43 | #include "Performance.h"
|
---|
44 |
|
---|
45 | #ifndef NT_ERROR
|
---|
46 | #define NT_ERROR(Status) ((ULONG)(Status) >> 30 == 3)
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | namespace pm {
|
---|
50 |
|
---|
51 | class CollectorWin : public CollectorHAL
|
---|
52 | {
|
---|
53 | public:
|
---|
54 | CollectorWin();
|
---|
55 | virtual ~CollectorWin();
|
---|
56 | virtual int preCollect(const CollectorHints& hints, uint64_t /* iTick */);
|
---|
57 | virtual int getHostCpuLoad(ULONG *user, ULONG *kernel, ULONG *idle);
|
---|
58 | virtual int getHostCpuMHz(ULONG *mhz);
|
---|
59 | virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
|
---|
60 | virtual int getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel);
|
---|
61 | virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);
|
---|
62 |
|
---|
63 | virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
|
---|
64 | virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
|
---|
65 |
|
---|
66 | private:
|
---|
67 | struct VMProcessStats
|
---|
68 | {
|
---|
69 | uint64_t cpuUser;
|
---|
70 | uint64_t cpuKernel;
|
---|
71 | uint64_t cpuTotal;
|
---|
72 | uint64_t ramUsed;
|
---|
73 | };
|
---|
74 |
|
---|
75 | typedef std::map<RTPROCESS, VMProcessStats> VMProcessMap;
|
---|
76 |
|
---|
77 | VMProcessMap mProcessStats;
|
---|
78 |
|
---|
79 | typedef BOOL (WINAPI *PFNGST)(LPFILETIME lpIdleTime,
|
---|
80 | LPFILETIME lpKernelTime,
|
---|
81 | LPFILETIME lpUserTime);
|
---|
82 | typedef NTSTATUS (WINAPI *PFNNQSI)(SYSTEM_INFORMATION_CLASS SystemInformationClass,
|
---|
83 | PVOID SystemInformation,
|
---|
84 | ULONG SystemInformationLength,
|
---|
85 | PULONG ReturnLength);
|
---|
86 |
|
---|
87 | PFNGST mpfnGetSystemTimes;
|
---|
88 | PFNNQSI mpfnNtQuerySystemInformation;
|
---|
89 |
|
---|
90 | ULONG totalRAM;
|
---|
91 | };
|
---|
92 |
|
---|
93 | CollectorHAL *createHAL()
|
---|
94 | {
|
---|
95 | return new CollectorWin();
|
---|
96 | }
|
---|
97 |
|
---|
98 | CollectorWin::CollectorWin() : CollectorHAL(), mpfnNtQuerySystemInformation(NULL)
|
---|
99 | {
|
---|
100 | /* Note! Both kernel32.dll and ntdll.dll can be assumed to always be present. */
|
---|
101 | mpfnGetSystemTimes = (PFNGST)RTLdrGetSystemSymbol("kernel32.dll", "GetSystemTimes");
|
---|
102 | if (!mpfnGetSystemTimes)
|
---|
103 | {
|
---|
104 | /* Fall back to deprecated NtQuerySystemInformation */
|
---|
105 | mpfnNtQuerySystemInformation = (PFNNQSI)RTLdrGetSystemSymbol("ntdll.dll", "NtQuerySystemInformation");
|
---|
106 | if (!mpfnNtQuerySystemInformation)
|
---|
107 | LogRel(("Warning! Neither GetSystemTimes() nor NtQuerySystemInformation() is not available.\n"
|
---|
108 | " CPU and VM metrics will not be collected! (lasterr %u)\n", GetLastError()));
|
---|
109 | }
|
---|
110 |
|
---|
111 | uint64_t cb;
|
---|
112 | int rc = RTSystemQueryTotalRam(&cb);
|
---|
113 | if (RT_FAILURE(rc))
|
---|
114 | totalRAM = 0;
|
---|
115 | else
|
---|
116 | totalRAM = (ULONG)(cb / 1024);
|
---|
117 | }
|
---|
118 |
|
---|
119 | CollectorWin::~CollectorWin()
|
---|
120 | {
|
---|
121 | }
|
---|
122 |
|
---|
123 | #define FILETTIME_TO_100NS(ft) (((uint64_t)ft.dwHighDateTime << 32) + ft.dwLowDateTime)
|
---|
124 |
|
---|
125 | int CollectorWin::preCollect(const CollectorHints& hints, uint64_t /* iTick */)
|
---|
126 | {
|
---|
127 | LogFlowThisFuncEnter();
|
---|
128 |
|
---|
129 | uint64_t user, kernel, idle, total;
|
---|
130 | int rc = getRawHostCpuLoad(&user, &kernel, &idle);
|
---|
131 | if (RT_FAILURE(rc))
|
---|
132 | return rc;
|
---|
133 | total = user + kernel + idle;
|
---|
134 |
|
---|
135 | DWORD dwError;
|
---|
136 | const CollectorHints::ProcessList& processes = hints.getProcessFlags();
|
---|
137 | CollectorHints::ProcessList::const_iterator it;
|
---|
138 |
|
---|
139 | mProcessStats.clear();
|
---|
140 |
|
---|
141 | for (it = processes.begin(); it != processes.end() && RT_SUCCESS(rc); ++it)
|
---|
142 | {
|
---|
143 | RTPROCESS process = it->first;
|
---|
144 | HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
|
---|
145 | FALSE, process);
|
---|
146 |
|
---|
147 | if (!h)
|
---|
148 | {
|
---|
149 | dwError = GetLastError();
|
---|
150 | Log (("OpenProcess() -> 0x%x\n", dwError));
|
---|
151 | rc = RTErrConvertFromWin32(dwError);
|
---|
152 | break;
|
---|
153 | }
|
---|
154 |
|
---|
155 | VMProcessStats vmStats;
|
---|
156 | RT_ZERO(vmStats);
|
---|
157 | if ((it->second & COLLECT_CPU_LOAD) != 0)
|
---|
158 | {
|
---|
159 | FILETIME ftCreate, ftExit, ftKernel, ftUser;
|
---|
160 | if (!GetProcessTimes(h, &ftCreate, &ftExit, &ftKernel, &ftUser))
|
---|
161 | {
|
---|
162 | dwError = GetLastError();
|
---|
163 | Log (("GetProcessTimes() -> 0x%x\n", dwError));
|
---|
164 | rc = RTErrConvertFromWin32(dwError);
|
---|
165 | }
|
---|
166 | else
|
---|
167 | {
|
---|
168 | vmStats.cpuKernel = FILETTIME_TO_100NS(ftKernel);
|
---|
169 | vmStats.cpuUser = FILETTIME_TO_100NS(ftUser);
|
---|
170 | vmStats.cpuTotal = total;
|
---|
171 | }
|
---|
172 | }
|
---|
173 | if (RT_SUCCESS(rc) && (it->second & COLLECT_RAM_USAGE) != 0)
|
---|
174 | {
|
---|
175 | PROCESS_MEMORY_COUNTERS pmc;
|
---|
176 | if (!GetProcessMemoryInfo(h, &pmc, sizeof(pmc)))
|
---|
177 | {
|
---|
178 | dwError = GetLastError();
|
---|
179 | Log (("GetProcessMemoryInfo() -> 0x%x\n", dwError));
|
---|
180 | rc = RTErrConvertFromWin32(dwError);
|
---|
181 | }
|
---|
182 | else
|
---|
183 | vmStats.ramUsed = pmc.WorkingSetSize;
|
---|
184 | }
|
---|
185 | CloseHandle(h);
|
---|
186 | mProcessStats[process] = vmStats;
|
---|
187 | }
|
---|
188 |
|
---|
189 | LogFlowThisFuncLeave();
|
---|
190 |
|
---|
191 | return rc;
|
---|
192 | }
|
---|
193 |
|
---|
194 | int CollectorWin::getHostCpuLoad(ULONG *user, ULONG *kernel, ULONG *idle)
|
---|
195 | {
|
---|
196 | RT_NOREF(user, kernel, idle);
|
---|
197 | return VERR_NOT_IMPLEMENTED;
|
---|
198 | }
|
---|
199 |
|
---|
200 | typedef struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
|
---|
201 | {
|
---|
202 | LARGE_INTEGER IdleTime;
|
---|
203 | LARGE_INTEGER KernelTime;
|
---|
204 | LARGE_INTEGER UserTime;
|
---|
205 | LARGE_INTEGER Reserved1[2];
|
---|
206 | ULONG Reserved2;
|
---|
207 | } SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;
|
---|
208 |
|
---|
209 | int CollectorWin::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
|
---|
210 | {
|
---|
211 | LogFlowThisFuncEnter();
|
---|
212 |
|
---|
213 | FILETIME ftIdle, ftKernel, ftUser;
|
---|
214 |
|
---|
215 | if (mpfnGetSystemTimes)
|
---|
216 | {
|
---|
217 | if (!mpfnGetSystemTimes(&ftIdle, &ftKernel, &ftUser))
|
---|
218 | {
|
---|
219 | DWORD dwError = GetLastError();
|
---|
220 | Log (("GetSystemTimes() -> 0x%x\n", dwError));
|
---|
221 | return RTErrConvertFromWin32(dwError);
|
---|
222 | }
|
---|
223 |
|
---|
224 | *user = FILETTIME_TO_100NS(ftUser);
|
---|
225 | *idle = FILETTIME_TO_100NS(ftIdle);
|
---|
226 | *kernel = FILETTIME_TO_100NS(ftKernel) - *idle;
|
---|
227 | }
|
---|
228 | else
|
---|
229 | {
|
---|
230 | /* GetSystemTimes is not available, fall back to NtQuerySystemInformation */
|
---|
231 | if (!mpfnNtQuerySystemInformation)
|
---|
232 | return VERR_NOT_IMPLEMENTED;
|
---|
233 |
|
---|
234 | SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION sppi[MAXIMUM_PROCESSORS];
|
---|
235 | ULONG ulReturned;
|
---|
236 | NTSTATUS status = mpfnNtQuerySystemInformation(
|
---|
237 | SystemProcessorPerformanceInformation, &sppi, sizeof(sppi), &ulReturned);
|
---|
238 | if (NT_ERROR(status))
|
---|
239 | {
|
---|
240 | Log(("NtQuerySystemInformation() -> 0x%x\n", status));
|
---|
241 | return RTErrConvertFromNtStatus(status);
|
---|
242 | }
|
---|
243 | /* Sum up values across all processors */
|
---|
244 | *user = *kernel = *idle = 0;
|
---|
245 | for (unsigned i = 0; i < ulReturned / sizeof(sppi[0]); ++i)
|
---|
246 | {
|
---|
247 | *idle += sppi[i].IdleTime.QuadPart;
|
---|
248 | *kernel += sppi[i].KernelTime.QuadPart - sppi[i].IdleTime.QuadPart;
|
---|
249 | *user += sppi[i].UserTime.QuadPart;
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | LogFlowThisFunc(("user=%lu kernel=%lu idle=%lu\n", *user, *kernel, *idle));
|
---|
254 | LogFlowThisFuncLeave();
|
---|
255 |
|
---|
256 | return VINF_SUCCESS;
|
---|
257 | }
|
---|
258 |
|
---|
259 | typedef struct _PROCESSOR_POWER_INFORMATION {
|
---|
260 | ULONG Number;
|
---|
261 | ULONG MaxMhz;
|
---|
262 | ULONG CurrentMhz;
|
---|
263 | ULONG MhzLimit;
|
---|
264 | ULONG MaxIdleState;
|
---|
265 | ULONG CurrentIdleState;
|
---|
266 | } PROCESSOR_POWER_INFORMATION , *PPROCESSOR_POWER_INFORMATION;
|
---|
267 |
|
---|
268 | int CollectorWin::getHostCpuMHz(ULONG *mhz)
|
---|
269 | {
|
---|
270 | uint64_t uTotalMhz = 0;
|
---|
271 | RTCPUID nProcessors = RTMpGetCount();
|
---|
272 | PPROCESSOR_POWER_INFORMATION ppi = (PPROCESSOR_POWER_INFORMATION)
|
---|
273 | RTMemAllocZ(nProcessors * sizeof(PROCESSOR_POWER_INFORMATION));
|
---|
274 |
|
---|
275 | if (!ppi)
|
---|
276 | return VERR_NO_MEMORY;
|
---|
277 |
|
---|
278 | LONG ns = CallNtPowerInformation(ProcessorInformation, NULL, 0, ppi,
|
---|
279 | nProcessors * sizeof(PROCESSOR_POWER_INFORMATION));
|
---|
280 | if (ns)
|
---|
281 | {
|
---|
282 | Log(("CallNtPowerInformation() -> %x\n", ns));
|
---|
283 | RTMemFree(ppi);
|
---|
284 | return VERR_INTERNAL_ERROR;
|
---|
285 | }
|
---|
286 |
|
---|
287 | /* Compute an average over all CPUs */
|
---|
288 | for (unsigned i = 0; i < nProcessors; i++)
|
---|
289 | uTotalMhz += ppi[i].CurrentMhz;
|
---|
290 | *mhz = (ULONG)(uTotalMhz / nProcessors);
|
---|
291 |
|
---|
292 | RTMemFree(ppi);
|
---|
293 | LogFlowThisFunc(("mhz=%u\n", *mhz));
|
---|
294 | LogFlowThisFuncLeave();
|
---|
295 |
|
---|
296 | return VINF_SUCCESS;
|
---|
297 | }
|
---|
298 |
|
---|
299 | int CollectorWin::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
|
---|
300 | {
|
---|
301 | AssertReturn(totalRAM, VERR_INTERNAL_ERROR);
|
---|
302 | uint64_t cb;
|
---|
303 | int rc = RTSystemQueryAvailableRam(&cb);
|
---|
304 | if (RT_SUCCESS(rc))
|
---|
305 | {
|
---|
306 | *total = totalRAM;
|
---|
307 | *available = (ULONG)(cb / 1024);
|
---|
308 | *used = *total - *available;
|
---|
309 | }
|
---|
310 | return rc;
|
---|
311 | }
|
---|
312 |
|
---|
313 | int CollectorWin::getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel)
|
---|
314 | {
|
---|
315 | RT_NOREF(process, user, kernel);
|
---|
316 | return VERR_NOT_IMPLEMENTED;
|
---|
317 | }
|
---|
318 |
|
---|
319 | int CollectorWin::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
|
---|
320 | {
|
---|
321 | VMProcessMap::const_iterator it = mProcessStats.find(process);
|
---|
322 |
|
---|
323 | if (it == mProcessStats.end())
|
---|
324 | {
|
---|
325 | Log (("No stats pre-collected for process %x\n", process));
|
---|
326 | return VERR_INTERNAL_ERROR;
|
---|
327 | }
|
---|
328 | *user = it->second.cpuUser;
|
---|
329 | *kernel = it->second.cpuKernel;
|
---|
330 | *total = it->second.cpuTotal;
|
---|
331 | return VINF_SUCCESS;
|
---|
332 | }
|
---|
333 |
|
---|
334 | int CollectorWin::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
|
---|
335 | {
|
---|
336 | VMProcessMap::const_iterator it = mProcessStats.find(process);
|
---|
337 |
|
---|
338 | if (it == mProcessStats.end())
|
---|
339 | {
|
---|
340 | Log (("No stats pre-collected for process %x\n", process));
|
---|
341 | return VERR_INTERNAL_ERROR;
|
---|
342 | }
|
---|
343 | *used = (ULONG)(it->second.ramUsed / 1024);
|
---|
344 | return VINF_SUCCESS;
|
---|
345 | }
|
---|
346 |
|
---|
347 | }
|
---|