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