VirtualBox

source: vbox/trunk/src/VBox/Main/include/Performance.h@ 10753

Last change on this file since 10753 was 10753, checked in by vboxsync, 17 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: 12.2 KB
Line 
1/* $Id: Performance.h 10753 2008-07-18 19:22:21Z vboxsync $ */
2
3/** @file
4 *
5 * VBox Performance Classes declaration.
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
25#include <iprt/types.h>
26#include <VBox/com/defs.h>
27#include <VBox/com/ptr.h>
28#include <list>
29#include <string>
30
31namespace pm {
32 /* CPU load is measured in 1/1000 of per cent. */
33 const uint64_t PM_CPU_LOAD_MULTIPLIER = UINT64_C(100000);
34
35 /* Sub Metrics **********************************************************/
36 class CircularBuffer
37 {
38 public:
39 CircularBuffer() : mData(0), mLength(0), mEnd(0), mWrapped(false) {};
40 void init(unsigned long length);
41 unsigned long length();
42 void put(unsigned long value);
43 void copyTo(unsigned long *data);
44 private:
45 unsigned long *mData;
46 unsigned long mLength;
47 unsigned long mEnd;
48 bool mWrapped;
49 };
50
51 class SubMetric : public CircularBuffer
52 {
53 public:
54 SubMetric(const char *name)
55 : mName(name) {};
56 void query(unsigned long *data);
57 const char *getName() { return mName; };
58 private:
59 const char *mName;
60 };
61
62
63 /* Collector Hardware Abstraction Layer *********************************/
64 class CollectorHAL
65 {
66 public:
67 virtual int getHostCpuLoad(unsigned long *user, unsigned long *kernel, unsigned long *idle);
68 virtual int getHostCpuMHz(unsigned long *mhz) = 0;
69 virtual int getHostMemoryUsage(unsigned long *total, unsigned long *used, unsigned long *available) = 0;
70 virtual int getProcessCpuLoad(RTPROCESS process, unsigned long *user, unsigned long *kernel);
71 virtual int getProcessMemoryUsage(RTPROCESS process, unsigned long *used) = 0;
72
73 virtual int getRawHostCpuLoad(unsigned long *user, unsigned long *kernel, unsigned long *idle);
74 virtual int getRawProcessCpuLoad(RTPROCESS process, unsigned long *user, unsigned long *kernel);
75 };
76
77 /* Base Metrics *********************************************************/
78 class BaseMetric
79 {
80 public:
81 BaseMetric(CollectorHAL *hal, const char *name, ComPtr<IUnknown> object)
82 : mHAL(hal), mLength(0), mName(name), mObject(object), mLastSampleTaken(0), mEnabled(false) {};
83
84 virtual void init(unsigned long period, unsigned long length) = 0;
85 virtual void collect() = 0;
86 virtual const char *getUnit() = 0;
87 virtual unsigned long getMinValue() = 0;
88 virtual unsigned long getMaxValue() = 0;
89
90 void collectorBeat(uint64_t nowAt);
91
92 void enable() { mEnabled = true; };
93 void disable() { mEnabled = false; };
94
95 bool isEnabled() { return mEnabled; };
96 unsigned long getPeriod() { return mPeriod; };
97 unsigned long getLength() { return mLength; };
98 const char *getName() { return mName; };
99 ComPtr<IUnknown> getObject() { return mObject; };
100 bool associatedWith(ComPtr<IUnknown> object) { return mObject == object; };
101
102 protected:
103 CollectorHAL *mHAL;
104 unsigned long mPeriod;
105 unsigned long mLength;
106 const char *mName;
107 ComPtr<IUnknown> mObject;
108 uint64_t mLastSampleTaken;
109 bool mEnabled;
110 };
111
112 class HostCpuLoad : public BaseMetric
113 {
114 public:
115 HostCpuLoad(CollectorHAL *hal, ComPtr<IUnknown> object, SubMetric *user, SubMetric *kernel, SubMetric *idle)
116 : BaseMetric(hal, "CPU/Load", object), mUser(user), mKernel(kernel), mIdle(idle) {};
117 void init(unsigned long period, unsigned long length);
118
119 void collect();
120 const char *getUnit() { return "%"; };
121 unsigned long getMinValue() { return 0; };
122 unsigned long getMaxValue() { return 100000000; };
123
124 protected:
125 SubMetric *mUser;
126 SubMetric *mKernel;
127 SubMetric *mIdle;
128 };
129
130 class HostCpuLoadRaw : public HostCpuLoad
131 {
132 public:
133 HostCpuLoadRaw(CollectorHAL *hal, ComPtr<IUnknown> object, SubMetric *user, SubMetric *kernel, SubMetric *idle)
134 : HostCpuLoad(hal, object, user, kernel, idle), mUserPrev(0), mKernelPrev(0), mIdlePrev(0) {};
135
136 void collect();
137 private:
138 unsigned long mUserPrev;
139 unsigned long mKernelPrev;
140 unsigned long mIdlePrev;
141 };
142
143 class HostCpuMhz : public BaseMetric
144 {
145 public:
146 HostCpuMhz(CollectorHAL *hal, ComPtr<IUnknown> object, SubMetric *mhz)
147 : BaseMetric(hal, "CPU/MHz", object), mMHz(mhz) {};
148
149 void init(unsigned long period, unsigned long length);
150 void collect();
151 const char *getUnit() { return "MHz"; };
152 unsigned long getMinValue() { return 0; };
153 unsigned long getMaxValue() { return UINT32_MAX; };
154 private:
155 SubMetric *mMHz;
156 };
157
158 class HostRamUsage : public BaseMetric
159 {
160 public:
161 HostRamUsage(CollectorHAL *hal, ComPtr<IUnknown> object, SubMetric *total, SubMetric *used, SubMetric *available)
162 : BaseMetric(hal, "RAM/Usage", object), mTotal(total), mUsed(used), mAvailable(available) {};
163
164 void init(unsigned long period, unsigned long length);
165 void collect();
166 const char *getUnit() { return "kB"; };
167 unsigned long getMinValue() { return 0; };
168 unsigned long getMaxValue() { return UINT32_MAX; };
169 private:
170 SubMetric *mTotal;
171 SubMetric *mUsed;
172 SubMetric *mAvailable;
173 };
174
175 class MachineCpuLoad : public BaseMetric
176 {
177 public:
178 MachineCpuLoad(CollectorHAL *hal, ComPtr<IUnknown> object, RTPROCESS process, SubMetric *user, SubMetric *kernel)
179 : BaseMetric(hal, "CPU/Load", object), mProcess(process), mUser(user), mKernel(kernel) {};
180
181 void init(unsigned long period, unsigned long length);
182 void collect();
183 const char *getUnit() { return "%"; };
184 unsigned long getMinValue() { return 0; };
185 unsigned long getMaxValue() { return 100000000; };
186 protected:
187 RTPROCESS mProcess;
188 SubMetric *mUser;
189 SubMetric *mKernel;
190 };
191
192 class MachineCpuLoadRaw : public MachineCpuLoad
193 {
194 public:
195 MachineCpuLoadRaw(CollectorHAL *hal, ComPtr<IUnknown> object, RTPROCESS process, SubMetric *user, SubMetric *kernel)
196 : MachineCpuLoad(hal, object, process, user, kernel), mHostTotalPrev(0), mProcessUserPrev(0), mProcessKernelPrev(0) {};
197
198 void collect();
199 private:
200 unsigned long mHostTotalPrev;
201 unsigned long mProcessUserPrev;
202 unsigned long mProcessKernelPrev;
203 };
204
205 class MachineRamUsage : public BaseMetric
206 {
207 public:
208 MachineRamUsage(CollectorHAL *hal, ComPtr<IUnknown> object, RTPROCESS process, SubMetric *used)
209 : BaseMetric(hal, "RAM/Usage", object), mProcess(process), mUsed(used) {};
210
211 void init(unsigned long period, unsigned long length);
212 void collect();
213 const char *getUnit() { return "kB"; };
214 unsigned long getMinValue() { return 0; };
215 unsigned long getMaxValue() { return UINT32_MAX; };
216 private:
217 RTPROCESS mProcess;
218 SubMetric *mUsed;
219 };
220
221 /* Aggregate Functions **************************************************/
222 class Aggregate
223 {
224 public:
225 virtual unsigned long compute(unsigned long *data, unsigned long length) = 0;
226 virtual const char *getName() = 0;
227 };
228
229 class AggregateAvg : public Aggregate
230 {
231 public:
232 virtual unsigned long compute(unsigned long *data, unsigned long length);
233 virtual const char *getName();
234 };
235
236 class AggregateMin : public Aggregate
237 {
238 public:
239 virtual unsigned long compute(unsigned long *data, unsigned long length);
240 virtual const char *getName();
241 };
242
243 class AggregateMax : public Aggregate
244 {
245 public:
246 virtual unsigned long compute(unsigned long *data, unsigned long length);
247 virtual const char *getName();
248 };
249
250 /* Metric Class *********************************************************/
251 class Metric
252 {
253 public:
254 Metric(BaseMetric *baseMetric, SubMetric *subMetric, Aggregate *aggregate) :
255 mName(subMetric->getName()), mBaseMetric(baseMetric), mSubMetric(subMetric), mAggregate(aggregate)
256 {
257 if (mAggregate)
258 {
259 mName += ":";
260 mName += mAggregate->getName();
261 }
262 }
263
264 ~Metric()
265 {
266 delete mAggregate;
267 }
268 bool associatedWith(ComPtr<IUnknown> object) { return getObject() == object; };
269
270 const char *getName() { return mName.c_str(); };
271 ComPtr<IUnknown> getObject() { return mBaseMetric->getObject(); };
272 const char *getUnit() { return mBaseMetric->getUnit(); };
273 unsigned long getMinValue() { return mBaseMetric->getMinValue(); };
274 unsigned long getMaxValue() { return mBaseMetric->getMaxValue(); };
275 unsigned long getPeriod() { return mBaseMetric->getPeriod(); };
276 unsigned long getLength() { return mAggregate ? 1 : mBaseMetric->getLength(); };
277 void query(unsigned long **data, unsigned long *count);
278
279 private:
280 std::string mName;
281 BaseMetric *mBaseMetric;
282 SubMetric *mSubMetric;
283 Aggregate *mAggregate;
284 };
285
286 /* Metric Factories *****************************************************/
287 class MetricFactory
288 {
289 public:
290 MetricFactory() : mHAL(0) {};
291 ~MetricFactory() { delete mHAL; };
292
293 virtual BaseMetric *createHostCpuLoad(ComPtr<IUnknown> object, SubMetric *user, SubMetric *kernel, SubMetric *idle);
294 virtual BaseMetric *createHostCpuMHz(ComPtr<IUnknown> object, SubMetric *mhz);
295 virtual BaseMetric *createHostRamUsage(ComPtr<IUnknown> object, SubMetric *total, SubMetric *used, SubMetric *available);
296 virtual BaseMetric *createMachineCpuLoad(ComPtr<IUnknown> object, RTPROCESS process, SubMetric *user, SubMetric *kernel);
297 virtual BaseMetric *createMachineRamUsage(ComPtr<IUnknown> object, RTPROCESS process, SubMetric *used);
298 protected:
299 CollectorHAL *mHAL;
300 };
301
302 class MetricFactorySolaris : public MetricFactory
303 {
304 public:
305 MetricFactorySolaris();
306 virtual BaseMetric *createHostCpuLoad(ComPtr<IUnknown> object, SubMetric *user, SubMetric *kernel, SubMetric *idle);
307 virtual BaseMetric *createMachineCpuLoad(ComPtr<IUnknown> object, RTPROCESS process, SubMetric *user, SubMetric *kernel);
308 };
309
310 class MetricFactoryLinux : public MetricFactory
311 {
312 public:
313 MetricFactoryLinux();
314 virtual BaseMetric *createHostCpuLoad(ComPtr<IUnknown> object, SubMetric *user, SubMetric *kernel, SubMetric *idle);
315 virtual BaseMetric *createMachineCpuLoad(ComPtr<IUnknown> object, RTPROCESS process, SubMetric *user, SubMetric *kernel);
316 };
317
318 class MetricFactoryWin : public MetricFactory
319 {
320 public:
321 MetricFactoryWin();
322 // Nothing else to do here (yet)
323 };
324
325 class MetricFactoryOS2 : public MetricFactory
326 {
327 public:
328 MetricFactoryOS2();
329 // Nothing else to do here (yet)
330 };
331
332 class MetricFactoryDarwin : public MetricFactory
333 {
334 public:
335 MetricFactoryDarwin();
336 // Nothing else to do here (yet)
337 };
338
339 class Filter
340 {
341 public:
342 Filter(ComSafeArrayIn(INPTR BSTR, metricNames),
343 ComSafeArrayIn(IUnknown * , objects));
344 bool match(const ComPtr<IUnknown> object, const std::string &name) const;
345 private:
346 typedef std::pair<const ComPtr<IUnknown>, const std::string> FilterElement;
347 typedef std::list<FilterElement> ElementList;
348
349 ElementList mElements;
350
351 void processMetricList(const std::string &name, const ComPtr<IUnknown> object);
352 };
353}
354
Note: See TracBrowser for help on using the repository browser.

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