VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.5 KB
Line 
1/* $Id: Performance.h 28800 2010-04-27 08:22:32Z 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)
162 {
163 mMemAllocVMM = memAlloc;
164 mMemFreeVMM = memFree;
165 mMemBalloonedVMM = memBallooned;
166 return S_OK;
167 }
168
169 virtual void getMemHypervisorStats(ULONG *pMemAlloc, ULONG *pMemFree, ULONG *pMemBallooned)
170 {
171 *pMemAlloc = mMemAllocVMM;
172 *pMemFree = mMemFreeVMM;
173 *pMemBallooned = mMemBalloonedVMM;
174 }
175
176 private:
177 ULONG mMemAllocVMM;
178 ULONG mMemFreeVMM;
179 ULONG mMemBalloonedVMM;
180 };
181
182 class CollectorGuestHAL : public CollectorHAL
183 {
184 public:
185 CollectorGuestHAL(Machine *machine, CollectorHAL *hostHAL) : CollectorHAL(), cEnabled(0), mMachine(machine), mConsole(NULL), mGuest(NULL),
186 mLastTick(0), mHostHAL(hostHAL), mCpuUser(0), mCpuKernel(0), mCpuIdle(0), mMemTotal(0), mMemFree(0),
187 mMemBalloon(0), mMemCache(0), mPageTotal(0) {};
188 ~CollectorGuestHAL();
189
190 virtual int preCollect(const CollectorHints& hints, uint64_t iTick);
191
192 /** Enable metrics collecting (if applicable) */
193 virtual int enable();
194 /** Disable metrics collecting (if applicable) */
195 virtual int disable();
196
197 /** Return guest cpu absolute load values (0-100). */
198 void getGuestCpuLoad(ULONG *pulCpuUser, ULONG *pulCpuKernel, ULONG *pulCpuIdle)
199 {
200 *pulCpuUser = mCpuUser;
201 *pulCpuKernel = mCpuKernel;
202 *pulCpuIdle = mCpuIdle;
203 }
204
205 /** Return guest memory information in KB. */
206 void getGuestMemLoad(ULONG *pulMemTotal, ULONG *pulMemFree, ULONG *pulMemBalloon, ULONG *pulMemCache, ULONG *pulPageTotal)
207 {
208 *pulMemTotal = mMemTotal;
209 *pulMemFree = mMemFree;
210 *pulMemBalloon = mMemBalloon;
211 *pulMemCache = mMemCache;
212 *pulPageTotal = mPageTotal;
213 }
214
215
216 protected:
217 uint32_t cEnabled;
218 Machine *mMachine;
219 ComPtr<IConsole> mConsole;
220 ComPtr<IGuest> mGuest;
221 uint64_t mLastTick;
222
223 CollectorHAL *mHostHAL;
224
225 ULONG mCpuUser;
226 ULONG mCpuKernel;
227 ULONG mCpuIdle;
228 ULONG mMemTotal;
229 ULONG mMemFree;
230 ULONG mMemBalloon;
231 ULONG mMemCache;
232 ULONG mPageTotal;
233 };
234
235 extern CollectorHAL *createHAL();
236
237 /* Base Metrics *********************************************************/
238 class BaseMetric
239 {
240 public:
241 BaseMetric(CollectorHAL *hal, const char *name, ComPtr<IUnknown> object)
242 : mPeriod(0), mLength(0), mHAL(hal), mName(name), mObject(object), mLastSampleTaken(0), mEnabled(false) {};
243 virtual ~BaseMetric() {};
244
245 virtual void init(ULONG period, ULONG length) = 0;
246 virtual void preCollect(CollectorHints& hints, uint64_t iTick) = 0;
247 virtual void collect() = 0;
248 virtual const char *getUnit() = 0;
249 virtual ULONG getMinValue() = 0;
250 virtual ULONG getMaxValue() = 0;
251 virtual ULONG getScale() = 0;
252
253 bool collectorBeat(uint64_t nowAt);
254
255 void enable()
256 {
257 mEnabled = true;
258 mHAL->enable();
259 };
260 void disable()
261 {
262 mHAL->disable();
263 mEnabled = false;
264 };
265
266 bool isEnabled() { return mEnabled; };
267 ULONG getPeriod() { return mPeriod; };
268 ULONG getLength() { return mLength; };
269 const char *getName() { return mName; };
270 ComPtr<IUnknown> getObject() { return mObject; };
271 bool associatedWith(ComPtr<IUnknown> object) { return mObject == object; };
272
273 protected:
274 ULONG mPeriod;
275 ULONG mLength;
276 CollectorHAL *mHAL;
277 const char *mName;
278 ComPtr<IUnknown> mObject;
279 uint64_t mLastSampleTaken;
280 bool mEnabled;
281 };
282
283 class HostCpuLoad : public BaseMetric
284 {
285 public:
286 HostCpuLoad(CollectorHAL *hal, ComPtr<IUnknown> object, SubMetric *user, SubMetric *kernel, SubMetric *idle)
287 : BaseMetric(hal, "CPU/Load", object), mUser(user), mKernel(kernel), mIdle(idle) {};
288 ~HostCpuLoad() { delete mUser; delete mKernel; delete mIdle; };
289
290 void init(ULONG period, ULONG length);
291
292 void collect();
293 const char *getUnit() { return "%"; };
294 ULONG getMinValue() { return 0; };
295 ULONG getMaxValue() { return PM_CPU_LOAD_MULTIPLIER; };
296 ULONG getScale() { return PM_CPU_LOAD_MULTIPLIER / 100; }
297
298 protected:
299 SubMetric *mUser;
300 SubMetric *mKernel;
301 SubMetric *mIdle;
302 };
303
304 class HostCpuLoadRaw : public HostCpuLoad
305 {
306 public:
307 HostCpuLoadRaw(CollectorHAL *hal, ComPtr<IUnknown> object, SubMetric *user, SubMetric *kernel, SubMetric *idle)
308 : HostCpuLoad(hal, object, user, kernel, idle), mUserPrev(0), mKernelPrev(0), mIdlePrev(0) {};
309
310 void preCollect(CollectorHints& hints, uint64_t iTick);
311 void collect();
312 private:
313 uint64_t mUserPrev;
314 uint64_t mKernelPrev;
315 uint64_t mIdlePrev;
316 };
317
318 class HostCpuMhz : public BaseMetric
319 {
320 public:
321 HostCpuMhz(CollectorHAL *hal, ComPtr<IUnknown> object, SubMetric *mhz)
322 : BaseMetric(hal, "CPU/MHz", object), mMHz(mhz) {};
323 ~HostCpuMhz() { delete mMHz; };
324
325 void init(ULONG period, ULONG length);
326 void preCollect(CollectorHints& /* hints */, uint64_t /* iTick */) {}
327 void collect();
328 const char *getUnit() { return "MHz"; };
329 ULONG getMinValue() { return 0; };
330 ULONG getMaxValue() { return INT32_MAX; };
331 ULONG getScale() { return 1; }
332 private:
333 SubMetric *mMHz;
334 };
335
336 class HostRamUsage : public BaseMetric
337 {
338 public:
339 HostRamUsage(CollectorHAL *hal, ComPtr<IUnknown> object, SubMetric *total, SubMetric *used, SubMetric *available, SubMetric *allocVMM, SubMetric *freeVMM, SubMetric *balloonVMM)
340 : BaseMetric(hal, "RAM/Usage", object), mTotal(total), mUsed(used), mAvailable(available), mAllocVMM(allocVMM), mFreeVMM(freeVMM), mBalloonVMM(balloonVMM) {};
341 ~HostRamUsage() { delete mTotal; delete mUsed; delete mAvailable; delete mAllocVMM; delete mFreeVMM; delete mBalloonVMM; };
342
343 void init(ULONG period, ULONG length);
344 void preCollect(CollectorHints& hints, uint64_t iTick);
345 void collect();
346 const char *getUnit() { return "kB"; };
347 ULONG getMinValue() { return 0; };
348 ULONG getMaxValue() { return INT32_MAX; };
349 ULONG getScale() { return 1; }
350 private:
351 SubMetric *mTotal;
352 SubMetric *mUsed;
353 SubMetric *mAvailable;
354 SubMetric *mAllocVMM;
355 SubMetric *mFreeVMM;
356 SubMetric *mBalloonVMM;
357 };
358
359 class MachineCpuLoad : public BaseMetric
360 {
361 public:
362 MachineCpuLoad(CollectorHAL *hal, ComPtr<IUnknown> object, RTPROCESS process, SubMetric *user, SubMetric *kernel)
363 : BaseMetric(hal, "CPU/Load", object), mProcess(process), mUser(user), mKernel(kernel) {};
364 ~MachineCpuLoad() { delete mUser; delete mKernel; };
365
366 void init(ULONG period, ULONG length);
367 void collect();
368 const char *getUnit() { return "%"; };
369 ULONG getMinValue() { return 0; };
370 ULONG getMaxValue() { return PM_CPU_LOAD_MULTIPLIER; };
371 ULONG getScale() { return PM_CPU_LOAD_MULTIPLIER / 100; }
372 protected:
373 RTPROCESS mProcess;
374 SubMetric *mUser;
375 SubMetric *mKernel;
376 };
377
378 class MachineCpuLoadRaw : public MachineCpuLoad
379 {
380 public:
381 MachineCpuLoadRaw(CollectorHAL *hal, ComPtr<IUnknown> object, RTPROCESS process, SubMetric *user, SubMetric *kernel)
382 : MachineCpuLoad(hal, object, process, user, kernel), mHostTotalPrev(0), mProcessUserPrev(0), mProcessKernelPrev(0) {};
383
384 void preCollect(CollectorHints& hints, uint64_t iTick);
385 void collect();
386 private:
387 uint64_t mHostTotalPrev;
388 uint64_t mProcessUserPrev;
389 uint64_t mProcessKernelPrev;
390 };
391
392 class MachineRamUsage : public BaseMetric
393 {
394 public:
395 MachineRamUsage(CollectorHAL *hal, ComPtr<IUnknown> object, RTPROCESS process, SubMetric *used)
396 : BaseMetric(hal, "RAM/Usage", object), mProcess(process), mUsed(used) {};
397 ~MachineRamUsage() { delete mUsed; };
398
399 void init(ULONG period, ULONG length);
400 void preCollect(CollectorHints& hints, uint64_t iTick);
401 void collect();
402 const char *getUnit() { return "kB"; };
403 ULONG getMinValue() { return 0; };
404 ULONG getMaxValue() { return INT32_MAX; };
405 ULONG getScale() { return 1; }
406 private:
407 RTPROCESS mProcess;
408 SubMetric *mUsed;
409 };
410
411
412 class GuestCpuLoad : public BaseMetric
413 {
414 public:
415 GuestCpuLoad(CollectorGuestHAL *hal, ComPtr<IUnknown> object, SubMetric *user, SubMetric *kernel, SubMetric *idle)
416 : BaseMetric(hal, "CPU/Load", object), mUser(user), mKernel(kernel), mIdle(idle), mGuestHAL(hal) {};
417 ~GuestCpuLoad() { delete mUser; delete mKernel; delete mIdle; };
418
419 void init(ULONG period, ULONG length);
420 void preCollect(CollectorHints& hints, uint64_t iTick);
421 void collect();
422 const char *getUnit() { return "%"; };
423 ULONG getMinValue() { return 0; };
424 ULONG getMaxValue() { return PM_CPU_LOAD_MULTIPLIER; };
425 ULONG getScale() { return PM_CPU_LOAD_MULTIPLIER / 100; }
426 protected:
427 SubMetric *mUser;
428 SubMetric *mKernel;
429 SubMetric *mIdle;
430 CollectorGuestHAL *mGuestHAL;
431 };
432
433 class GuestRamUsage : public BaseMetric
434 {
435 public:
436 GuestRamUsage(CollectorGuestHAL *hal, ComPtr<IUnknown> object, SubMetric *total, SubMetric *free, SubMetric *balloon, SubMetric *cache, SubMetric *pagedtotal)
437 : BaseMetric(hal, "RAM/Usage", object), mTotal(total), mFree(free), mBallooned(balloon), mCache(cache), mPagedTotal(pagedtotal), mGuestHAL(hal) {};
438 ~GuestRamUsage() { delete mTotal; delete mFree; delete mBallooned; delete mCache; delete mPagedTotal; };
439
440 void init(ULONG period, ULONG length);
441 void preCollect(CollectorHints& hints, uint64_t iTick);
442 void collect();
443 const char *getUnit() { return "kB"; };
444 ULONG getMinValue() { return 0; };
445 ULONG getMaxValue() { return INT32_MAX; };
446 ULONG getScale() { return 1; }
447 private:
448 SubMetric *mTotal, *mFree, *mBallooned, *mCache, *mPagedTotal;
449 CollectorGuestHAL *mGuestHAL;
450 };
451
452 /* Aggregate Functions **************************************************/
453 class Aggregate
454 {
455 public:
456 virtual ULONG compute(ULONG *data, ULONG length) = 0;
457 virtual const char *getName() = 0;
458 };
459
460 class AggregateAvg : public Aggregate
461 {
462 public:
463 virtual ULONG compute(ULONG *data, ULONG length);
464 virtual const char *getName();
465 };
466
467 class AggregateMin : public Aggregate
468 {
469 public:
470 virtual ULONG compute(ULONG *data, ULONG length);
471 virtual const char *getName();
472 };
473
474 class AggregateMax : public Aggregate
475 {
476 public:
477 virtual ULONG compute(ULONG *data, ULONG length);
478 virtual const char *getName();
479 };
480
481 /* Metric Class *********************************************************/
482 class Metric
483 {
484 public:
485 Metric(BaseMetric *baseMetric, SubMetric *subMetric, Aggregate *aggregate) :
486 mName(subMetric->getName()), mBaseMetric(baseMetric), mSubMetric(subMetric), mAggregate(aggregate)
487 {
488 if (mAggregate)
489 {
490 mName.append(":");
491 mName.append(mAggregate->getName());
492 }
493 }
494
495 ~Metric()
496 {
497 delete mAggregate;
498 }
499 bool associatedWith(ComPtr<IUnknown> object) { return getObject() == object; };
500
501 const char *getName() { return mName.c_str(); };
502 ComPtr<IUnknown> getObject() { return mBaseMetric->getObject(); };
503 const char *getDescription()
504 { return mAggregate ? "" : mSubMetric->getDescription(); };
505 const char *getUnit() { return mBaseMetric->getUnit(); };
506 ULONG getMinValue() { return mBaseMetric->getMinValue(); };
507 ULONG getMaxValue() { return mBaseMetric->getMaxValue(); };
508 ULONG getPeriod() { return mBaseMetric->getPeriod(); };
509 ULONG getLength()
510 { return mAggregate ? 1 : mBaseMetric->getLength(); };
511 ULONG getScale() { return mBaseMetric->getScale(); }
512 void query(ULONG **data, ULONG *count, ULONG *sequenceNumber);
513
514 private:
515 iprt::MiniString mName;
516 BaseMetric *mBaseMetric;
517 SubMetric *mSubMetric;
518 Aggregate *mAggregate;
519 };
520
521 /* Filter Class *********************************************************/
522
523 class Filter
524 {
525 public:
526 Filter(ComSafeArrayIn(IN_BSTR, metricNames),
527 ComSafeArrayIn(IUnknown * , objects));
528 static bool patternMatch(const char *pszPat, const char *pszName,
529 bool fSeenColon = false);
530 bool match(const ComPtr<IUnknown> object, const iprt::MiniString &name) const;
531 private:
532 void init(ComSafeArrayIn(IN_BSTR, metricNames),
533 ComSafeArrayIn(IUnknown * , objects));
534
535 typedef std::pair<const ComPtr<IUnknown>, const iprt::MiniString> FilterElement;
536 typedef std::list<FilterElement> ElementList;
537
538 ElementList mElements;
539
540 void processMetricList(const com::Utf8Str &name, const ComPtr<IUnknown> object);
541 };
542}
543#endif /* ___performance_h */
544/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette