1 | /* $Id: PerformanceWin.cpp 46328 2013-05-30 12:37:09Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | *
|
---|
5 | * VBox Windows-specific Performance Classes implementation.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2008-2012 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 <windows.h>
|
---|
29 | #include <winternl.h>
|
---|
30 | #include <psapi.h>
|
---|
31 | extern "C" {
|
---|
32 | #include <powrprof.h>
|
---|
33 | }
|
---|
34 |
|
---|
35 | #include <iprt/err.h>
|
---|
36 | #include <iprt/mp.h>
|
---|
37 | #include <iprt/mem.h>
|
---|
38 | #include <iprt/system.h>
|
---|
39 |
|
---|
40 | #include <map>
|
---|
41 |
|
---|
42 | #include "Logging.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 | private:
|
---|
66 | struct VMProcessStats
|
---|
67 | {
|
---|
68 | uint64_t cpuUser;
|
---|
69 | uint64_t cpuKernel;
|
---|
70 | uint64_t cpuTotal;
|
---|
71 | uint64_t ramUsed;
|
---|
72 | };
|
---|
73 |
|
---|
74 | typedef std::map<RTPROCESS, VMProcessStats> VMProcessMap;
|
---|
75 |
|
---|
76 | VMProcessMap mProcessStats;
|
---|
77 |
|
---|
78 | typedef BOOL (WINAPI *PFNGST)(
|
---|
79 | LPFILETIME lpIdleTime,
|
---|
80 | LPFILETIME lpKernelTime,
|
---|
81 | LPFILETIME lpUserTime);
|
---|
82 | typedef NTSTATUS (WINAPI *PFNNQSI)(
|
---|
83 | SYSTEM_INFORMATION_CLASS SystemInformationClass,
|
---|
84 | PVOID SystemInformation,
|
---|
85 | ULONG SystemInformationLength,
|
---|
86 | PULONG ReturnLength);
|
---|
87 |
|
---|
88 | PFNGST mpfnGetSystemTimes;
|
---|
89 | PFNNQSI mpfnNtQuerySystemInformation;
|
---|
90 | HMODULE mhNtDll;
|
---|
91 |
|
---|
92 | ULONG totalRAM;
|
---|
93 | };
|
---|
94 |
|
---|
95 | CollectorHAL *createHAL()
|
---|
96 | {
|
---|
97 | return new CollectorWin();
|
---|
98 | }
|
---|
99 |
|
---|
100 | CollectorWin::CollectorWin() : CollectorHAL(), mhNtDll(0)
|
---|
101 | {
|
---|
102 | mpfnGetSystemTimes = (PFNGST)GetProcAddress(
|
---|
103 | GetModuleHandle(TEXT("kernel32.dll")),
|
---|
104 | "GetSystemTimes");
|
---|
105 | if (!mpfnGetSystemTimes)
|
---|
106 | {
|
---|
107 | /* Fall back to deprecated NtQuerySystemInformation */
|
---|
108 | if (!(mhNtDll = LoadLibrary(TEXT("ntdll.dll"))))
|
---|
109 | {
|
---|
110 | LogRel(("Failed to load NTDLL.DLL with error 0x%x. GetSystemTimes() is"
|
---|
111 | " not available either. CPU and VM metrics will not be collected.\n",
|
---|
112 | GetLastError()));
|
---|
113 | mpfnNtQuerySystemInformation = 0;
|
---|
114 | }
|
---|
115 | else if (!(mpfnNtQuerySystemInformation = (PFNNQSI)GetProcAddress(mhNtDll,
|
---|
116 | "NtQuerySystemInformation")))
|
---|
117 | {
|
---|
118 | LogRel(("Neither GetSystemTimes() nor NtQuerySystemInformation() is"
|
---|
119 | " not available. CPU and VM metrics will not be collected.\n"));
|
---|
120 | mpfnNtQuerySystemInformation = 0;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | uint64_t cb;
|
---|
125 | int rc = RTSystemQueryTotalRam(&cb);
|
---|
126 | if (RT_FAILURE(rc))
|
---|
127 | totalRAM = 0;
|
---|
128 | else
|
---|
129 | totalRAM = (ULONG)(cb / 1024);
|
---|
130 | }
|
---|
131 |
|
---|
132 | CollectorWin::~CollectorWin()
|
---|
133 | {
|
---|
134 | if (mhNtDll)
|
---|
135 | FreeLibrary(mhNtDll);
|
---|
136 | }
|
---|
137 |
|
---|
138 | #define FILETTIME_TO_100NS(ft) (((uint64_t)ft.dwHighDateTime << 32) + ft.dwLowDateTime)
|
---|
139 |
|
---|
140 | int CollectorWin::preCollect(const CollectorHints& hints, uint64_t /* iTick */)
|
---|
141 | {
|
---|
142 | LogFlowThisFuncEnter();
|
---|
143 |
|
---|
144 | uint64_t user, kernel, idle, total;
|
---|
145 | int rc = getRawHostCpuLoad(&user, &kernel, &idle);
|
---|
146 | if (RT_FAILURE(rc))
|
---|
147 | return rc;
|
---|
148 | total = user + kernel + idle;
|
---|
149 |
|
---|
150 | DWORD dwError;
|
---|
151 | const CollectorHints::ProcessList& processes = hints.getProcessFlags();
|
---|
152 | CollectorHints::ProcessList::const_iterator it;
|
---|
153 |
|
---|
154 | mProcessStats.clear();
|
---|
155 |
|
---|
156 | for (it = processes.begin(); it != processes.end() && RT_SUCCESS(rc); it++)
|
---|
157 | {
|
---|
158 | RTPROCESS process = it->first;
|
---|
159 | HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
|
---|
160 | FALSE, process);
|
---|
161 |
|
---|
162 | if (!h)
|
---|
163 | {
|
---|
164 | dwError = GetLastError();
|
---|
165 | Log (("OpenProcess() -> 0x%x\n", dwError));
|
---|
166 | rc = RTErrConvertFromWin32(dwError);
|
---|
167 | break;
|
---|
168 | }
|
---|
169 |
|
---|
170 | VMProcessStats vmStats;
|
---|
171 | if ((it->second & COLLECT_CPU_LOAD) != 0)
|
---|
172 | {
|
---|
173 | FILETIME ftCreate, ftExit, ftKernel, ftUser;
|
---|
174 | if (!GetProcessTimes(h, &ftCreate, &ftExit, &ftKernel, &ftUser))
|
---|
175 | {
|
---|
176 | dwError = GetLastError();
|
---|
177 | Log (("GetProcessTimes() -> 0x%x\n", dwError));
|
---|
178 | rc = RTErrConvertFromWin32(dwError);
|
---|
179 | }
|
---|
180 | else
|
---|
181 | {
|
---|
182 | vmStats.cpuKernel = FILETTIME_TO_100NS(ftKernel);
|
---|
183 | vmStats.cpuUser = FILETTIME_TO_100NS(ftUser);
|
---|
184 | vmStats.cpuTotal = total;
|
---|
185 | }
|
---|
186 | }
|
---|
187 | if (RT_SUCCESS(rc) && (it->second & COLLECT_RAM_USAGE) != 0)
|
---|
188 | {
|
---|
189 | PROCESS_MEMORY_COUNTERS pmc;
|
---|
190 | if (!GetProcessMemoryInfo(h, &pmc, sizeof(pmc)))
|
---|
191 | {
|
---|
192 | dwError = GetLastError();
|
---|
193 | Log (("GetProcessMemoryInfo() -> 0x%x\n", dwError));
|
---|
194 | rc = RTErrConvertFromWin32(dwError);
|
---|
195 | }
|
---|
196 | else
|
---|
197 | vmStats.ramUsed = pmc.WorkingSetSize;
|
---|
198 | }
|
---|
199 | CloseHandle(h);
|
---|
200 | mProcessStats[process] = vmStats;
|
---|
201 | }
|
---|
202 |
|
---|
203 | LogFlowThisFuncLeave();
|
---|
204 |
|
---|
205 | return rc;
|
---|
206 | }
|
---|
207 |
|
---|
208 | int CollectorWin::getHostCpuLoad(ULONG *user, ULONG *kernel, ULONG *idle)
|
---|
209 | {
|
---|
210 | return VERR_NOT_IMPLEMENTED;
|
---|
211 | }
|
---|
212 |
|
---|
213 | typedef struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
|
---|
214 | {
|
---|
215 | LARGE_INTEGER IdleTime;
|
---|
216 | LARGE_INTEGER KernelTime;
|
---|
217 | LARGE_INTEGER UserTime;
|
---|
218 | LARGE_INTEGER Reserved1[2];
|
---|
219 | ULONG Reserved2;
|
---|
220 | } SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;
|
---|
221 |
|
---|
222 | int CollectorWin::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
|
---|
223 | {
|
---|
224 | LogFlowThisFuncEnter();
|
---|
225 |
|
---|
226 | FILETIME ftIdle, ftKernel, ftUser;
|
---|
227 |
|
---|
228 | if (mpfnGetSystemTimes)
|
---|
229 | {
|
---|
230 | if (!mpfnGetSystemTimes(&ftIdle, &ftKernel, &ftUser))
|
---|
231 | {
|
---|
232 | DWORD dwError = GetLastError();
|
---|
233 | Log (("GetSystemTimes() -> 0x%x\n", dwError));
|
---|
234 | return RTErrConvertFromWin32(dwError);
|
---|
235 | }
|
---|
236 |
|
---|
237 | *user = FILETTIME_TO_100NS(ftUser);
|
---|
238 | *idle = FILETTIME_TO_100NS(ftIdle);
|
---|
239 | *kernel = FILETTIME_TO_100NS(ftKernel) - *idle;
|
---|
240 | }
|
---|
241 | else
|
---|
242 | {
|
---|
243 | /* GetSystemTimes is not available, fall back to NtQuerySystemInformation */
|
---|
244 | if (!mpfnNtQuerySystemInformation)
|
---|
245 | return VERR_NOT_IMPLEMENTED;
|
---|
246 |
|
---|
247 | SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION sppi[MAXIMUM_PROCESSORS];
|
---|
248 | ULONG ulReturned;
|
---|
249 | NTSTATUS status = mpfnNtQuerySystemInformation(
|
---|
250 | SystemProcessorPerformanceInformation, &sppi, sizeof(sppi), &ulReturned);
|
---|
251 | if (NT_ERROR(status))
|
---|
252 | {
|
---|
253 | Log(("NtQuerySystemInformation() -> 0x%x\n", status));
|
---|
254 | return RTErrConvertFromNtStatus(status);
|
---|
255 | }
|
---|
256 | /* Sum up values across all processors */
|
---|
257 | *user = *kernel = *idle = 0;
|
---|
258 | for (unsigned i = 0; i < ulReturned / sizeof(sppi[0]); ++i)
|
---|
259 | {
|
---|
260 | *idle += sppi[i].IdleTime.QuadPart;
|
---|
261 | *kernel += sppi[i].KernelTime.QuadPart - sppi[i].IdleTime.QuadPart;
|
---|
262 | *user += sppi[i].UserTime.QuadPart;
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | LogFlowThisFunc(("user=%lu kernel=%lu idle=%lu\n", *user, *kernel, *idle));
|
---|
267 | LogFlowThisFuncLeave();
|
---|
268 |
|
---|
269 | return VINF_SUCCESS;
|
---|
270 | }
|
---|
271 |
|
---|
272 | typedef struct _PROCESSOR_POWER_INFORMATION {
|
---|
273 | ULONG Number;
|
---|
274 | ULONG MaxMhz;
|
---|
275 | ULONG CurrentMhz;
|
---|
276 | ULONG MhzLimit;
|
---|
277 | ULONG MaxIdleState;
|
---|
278 | ULONG CurrentIdleState;
|
---|
279 | } PROCESSOR_POWER_INFORMATION , *PPROCESSOR_POWER_INFORMATION;
|
---|
280 |
|
---|
281 | int CollectorWin::getHostCpuMHz(ULONG *mhz)
|
---|
282 | {
|
---|
283 | uint64_t uTotalMhz = 0;
|
---|
284 | RTCPUID nProcessors = RTMpGetCount();
|
---|
285 | PPROCESSOR_POWER_INFORMATION ppi = (PPROCESSOR_POWER_INFORMATION)RTMemAllocZ(nProcessors * sizeof(PROCESSOR_POWER_INFORMATION));
|
---|
286 |
|
---|
287 | if (!ppi)
|
---|
288 | return VERR_NO_MEMORY;
|
---|
289 |
|
---|
290 | LONG ns = CallNtPowerInformation(ProcessorInformation, NULL, 0, ppi,
|
---|
291 | nProcessors * sizeof(PROCESSOR_POWER_INFORMATION));
|
---|
292 | if (ns)
|
---|
293 | {
|
---|
294 | Log(("CallNtPowerInformation() -> %x\n", ns));
|
---|
295 | RTMemFree(ppi);
|
---|
296 | return VERR_INTERNAL_ERROR;
|
---|
297 | }
|
---|
298 |
|
---|
299 | /* Compute an average over all CPUs */
|
---|
300 | for (unsigned i = 0; i < nProcessors; i++)
|
---|
301 | uTotalMhz += ppi[i].CurrentMhz;
|
---|
302 | *mhz = (ULONG)(uTotalMhz / nProcessors);
|
---|
303 |
|
---|
304 | RTMemFree(ppi);
|
---|
305 | LogFlowThisFunc(("mhz=%u\n", *mhz));
|
---|
306 | LogFlowThisFuncLeave();
|
---|
307 |
|
---|
308 | return VINF_SUCCESS;
|
---|
309 | }
|
---|
310 |
|
---|
311 | int CollectorWin::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
|
---|
312 | {
|
---|
313 | AssertReturn(totalRAM, VERR_INTERNAL_ERROR);
|
---|
314 | uint64_t cb;
|
---|
315 | int rc = RTSystemQueryAvailableRam(&cb);
|
---|
316 | if (RT_SUCCESS(rc))
|
---|
317 | {
|
---|
318 | *total = totalRAM;
|
---|
319 | *available = (ULONG)(cb / 1024);
|
---|
320 | *used = *total - *available;
|
---|
321 | }
|
---|
322 | return rc;
|
---|
323 | }
|
---|
324 |
|
---|
325 | int CollectorWin::getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel)
|
---|
326 | {
|
---|
327 | return VERR_NOT_IMPLEMENTED;
|
---|
328 | }
|
---|
329 |
|
---|
330 | int CollectorWin::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
|
---|
331 | {
|
---|
332 | VMProcessMap::const_iterator it = mProcessStats.find(process);
|
---|
333 |
|
---|
334 | if (it == mProcessStats.end())
|
---|
335 | {
|
---|
336 | Log (("No stats pre-collected for process %x\n", process));
|
---|
337 | return VERR_INTERNAL_ERROR;
|
---|
338 | }
|
---|
339 | *user = it->second.cpuUser;
|
---|
340 | *kernel = it->second.cpuKernel;
|
---|
341 | *total = it->second.cpuTotal;
|
---|
342 | return VINF_SUCCESS;
|
---|
343 | }
|
---|
344 |
|
---|
345 | int CollectorWin::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
|
---|
346 | {
|
---|
347 | VMProcessMap::const_iterator it = mProcessStats.find(process);
|
---|
348 |
|
---|
349 | if (it == mProcessStats.end())
|
---|
350 | {
|
---|
351 | Log (("No stats pre-collected for process %x\n", process));
|
---|
352 | return VERR_INTERNAL_ERROR;
|
---|
353 | }
|
---|
354 | *used = (ULONG)(it->second.ramUsed / 1024);
|
---|
355 | return VINF_SUCCESS;
|
---|
356 | }
|
---|
357 |
|
---|
358 | }
|
---|