VirtualBox

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

Last change on this file since 32736 was 30847, checked in by vboxsync, 14 years ago

Main/Machine+Performance+VirtualBox: move deleting the performance metrics registration of a VM out of SessionMachine::uninit, where it was a risk of hangs due to callbacks. Doing it a little later won't hurt anyone. Additionally also make sure the total VMM memory stats are zeroed when the last VM has disabled its collector.

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