VirtualBox

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

Last change on this file since 10753 was 10753, checked in by vboxsync, 16 years ago

Stubs for all platforms. Implementation of host CPU load and RAM usage counters for Windows. Locking. Fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1/* $Id: PerformanceWin.cpp 10753 2008-07-18 19:22:21Z vboxsync $ */
2
3/** @file
4 *
5 * VBox Windows-specific Performance Classes implementation.
6 */
7
8/*
9 * Copyright (C) 2008 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20 * Clara, CA 95054 USA or visit http://www.sun.com if you need
21 * additional information or have any questions.
22 */
23
24#include <pdh.h>
25#include <pdhmsg.h>
26#include <iprt/err.h>
27
28#include "Performance.h"
29
30//#pragma comment(lib, "pdh.lib")
31
32namespace pm {
33
34class CollectorWin : public CollectorHAL
35{
36public:
37 CollectorWin();
38 ~CollectorWin();
39
40 virtual int getHostCpuLoad(unsigned long *user, unsigned long *kernel, unsigned long *idle);
41 virtual int getHostCpuMHz(unsigned long *mhz);
42 virtual int getHostMemoryUsage(unsigned long *total, unsigned long *used, unsigned long *available);
43 virtual int getProcessCpuLoad(RTPROCESS process, unsigned long *user, unsigned long *kernel);
44 virtual int getProcessMemoryUsage(RTPROCESS process, unsigned long *used);
45
46private:
47 int convertPdhStatusToRTErr(PDH_STATUS pdhStatus);
48 HCOUNTER addCounter(HQUERY hQuery, LPCTSTR name);
49 int getCounterValue(HCOUNTER hCounter, DWORD flags, unsigned long *value);
50
51 HQUERY mHostCpuLoadQuery;
52 HCOUNTER mHostCpuLoadUserCounter;
53 HCOUNTER mHostCpuLoadKernelCounter;
54 HCOUNTER mHostCpuLoadIdleCounter;
55};
56
57MetricFactoryWin::MetricFactoryWin()
58{
59 mHAL = new CollectorWin();
60 Assert(mHAL);
61}
62
63CollectorWin::CollectorWin() : mHostCpuLoadQuery(0)
64{
65 PDH_STATUS pdhStatus;
66
67 pdhStatus = PdhOpenQuery(NULL, NULL, &mHostCpuLoadQuery);
68 if (pdhStatus != ERROR_SUCCESS)
69 return;
70
71 mHostCpuLoadUserCounter = addCounter (mHostCpuLoadQuery,
72 L"\\Processor(_Total)\\% User Time");
73 mHostCpuLoadKernelCounter = addCounter (mHostCpuLoadQuery,
74 L"\\Processor(_Total)\\% Privileged Time");
75 mHostCpuLoadIdleCounter = addCounter (mHostCpuLoadQuery,
76 L"\\Processor(_Total)\\% Idle Time");
77}
78
79CollectorWin::~CollectorWin()
80{
81 if (mHostCpuLoadQuery)
82 PdhCloseQuery (mHostCpuLoadQuery);
83}
84
85int CollectorWin::convertPdhStatusToRTErr(PDH_STATUS pdhStatus)
86{
87 switch (pdhStatus)
88 {
89 case PDH_INVALID_ARGUMENT:
90 return VERR_INVALID_PARAMETER;
91 case PDH_INVALID_HANDLE:
92 return VERR_INVALID_HANDLE;
93 }
94
95 return RTErrConvertFromWin32(pdhStatus);
96}
97
98HCOUNTER CollectorWin::addCounter(HQUERY hQuery, LPCTSTR name)
99{
100 PDH_STATUS pdhStatus;
101 HCOUNTER hCounter;
102
103 pdhStatus = PdhAddCounter (hQuery, name, 0, &hCounter);
104 if (pdhStatus != ERROR_SUCCESS)
105 return 0;
106 return hCounter;
107}
108
109int CollectorWin::getCounterValue(HCOUNTER hCounter, DWORD flags, unsigned long *value)
110{
111 PDH_STATUS pdhStatus;
112 PDH_FMT_COUNTERVALUE fmtValue;
113
114 pdhStatus = PdhGetFormattedCounterValue (hCounter,
115 PDH_FMT_LONG | flags,
116 NULL,
117 &fmtValue);
118 if (pdhStatus != ERROR_SUCCESS)
119 return convertPdhStatusToRTErr(pdhStatus);
120
121 *value = fmtValue.longValue;
122
123 return VINF_SUCCESS;
124}
125
126int CollectorWin::getHostCpuLoad(unsigned long *user, unsigned long *kernel, unsigned long *idle)
127{
128 int rc;
129 PDH_STATUS pdhStatus;
130
131 pdhStatus = PdhCollectQueryData (mHostCpuLoadQuery);
132 if (pdhStatus != ERROR_SUCCESS)
133 return convertPdhStatusToRTErr(pdhStatus);
134
135 rc = getCounterValue (mHostCpuLoadUserCounter, PDH_FMT_1000, user);
136 AssertRCReturn(rc, rc);
137
138 rc = getCounterValue (mHostCpuLoadKernelCounter, PDH_FMT_1000, kernel);
139 AssertRCReturn(rc, rc);
140
141 rc = getCounterValue (mHostCpuLoadIdleCounter, PDH_FMT_1000, idle);
142 AssertRCReturn(rc, rc);
143
144 return VINF_SUCCESS;
145}
146
147int CollectorWin::getHostCpuMHz(unsigned long *mhz)
148{
149 return E_NOTIMPL;
150}
151
152int CollectorWin::getHostMemoryUsage(unsigned long *total, unsigned long *used, unsigned long *available)
153{
154 MEMORYSTATUSEX mstat;
155
156 mstat.dwLength = sizeof(mstat);
157 if (GlobalMemoryStatusEx(&mstat))
158 {
159 *total = (unsigned long)( mstat.ullTotalPhys / 1000 );
160 *available = (unsigned long)( mstat.ullAvailPhys / 1000 );
161 *used = *total - *available;
162 }
163 else
164 return RTErrConvertFromWin32(GetLastError());
165
166 return VINF_SUCCESS;
167}
168
169int CollectorWin::getProcessCpuLoad(RTPROCESS process, unsigned long *user, unsigned long *kernel)
170{
171 return E_NOTIMPL;
172}
173
174int CollectorWin::getProcessMemoryUsage(RTPROCESS process, unsigned long *used)
175{
176 return E_NOTIMPL;
177}
178
179}
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