VirtualBox

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

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

Main: Performance: Second m is redundant. Some locking-related todos.

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