VirtualBox

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

Last change on this file since 56516 was 55769, checked in by vboxsync, 10 years ago

Main: it++ => ++it

  • 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 55769 2015-05-08 20:01:46Z 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 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
194int CollectorWin::getHostCpuLoad(ULONG *user, ULONG *kernel, ULONG *idle)
195{
196 return VERR_NOT_IMPLEMENTED;
197}
198
199typedef struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
200{
201 LARGE_INTEGER IdleTime;
202 LARGE_INTEGER KernelTime;
203 LARGE_INTEGER UserTime;
204 LARGE_INTEGER Reserved1[2];
205 ULONG Reserved2;
206} SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;
207
208int CollectorWin::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
209{
210 LogFlowThisFuncEnter();
211
212 FILETIME ftIdle, ftKernel, ftUser;
213
214 if (mpfnGetSystemTimes)
215 {
216 if (!mpfnGetSystemTimes(&ftIdle, &ftKernel, &ftUser))
217 {
218 DWORD dwError = GetLastError();
219 Log (("GetSystemTimes() -> 0x%x\n", dwError));
220 return RTErrConvertFromWin32(dwError);
221 }
222
223 *user = FILETTIME_TO_100NS(ftUser);
224 *idle = FILETTIME_TO_100NS(ftIdle);
225 *kernel = FILETTIME_TO_100NS(ftKernel) - *idle;
226 }
227 else
228 {
229 /* GetSystemTimes is not available, fall back to NtQuerySystemInformation */
230 if (!mpfnNtQuerySystemInformation)
231 return VERR_NOT_IMPLEMENTED;
232
233 SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION sppi[MAXIMUM_PROCESSORS];
234 ULONG ulReturned;
235 NTSTATUS status = mpfnNtQuerySystemInformation(
236 SystemProcessorPerformanceInformation, &sppi, sizeof(sppi), &ulReturned);
237 if (NT_ERROR(status))
238 {
239 Log(("NtQuerySystemInformation() -> 0x%x\n", status));
240 return RTErrConvertFromNtStatus(status);
241 }
242 /* Sum up values across all processors */
243 *user = *kernel = *idle = 0;
244 for (unsigned i = 0; i < ulReturned / sizeof(sppi[0]); ++i)
245 {
246 *idle += sppi[i].IdleTime.QuadPart;
247 *kernel += sppi[i].KernelTime.QuadPart - sppi[i].IdleTime.QuadPart;
248 *user += sppi[i].UserTime.QuadPart;
249 }
250 }
251
252 LogFlowThisFunc(("user=%lu kernel=%lu idle=%lu\n", *user, *kernel, *idle));
253 LogFlowThisFuncLeave();
254
255 return VINF_SUCCESS;
256}
257
258typedef struct _PROCESSOR_POWER_INFORMATION {
259 ULONG Number;
260 ULONG MaxMhz;
261 ULONG CurrentMhz;
262 ULONG MhzLimit;
263 ULONG MaxIdleState;
264 ULONG CurrentIdleState;
265} PROCESSOR_POWER_INFORMATION , *PPROCESSOR_POWER_INFORMATION;
266
267int CollectorWin::getHostCpuMHz(ULONG *mhz)
268{
269 uint64_t uTotalMhz = 0;
270 RTCPUID nProcessors = RTMpGetCount();
271 PPROCESSOR_POWER_INFORMATION ppi = (PPROCESSOR_POWER_INFORMATION)
272 RTMemAllocZ(nProcessors * sizeof(PROCESSOR_POWER_INFORMATION));
273
274 if (!ppi)
275 return VERR_NO_MEMORY;
276
277 LONG ns = CallNtPowerInformation(ProcessorInformation, NULL, 0, ppi,
278 nProcessors * sizeof(PROCESSOR_POWER_INFORMATION));
279 if (ns)
280 {
281 Log(("CallNtPowerInformation() -> %x\n", ns));
282 RTMemFree(ppi);
283 return VERR_INTERNAL_ERROR;
284 }
285
286 /* Compute an average over all CPUs */
287 for (unsigned i = 0; i < nProcessors; i++)
288 uTotalMhz += ppi[i].CurrentMhz;
289 *mhz = (ULONG)(uTotalMhz / nProcessors);
290
291 RTMemFree(ppi);
292 LogFlowThisFunc(("mhz=%u\n", *mhz));
293 LogFlowThisFuncLeave();
294
295 return VINF_SUCCESS;
296}
297
298int CollectorWin::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
299{
300 AssertReturn(totalRAM, VERR_INTERNAL_ERROR);
301 uint64_t cb;
302 int rc = RTSystemQueryAvailableRam(&cb);
303 if (RT_SUCCESS(rc))
304 {
305 *total = totalRAM;
306 *available = (ULONG)(cb / 1024);
307 *used = *total - *available;
308 }
309 return rc;
310}
311
312int CollectorWin::getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel)
313{
314 return VERR_NOT_IMPLEMENTED;
315}
316
317int CollectorWin::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
318{
319 VMProcessMap::const_iterator it = mProcessStats.find(process);
320
321 if (it == mProcessStats.end())
322 {
323 Log (("No stats pre-collected for process %x\n", process));
324 return VERR_INTERNAL_ERROR;
325 }
326 *user = it->second.cpuUser;
327 *kernel = it->second.cpuKernel;
328 *total = it->second.cpuTotal;
329 return VINF_SUCCESS;
330}
331
332int CollectorWin::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
333{
334 VMProcessMap::const_iterator it = mProcessStats.find(process);
335
336 if (it == mProcessStats.end())
337 {
338 Log (("No stats pre-collected for process %x\n", process));
339 return VERR_INTERNAL_ERROR;
340 }
341 *used = (ULONG)(it->second.ramUsed / 1024);
342 return VINF_SUCCESS;
343}
344
345}
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