VirtualBox

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

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

Perf API: Filtering

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