VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/PerformanceImpl.cpp@ 36664

Last change on this file since 36664 was 36128, checked in by vboxsync, 14 years ago

Main/Metrics: Hypervisor and guest metrics re-done (#5566)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 25.0 KB
Line 
1/* $Id: PerformanceImpl.cpp 36128 2011-03-02 05:44:04Z vboxsync $ */
2
3/** @file
4 *
5 * VBox Performance API COM Classes implementation
6 */
7
8/*
9 * Copyright (C) 2008-2010 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
20#include "PerformanceImpl.h"
21
22#include "AutoCaller.h"
23#include "Logging.h"
24
25#include <iprt/process.h>
26
27#include <VBox/err.h>
28#include <VBox/settings.h>
29
30#include <vector>
31#include <algorithm>
32#include <functional>
33
34#include "Performance.h"
35
36static const char *g_papcszMetricNames[] =
37{
38 "CPU/Load/User",
39 "CPU/Load/User:avg",
40 "CPU/Load/User:min",
41 "CPU/Load/User:max",
42 "CPU/Load/Kernel",
43 "CPU/Load/Kernel:avg",
44 "CPU/Load/Kernel:min",
45 "CPU/Load/Kernel:max",
46 "CPU/Load/Idle",
47 "CPU/Load/Idle:avg",
48 "CPU/Load/Idle:min",
49 "CPU/Load/Idle:max",
50 "CPU/MHz",
51 "CPU/MHz:avg",
52 "CPU/MHz:min",
53 "CPU/MHz:max",
54 "RAM/Usage/Total",
55 "RAM/Usage/Total:avg",
56 "RAM/Usage/Total:min",
57 "RAM/Usage/Total:max",
58 "RAM/Usage/Used",
59 "RAM/Usage/Used:avg",
60 "RAM/Usage/Used:min",
61 "RAM/Usage/Used:max",
62 "RAM/Usage/Free",
63 "RAM/Usage/Free:avg",
64 "RAM/Usage/Free:min",
65 "RAM/Usage/Free:max",
66 "RAM/VMM/Used",
67 "RAM/VMM/Used:avg",
68 "RAM/VMM/Used:min",
69 "RAM/VMM/Used:max",
70 "RAM/VMM/Free",
71 "RAM/VMM/Free:avg",
72 "RAM/VMM/Free:min",
73 "RAM/VMM/Free:max",
74 "RAM/VMM/Ballooned",
75 "RAM/VMM/Ballooned:avg",
76 "RAM/VMM/Ballooned:min",
77 "RAM/VMM/Ballooned:max",
78 "RAM/VMM/Shared",
79 "RAM/VMM/Shared:avg",
80 "RAM/VMM/Shared:min",
81 "RAM/VMM/Shared:max",
82 "Guest/CPU/Load/User",
83 "Guest/CPU/Load/User:avg",
84 "Guest/CPU/Load/User:min",
85 "Guest/CPU/Load/User:max",
86 "Guest/CPU/Load/Kernel",
87 "Guest/CPU/Load/Kernel:avg",
88 "Guest/CPU/Load/Kernel:min",
89 "Guest/CPU/Load/Kernel:max",
90 "Guest/CPU/Load/Idle",
91 "Guest/CPU/Load/Idle:avg",
92 "Guest/CPU/Load/Idle:min",
93 "Guest/CPU/Load/Idle:max",
94 "Guest/RAM/Usage/Total",
95 "Guest/RAM/Usage/Total:avg",
96 "Guest/RAM/Usage/Total:min",
97 "Guest/RAM/Usage/Total:max",
98 "Guest/RAM/Usage/Free",
99 "Guest/RAM/Usage/Free:avg",
100 "Guest/RAM/Usage/Free:min",
101 "Guest/RAM/Usage/Free:max",
102 "Guest/RAM/Usage/Balloon",
103 "Guest/RAM/Usage/Balloon:avg",
104 "Guest/RAM/Usage/Balloon:min",
105 "Guest/RAM/Usage/Balloon:max",
106 "Guest/RAM/Usage/Shared",
107 "Guest/RAM/Usage/Shared:avg",
108 "Guest/RAM/Usage/Shared:min",
109 "Guest/RAM/Usage/Shared:max",
110 "Guest/RAM/Usage/Cache",
111 "Guest/RAM/Usage/Cache:avg",
112 "Guest/RAM/Usage/Cache:min",
113 "Guest/RAM/Usage/Cache:max",
114 "Guest/Pagefile/Usage/Total",
115 "Guest/Pagefile/Usage/Total:avg",
116 "Guest/Pagefile/Usage/Total:min",
117 "Guest/Pagefile/Usage/Total:max",
118};
119
120////////////////////////////////////////////////////////////////////////////////
121// PerformanceCollector class
122////////////////////////////////////////////////////////////////////////////////
123
124// constructor / destructor
125////////////////////////////////////////////////////////////////////////////////
126
127PerformanceCollector::PerformanceCollector() : mMagic(0) {}
128
129PerformanceCollector::~PerformanceCollector() {}
130
131HRESULT PerformanceCollector::FinalConstruct()
132{
133 LogFlowThisFunc(("\n"));
134
135 return BaseFinalConstruct();
136}
137
138void PerformanceCollector::FinalRelease()
139{
140 LogFlowThisFunc(("\n"));
141 BaseFinalRelease();
142}
143
144// public initializer/uninitializer for internal purposes only
145////////////////////////////////////////////////////////////////////////////////
146
147/**
148 * Initializes the PerformanceCollector object.
149 */
150HRESULT PerformanceCollector::init()
151{
152 /* Enclose the state transition NotReady->InInit->Ready */
153 AutoInitSpan autoInitSpan(this);
154 AssertReturn(autoInitSpan.isOk(), E_FAIL);
155
156 LogFlowThisFuncEnter();
157
158 HRESULT rc = S_OK;
159
160 m.hal = pm::createHAL();
161 m.gm = new pm::CollectorGuestManager;
162
163 /* Let the sampler know it gets a valid collector. */
164 mMagic = MAGIC;
165
166 /* Start resource usage sampler */
167 int vrc = RTTimerLRCreate (&m.sampler, VBOX_USAGE_SAMPLER_MIN_INTERVAL,
168 &PerformanceCollector::staticSamplerCallback, this);
169 AssertMsgRC (vrc, ("Failed to create resource usage "
170 "sampling timer(%Rra)\n", vrc));
171 if (RT_FAILURE(vrc))
172 rc = E_FAIL;
173
174 if (SUCCEEDED(rc))
175 autoInitSpan.setSucceeded();
176
177 LogFlowThisFuncLeave();
178
179 return rc;
180}
181
182/**
183 * Uninitializes the PerformanceCollector object.
184 *
185 * Called either from FinalRelease() or by the parent when it gets destroyed.
186 */
187void PerformanceCollector::uninit()
188{
189 LogFlowThisFuncEnter();
190
191 /* Enclose the state transition Ready->InUninit->NotReady */
192 AutoUninitSpan autoUninitSpan(this);
193 if (autoUninitSpan.uninitDone())
194 {
195 LogFlowThisFunc(("Already uninitialized.\n"));
196 LogFlowThisFuncLeave();
197 return;
198 }
199
200 mMagic = 0;
201
202 /* Destroy resource usage sampler */
203 int vrc = RTTimerLRDestroy (m.sampler);
204 AssertMsgRC (vrc, ("Failed to destroy resource usage "
205 "sampling timer (%Rra)\n", vrc));
206 m.sampler = NULL;
207
208 //delete m.factory;
209 //m.factory = NULL;
210
211 delete m.gm;
212 m.gm = NULL;
213 delete m.hal;
214 m.hal = NULL;
215
216 LogFlowThisFuncLeave();
217}
218
219// IPerformanceCollector properties
220////////////////////////////////////////////////////////////////////////////////
221
222STDMETHODIMP PerformanceCollector::COMGETTER(MetricNames)(ComSafeArrayOut(BSTR, theMetricNames))
223{
224 if (ComSafeArrayOutIsNull(theMetricNames))
225 return E_POINTER;
226
227 AutoCaller autoCaller(this);
228 if (FAILED(autoCaller.rc())) return autoCaller.rc();
229
230 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
231
232 com::SafeArray<BSTR> metricNames(RT_ELEMENTS(g_papcszMetricNames));
233 for (size_t i = 0; i < RT_ELEMENTS(g_papcszMetricNames); i++)
234 {
235 Bstr tmp(g_papcszMetricNames[i]); /* gcc-3.3 cruft */
236 tmp.cloneTo(&metricNames[i]);
237 }
238 //gMetricNames.detachTo(ComSafeArrayOutArg(theMetricNames));
239 metricNames.detachTo(ComSafeArrayOutArg(theMetricNames));
240
241 return S_OK;
242}
243
244// IPerformanceCollector methods
245////////////////////////////////////////////////////////////////////////////////
246
247HRESULT PerformanceCollector::toIPerformanceMetric(pm::Metric *src, IPerformanceMetric **dst)
248{
249 ComObjPtr<PerformanceMetric> metric;
250 HRESULT rc = metric.createObject();
251 if (SUCCEEDED(rc))
252 rc = metric->init (src);
253 AssertComRCReturnRC(rc);
254 metric.queryInterfaceTo(dst);
255 return rc;
256}
257
258HRESULT PerformanceCollector::toIPerformanceMetric(pm::BaseMetric *src, IPerformanceMetric **dst)
259{
260 ComObjPtr<PerformanceMetric> metric;
261 HRESULT rc = metric.createObject();
262 if (SUCCEEDED(rc))
263 rc = metric->init (src);
264 AssertComRCReturnRC(rc);
265 metric.queryInterfaceTo(dst);
266 return rc;
267}
268
269STDMETHODIMP PerformanceCollector::GetMetrics(ComSafeArrayIn(IN_BSTR, metricNames),
270 ComSafeArrayIn(IUnknown *, objects),
271 ComSafeArrayOut(IPerformanceMetric *, outMetrics))
272{
273 LogFlowThisFuncEnter();
274 //LogFlowThisFunc(("mState=%d, mType=%d\n", mState, mType));
275
276 HRESULT rc = S_OK;
277
278 AutoCaller autoCaller(this);
279 if (FAILED(autoCaller.rc())) return autoCaller.rc();
280
281 pm::Filter filter (ComSafeArrayInArg (metricNames),
282 ComSafeArrayInArg (objects));
283
284 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
285
286 MetricList filteredMetrics;
287 MetricList::iterator it;
288 for (it = m.metrics.begin(); it != m.metrics.end(); ++it)
289 if (filter.match ((*it)->getObject(), (*it)->getName()))
290 filteredMetrics.push_back (*it);
291
292 com::SafeIfaceArray<IPerformanceMetric> retMetrics (filteredMetrics.size());
293 int i = 0;
294 for (it = filteredMetrics.begin(); it != filteredMetrics.end(); ++it)
295 {
296 ComObjPtr<PerformanceMetric> metric;
297 rc = metric.createObject();
298 if (SUCCEEDED(rc))
299 rc = metric->init (*it);
300 AssertComRCReturnRC(rc);
301 LogFlow (("PerformanceCollector::GetMetrics() store a metric at "
302 "retMetrics[%d]...\n", i));
303 metric.queryInterfaceTo(&retMetrics[i++]);
304 }
305 retMetrics.detachTo(ComSafeArrayOutArg(outMetrics));
306 LogFlowThisFuncLeave();
307 return rc;
308}
309
310STDMETHODIMP PerformanceCollector::SetupMetrics(ComSafeArrayIn(IN_BSTR, metricNames),
311 ComSafeArrayIn(IUnknown *, objects),
312 ULONG aPeriod,
313 ULONG aCount,
314 ComSafeArrayOut(IPerformanceMetric *, outMetrics))
315{
316 AutoCaller autoCaller(this);
317 if (FAILED(autoCaller.rc())) return autoCaller.rc();
318
319 pm::Filter filter(ComSafeArrayInArg (metricNames),
320 ComSafeArrayInArg (objects));
321
322 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
323
324 HRESULT rc = S_OK;
325 BaseMetricList filteredMetrics;
326 BaseMetricList::iterator it;
327 for (it = m.baseMetrics.begin(); it != m.baseMetrics.end(); ++it)
328 if (filter.match((*it)->getObject(), (*it)->getName()))
329 {
330 LogFlow (("PerformanceCollector::SetupMetrics() setting period to %u,"
331 " count to %u for %s\n", aPeriod, aCount, (*it)->getName()));
332 (*it)->init(aPeriod, aCount);
333 if (aPeriod == 0 || aCount == 0)
334 {
335 LogFlow (("PerformanceCollector::SetupMetrics() disabling %s\n",
336 (*it)->getName()));
337 (*it)->disable();
338 }
339 else
340 {
341 LogFlow (("PerformanceCollector::SetupMetrics() enabling %s\n",
342 (*it)->getName()));
343 (*it)->enable();
344 }
345 filteredMetrics.push_back(*it);
346 }
347
348 com::SafeIfaceArray<IPerformanceMetric> retMetrics(filteredMetrics.size());
349 int i = 0;
350 for (it = filteredMetrics.begin();
351 it != filteredMetrics.end() && SUCCEEDED(rc); ++it)
352 rc = toIPerformanceMetric(*it, &retMetrics[i++]);
353 retMetrics.detachTo(ComSafeArrayOutArg(outMetrics));
354
355 LogFlowThisFuncLeave();
356 return rc;
357}
358
359STDMETHODIMP PerformanceCollector::EnableMetrics(ComSafeArrayIn(IN_BSTR, metricNames),
360 ComSafeArrayIn(IUnknown *, objects),
361 ComSafeArrayOut(IPerformanceMetric *, outMetrics))
362{
363 AutoCaller autoCaller(this);
364 if (FAILED(autoCaller.rc())) return autoCaller.rc();
365
366 pm::Filter filter(ComSafeArrayInArg(metricNames),
367 ComSafeArrayInArg(objects));
368
369 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); /* Write lock is not needed atm since we are */
370 /* fiddling with enable bit only, but we */
371 /* care for those who come next :-). */
372
373 HRESULT rc = S_OK;
374 BaseMetricList filteredMetrics;
375 BaseMetricList::iterator it;
376 for (it = m.baseMetrics.begin(); it != m.baseMetrics.end(); ++it)
377 if (filter.match((*it)->getObject(), (*it)->getName()))
378 {
379 (*it)->enable();
380 filteredMetrics.push_back(*it);
381 }
382
383 com::SafeIfaceArray<IPerformanceMetric> retMetrics(filteredMetrics.size());
384 int i = 0;
385 for (it = filteredMetrics.begin();
386 it != filteredMetrics.end() && SUCCEEDED(rc); ++it)
387 rc = toIPerformanceMetric(*it, &retMetrics[i++]);
388 retMetrics.detachTo(ComSafeArrayOutArg(outMetrics));
389
390 LogFlowThisFuncLeave();
391 return rc;
392}
393
394STDMETHODIMP PerformanceCollector::DisableMetrics(ComSafeArrayIn(IN_BSTR, metricNames),
395 ComSafeArrayIn(IUnknown *, objects),
396 ComSafeArrayOut(IPerformanceMetric *, outMetrics))
397{
398 AutoCaller autoCaller(this);
399 if (FAILED(autoCaller.rc())) return autoCaller.rc();
400
401 pm::Filter filter(ComSafeArrayInArg(metricNames),
402 ComSafeArrayInArg(objects));
403
404 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); /* Write lock is not needed atm since we are */
405 /* fiddling with enable bit only, but we */
406 /* care for those who come next :-). */
407
408 HRESULT rc = S_OK;
409 BaseMetricList filteredMetrics;
410 BaseMetricList::iterator it;
411 for (it = m.baseMetrics.begin(); it != m.baseMetrics.end(); ++it)
412 if (filter.match((*it)->getObject(), (*it)->getName()))
413 {
414 (*it)->disable();
415 filteredMetrics.push_back(*it);
416 }
417
418 com::SafeIfaceArray<IPerformanceMetric> retMetrics(filteredMetrics.size());
419 int i = 0;
420 for (it = filteredMetrics.begin();
421 it != filteredMetrics.end() && SUCCEEDED(rc); ++it)
422 rc = toIPerformanceMetric(*it, &retMetrics[i++]);
423 retMetrics.detachTo(ComSafeArrayOutArg(outMetrics));
424
425 LogFlowThisFuncLeave();
426 return rc;
427}
428
429STDMETHODIMP PerformanceCollector::QueryMetricsData(ComSafeArrayIn (IN_BSTR, metricNames),
430 ComSafeArrayIn (IUnknown *, objects),
431 ComSafeArrayOut(BSTR, outMetricNames),
432 ComSafeArrayOut(IUnknown *, outObjects),
433 ComSafeArrayOut(BSTR, outUnits),
434 ComSafeArrayOut(ULONG, outScales),
435 ComSafeArrayOut(ULONG, outSequenceNumbers),
436 ComSafeArrayOut(ULONG, outDataIndices),
437 ComSafeArrayOut(ULONG, outDataLengths),
438 ComSafeArrayOut(LONG, outData))
439{
440 AutoCaller autoCaller(this);
441 if (FAILED(autoCaller.rc())) return autoCaller.rc();
442
443 pm::Filter filter(ComSafeArrayInArg(metricNames),
444 ComSafeArrayInArg(objects));
445
446 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
447
448 /* Let's compute the size of the resulting flat array */
449 size_t flatSize = 0;
450 MetricList filteredMetrics;
451 MetricList::iterator it;
452 for (it = m.metrics.begin(); it != m.metrics.end(); ++it)
453 if (filter.match ((*it)->getObject(), (*it)->getName()))
454 {
455 filteredMetrics.push_back (*it);
456 flatSize += (*it)->getLength();
457 }
458
459 int i = 0;
460 size_t flatIndex = 0;
461 size_t numberOfMetrics = filteredMetrics.size();
462 com::SafeArray<BSTR> retNames(numberOfMetrics);
463 com::SafeIfaceArray<IUnknown> retObjects(numberOfMetrics);
464 com::SafeArray<BSTR> retUnits(numberOfMetrics);
465 com::SafeArray<ULONG> retScales(numberOfMetrics);
466 com::SafeArray<ULONG> retSequenceNumbers(numberOfMetrics);
467 com::SafeArray<ULONG> retIndices(numberOfMetrics);
468 com::SafeArray<ULONG> retLengths(numberOfMetrics);
469 com::SafeArray<LONG> retData(flatSize);
470
471 for (it = filteredMetrics.begin(); it != filteredMetrics.end(); ++it, ++i)
472 {
473 ULONG *values, length, sequenceNumber;
474 /* @todo We may want to revise the query method to get rid of excessive alloc/memcpy calls. */
475 (*it)->query(&values, &length, &sequenceNumber);
476 LogFlow (("PerformanceCollector::QueryMetricsData() querying metric %s "
477 "returned %d values.\n", (*it)->getName(), length));
478 memcpy(retData.raw() + flatIndex, values, length * sizeof(*values));
479 Bstr tmp((*it)->getName());
480 tmp.detachTo(&retNames[i]);
481 (*it)->getObject().queryInterfaceTo(&retObjects[i]);
482 tmp = (*it)->getUnit();
483 tmp.detachTo(&retUnits[i]);
484 retScales[i] = (*it)->getScale();
485 retSequenceNumbers[i] = sequenceNumber;
486 retLengths[i] = length;
487 retIndices[i] = (ULONG)flatIndex;
488 flatIndex += length;
489 }
490
491 retNames.detachTo(ComSafeArrayOutArg(outMetricNames));
492 retObjects.detachTo(ComSafeArrayOutArg(outObjects));
493 retUnits.detachTo(ComSafeArrayOutArg(outUnits));
494 retScales.detachTo(ComSafeArrayOutArg(outScales));
495 retSequenceNumbers.detachTo(ComSafeArrayOutArg(outSequenceNumbers));
496 retIndices.detachTo(ComSafeArrayOutArg(outDataIndices));
497 retLengths.detachTo(ComSafeArrayOutArg(outDataLengths));
498 retData.detachTo(ComSafeArrayOutArg(outData));
499 return S_OK;
500}
501
502// public methods for internal purposes
503///////////////////////////////////////////////////////////////////////////////
504
505void PerformanceCollector::registerBaseMetric(pm::BaseMetric *baseMetric)
506{
507 //LogFlowThisFuncEnter();
508 AutoCaller autoCaller(this);
509 if (!SUCCEEDED(autoCaller.rc())) return;
510
511 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
512 LogAleksey(("{%p} " LOG_FN_FMT ": obj=%p name=%s\n", this, __PRETTY_FUNCTION__, (void *)baseMetric->getObject(), baseMetric->getName()));
513 m.baseMetrics.push_back (baseMetric);
514 //LogFlowThisFuncLeave();
515}
516
517void PerformanceCollector::registerMetric(pm::Metric *metric)
518{
519 //LogFlowThisFuncEnter();
520 AutoCaller autoCaller(this);
521 if (!SUCCEEDED(autoCaller.rc())) return;
522
523 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
524 LogAleksey(("{%p} " LOG_FN_FMT ": obj=%p name=%s\n", this, __PRETTY_FUNCTION__, (void *)metric->getObject(), metric->getName()));
525 m.metrics.push_back (metric);
526 //LogFlowThisFuncLeave();
527}
528
529void PerformanceCollector::unregisterBaseMetricsFor(const ComPtr<IUnknown> &aObject)
530{
531 //LogFlowThisFuncEnter();
532 AutoCaller autoCaller(this);
533 if (!SUCCEEDED(autoCaller.rc())) return;
534
535 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
536 LogAleksey(("{%p} " LOG_FN_FMT ": before remove_if: m.baseMetrics.size()=%d\n", this, __PRETTY_FUNCTION__, m.baseMetrics.size()));
537 BaseMetricList::iterator it;
538 for (it = m.baseMetrics.begin(); it != m.baseMetrics.end();)
539 if ((*it)->associatedWith(aObject))
540 {
541 delete *it;
542 it = m.baseMetrics.erase(it);
543 }
544 else
545 ++it;
546 LogAleksey(("{%p} " LOG_FN_FMT ": after remove_if: m.baseMetrics.size()=%d\n", this, __PRETTY_FUNCTION__, m.baseMetrics.size()));
547 //LogFlowThisFuncLeave();
548}
549
550void PerformanceCollector::unregisterMetricsFor(const ComPtr<IUnknown> &aObject)
551{
552 //LogFlowThisFuncEnter();
553 AutoCaller autoCaller(this);
554 if (!SUCCEEDED(autoCaller.rc())) return;
555
556 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
557 LogAleksey(("{%p} " LOG_FN_FMT ": obj=%p\n", this, __PRETTY_FUNCTION__, (void *)aObject));
558 MetricList::iterator it;
559 for (it = m.metrics.begin(); it != m.metrics.end();)
560 if ((*it)->associatedWith(aObject))
561 {
562 delete *it;
563 it = m.metrics.erase(it);
564 }
565 else
566 ++it;
567 //LogFlowThisFuncLeave();
568}
569
570void PerformanceCollector::registerGuest(pm::CollectorGuest* pGuest)
571{
572 AutoCaller autoCaller(this);
573 if (!SUCCEEDED(autoCaller.rc())) return;
574
575 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
576 m.gm->registerGuest(pGuest);
577}
578
579void PerformanceCollector::unregisterGuest(pm::CollectorGuest* pGuest)
580{
581 AutoCaller autoCaller(this);
582 if (!SUCCEEDED(autoCaller.rc())) return;
583
584 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
585 m.gm->unregisterGuest(pGuest);
586}
587
588void PerformanceCollector::suspendSampling()
589{
590 AutoCaller autoCaller(this);
591 if (!SUCCEEDED(autoCaller.rc())) return;
592
593 int rc = RTTimerLRStop(m.sampler);
594 AssertRC(rc);
595}
596
597void PerformanceCollector::resumeSampling()
598{
599 AutoCaller autoCaller(this);
600 if (!SUCCEEDED(autoCaller.rc())) return;
601
602 int rc = RTTimerLRStart(m.sampler, 0);
603 AssertRC(rc);
604}
605
606
607// private methods
608///////////////////////////////////////////////////////////////////////////////
609
610/* static */
611void PerformanceCollector::staticSamplerCallback(RTTIMERLR hTimerLR, void *pvUser,
612 uint64_t iTick)
613{
614 AssertReturnVoid (pvUser != NULL);
615 PerformanceCollector *collector = static_cast <PerformanceCollector *> (pvUser);
616 Assert(collector->mMagic == MAGIC);
617 if (collector->mMagic == MAGIC)
618 collector->samplerCallback(iTick);
619
620 NOREF (hTimerLR);
621}
622
623/*
624 * Metrics collection is a three stage process:
625 * 1) Pre-collection (hinting)
626 * At this stage we compose the list of all metrics to be collected
627 * If any metrics cannot be collected separately or if it is more
628 * efficient to collect several metric at once, these metrics should
629 * use hints to mark that they will need to be collected.
630 * 2) Pre-collection (bulk)
631 * Using hints set at stage 1 platform-specific HAL
632 * instance collects all marked host-related metrics.
633 * Hinted guest-related metrics then get collected by CollectorGuestManager.
634 * 3) Collection
635 * Metrics that are collected individually get collected and stored. Values
636 * saved in HAL and CollectorGuestManager are extracted and stored to
637 * individual metrics.
638 */
639void PerformanceCollector::samplerCallback(uint64_t iTick)
640{
641 Log4(("{%p} " LOG_FN_FMT ": ENTER\n", this, __PRETTY_FUNCTION__));
642 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
643
644 pm::CollectorHints hints;
645 uint64_t timestamp = RTTimeMilliTS();
646 BaseMetricList toBeCollected;
647 BaseMetricList::iterator it;
648 /* Compose the list of metrics being collected at this moment */
649 for (it = m.baseMetrics.begin(); it != m.baseMetrics.end(); it++)
650 if ((*it)->collectorBeat(timestamp))
651 {
652 (*it)->preCollect(hints, iTick);
653 toBeCollected.push_back(*it);
654 }
655
656 if (toBeCollected.size() == 0)
657 {
658 Log4(("{%p} " LOG_FN_FMT ": LEAVE (nothing to collect)\n", this, __PRETTY_FUNCTION__));
659 return;
660 }
661
662 /* Let know the platform specific code what is being collected */
663 m.hal->preCollect(hints, iTick);
664 /* Collect the data in bulk from all hinted guests */
665 m.gm->preCollect(hints, iTick);
666
667 /* Finally, collect the data */
668 std::for_each (toBeCollected.begin(), toBeCollected.end(),
669 std::mem_fun (&pm::BaseMetric::collect));
670 Log4(("{%p} " LOG_FN_FMT ": LEAVE\n", this, __PRETTY_FUNCTION__));
671}
672
673////////////////////////////////////////////////////////////////////////////////
674// PerformanceMetric class
675////////////////////////////////////////////////////////////////////////////////
676
677// constructor / destructor
678////////////////////////////////////////////////////////////////////////////////
679
680PerformanceMetric::PerformanceMetric()
681{
682}
683
684PerformanceMetric::~PerformanceMetric()
685{
686}
687
688HRESULT PerformanceMetric::FinalConstruct()
689{
690 LogFlowThisFunc(("\n"));
691
692 return BaseFinalConstruct();
693}
694
695void PerformanceMetric::FinalRelease()
696{
697 LogFlowThisFunc(("\n"));
698
699 uninit ();
700
701 BaseFinalRelease();
702}
703
704// public initializer/uninitializer for internal purposes only
705////////////////////////////////////////////////////////////////////////////////
706
707HRESULT PerformanceMetric::init(pm::Metric *aMetric)
708{
709 m.name = aMetric->getName();
710 m.object = aMetric->getObject();
711 m.description = aMetric->getDescription();
712 m.period = aMetric->getPeriod();
713 m.count = aMetric->getLength();
714 m.unit = aMetric->getUnit();
715 m.min = aMetric->getMinValue();
716 m.max = aMetric->getMaxValue();
717 return S_OK;
718}
719
720HRESULT PerformanceMetric::init(pm::BaseMetric *aMetric)
721{
722 m.name = aMetric->getName();
723 m.object = aMetric->getObject();
724 m.description = "";
725 m.period = aMetric->getPeriod();
726 m.count = aMetric->getLength();
727 m.unit = aMetric->getUnit();
728 m.min = aMetric->getMinValue();
729 m.max = aMetric->getMaxValue();
730 return S_OK;
731}
732
733void PerformanceMetric::uninit()
734{
735}
736
737STDMETHODIMP PerformanceMetric::COMGETTER(MetricName)(BSTR *aMetricName)
738{
739 /// @todo (r=dmik) why do all these getters not do AutoCaller and
740 /// AutoReadLock? Is the underlying metric a constant object?
741
742 m.name.cloneTo(aMetricName);
743 return S_OK;
744}
745
746STDMETHODIMP PerformanceMetric::COMGETTER(Object)(IUnknown **anObject)
747{
748 m.object.queryInterfaceTo(anObject);
749 return S_OK;
750}
751
752STDMETHODIMP PerformanceMetric::COMGETTER(Description)(BSTR *aDescription)
753{
754 m.description.cloneTo(aDescription);
755 return S_OK;
756}
757
758STDMETHODIMP PerformanceMetric::COMGETTER(Period)(ULONG *aPeriod)
759{
760 *aPeriod = m.period;
761 return S_OK;
762}
763
764STDMETHODIMP PerformanceMetric::COMGETTER(Count)(ULONG *aCount)
765{
766 *aCount = m.count;
767 return S_OK;
768}
769
770STDMETHODIMP PerformanceMetric::COMGETTER(Unit)(BSTR *aUnit)
771{
772 m.unit.cloneTo(aUnit);
773 return S_OK;
774}
775
776STDMETHODIMP PerformanceMetric::COMGETTER(MinimumValue)(LONG *aMinValue)
777{
778 *aMinValue = m.min;
779 return S_OK;
780}
781
782STDMETHODIMP PerformanceMetric::COMGETTER(MaximumValue)(LONG *aMaxValue)
783{
784 *aMaxValue = m.max;
785 return S_OK;
786}
787/* 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