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