VirtualBox

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

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

Main/Metrics: Locking revised to prevent lockups on VM shutdown (#5637)

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

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