VirtualBox

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

最後變更 在這個檔案從44529是 44529,由 vboxsync 提交於 12 年 前

header (C) fixes

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 33.7 KB
 
1/* $Id: Performance.h 44529 2013-02-04 15:54:15Z vboxsync $ */
2/** @file
3 * VirtualBox Main - Performance Classes declaration.
4 */
5
6/*
7 * Copyright (C) 2008-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17#ifndef ___performance_h
18#define ___performance_h
19
20#include <VBox/com/defs.h>
21#include <VBox/com/ptr.h>
22#include <VBox/com/string.h>
23#include <VBox/com/VirtualBox.h>
24
25#include <iprt/types.h>
26#include <iprt/err.h>
27#include <iprt/cpp/lock.h>
28
29#include <algorithm>
30#include <functional> /* For std::fun_ptr in testcase */
31#include <list>
32#include <vector>
33#include <queue>
34
35#include "MediumImpl.h"
36
37/* Forward decl. */
38class Machine;
39
40namespace pm
41{
42 /* CPU load is measured in 1/1000 of per cent. */
43 const uint64_t PM_CPU_LOAD_MULTIPLIER = UINT64_C(100000);
44 /* Network load is measured in 1/1000 of per cent. */
45 const uint64_t PM_NETWORK_LOAD_MULTIPLIER = UINT64_C(100000);
46 /* Disk load is measured in 1/1000 of per cent. */
47 const uint64_t PM_DISK_LOAD_MULTIPLIER = UINT64_C(100000);
48 /* Sampler precision in milliseconds. */
49 const uint64_t PM_SAMPLER_PRECISION_MS = 50;
50
51 /* Sub Metrics **********************************************************/
52 class CircularBuffer
53 {
54 public:
55 CircularBuffer() : mData(0), mLength(0), mEnd(0), mWrapped(false) {};
56 ~CircularBuffer() { if (mData) RTMemFree(mData); };
57 void init(ULONG length);
58 ULONG length();
59 ULONG getSequenceNumber() { return mSequenceNumber; }
60 void put(ULONG value);
61 void copyTo(ULONG *data);
62 private:
63 ULONG *mData;
64 ULONG mLength;
65 ULONG mEnd;
66 ULONG mSequenceNumber;
67 bool mWrapped;
68 };
69
70 class SubMetric : public CircularBuffer
71 {
72 public:
73 SubMetric(com::Utf8Str name, const char *description)
74 : mName(name), mDescription(description) {};
75 void query(ULONG *data);
76 const char *getName() { return mName.c_str(); };
77 const char *getDescription() { return mDescription; };
78 private:
79 const com::Utf8Str mName;
80 const char *mDescription;
81 };
82
83
84 enum {
85 COLLECT_NONE = 0x0,
86 COLLECT_CPU_LOAD = 0x1,
87 COLLECT_RAM_USAGE = 0x2,
88 COLLECT_GUEST_STATS = 0x4
89 };
90 typedef int HintFlags;
91 typedef std::pair<RTPROCESS, HintFlags> ProcessFlagsPair;
92
93 class CollectorHints
94 {
95 public:
96 typedef std::list<ProcessFlagsPair> ProcessList;
97
98 CollectorHints() : mHostFlags(COLLECT_NONE) {}
99 void collectHostCpuLoad()
100 { mHostFlags |= COLLECT_CPU_LOAD; }
101 void collectHostRamUsage()
102 { mHostFlags |= COLLECT_RAM_USAGE; }
103 void collectHostRamVmm()
104 { mHostFlags |= COLLECT_GUEST_STATS; }
105 void collectProcessCpuLoad(RTPROCESS process)
106 { findProcess(process).second |= COLLECT_CPU_LOAD; }
107 void collectProcessRamUsage(RTPROCESS process)
108 { findProcess(process).second |= COLLECT_RAM_USAGE; }
109 void collectGuestStats(RTPROCESS process)
110 { findProcess(process).second |= COLLECT_GUEST_STATS; }
111 bool isHostCpuLoadCollected() const
112 { return (mHostFlags & COLLECT_CPU_LOAD) != 0; }
113 bool isHostRamUsageCollected() const
114 { return (mHostFlags & COLLECT_RAM_USAGE) != 0; }
115 bool isHostRamVmmCollected() const
116 { return (mHostFlags & COLLECT_GUEST_STATS) != 0; }
117 bool isProcessCpuLoadCollected(RTPROCESS process)
118 { return (findProcess(process).second & COLLECT_CPU_LOAD) != 0; }
119 bool isProcessRamUsageCollected(RTPROCESS process)
120 { return (findProcess(process).second & COLLECT_RAM_USAGE) != 0; }
121 bool isGuestStatsCollected(RTPROCESS process)
122 { return (findProcess(process).second & COLLECT_GUEST_STATS) != 0; }
123 void getProcesses(std::vector<RTPROCESS>& processes) const
124 {
125 processes.clear();
126 processes.reserve(mProcesses.size());
127 for (ProcessList::const_iterator it = mProcesses.begin(); it != mProcesses.end(); it++)
128 processes.push_back(it->first);
129 }
130 const ProcessList& getProcessFlags() const
131 {
132 return mProcesses;
133 }
134 private:
135 HintFlags mHostFlags;
136 ProcessList mProcesses;
137
138 ProcessFlagsPair& findProcess(RTPROCESS process)
139 {
140 ProcessList::iterator it;
141 for (it = mProcesses.begin(); it != mProcesses.end(); it++)
142 if (it->first == process)
143 return *it;
144
145 /* Not found -- add new */
146 mProcesses.push_back(ProcessFlagsPair(process, COLLECT_NONE));
147 return mProcesses.back();
148 }
149 };
150
151 /* Guest Collector Classes *********************************/
152 /*
153 * WARNING! The bits in the following masks must correspond to parameters
154 * of CollectorGuest::updateStats().
155 */
156 typedef enum
157 {
158 VMSTATMASK_NONE = 0x00000000,
159 VMSTATMASK_GUEST_CPUUSER = 0x00000001,
160 VMSTATMASK_GUEST_CPUKERNEL = 0x00000002,
161 VMSTATMASK_GUEST_CPUIDLE = 0x00000004,
162 VMSTATMASK_GUEST_MEMTOTAL = 0x00000008,
163 VMSTATMASK_GUEST_MEMFREE = 0x00000010,
164 VMSTATMASK_GUEST_MEMBALLOON = 0x00000020,
165 VMSTATMASK_GUEST_MEMSHARED = 0x00000040,
166 VMSTATMASK_GUEST_MEMCACHE = 0x00000080,
167 VMSTATMASK_GUEST_PAGETOTAL = 0x00000100,
168 VMSTATMASK_VMM_ALLOC = 0x00010000,
169 VMSTATMASK_VMM_FREE = 0x00020000,
170 VMSTATMASK_VMM_BALOON = 0x00040000,
171 VMSTATMASK_VMM_SHARED = 0x00080000,
172 VMSTATMASK_NET_RX = 0x01000000,
173 VMSTATMASK_NET_TX = 0x02000000
174 } VMSTATMASK;
175
176 const ULONG VMSTATS_GUEST_CPULOAD =
177 VMSTATMASK_GUEST_CPUUSER | VMSTATMASK_GUEST_CPUKERNEL |
178 VMSTATMASK_GUEST_CPUIDLE;
179 const ULONG VMSTATS_GUEST_RAMUSAGE =
180 VMSTATMASK_GUEST_MEMTOTAL | VMSTATMASK_GUEST_MEMFREE |
181 VMSTATMASK_GUEST_MEMBALLOON | VMSTATMASK_GUEST_MEMSHARED |
182 VMSTATMASK_GUEST_MEMCACHE | VMSTATMASK_GUEST_PAGETOTAL;
183 const ULONG VMSTATS_VMM_RAM =
184 VMSTATMASK_VMM_ALLOC | VMSTATMASK_VMM_FREE|
185 VMSTATMASK_VMM_BALOON | VMSTATMASK_VMM_SHARED;
186 const ULONG VMSTATS_NET_RATE =
187 VMSTATMASK_NET_RX | VMSTATMASK_NET_TX;
188 const ULONG VMSTATS_ALL =
189 VMSTATS_GUEST_CPULOAD | VMSTATS_GUEST_RAMUSAGE |
190 VMSTATS_VMM_RAM | VMSTATS_NET_RATE;
191 class CollectorGuest;
192
193 class CollectorGuestRequest
194 {
195 public:
196 CollectorGuestRequest()
197 : mCGuest(0) {};
198 virtual ~CollectorGuestRequest() {};
199 void setGuest(CollectorGuest *aGuest) { mCGuest = aGuest; };
200 CollectorGuest *getGuest() { return mCGuest; };
201 virtual int execute() = 0;
202
203 virtual void debugPrint(void *aObject, const char *aFunction, const char *aText) = 0;
204 protected:
205 CollectorGuest *mCGuest;
206 const char *mDebugName;
207 };
208
209 class CGRQEnable : public CollectorGuestRequest
210 {
211 public:
212 CGRQEnable(ULONG aMask)
213 : mMask(aMask) {};
214 int execute();
215
216 void debugPrint(void *aObject, const char *aFunction, const char *aText);
217 private:
218 ULONG mMask;
219 };
220
221 class CGRQDisable : public CollectorGuestRequest
222 {
223 public:
224 CGRQDisable(ULONG aMask)
225 : mMask(aMask) {};
226 int execute();
227
228 void debugPrint(void *aObject, const char *aFunction, const char *aText);
229 private:
230 ULONG mMask;
231 };
232
233 class CGRQAbort : public CollectorGuestRequest
234 {
235 public:
236 CGRQAbort() {};
237 int execute();
238
239 void debugPrint(void *aObject, const char *aFunction, const char *aText);
240 };
241
242 class CollectorGuestQueue
243 {
244 public:
245 CollectorGuestQueue();
246 ~CollectorGuestQueue();
247 void push(CollectorGuestRequest* rq);
248 CollectorGuestRequest* pop();
249 private:
250 RTCLockMtx mLockMtx;
251 RTSEMEVENT mEvent;
252 std::queue<CollectorGuestRequest*> mQueue;
253 };
254
255 class CollectorGuestManager;
256
257 class CollectorGuest
258 {
259 public:
260 CollectorGuest(Machine *machine, RTPROCESS process);
261 ~CollectorGuest();
262
263 void setManager(CollectorGuestManager *aManager)
264 { mManager = aManager; };
265 bool isUnregistered() { return mUnregistered; };
266 bool isEnabled() { return mEnabled != 0; };
267 bool isValid(ULONG mask) { return (mValid & mask) == mask; };
268 void invalidate(ULONG mask) { mValid &= ~mask; };
269 void unregister() { mUnregistered = true; };
270 void updateStats(ULONG aValidStats, ULONG aCpuUser,
271 ULONG aCpuKernel, ULONG aCpuIdle,
272 ULONG aMemTotal, ULONG aMemFree,
273 ULONG aMemBalloon, ULONG aMemShared,
274 ULONG aMemCache, ULONG aPageTotal,
275 ULONG aAllocVMM, ULONG aFreeVMM,
276 ULONG aBalloonedVMM, ULONG aSharedVMM,
277 ULONG aVmNetRx, ULONG aVmNetTx);
278 int enable(ULONG mask);
279 int disable(ULONG mask);
280
281 int enqueueRequest(CollectorGuestRequest *aRequest);
282 int enableInternal(ULONG mask);
283 int disableInternal(ULONG mask);
284
285 const com::Utf8Str& getVMName() const { return mMachineName; };
286
287 RTPROCESS getProcess() { return mProcess; };
288 ULONG getCpuUser() { return mCpuUser; };
289 ULONG getCpuKernel() { return mCpuKernel; };
290 ULONG getCpuIdle() { return mCpuIdle; };
291 ULONG getMemTotal() { return mMemTotal; };
292 ULONG getMemFree() { return mMemFree; };
293 ULONG getMemBalloon() { return mMemBalloon; };
294 ULONG getMemShared() { return mMemShared; };
295 ULONG getMemCache() { return mMemCache; };
296 ULONG getPageTotal() { return mPageTotal; };
297 ULONG getAllocVMM() { return mAllocVMM; };
298 ULONG getFreeVMM() { return mFreeVMM; };
299 ULONG getBalloonedVMM() { return mBalloonedVMM; };
300 ULONG getSharedVMM() { return mSharedVMM; };
301 ULONG getVmNetRx() { return mVmNetRx; };
302 ULONG getVmNetTx() { return mVmNetTx; };
303
304 private:
305 int enableVMMStats(bool mCollectVMMStats);
306
307 CollectorGuestManager *mManager;
308
309 bool mUnregistered;
310 ULONG mEnabled;
311 ULONG mValid;
312 Machine *mMachine;
313 com::Utf8Str mMachineName;
314 RTPROCESS mProcess;
315 ComPtr<IConsole> mConsole;
316 ComPtr<IGuest> mGuest;
317 ULONG mCpuUser;
318 ULONG mCpuKernel;
319 ULONG mCpuIdle;
320 ULONG mMemTotal;
321 ULONG mMemFree;
322 ULONG mMemBalloon;
323 ULONG mMemShared;
324 ULONG mMemCache;
325 ULONG mPageTotal;
326 ULONG mAllocVMM;
327 ULONG mFreeVMM;
328 ULONG mBalloonedVMM;
329 ULONG mSharedVMM;
330 ULONG mVmNetRx;
331 ULONG mVmNetTx;
332 };
333
334 typedef std::list<CollectorGuest*> CollectorGuestList;
335 class CollectorGuestManager
336 {
337 public:
338 CollectorGuestManager();
339 ~CollectorGuestManager();
340 void registerGuest(CollectorGuest* pGuest);
341 void unregisterGuest(CollectorGuest* pGuest);
342 CollectorGuest *getVMMStatsProvider() { return mVMMStatsProvider; };
343 void preCollect(CollectorHints& hints, uint64_t iTick);
344 void destroyUnregistered();
345 int enqueueRequest(CollectorGuestRequest *aRequest);
346
347 CollectorGuest *getBlockedGuest() { return mGuestBeingCalled; };
348
349 static DECLCALLBACK(int) requestProcessingThread(RTTHREAD aThread, void *pvUser);
350 private:
351 RTTHREAD mThread;
352 CollectorGuestList mGuests;
353 CollectorGuest *mVMMStatsProvider;
354 CollectorGuestQueue mQueue;
355 CollectorGuest *mGuestBeingCalled;
356 };
357
358 /* Collector Hardware Abstraction Layer *********************************/
359 typedef std::list<RTCString> DiskList;
360
361 class CollectorHAL
362 {
363 public:
364 CollectorHAL() {};
365 virtual ~CollectorHAL() { };
366 virtual int preCollect(const CollectorHints& /* hints */, uint64_t /* iTick */) { return VINF_SUCCESS; }
367 /** Returns averaged CPU usage in 1/1000th per cent across all host's CPUs. */
368 virtual int getHostCpuLoad(ULONG *user, ULONG *kernel, ULONG *idle);
369 /** Returns the average frequency in MHz across all host's CPUs. */
370 virtual int getHostCpuMHz(ULONG *mhz);
371 /** Returns the amount of physical memory in kilobytes. */
372 virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
373 /** Returns file system counters in megabytes. */
374 virtual int getHostFilesystemUsage(const char *name, ULONG *total, ULONG *used, ULONG *available);
375 /** Returns disk size in bytes. */
376 virtual int getHostDiskSize(const char *name, uint64_t *size);
377 /** Returns CPU usage in 1/1000th per cent by a particular process. */
378 virtual int getProcessCpuLoad(RTPROCESS process, ULONG *user, ULONG *kernel);
379 /** Returns the amount of memory used by a process in kilobytes. */
380 virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);
381
382 /** Returns CPU usage counters in platform-specific units. */
383 virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
384 /** Returns received and transmitted bytes. */
385 virtual int getRawHostNetworkLoad(const char *name, uint64_t *rx, uint64_t *tx);
386 /** Returns disk usage counters in platform-specific units. */
387 virtual int getRawHostDiskLoad(const char *name, uint64_t *disk_ms, uint64_t *total_ms);
388 /** Returns process' CPU usage counter in platform-specific units. */
389 virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
390
391 /** Returns the list of disks used by the specified file system. */
392 virtual int getDiskListByFs(const char *name, DiskList& list);
393 };
394
395 extern CollectorHAL *createHAL();
396
397 /* Base Metrics *********************************************************/
398 class BaseMetric
399 {
400 public:
401 BaseMetric(CollectorHAL *hal, const com::Utf8Str name, ComPtr<IUnknown> object)
402 : mPeriod(0), mLength(0), mHAL(hal), mName(name), mObject(object),
403 mLastSampleTaken(0), mEnabled(false), mUnregistered(false) {};
404 virtual ~BaseMetric() {};
405
406 virtual void init(ULONG period, ULONG length) = 0;
407 virtual void preCollect(CollectorHints& hints, uint64_t iTick) = 0;
408 virtual void collect() = 0;
409 virtual const char *getUnit() = 0;
410 virtual ULONG getMinValue() = 0;
411 virtual ULONG getMaxValue() = 0;
412 virtual ULONG getScale() = 0;
413
414 bool collectorBeat(uint64_t nowAt);
415
416 virtual int enable() { mEnabled = true; return S_OK; };
417 virtual int disable() { mEnabled = false; return S_OK; };
418 void unregister() { mUnregistered = true; };
419
420 bool isUnregistered() { return mUnregistered; };
421 bool isEnabled() { return mEnabled; };
422 ULONG getPeriod() { return mPeriod; };
423 ULONG getLength() { return mLength; };
424 const char *getName() { return mName.c_str(); };
425 ComPtr<IUnknown> getObject() { return mObject; };
426 bool associatedWith(ComPtr<IUnknown> object) { return mObject == object; };
427
428 protected:
429 ULONG mPeriod;
430 ULONG mLength;
431 CollectorHAL *mHAL;
432 const com::Utf8Str mName;
433 ComPtr<IUnknown> mObject;
434 uint64_t mLastSampleTaken;
435 bool mEnabled;
436 bool mUnregistered;
437 };
438
439 class BaseGuestMetric : public BaseMetric
440 {
441 public:
442 BaseGuestMetric(CollectorGuest *cguest, const char *name, ComPtr<IUnknown> object)
443 : BaseMetric(NULL, name, object), mCGuest(cguest) {};
444 protected:
445 CollectorGuest *mCGuest;
446 };
447
448 class HostCpuLoad : public BaseMetric
449 {
450 public:
451 HostCpuLoad(CollectorHAL *hal, ComPtr<IUnknown> object, SubMetric *user, SubMetric *kernel, SubMetric *idle)
452 : BaseMetric(hal, "CPU/Load", object), mUser(user), mKernel(kernel), mIdle(idle) {};
453 ~HostCpuLoad() { delete mUser; delete mKernel; delete mIdle; };
454
455 void init(ULONG period, ULONG length);
456
457 void collect();
458 const char *getUnit() { return "%"; };
459 ULONG getMinValue() { return 0; };
460 ULONG getMaxValue() { return PM_CPU_LOAD_MULTIPLIER; };
461 ULONG getScale() { return PM_CPU_LOAD_MULTIPLIER / 100; }
462
463 protected:
464 SubMetric *mUser;
465 SubMetric *mKernel;
466 SubMetric *mIdle;
467 };
468
469 class HostCpuLoadRaw : public HostCpuLoad
470 {
471 public:
472 HostCpuLoadRaw(CollectorHAL *hal, ComPtr<IUnknown> object, SubMetric *user, SubMetric *kernel, SubMetric *idle)
473 : HostCpuLoad(hal, object, user, kernel, idle), mUserPrev(0), mKernelPrev(0), mIdlePrev(0) {};
474
475 void init(ULONG period, ULONG length);
476 void preCollect(CollectorHints& hints, uint64_t iTick);
477 void collect();
478 private:
479 uint64_t mUserPrev;
480 uint64_t mKernelPrev;
481 uint64_t mIdlePrev;
482 };
483
484 class HostCpuMhz : public BaseMetric
485 {
486 public:
487 HostCpuMhz(CollectorHAL *hal, ComPtr<IUnknown> object, SubMetric *mhz)
488 : BaseMetric(hal, "CPU/MHz", object), mMHz(mhz) {};
489 ~HostCpuMhz() { delete mMHz; };
490
491 void init(ULONG period, ULONG length);
492 void preCollect(CollectorHints& /* hints */, uint64_t /* iTick */) {}
493 void collect();
494 const char *getUnit() { return "MHz"; };
495 ULONG getMinValue() { return 0; };
496 ULONG getMaxValue() { return INT32_MAX; };
497 ULONG getScale() { return 1; }
498 private:
499 SubMetric *mMHz;
500 };
501
502 class HostRamUsage : public BaseMetric
503 {
504 public:
505 HostRamUsage(CollectorHAL *hal, ComPtr<IUnknown> object, SubMetric *total, SubMetric *used, SubMetric *available)
506 : BaseMetric(hal, "RAM/Usage", object), mTotal(total), mUsed(used), mAvailable(available) {};
507 ~HostRamUsage() { delete mTotal; delete mUsed; delete mAvailable; };
508
509 void init(ULONG period, ULONG length);
510 void preCollect(CollectorHints& hints, uint64_t iTick);
511 void collect();
512 const char *getUnit() { return "kB"; };
513 ULONG getMinValue() { return 0; };
514 ULONG getMaxValue() { return INT32_MAX; };
515 ULONG getScale() { return 1; }
516 private:
517 SubMetric *mTotal;
518 SubMetric *mUsed;
519 SubMetric *mAvailable;
520 };
521
522 class HostNetworkSpeed : public BaseMetric
523 {
524 public:
525 HostNetworkSpeed(CollectorHAL *hal, ComPtr<IUnknown> object, com::Utf8Str name, com::Utf8Str /* shortname */, com::Utf8Str /* ifname */, uint32_t speed, SubMetric *linkspeed)
526 : BaseMetric(hal, name, object), mSpeed(speed), mLinkSpeed(linkspeed) {};
527 ~HostNetworkSpeed() { delete mLinkSpeed; };
528
529 void init(ULONG period, ULONG length) { mPeriod = period; mLength = length; mLinkSpeed->init(length); };
530 void preCollect(CollectorHints& /* hints */, uint64_t /* iTick */) {};
531 void collect() { if (mSpeed) mLinkSpeed->put(mSpeed); };
532 const char *getUnit() { return "mbit/s"; };
533 ULONG getMinValue() { return 0; };
534 ULONG getMaxValue() { return INT32_MAX; };
535 ULONG getScale() { return 1; }
536 private:
537 ULONG mSpeed;
538 SubMetric *mLinkSpeed;
539 };
540
541 class HostNetworkLoadRaw : public BaseMetric
542 {
543 public:
544 HostNetworkLoadRaw(CollectorHAL *hal, ComPtr<IUnknown> object, com::Utf8Str name, com::Utf8Str shortname, com::Utf8Str ifname, uint32_t speed, SubMetric *rx, SubMetric *tx)
545 : BaseMetric(hal, name, object), mShortName(shortname), mInterfaceName(ifname), mRx(rx), mTx(tx), mRxPrev(0), mTxPrev(0), mRc(VINF_SUCCESS) { mSpeed = (uint64_t)speed * (1000000/8); /* Convert to bytes/sec */ };
546 ~HostNetworkLoadRaw() { delete mRx; delete mTx; };
547
548 void init(ULONG period, ULONG length);
549
550 void preCollect(CollectorHints& hints, uint64_t iTick);
551 void collect();
552 const char *getUnit() { return "%"; };
553 ULONG getMinValue() { return 0; };
554 ULONG getMaxValue() { return PM_NETWORK_LOAD_MULTIPLIER; };
555 ULONG getScale() { return PM_NETWORK_LOAD_MULTIPLIER / 100; }
556
557 private:
558 com::Utf8Str mShortName;
559 com::Utf8Str mInterfaceName;
560 SubMetric *mRx;
561 SubMetric *mTx;
562 uint64_t mRxPrev;
563 uint64_t mTxPrev;
564 uint64_t mSpeed;
565 int mRc;
566 };
567
568 class HostFilesystemUsage : public BaseMetric
569 {
570 public:
571 HostFilesystemUsage(CollectorHAL *hal, ComPtr<IUnknown> object, com::Utf8Str name, com::Utf8Str fsname, SubMetric *total, SubMetric *used, SubMetric *available)
572 : BaseMetric(hal, name, object), mFsName(fsname), mTotal(total), mUsed(used), mAvailable(available) {};
573 ~HostFilesystemUsage() { delete mTotal; delete mUsed; delete mAvailable; };
574
575 void init(ULONG period, ULONG length);
576 void preCollect(CollectorHints& hints, uint64_t iTick);
577 void collect();
578 const char *getUnit() { return "mB"; };
579 ULONG getMinValue() { return 0; };
580 ULONG getMaxValue() { return INT32_MAX; };
581 ULONG getScale() { return 1; }
582 private:
583 com::Utf8Str mFsName;
584 SubMetric *mTotal;
585 SubMetric *mUsed;
586 SubMetric *mAvailable;
587 };
588
589 class HostDiskUsage : public BaseMetric
590 {
591 public:
592 HostDiskUsage(CollectorHAL *hal, ComPtr<IUnknown> object, com::Utf8Str name, com::Utf8Str diskname, SubMetric *total)
593 : BaseMetric(hal, name, object), mDiskName(diskname), mTotal(total) {};
594 ~HostDiskUsage() { delete mTotal; };
595
596 void init(ULONG period, ULONG length);
597 void preCollect(CollectorHints& hints, uint64_t iTick);
598 void collect();
599 const char *getUnit() { return "mB"; };
600 ULONG getMinValue() { return 0; };
601 ULONG getMaxValue() { return INT32_MAX; };
602 ULONG getScale() { return 1; }
603 private:
604 com::Utf8Str mDiskName;
605 SubMetric *mTotal;
606 };
607
608 class HostDiskLoadRaw : public BaseMetric
609 {
610 public:
611 HostDiskLoadRaw(CollectorHAL *hal, ComPtr<IUnknown> object, com::Utf8Str name, com::Utf8Str diskname, SubMetric *util)
612 : BaseMetric(hal, name, object), mDiskName(diskname), mUtil(util), mDiskPrev(0), mTotalPrev(0) {};
613 ~HostDiskLoadRaw() { delete mUtil; };
614
615 void init(ULONG period, ULONG length);
616
617 void preCollect(CollectorHints& hints, uint64_t iTick);
618 void collect();
619 const char *getUnit() { return "%"; };
620 ULONG getMinValue() { return 0; };
621 ULONG getMaxValue() { return PM_DISK_LOAD_MULTIPLIER; };
622 ULONG getScale() { return PM_DISK_LOAD_MULTIPLIER / 100; }
623
624 private:
625 com::Utf8Str mDiskName;
626 SubMetric *mUtil;
627 uint64_t mDiskPrev;
628 uint64_t mTotalPrev;
629 };
630
631
632#ifndef VBOX_COLLECTOR_TEST_CASE
633 class HostRamVmm : public BaseMetric
634 {
635 public:
636 HostRamVmm(CollectorGuestManager *gm, ComPtr<IUnknown> object, SubMetric *allocVMM, SubMetric *freeVMM, SubMetric *balloonVMM, SubMetric *sharedVMM)
637 : BaseMetric(NULL, "RAM/VMM", object), mCollectorGuestManager(gm),
638 mAllocVMM(allocVMM), mFreeVMM(freeVMM), mBalloonVMM(balloonVMM), mSharedVMM(sharedVMM),
639 mAllocCurrent(0), mFreeCurrent(0), mBalloonedCurrent(0), mSharedCurrent(0) {};
640 ~HostRamVmm() { delete mAllocVMM; delete mFreeVMM; delete mBalloonVMM; delete mSharedVMM; };
641
642 void init(ULONG period, ULONG length);
643 void preCollect(CollectorHints& hints, uint64_t iTick);
644 void collect();
645 int enable();
646 int disable();
647 const char *getUnit() { return "kB"; };
648 ULONG getMinValue() { return 0; };
649 ULONG getMaxValue() { return INT32_MAX; };
650 ULONG getScale() { return 1; }
651
652 private:
653 CollectorGuestManager *mCollectorGuestManager;
654 SubMetric *mAllocVMM;
655 SubMetric *mFreeVMM;
656 SubMetric *mBalloonVMM;
657 SubMetric *mSharedVMM;
658 ULONG mAllocCurrent;
659 ULONG mFreeCurrent;
660 ULONG mBalloonedCurrent;
661 ULONG mSharedCurrent;
662 };
663#endif /* VBOX_COLLECTOR_TEST_CASE */
664
665 class MachineCpuLoad : public BaseMetric
666 {
667 public:
668 MachineCpuLoad(CollectorHAL *hal, ComPtr<IUnknown> object, RTPROCESS process, SubMetric *user, SubMetric *kernel)
669 : BaseMetric(hal, "CPU/Load", object), mProcess(process), mUser(user), mKernel(kernel) {};
670 ~MachineCpuLoad() { delete mUser; delete mKernel; };
671
672 void init(ULONG period, ULONG length);
673 void collect();
674 const char *getUnit() { return "%"; };
675 ULONG getMinValue() { return 0; };
676 ULONG getMaxValue() { return PM_CPU_LOAD_MULTIPLIER; };
677 ULONG getScale() { return PM_CPU_LOAD_MULTIPLIER / 100; }
678 protected:
679 RTPROCESS mProcess;
680 SubMetric *mUser;
681 SubMetric *mKernel;
682 };
683
684 class MachineCpuLoadRaw : public MachineCpuLoad
685 {
686 public:
687 MachineCpuLoadRaw(CollectorHAL *hal, ComPtr<IUnknown> object, RTPROCESS process, SubMetric *user, SubMetric *kernel)
688 : MachineCpuLoad(hal, object, process, user, kernel), mHostTotalPrev(0), mProcessUserPrev(0), mProcessKernelPrev(0) {};
689
690 void preCollect(CollectorHints& hints, uint64_t iTick);
691 void collect();
692 private:
693 uint64_t mHostTotalPrev;
694 uint64_t mProcessUserPrev;
695 uint64_t mProcessKernelPrev;
696 };
697
698 class MachineRamUsage : public BaseMetric
699 {
700 public:
701 MachineRamUsage(CollectorHAL *hal, ComPtr<IUnknown> object, RTPROCESS process, SubMetric *used)
702 : BaseMetric(hal, "RAM/Usage", object), mProcess(process), mUsed(used) {};
703 ~MachineRamUsage() { delete mUsed; };
704
705 void init(ULONG period, ULONG length);
706 void preCollect(CollectorHints& hints, uint64_t iTick);
707 void collect();
708 const char *getUnit() { return "kB"; };
709 ULONG getMinValue() { return 0; };
710 ULONG getMaxValue() { return INT32_MAX; };
711 ULONG getScale() { return 1; }
712 private:
713 RTPROCESS mProcess;
714 SubMetric *mUsed;
715 };
716
717
718#ifndef VBOX_COLLECTOR_TEST_CASE
719 typedef std::list<ComObjPtr<Medium> > MediaList;
720 class MachineDiskUsage : public BaseMetric
721 {
722 public:
723 MachineDiskUsage(CollectorHAL *hal, ComPtr<IUnknown> object, MediaList &disks, SubMetric *used)
724 : BaseMetric(hal, "Disk/Usage", object), mDisks(disks), mUsed(used) {};
725 ~MachineDiskUsage() { delete mUsed; };
726
727 void init(ULONG period, ULONG length);
728 void preCollect(CollectorHints& hints, uint64_t iTick);
729 void collect();
730 const char *getUnit() { return "mB"; };
731 ULONG getMinValue() { return 0; };
732 ULONG getMaxValue() { return INT32_MAX; };
733 ULONG getScale() { return 1; }
734 private:
735 MediaList mDisks;
736 SubMetric *mUsed;
737 };
738
739 /*
740 * Although MachineNetRate is measured for VM, not for the guest, it is
741 * derived from BaseGuestMetric since it uses the same mechanism for
742 * data collection -- values get pushed by Guest class along with other
743 * guest statistics.
744 */
745 class MachineNetRate : public BaseGuestMetric
746 {
747 public:
748 MachineNetRate(CollectorGuest *cguest, ComPtr<IUnknown> object, SubMetric *rx, SubMetric *tx)
749 : BaseGuestMetric(cguest, "Net/Rate", object), mRx(rx), mTx(tx) {};
750 ~MachineNetRate() { delete mRx; delete mTx; };
751
752 void init(ULONG period, ULONG length);
753 void preCollect(CollectorHints& hints, uint64_t iTick);
754 void collect();
755 int enable();
756 int disable();
757 const char *getUnit() { return "B/s"; };
758 ULONG getMinValue() { return 0; };
759 ULONG getMaxValue() { return INT32_MAX; };
760 ULONG getScale() { return 1; }
761 private:
762 SubMetric *mRx, *mTx;
763 };
764
765 class GuestCpuLoad : public BaseGuestMetric
766 {
767 public:
768 GuestCpuLoad(CollectorGuest *cguest, ComPtr<IUnknown> object, SubMetric *user, SubMetric *kernel, SubMetric *idle)
769 : BaseGuestMetric(cguest, "Guest/CPU/Load", object), mUser(user), mKernel(kernel), mIdle(idle) {};
770 ~GuestCpuLoad() { delete mUser; delete mKernel; delete mIdle; };
771
772 void init(ULONG period, ULONG length);
773 void preCollect(CollectorHints& hints, uint64_t iTick);
774 void collect();
775 int enable();
776 int disable();
777 const char *getUnit() { return "%"; };
778 ULONG getMinValue() { return 0; };
779 ULONG getMaxValue() { return PM_CPU_LOAD_MULTIPLIER; };
780 ULONG getScale() { return PM_CPU_LOAD_MULTIPLIER / 100; }
781 protected:
782 SubMetric *mUser;
783 SubMetric *mKernel;
784 SubMetric *mIdle;
785 };
786
787 class GuestRamUsage : public BaseGuestMetric
788 {
789 public:
790 GuestRamUsage(CollectorGuest *cguest, ComPtr<IUnknown> object, SubMetric *total, SubMetric *free, SubMetric *balloon, SubMetric *shared, SubMetric *cache, SubMetric *pagedtotal)
791 : BaseGuestMetric(cguest, "Guest/RAM/Usage", object), mTotal(total), mFree(free), mBallooned(balloon), mCache(cache), mPagedTotal(pagedtotal), mShared(shared) {};
792 ~GuestRamUsage() { delete mTotal; delete mFree; delete mBallooned; delete mShared; delete mCache; delete mPagedTotal; };
793
794 void init(ULONG period, ULONG length);
795 void preCollect(CollectorHints& hints, uint64_t iTick);
796 void collect();
797 int enable();
798 int disable();
799 const char *getUnit() { return "kB"; };
800 ULONG getMinValue() { return 0; };
801 ULONG getMaxValue() { return INT32_MAX; };
802 ULONG getScale() { return 1; }
803 private:
804 SubMetric *mTotal, *mFree, *mBallooned, *mCache, *mPagedTotal, *mShared;
805 };
806#endif /* VBOX_COLLECTOR_TEST_CASE */
807
808 /* Aggregate Functions **************************************************/
809 class Aggregate
810 {
811 public:
812 virtual ULONG compute(ULONG *data, ULONG length) = 0;
813 virtual const char *getName() = 0;
814 };
815
816 class AggregateAvg : public Aggregate
817 {
818 public:
819 virtual ULONG compute(ULONG *data, ULONG length);
820 virtual const char *getName();
821 };
822
823 class AggregateMin : public Aggregate
824 {
825 public:
826 virtual ULONG compute(ULONG *data, ULONG length);
827 virtual const char *getName();
828 };
829
830 class AggregateMax : public Aggregate
831 {
832 public:
833 virtual ULONG compute(ULONG *data, ULONG length);
834 virtual const char *getName();
835 };
836
837 /* Metric Class *********************************************************/
838 class Metric
839 {
840 public:
841 Metric(BaseMetric *baseMetric, SubMetric *subMetric, Aggregate *aggregate) :
842 mName(subMetric->getName()), mBaseMetric(baseMetric), mSubMetric(subMetric), mAggregate(aggregate)
843 {
844 if (mAggregate)
845 {
846 mName.append(":");
847 mName.append(mAggregate->getName());
848 }
849 }
850
851 ~Metric()
852 {
853 delete mAggregate;
854 }
855 bool associatedWith(ComPtr<IUnknown> object) { return getObject() == object; };
856
857 const char *getName() { return mName.c_str(); };
858 ComPtr<IUnknown> getObject() { return mBaseMetric->getObject(); };
859 const char *getDescription()
860 { return mAggregate ? "" : mSubMetric->getDescription(); };
861 const char *getUnit() { return mBaseMetric->getUnit(); };
862 ULONG getMinValue() { return mBaseMetric->getMinValue(); };
863 ULONG getMaxValue() { return mBaseMetric->getMaxValue(); };
864 ULONG getPeriod() { return mBaseMetric->getPeriod(); };
865 ULONG getLength()
866 { return mAggregate ? 1 : mBaseMetric->getLength(); };
867 ULONG getScale() { return mBaseMetric->getScale(); }
868 void query(ULONG **data, ULONG *count, ULONG *sequenceNumber);
869
870 private:
871 RTCString mName;
872 BaseMetric *mBaseMetric;
873 SubMetric *mSubMetric;
874 Aggregate *mAggregate;
875 };
876
877 /* Filter Class *********************************************************/
878
879 class Filter
880 {
881 public:
882 Filter(ComSafeArrayIn(IN_BSTR, metricNames),
883 ComSafeArrayIn(IUnknown * , objects));
884 Filter(const com::Utf8Str name, const ComPtr<IUnknown> &aObject);
885 static bool patternMatch(const char *pszPat, const char *pszName,
886 bool fSeenColon = false);
887 bool match(const ComPtr<IUnknown> object, const RTCString &name) const;
888 private:
889 void init(ComSafeArrayIn(IN_BSTR, metricNames),
890 ComSafeArrayIn(IUnknown * , objects));
891
892 typedef std::pair<const ComPtr<IUnknown>, const RTCString> FilterElement;
893 typedef std::list<FilterElement> ElementList;
894
895 ElementList mElements;
896
897 void processMetricList(const com::Utf8Str &name, const ComPtr<IUnknown> object);
898 };
899}
900#endif /* ___performance_h */
901/* vi: set tabstop=4 shiftwidth=4 expandtab: */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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