VirtualBox

source: vbox/trunk/src/VBox/Main/win/PerformanceWin.cpp@ 33517

Last change on this file since 33517 was 32056, checked in by vboxsync, 14 years ago

Applied a simple memory leak detector to libjpeg and VBoxC (Windows host only), the code completely disabled by default.

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