VirtualBox

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

Last change on this file since 48935 was 46593, checked in by vboxsync, 11 years ago

updates

  • 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 46593 2013-06-17 14:32:51Z 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)RTMemAllocZ(nProcessors * sizeof(PROCESSOR_POWER_INFORMATION));
272
273 if (!ppi)
274 return VERR_NO_MEMORY;
275
276 LONG ns = CallNtPowerInformation(ProcessorInformation, NULL, 0, ppi,
277 nProcessors * sizeof(PROCESSOR_POWER_INFORMATION));
278 if (ns)
279 {
280 Log(("CallNtPowerInformation() -> %x\n", ns));
281 RTMemFree(ppi);
282 return VERR_INTERNAL_ERROR;
283 }
284
285 /* Compute an average over all CPUs */
286 for (unsigned i = 0; i < nProcessors; i++)
287 uTotalMhz += ppi[i].CurrentMhz;
288 *mhz = (ULONG)(uTotalMhz / nProcessors);
289
290 RTMemFree(ppi);
291 LogFlowThisFunc(("mhz=%u\n", *mhz));
292 LogFlowThisFuncLeave();
293
294 return VINF_SUCCESS;
295}
296
297int CollectorWin::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
298{
299 AssertReturn(totalRAM, VERR_INTERNAL_ERROR);
300 uint64_t cb;
301 int rc = RTSystemQueryAvailableRam(&cb);
302 if (RT_SUCCESS(rc))
303 {
304 *total = totalRAM;
305 *available = (ULONG)(cb / 1024);
306 *used = *total - *available;
307 }
308 return rc;
309}
310
311int CollectorWin::getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel)
312{
313 return VERR_NOT_IMPLEMENTED;
314}
315
316int CollectorWin::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
317{
318 VMProcessMap::const_iterator it = mProcessStats.find(process);
319
320 if (it == mProcessStats.end())
321 {
322 Log (("No stats pre-collected for process %x\n", process));
323 return VERR_INTERNAL_ERROR;
324 }
325 *user = it->second.cpuUser;
326 *kernel = it->second.cpuKernel;
327 *total = it->second.cpuTotal;
328 return VINF_SUCCESS;
329}
330
331int CollectorWin::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
332{
333 VMProcessMap::const_iterator it = mProcessStats.find(process);
334
335 if (it == mProcessStats.end())
336 {
337 Log (("No stats pre-collected for process %x\n", process));
338 return VERR_INTERNAL_ERROR;
339 }
340 *used = (ULONG)(it->second.ramUsed / 1024);
341 return VINF_SUCCESS;
342}
343
344}
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