VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/win/PerformanceWin.cpp@ 58414

Last change on this file since 58414 was 58103, checked in by vboxsync, 9 years ago

Main: warning

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.2 KB
Line 
1/* $Id: PerformanceWin.cpp 58103 2015-10-07 16:20:21Z 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>
31extern "C" {
32#include <powrprof.h>
33}
34
35#include <iprt/err.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
50namespace pm {
51
52class CollectorWin : public CollectorHAL
53{
54public:
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
67private:
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
94CollectorHAL *createHAL()
95{
96 return new CollectorWin();
97}
98
99CollectorWin::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
120CollectorWin::~CollectorWin()
121{
122}
123
124#define FILETTIME_TO_100NS(ft) (((uint64_t)ft.dwHighDateTime << 32) + ft.dwLowDateTime)
125
126int 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
195int CollectorWin::getHostCpuLoad(ULONG *user, ULONG *kernel, ULONG *idle)
196{
197 return VERR_NOT_IMPLEMENTED;
198}
199
200typedef 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
209int 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
259typedef 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
268int 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
299int 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
313int CollectorWin::getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel)
314{
315 return VERR_NOT_IMPLEMENTED;
316}
317
318int CollectorWin::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
319{
320 VMProcessMap::const_iterator it = mProcessStats.find(process);
321
322 if (it == mProcessStats.end())
323 {
324 Log (("No stats pre-collected for process %x\n", process));
325 return VERR_INTERNAL_ERROR;
326 }
327 *user = it->second.cpuUser;
328 *kernel = it->second.cpuKernel;
329 *total = it->second.cpuTotal;
330 return VINF_SUCCESS;
331}
332
333int CollectorWin::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
334{
335 VMProcessMap::const_iterator it = mProcessStats.find(process);
336
337 if (it == mProcessStats.end())
338 {
339 Log (("No stats pre-collected for process %x\n", process));
340 return VERR_INTERNAL_ERROR;
341 }
342 *used = (ULONG)(it->second.ramUsed / 1024);
343 return VINF_SUCCESS;
344}
345
346}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette