VirtualBox

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

Last change on this file since 29630 was 29630, checked in by vboxsync, 15 years ago

Main: gcc warning

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.9 KB
Line 
1/* $Id: Performance.h 29630 2010-05-18 12:58:18Z vboxsync $ */
2
3/** @file
4 *
5 * VBox Performance Classes declaration.
6 */
7
8/*
9 * Copyright (C) 2008 Oracle Corporation
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#ifndef ___performance_h
20#define ___performance_h
21
22#include <VBox/com/defs.h>
23#include <VBox/com/ptr.h>
24#include <VBox/com/string.h>
25#include <VBox/com/VirtualBox.h>
26
27#include <iprt/types.h>
28#include <iprt/err.h>
29
30#include <algorithm>
31#include <functional> /* For std::fun_ptr in testcase */
32#include <list>
33#include <vector>
34
35/* Forward decl. */
36class Machine;
37
38namespace pm
39{
40 /* CPU load is measured in 1/1000 of per cent. */
41 const uint64_t PM_CPU_LOAD_MULTIPLIER = UINT64_C(100000);
42
43 /* Sub Metrics **********************************************************/
44 class CircularBuffer
45 {
46 public:
47 CircularBuffer() : mData(0), mLength(0), mEnd(0), mWrapped(false) {};
48 void init(ULONG length);
49 ULONG length();
50 ULONG getSequenceNumber() { return mSequenceNumber; }
51 void put(ULONG value);
52 void copyTo(ULONG *data);
53 private:
54 ULONG *mData;
55 ULONG mLength;
56 ULONG mEnd;
57 ULONG mSequenceNumber;
58 bool mWrapped;
59 };
60
61 class SubMetric : public CircularBuffer
62 {
63 public:
64 SubMetric(const char *name, const char *description)
65 : mName(name), mDescription(description) {};
66 void query(ULONG *data);
67 const char *getName() { return mName; };
68 const char *getDescription() { return mDescription; };
69 private:
70 const char *mName;
71 const char *mDescription;
72 };
73
74
75 /* Collector Hardware Abstraction Layer *********************************/
76 enum {
77 COLLECT_NONE = 0x0,
78 COLLECT_CPU_LOAD = 0x1,
79 COLLECT_RAM_USAGE = 0x2
80 };
81 typedef int HintFlags;
82 typedef std::pair<RTPROCESS, HintFlags> ProcessFlagsPair;
83
84 class CollectorHints
85 {
86 public:
87 typedef std::list<ProcessFlagsPair> ProcessList;
88
89 CollectorHints() : mHostFlags(COLLECT_NONE) {}
90 void collectHostCpuLoad()
91 { mHostFlags |= COLLECT_CPU_LOAD; }
92 void collectHostRamUsage()
93 { mHostFlags |= COLLECT_RAM_USAGE; }
94 void collectProcessCpuLoad(RTPROCESS process)
95 { findProcess(process).second |= COLLECT_CPU_LOAD; }
96 void collectProcessRamUsage(RTPROCESS process)
97 { findProcess(process).second |= COLLECT_RAM_USAGE; }
98 bool isHostCpuLoadCollected() const
99 { return (mHostFlags & COLLECT_CPU_LOAD) != 0; }
100 bool isHostRamUsageCollected() const
101 { return (mHostFlags & COLLECT_RAM_USAGE) != 0; }
102 bool isProcessCpuLoadCollected(RTPROCESS process)
103 { return (findProcess(process).second & COLLECT_CPU_LOAD) != 0; }
104 bool isProcessRamUsageCollected(RTPROCESS process)
105 { return (findProcess(process).second & COLLECT_RAM_USAGE) != 0; }
106 void getProcesses(std::vector<RTPROCESS>& processes) const
107 {
108 processes.clear();
109 processes.reserve(mProcesses.size());
110 for (ProcessList::const_iterator it = mProcesses.begin(); it != mProcesses.end(); it++)
111 processes.push_back(it->first);
112 }
113 const ProcessList& getProcessFlags() const
114 {
115 return mProcesses;
116 }
117 private:
118 HintFlags mHostFlags;
119 ProcessList mProcesses;
120
121 ProcessFlagsPair& findProcess(RTPROCESS process)
122 {
123 ProcessList::iterator it;
124 for (it = mProcesses.begin(); it != mProcesses.end(); it++)
125 if (it->first == process)
126 return *it;
127
128 /* Not found -- add new */
129 mProcesses.push_back(ProcessFlagsPair(process, COLLECT_NONE));
130 return mProcesses.back();
131 }
132 };
133
134 class CollectorHAL
135 {
136 public:
137 CollectorHAL() : mMemAllocVMM(0), mMemFreeVMM(0), mMemBalloonedVMM(0) {};
138 virtual ~CollectorHAL() { };
139 virtual int preCollect(const CollectorHints& /* hints */, uint64_t /* iTick */) { return VINF_SUCCESS; }
140 /** Returns averaged CPU usage in 1/1000th per cent across all host's CPUs. */
141 virtual int getHostCpuLoad(ULONG *user, ULONG *kernel, ULONG *idle);
142 /** Returns the average frequency in MHz across all host's CPUs. */
143 virtual int getHostCpuMHz(ULONG *mhz);
144 /** Returns the amount of physical memory in kilobytes. */
145 virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
146 /** Returns CPU usage in 1/1000th per cent by a particular process. */
147 virtual int getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel);
148 /** Returns the amount of memory used by a process in kilobytes. */
149 virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);
150
151 /** Returns CPU usage counters in platform-specific units. */
152 virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
153 /** Returns process' CPU usage counter in platform-specific units. */
154 virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
155
156 /** Enable metrics collecting (if applicable) */
157 virtual int enable();
158 /** Disable metrics collecting (if applicable) */
159 virtual int disable();
160
161 virtual int setMemHypervisorStats(ULONG memAlloc, ULONG memFree, ULONG memBallooned, ULONG memShared)
162 {
163 mMemAllocVMM = memAlloc;
164 mMemFreeVMM = memFree;
165 mMemBalloonedVMM = memBallooned;
166 mMemSharedVMM = memShared;
167 return S_OK;
168 }
169
170 virtual void getMemHypervisorStats(ULONG *pMemAlloc, ULONG *pMemFree, ULONG *pMemBallooned, ULONG *pMemShared)
171 {
172 *pMemAlloc = mMemAllocVMM;
173 *pMemFree = mMemFreeVMM;
174 *pMemBallooned = mMemBalloonedVMM;
175 *pMemShared = mMemSharedVMM;
176 }
177
178 private:
179 ULONG mMemAllocVMM;
180 ULONG mMemFreeVMM;
181 ULONG mMemBalloonedVMM;
182 ULONG mMemSharedVMM;
183 };
184
185 class CollectorGuestHAL : public CollectorHAL
186 {
187 public:
188 CollectorGuestHAL(Machine *machine, CollectorHAL *hostHAL) : CollectorHAL(), cEnabled(0), mMachine(machine), mConsole(NULL), mGuest(NULL),
189 mLastTick(0), mHostHAL(hostHAL), mCpuUser(0), mCpuKernel(0), mCpuIdle(0), mMemTotal(0), mMemFree(0),
190 mMemBalloon(0), mMemShared(0), mMemCache(0), mPageTotal(0) {};
191 ~CollectorGuestHAL();
192
193 virtual int preCollect(const CollectorHints& hints, uint64_t iTick);
194
195 /** Enable metrics collecting (if applicable) */
196 virtual int enable();
197 /** Disable metrics collecting (if applicable) */
198 virtual int disable();
199
200 /** Return guest cpu absolute load values (0-100). */
201 void getGuestCpuLoad(ULONG *pulCpuUser, ULONG *pulCpuKernel, ULONG *pulCpuIdle)
202 {
203 *pulCpuUser = mCpuUser;
204 *pulCpuKernel = mCpuKernel;
205 *pulCpuIdle = mCpuIdle;
206 }
207
208 /** Return guest memory information in KB. */
209 void getGuestMemLoad(ULONG *pulMemTotal, ULONG *pulMemFree, ULONG *pulMemBalloon, ULONG *pulMemShared, ULONG *pulMemCache, ULONG *pulPageTotal)
210 {
211 *pulMemTotal = mMemTotal;
212 *pulMemFree = mMemFree;
213 *pulMemBalloon = mMemBalloon;
214 *pulMemShared = mMemShared;
215 *pulMemCache = mMemCache;
216 *pulPageTotal = mPageTotal;
217 }
218
219
220 protected:
221 uint32_t cEnabled;
222 Machine *mMachine;
223 ComPtr<IConsole> mConsole;
224 ComPtr<IGuest> mGuest;
225 uint64_t mLastTick;
226
227 CollectorHAL *mHostHAL;
228
229 ULONG mCpuUser;
230 ULONG mCpuKernel;
231 ULONG mCpuIdle;
232 ULONG mMemTotal;
233 ULONG mMemFree;
234 ULONG mMemBalloon;
235 ULONG mMemShared;
236 ULONG mMemCache;
237 ULONG mPageTotal;
238 };
239
240 extern CollectorHAL *createHAL();
241
242 /* Base Metrics *********************************************************/
243 class BaseMetric
244 {
245 public:
246 BaseMetric(CollectorHAL *hal, const char *name, ComPtr<IUnknown> object)
247 : mPeriod(0), mLength(0), mHAL(hal), mName(name), mObject(object), mLastSampleTaken(0), mEnabled(false) {};
248 virtual ~BaseMetric() {};
249
250 virtual void init(ULONG period, ULONG length) = 0;
251 virtual void preCollect(CollectorHints& hints, uint64_t iTick) = 0;
252 virtual void collect() = 0;
253 virtual const char *getUnit() = 0;
254 virtual ULONG getMinValue() = 0;
255 virtual ULONG getMaxValue() = 0;
256 virtual ULONG getScale() = 0;
257
258 bool collectorBeat(uint64_t nowAt);
259
260 void enable()
261 {
262 mEnabled = true;
263 mHAL->enable();
264 };
265 void disable()
266 {
267 mHAL->disable();
268 mEnabled = false;
269 };
270
271 bool isEnabled() { return mEnabled; };
272 ULONG getPeriod() { return mPeriod; };
273 ULONG getLength() { return mLength; };
274 const char *getName() { return mName; };
275 ComPtr<IUnknown> getObject() { return mObject; };
276 bool associatedWith(ComPtr<IUnknown> object) { return mObject == object; };
277
278 protected:
279 ULONG mPeriod;
280 ULONG mLength;
281 CollectorHAL *mHAL;
282 const char *mName;
283 ComPtr<IUnknown> mObject;
284 uint64_t mLastSampleTaken;
285 bool mEnabled;
286 };
287
288 class HostCpuLoad : public BaseMetric
289 {
290 public:
291 HostCpuLoad(CollectorHAL *hal, ComPtr<IUnknown> object, SubMetric *user, SubMetric *kernel, SubMetric *idle)
292 : BaseMetric(hal, "CPU/Load", object), mUser(user), mKernel(kernel), mIdle(idle) {};
293 ~HostCpuLoad() { delete mUser; delete mKernel; delete mIdle; };
294
295 void init(ULONG period, ULONG length);
296
297 void collect();
298 const char *getUnit() { return "%"; };
299 ULONG getMinValue() { return 0; };
300 ULONG getMaxValue() { return PM_CPU_LOAD_MULTIPLIER; };
301 ULONG getScale() { return PM_CPU_LOAD_MULTIPLIER / 100; }
302
303 protected:
304 SubMetric *mUser;
305 SubMetric *mKernel;
306 SubMetric *mIdle;
307 };
308
309 class HostCpuLoadRaw : public HostCpuLoad
310 {
311 public:
312 HostCpuLoadRaw(CollectorHAL *hal, ComPtr<IUnknown> object, SubMetric *user, SubMetric *kernel, SubMetric *idle)
313 : HostCpuLoad(hal, object, user, kernel, idle), mUserPrev(0), mKernelPrev(0), mIdlePrev(0) {};
314
315 void preCollect(CollectorHints& hints, uint64_t iTick);
316 void collect();
317 private:
318 uint64_t mUserPrev;
319 uint64_t mKernelPrev;
320 uint64_t mIdlePrev;
321 };
322
323 class HostCpuMhz : public BaseMetric
324 {
325 public:
326 HostCpuMhz(CollectorHAL *hal, ComPtr<IUnknown> object, SubMetric *mhz)
327 : BaseMetric(hal, "CPU/MHz", object), mMHz(mhz) {};
328 ~HostCpuMhz() { delete mMHz; };
329
330 void init(ULONG period, ULONG length);
331 void preCollect(CollectorHints& /* hints */, uint64_t /* iTick */) {}
332 void collect();
333 const char *getUnit() { return "MHz"; };
334 ULONG getMinValue() { return 0; };
335 ULONG getMaxValue() { return INT32_MAX; };
336 ULONG getScale() { return 1; }
337 private:
338 SubMetric *mMHz;
339 };
340
341 class HostRamUsage : public BaseMetric
342 {
343 public:
344 HostRamUsage(CollectorHAL *hal, ComPtr<IUnknown> object, SubMetric *total, SubMetric *used, SubMetric *available, SubMetric *allocVMM, SubMetric *freeVMM, SubMetric *balloonVMM, SubMetric *sharedVMM)
345 : BaseMetric(hal, "RAM/Usage", object), mTotal(total), mUsed(used), mAvailable(available), mAllocVMM(allocVMM), mFreeVMM(freeVMM), mBalloonVMM(balloonVMM), mSharedVMM(sharedVMM) {};
346 ~HostRamUsage() { delete mTotal; delete mUsed; delete mAvailable; delete mAllocVMM; delete mFreeVMM; delete mBalloonVMM; delete mSharedVMM; };
347
348 void init(ULONG period, ULONG length);
349 void preCollect(CollectorHints& hints, uint64_t iTick);
350 void collect();
351 const char *getUnit() { return "kB"; };
352 ULONG getMinValue() { return 0; };
353 ULONG getMaxValue() { return INT32_MAX; };
354 ULONG getScale() { return 1; }
355 private:
356 SubMetric *mTotal;
357 SubMetric *mUsed;
358 SubMetric *mAvailable;
359 SubMetric *mAllocVMM;
360 SubMetric *mFreeVMM;
361 SubMetric *mBalloonVMM;
362 SubMetric *mSharedVMM;
363 };
364
365 class MachineCpuLoad : public BaseMetric
366 {
367 public:
368 MachineCpuLoad(CollectorHAL *hal, ComPtr<IUnknown> object, RTPROCESS process, SubMetric *user, SubMetric *kernel)
369 : BaseMetric(hal, "CPU/Load", object), mProcess(process), mUser(user), mKernel(kernel) {};
370 ~MachineCpuLoad() { delete mUser; delete mKernel; };
371
372 void init(ULONG period, ULONG length);
373 void collect();
374 const char *getUnit() { return "%"; };
375 ULONG getMinValue() { return 0; };
376 ULONG getMaxValue() { return PM_CPU_LOAD_MULTIPLIER; };
377 ULONG getScale() { return PM_CPU_LOAD_MULTIPLIER / 100; }
378 protected:
379 RTPROCESS mProcess;
380 SubMetric *mUser;
381 SubMetric *mKernel;
382 };
383
384 class MachineCpuLoadRaw : public MachineCpuLoad
385 {
386 public:
387 MachineCpuLoadRaw(CollectorHAL *hal, ComPtr<IUnknown> object, RTPROCESS process, SubMetric *user, SubMetric *kernel)
388 : MachineCpuLoad(hal, object, process, user, kernel), mHostTotalPrev(0), mProcessUserPrev(0), mProcessKernelPrev(0) {};
389
390 void preCollect(CollectorHints& hints, uint64_t iTick);
391 void collect();
392 private:
393 uint64_t mHostTotalPrev;
394 uint64_t mProcessUserPrev;
395 uint64_t mProcessKernelPrev;
396 };
397
398 class MachineRamUsage : public BaseMetric
399 {
400 public:
401 MachineRamUsage(CollectorHAL *hal, ComPtr<IUnknown> object, RTPROCESS process, SubMetric *used)
402 : BaseMetric(hal, "RAM/Usage", object), mProcess(process), mUsed(used) {};
403 ~MachineRamUsage() { delete mUsed; };
404
405 void init(ULONG period, ULONG length);
406 void preCollect(CollectorHints& hints, uint64_t iTick);
407 void collect();
408 const char *getUnit() { return "kB"; };
409 ULONG getMinValue() { return 0; };
410 ULONG getMaxValue() { return INT32_MAX; };
411 ULONG getScale() { return 1; }
412 private:
413 RTPROCESS mProcess;
414 SubMetric *mUsed;
415 };
416
417
418 class GuestCpuLoad : public BaseMetric
419 {
420 public:
421 GuestCpuLoad(CollectorGuestHAL *hal, ComPtr<IUnknown> object, SubMetric *user, SubMetric *kernel, SubMetric *idle)
422 : BaseMetric(hal, "CPU/Load", object), mUser(user), mKernel(kernel), mIdle(idle), mGuestHAL(hal) {};
423 ~GuestCpuLoad() { delete mUser; delete mKernel; delete mIdle; };
424
425 void init(ULONG period, ULONG length);
426 void preCollect(CollectorHints& hints, uint64_t iTick);
427 void collect();
428 const char *getUnit() { return "%"; };
429 ULONG getMinValue() { return 0; };
430 ULONG getMaxValue() { return PM_CPU_LOAD_MULTIPLIER; };
431 ULONG getScale() { return PM_CPU_LOAD_MULTIPLIER / 100; }
432 protected:
433 SubMetric *mUser;
434 SubMetric *mKernel;
435 SubMetric *mIdle;
436 CollectorGuestHAL *mGuestHAL;
437 };
438
439 class GuestRamUsage : public BaseMetric
440 {
441 public:
442 GuestRamUsage(CollectorGuestHAL *hal, ComPtr<IUnknown> object, SubMetric *total, SubMetric *free, SubMetric *balloon, SubMetric *shared, SubMetric *cache, SubMetric *pagedtotal)
443 : BaseMetric(hal, "RAM/Usage", object), mTotal(total), mFree(free), mBallooned(balloon), mCache(cache), mPagedTotal(pagedtotal), mShared(shared), mGuestHAL(hal) {};
444 ~GuestRamUsage() { delete mTotal; delete mFree; delete mBallooned; delete mShared; delete mCache; delete mPagedTotal; };
445
446 void init(ULONG period, ULONG length);
447 void preCollect(CollectorHints& hints, uint64_t iTick);
448 void collect();
449 const char *getUnit() { return "kB"; };
450 ULONG getMinValue() { return 0; };
451 ULONG getMaxValue() { return INT32_MAX; };
452 ULONG getScale() { return 1; }
453 private:
454 SubMetric *mTotal, *mFree, *mBallooned, *mCache, *mPagedTotal, *mShared;
455 CollectorGuestHAL *mGuestHAL;
456 };
457
458 /* Aggregate Functions **************************************************/
459 class Aggregate
460 {
461 public:
462 virtual ULONG compute(ULONG *data, ULONG length) = 0;
463 virtual const char *getName() = 0;
464 };
465
466 class AggregateAvg : public Aggregate
467 {
468 public:
469 virtual ULONG compute(ULONG *data, ULONG length);
470 virtual const char *getName();
471 };
472
473 class AggregateMin : public Aggregate
474 {
475 public:
476 virtual ULONG compute(ULONG *data, ULONG length);
477 virtual const char *getName();
478 };
479
480 class AggregateMax : public Aggregate
481 {
482 public:
483 virtual ULONG compute(ULONG *data, ULONG length);
484 virtual const char *getName();
485 };
486
487 /* Metric Class *********************************************************/
488 class Metric
489 {
490 public:
491 Metric(BaseMetric *baseMetric, SubMetric *subMetric, Aggregate *aggregate) :
492 mName(subMetric->getName()), mBaseMetric(baseMetric), mSubMetric(subMetric), mAggregate(aggregate)
493 {
494 if (mAggregate)
495 {
496 mName.append(":");
497 mName.append(mAggregate->getName());
498 }
499 }
500
501 ~Metric()
502 {
503 delete mAggregate;
504 }
505 bool associatedWith(ComPtr<IUnknown> object) { return getObject() == object; };
506
507 const char *getName() { return mName.c_str(); };
508 ComPtr<IUnknown> getObject() { return mBaseMetric->getObject(); };
509 const char *getDescription()
510 { return mAggregate ? "" : mSubMetric->getDescription(); };
511 const char *getUnit() { return mBaseMetric->getUnit(); };
512 ULONG getMinValue() { return mBaseMetric->getMinValue(); };
513 ULONG getMaxValue() { return mBaseMetric->getMaxValue(); };
514 ULONG getPeriod() { return mBaseMetric->getPeriod(); };
515 ULONG getLength()
516 { return mAggregate ? 1 : mBaseMetric->getLength(); };
517 ULONG getScale() { return mBaseMetric->getScale(); }
518 void query(ULONG **data, ULONG *count, ULONG *sequenceNumber);
519
520 private:
521 iprt::MiniString mName;
522 BaseMetric *mBaseMetric;
523 SubMetric *mSubMetric;
524 Aggregate *mAggregate;
525 };
526
527 /* Filter Class *********************************************************/
528
529 class Filter
530 {
531 public:
532 Filter(ComSafeArrayIn(IN_BSTR, metricNames),
533 ComSafeArrayIn(IUnknown * , objects));
534 static bool patternMatch(const char *pszPat, const char *pszName,
535 bool fSeenColon = false);
536 bool match(const ComPtr<IUnknown> object, const iprt::MiniString &name) const;
537 private:
538 void init(ComSafeArrayIn(IN_BSTR, metricNames),
539 ComSafeArrayIn(IUnknown * , objects));
540
541 typedef std::pair<const ComPtr<IUnknown>, const iprt::MiniString> FilterElement;
542 typedef std::list<FilterElement> ElementList;
543
544 ElementList mElements;
545
546 void processMetricList(const com::Utf8Str &name, const ComPtr<IUnknown> object);
547 };
548}
549#endif /* ___performance_h */
550/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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