VirtualBox

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

Last change on this file since 56587 was 56587, checked in by vboxsync, 9 years ago

Main/Performance: convert to API wrapper usage, and quite a bit of cleanup

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