1 | /* $Id: Performance.h 11571 2008-08-22 12:19: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 <VBox/com/ptr.h>
|
---|
28 | #include <list>
|
---|
29 | #include <string>
|
---|
30 |
|
---|
31 | namespace 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(ULONG length);
|
---|
42 | ULONG length();
|
---|
43 | void put(ULONG value);
|
---|
44 | void copyTo(ULONG *data);
|
---|
45 | private:
|
---|
46 | ULONG *mData;
|
---|
47 | ULONG mLength;
|
---|
48 | ULONG 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(ULONG *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(ULONG *user, ULONG *kernel, ULONG *idle);
|
---|
69 | virtual int getHostCpuMHz(ULONG *mhz);
|
---|
70 | virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available) = 0;
|
---|
71 | virtual int getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel);
|
---|
72 | virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used) = 0;
|
---|
73 |
|
---|
74 | virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
|
---|
75 | virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
|
---|
76 | };
|
---|
77 |
|
---|
78 | /* Base Metrics *********************************************************/
|
---|
79 | class BaseMetric
|
---|
80 | {
|
---|
81 | public:
|
---|
82 | BaseMetric(CollectorHAL *hal, const char *name, ComPtr<IUnknown> object)
|
---|
83 | : mHAL(hal), mPeriod(0), mLength(0), mName(name), mObject(object), mLastSampleTaken(0), mEnabled(false) {};
|
---|
84 |
|
---|
85 | virtual void init(ULONG period, ULONG length) = 0;
|
---|
86 | virtual void collect() = 0;
|
---|
87 | virtual const char *getUnit() = 0;
|
---|
88 | virtual ULONG getMinValue() = 0;
|
---|
89 | virtual ULONG 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 | ULONG getPeriod() { return mPeriod; };
|
---|
98 | ULONG 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 | ULONG mPeriod;
|
---|
106 | ULONG 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(ULONG period, ULONG length);
|
---|
119 |
|
---|
120 | void collect();
|
---|
121 | const char *getUnit() { return "%"; };
|
---|
122 | ULONG getMinValue() { return 0; };
|
---|
123 | ULONG getMaxValue() { return PM_CPU_LOAD_MULTIPLIER; };
|
---|
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 | uint64_t mUserPrev;
|
---|
140 | uint64_t mKernelPrev;
|
---|
141 | uint64_t 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(ULONG period, ULONG length);
|
---|
151 | void collect();
|
---|
152 | const char *getUnit() { return "MHz"; };
|
---|
153 | ULONG getMinValue() { return 0; };
|
---|
154 | ULONG getMaxValue() { return INT32_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(ULONG period, ULONG length);
|
---|
166 | void collect();
|
---|
167 | const char *getUnit() { return "kB"; };
|
---|
168 | ULONG getMinValue() { return 0; };
|
---|
169 | ULONG getMaxValue() { return INT32_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(ULONG period, ULONG length);
|
---|
183 | void collect();
|
---|
184 | const char *getUnit() { return "%"; };
|
---|
185 | ULONG getMinValue() { return 0; };
|
---|
186 | ULONG getMaxValue() { return PM_CPU_LOAD_MULTIPLIER; };
|
---|
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 | uint64_t mHostTotalPrev;
|
---|
202 | uint64_t mProcessUserPrev;
|
---|
203 | uint64_t 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(ULONG period, ULONG length);
|
---|
213 | void collect();
|
---|
214 | const char *getUnit() { return "kB"; };
|
---|
215 | ULONG getMinValue() { return 0; };
|
---|
216 | ULONG getMaxValue() { return INT32_MAX; };
|
---|
217 | private:
|
---|
218 | RTPROCESS mProcess;
|
---|
219 | SubMetric *mUsed;
|
---|
220 | };
|
---|
221 |
|
---|
222 | /* Aggregate Functions **************************************************/
|
---|
223 | class Aggregate
|
---|
224 | {
|
---|
225 | public:
|
---|
226 | virtual ULONG compute(ULONG *data, ULONG length) = 0;
|
---|
227 | virtual const char *getName() = 0;
|
---|
228 | };
|
---|
229 |
|
---|
230 | class AggregateAvg : public Aggregate
|
---|
231 | {
|
---|
232 | public:
|
---|
233 | virtual ULONG compute(ULONG *data, ULONG length);
|
---|
234 | virtual const char *getName();
|
---|
235 | };
|
---|
236 |
|
---|
237 | class AggregateMin : public Aggregate
|
---|
238 | {
|
---|
239 | public:
|
---|
240 | virtual ULONG compute(ULONG *data, ULONG length);
|
---|
241 | virtual const char *getName();
|
---|
242 | };
|
---|
243 |
|
---|
244 | class AggregateMax : public Aggregate
|
---|
245 | {
|
---|
246 | public:
|
---|
247 | virtual ULONG compute(ULONG *data, ULONG 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 | ULONG getMinValue() { return mBaseMetric->getMinValue(); };
|
---|
275 | ULONG getMaxValue() { return mBaseMetric->getMaxValue(); };
|
---|
276 | ULONG getPeriod() { return mBaseMetric->getPeriod(); };
|
---|
277 | ULONG getLength() { return mAggregate ? 1 : mBaseMetric->getLength(); };
|
---|
278 | void query(ULONG **data, ULONG *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 | // Nothing else to do here (yet)
|
---|
308 | };
|
---|
309 |
|
---|
310 | class MetricFactoryLinux : public MetricFactory
|
---|
311 | {
|
---|
312 | public:
|
---|
313 | MetricFactoryLinux();
|
---|
314 | // Nothing else to do here (yet)
|
---|
315 | };
|
---|
316 |
|
---|
317 | class MetricFactoryWin : public MetricFactory
|
---|
318 | {
|
---|
319 | public:
|
---|
320 | MetricFactoryWin();
|
---|
321 | // Nothing else to do here (yet)
|
---|
322 | };
|
---|
323 |
|
---|
324 | class MetricFactoryOS2 : public MetricFactory
|
---|
325 | {
|
---|
326 | public:
|
---|
327 | MetricFactoryOS2();
|
---|
328 | // Nothing else to do here (yet)
|
---|
329 | };
|
---|
330 |
|
---|
331 | class MetricFactoryDarwin : public MetricFactory
|
---|
332 | {
|
---|
333 | public:
|
---|
334 | MetricFactoryDarwin();
|
---|
335 | // Nothing else to do here (yet)
|
---|
336 | };
|
---|
337 |
|
---|
338 | class Filter
|
---|
339 | {
|
---|
340 | public:
|
---|
341 | Filter(ComSafeArrayIn(INPTR BSTR, metricNames),
|
---|
342 | ComSafeArrayIn(IUnknown * , objects));
|
---|
343 | static bool patternMatch(const char *pszPat, const char *pszName);
|
---|
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 |
|
---|