1 | /* $Id: PerformanceImpl.cpp 35638 2011-01-19 19:10:49Z 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 |
|
---|
36 | static 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 |
|
---|
127 | PerformanceCollector::PerformanceCollector() : mMagic(0) {}
|
---|
128 |
|
---|
129 | PerformanceCollector::~PerformanceCollector() {}
|
---|
130 |
|
---|
131 | HRESULT PerformanceCollector::FinalConstruct()
|
---|
132 | {
|
---|
133 | LogFlowThisFunc(("\n"));
|
---|
134 |
|
---|
135 | return BaseFinalConstruct();
|
---|
136 | }
|
---|
137 |
|
---|
138 | void 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 | */
|
---|
150 | HRESULT 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 |
|
---|
162 | /* Let the sampler know it gets a valid collector. */
|
---|
163 | mMagic = MAGIC;
|
---|
164 |
|
---|
165 | /* Start resource usage sampler */
|
---|
166 | int vrc = RTTimerLRCreate (&m.sampler, VBOX_USAGE_SAMPLER_MIN_INTERVAL,
|
---|
167 | &PerformanceCollector::staticSamplerCallback, this);
|
---|
168 | AssertMsgRC (vrc, ("Failed to create resource usage "
|
---|
169 | "sampling timer(%Rra)\n", vrc));
|
---|
170 | if (RT_FAILURE(vrc))
|
---|
171 | rc = E_FAIL;
|
---|
172 |
|
---|
173 | if (SUCCEEDED(rc))
|
---|
174 | autoInitSpan.setSucceeded();
|
---|
175 |
|
---|
176 | LogFlowThisFuncLeave();
|
---|
177 |
|
---|
178 | return rc;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Uninitializes the PerformanceCollector object.
|
---|
183 | *
|
---|
184 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
185 | */
|
---|
186 | void PerformanceCollector::uninit()
|
---|
187 | {
|
---|
188 | LogFlowThisFuncEnter();
|
---|
189 |
|
---|
190 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
191 | AutoUninitSpan autoUninitSpan(this);
|
---|
192 | if (autoUninitSpan.uninitDone())
|
---|
193 | {
|
---|
194 | LogFlowThisFunc(("Already uninitialized.\n"));
|
---|
195 | LogFlowThisFuncLeave();
|
---|
196 | return;
|
---|
197 | }
|
---|
198 |
|
---|
199 | mMagic = 0;
|
---|
200 |
|
---|
201 | /* Destroy resource usage sampler */
|
---|
202 | int vrc = RTTimerLRDestroy (m.sampler);
|
---|
203 | AssertMsgRC (vrc, ("Failed to destroy resource usage "
|
---|
204 | "sampling timer (%Rra)\n", vrc));
|
---|
205 | m.sampler = NULL;
|
---|
206 |
|
---|
207 | //delete m.factory;
|
---|
208 | //m.factory = NULL;
|
---|
209 |
|
---|
210 | delete m.hal;
|
---|
211 | m.hal = NULL;
|
---|
212 |
|
---|
213 | LogFlowThisFuncLeave();
|
---|
214 | }
|
---|
215 |
|
---|
216 | // IPerformanceCollector properties
|
---|
217 | ////////////////////////////////////////////////////////////////////////////////
|
---|
218 |
|
---|
219 | STDMETHODIMP PerformanceCollector::COMGETTER(MetricNames)(ComSafeArrayOut(BSTR, theMetricNames))
|
---|
220 | {
|
---|
221 | if (ComSafeArrayOutIsNull(theMetricNames))
|
---|
222 | return E_POINTER;
|
---|
223 |
|
---|
224 | AutoCaller autoCaller(this);
|
---|
225 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
226 |
|
---|
227 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
228 |
|
---|
229 | com::SafeArray<BSTR> metricNames(RT_ELEMENTS(g_papcszMetricNames));
|
---|
230 | for (size_t i = 0; i < RT_ELEMENTS(g_papcszMetricNames); i++)
|
---|
231 | {
|
---|
232 | Bstr tmp(g_papcszMetricNames[i]); /* gcc-3.3 cruft */
|
---|
233 | tmp.cloneTo(&metricNames[i]);
|
---|
234 | }
|
---|
235 | //gMetricNames.detachTo(ComSafeArrayOutArg(theMetricNames));
|
---|
236 | metricNames.detachTo(ComSafeArrayOutArg(theMetricNames));
|
---|
237 |
|
---|
238 | return S_OK;
|
---|
239 | }
|
---|
240 |
|
---|
241 | // IPerformanceCollector methods
|
---|
242 | ////////////////////////////////////////////////////////////////////////////////
|
---|
243 |
|
---|
244 | HRESULT PerformanceCollector::toIPerformanceMetric(pm::Metric *src, IPerformanceMetric **dst)
|
---|
245 | {
|
---|
246 | ComObjPtr<PerformanceMetric> metric;
|
---|
247 | HRESULT rc = metric.createObject();
|
---|
248 | if (SUCCEEDED(rc))
|
---|
249 | rc = metric->init (src);
|
---|
250 | AssertComRCReturnRC(rc);
|
---|
251 | metric.queryInterfaceTo(dst);
|
---|
252 | return rc;
|
---|
253 | }
|
---|
254 |
|
---|
255 | HRESULT PerformanceCollector::toIPerformanceMetric(pm::BaseMetric *src, IPerformanceMetric **dst)
|
---|
256 | {
|
---|
257 | ComObjPtr<PerformanceMetric> metric;
|
---|
258 | HRESULT rc = metric.createObject();
|
---|
259 | if (SUCCEEDED(rc))
|
---|
260 | rc = metric->init (src);
|
---|
261 | AssertComRCReturnRC(rc);
|
---|
262 | metric.queryInterfaceTo(dst);
|
---|
263 | return rc;
|
---|
264 | }
|
---|
265 |
|
---|
266 | STDMETHODIMP PerformanceCollector::GetMetrics(ComSafeArrayIn(IN_BSTR, metricNames),
|
---|
267 | ComSafeArrayIn(IUnknown *, objects),
|
---|
268 | ComSafeArrayOut(IPerformanceMetric *, outMetrics))
|
---|
269 | {
|
---|
270 | LogFlowThisFuncEnter();
|
---|
271 | //LogFlowThisFunc(("mState=%d, mType=%d\n", mState, mType));
|
---|
272 |
|
---|
273 | HRESULT rc = S_OK;
|
---|
274 |
|
---|
275 | AutoCaller autoCaller(this);
|
---|
276 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
277 |
|
---|
278 | pm::Filter filter (ComSafeArrayInArg (metricNames),
|
---|
279 | ComSafeArrayInArg (objects));
|
---|
280 |
|
---|
281 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
282 |
|
---|
283 | MetricList filteredMetrics;
|
---|
284 | MetricList::iterator it;
|
---|
285 | for (it = m.metrics.begin(); it != m.metrics.end(); ++it)
|
---|
286 | if (filter.match ((*it)->getObject(), (*it)->getName()))
|
---|
287 | filteredMetrics.push_back (*it);
|
---|
288 |
|
---|
289 | com::SafeIfaceArray<IPerformanceMetric> retMetrics (filteredMetrics.size());
|
---|
290 | int i = 0;
|
---|
291 | for (it = filteredMetrics.begin(); it != filteredMetrics.end(); ++it)
|
---|
292 | {
|
---|
293 | ComObjPtr<PerformanceMetric> metric;
|
---|
294 | rc = metric.createObject();
|
---|
295 | if (SUCCEEDED(rc))
|
---|
296 | rc = metric->init (*it);
|
---|
297 | AssertComRCReturnRC(rc);
|
---|
298 | LogFlow (("PerformanceCollector::GetMetrics() store a metric at "
|
---|
299 | "retMetrics[%d]...\n", i));
|
---|
300 | metric.queryInterfaceTo(&retMetrics[i++]);
|
---|
301 | }
|
---|
302 | retMetrics.detachTo(ComSafeArrayOutArg(outMetrics));
|
---|
303 | LogFlowThisFuncLeave();
|
---|
304 | return rc;
|
---|
305 | }
|
---|
306 |
|
---|
307 | STDMETHODIMP PerformanceCollector::SetupMetrics(ComSafeArrayIn(IN_BSTR, metricNames),
|
---|
308 | ComSafeArrayIn(IUnknown *, objects),
|
---|
309 | ULONG aPeriod,
|
---|
310 | ULONG aCount,
|
---|
311 | ComSafeArrayOut(IPerformanceMetric *, outMetrics))
|
---|
312 | {
|
---|
313 | AutoCaller autoCaller(this);
|
---|
314 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
315 |
|
---|
316 | pm::Filter filter(ComSafeArrayInArg (metricNames),
|
---|
317 | ComSafeArrayInArg (objects));
|
---|
318 |
|
---|
319 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
320 |
|
---|
321 | HRESULT rc = S_OK;
|
---|
322 | BaseMetricList filteredMetrics;
|
---|
323 | BaseMetricList::iterator it;
|
---|
324 | for (it = m.baseMetrics.begin(); it != m.baseMetrics.end(); ++it)
|
---|
325 | if (filter.match((*it)->getObject(), (*it)->getName()))
|
---|
326 | {
|
---|
327 | LogFlow (("PerformanceCollector::SetupMetrics() setting period to %u,"
|
---|
328 | " count to %u for %s\n", aPeriod, aCount, (*it)->getName()));
|
---|
329 | (*it)->init(aPeriod, aCount);
|
---|
330 | if (aPeriod == 0 || aCount == 0)
|
---|
331 | {
|
---|
332 | LogFlow (("PerformanceCollector::SetupMetrics() disabling %s\n",
|
---|
333 | (*it)->getName()));
|
---|
334 | (*it)->disable();
|
---|
335 | }
|
---|
336 | else
|
---|
337 | {
|
---|
338 | LogFlow (("PerformanceCollector::SetupMetrics() enabling %s\n",
|
---|
339 | (*it)->getName()));
|
---|
340 | (*it)->enable();
|
---|
341 | }
|
---|
342 | filteredMetrics.push_back(*it);
|
---|
343 | }
|
---|
344 |
|
---|
345 | com::SafeIfaceArray<IPerformanceMetric> retMetrics(filteredMetrics.size());
|
---|
346 | int i = 0;
|
---|
347 | for (it = filteredMetrics.begin();
|
---|
348 | it != filteredMetrics.end() && SUCCEEDED(rc); ++it)
|
---|
349 | rc = toIPerformanceMetric(*it, &retMetrics[i++]);
|
---|
350 | retMetrics.detachTo(ComSafeArrayOutArg(outMetrics));
|
---|
351 |
|
---|
352 | LogFlowThisFuncLeave();
|
---|
353 | return rc;
|
---|
354 | }
|
---|
355 |
|
---|
356 | STDMETHODIMP PerformanceCollector::EnableMetrics(ComSafeArrayIn(IN_BSTR, metricNames),
|
---|
357 | ComSafeArrayIn(IUnknown *, objects),
|
---|
358 | ComSafeArrayOut(IPerformanceMetric *, outMetrics))
|
---|
359 | {
|
---|
360 | AutoCaller autoCaller(this);
|
---|
361 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
362 |
|
---|
363 | pm::Filter filter(ComSafeArrayInArg(metricNames),
|
---|
364 | ComSafeArrayInArg(objects));
|
---|
365 |
|
---|
366 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); /* Write lock is not needed atm since we are */
|
---|
367 | /* fiddling with enable bit only, but we */
|
---|
368 | /* care for those who come next :-). */
|
---|
369 |
|
---|
370 | HRESULT rc = S_OK;
|
---|
371 | BaseMetricList filteredMetrics;
|
---|
372 | BaseMetricList::iterator it;
|
---|
373 | for (it = m.baseMetrics.begin(); it != m.baseMetrics.end(); ++it)
|
---|
374 | if (filter.match((*it)->getObject(), (*it)->getName()))
|
---|
375 | {
|
---|
376 | (*it)->enable();
|
---|
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 |
|
---|
391 | STDMETHODIMP PerformanceCollector::DisableMetrics(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)->disable();
|
---|
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 |
|
---|
426 | STDMETHODIMP PerformanceCollector::QueryMetricsData(ComSafeArrayIn (IN_BSTR, metricNames),
|
---|
427 | ComSafeArrayIn (IUnknown *, objects),
|
---|
428 | ComSafeArrayOut(BSTR, outMetricNames),
|
---|
429 | ComSafeArrayOut(IUnknown *, outObjects),
|
---|
430 | ComSafeArrayOut(BSTR, outUnits),
|
---|
431 | ComSafeArrayOut(ULONG, outScales),
|
---|
432 | ComSafeArrayOut(ULONG, outSequenceNumbers),
|
---|
433 | ComSafeArrayOut(ULONG, outDataIndices),
|
---|
434 | ComSafeArrayOut(ULONG, outDataLengths),
|
---|
435 | ComSafeArrayOut(LONG, outData))
|
---|
436 | {
|
---|
437 | AutoCaller autoCaller(this);
|
---|
438 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
439 |
|
---|
440 | pm::Filter filter(ComSafeArrayInArg(metricNames),
|
---|
441 | ComSafeArrayInArg(objects));
|
---|
442 |
|
---|
443 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
444 |
|
---|
445 | /* Let's compute the size of the resulting flat array */
|
---|
446 | size_t flatSize = 0;
|
---|
447 | MetricList filteredMetrics;
|
---|
448 | MetricList::iterator it;
|
---|
449 | for (it = m.metrics.begin(); it != m.metrics.end(); ++it)
|
---|
450 | if (filter.match ((*it)->getObject(), (*it)->getName()))
|
---|
451 | {
|
---|
452 | filteredMetrics.push_back (*it);
|
---|
453 | flatSize += (*it)->getLength();
|
---|
454 | }
|
---|
455 |
|
---|
456 | int i = 0;
|
---|
457 | size_t flatIndex = 0;
|
---|
458 | size_t numberOfMetrics = filteredMetrics.size();
|
---|
459 | com::SafeArray<BSTR> retNames(numberOfMetrics);
|
---|
460 | com::SafeIfaceArray<IUnknown> retObjects(numberOfMetrics);
|
---|
461 | com::SafeArray<BSTR> retUnits(numberOfMetrics);
|
---|
462 | com::SafeArray<ULONG> retScales(numberOfMetrics);
|
---|
463 | com::SafeArray<ULONG> retSequenceNumbers(numberOfMetrics);
|
---|
464 | com::SafeArray<ULONG> retIndices(numberOfMetrics);
|
---|
465 | com::SafeArray<ULONG> retLengths(numberOfMetrics);
|
---|
466 | com::SafeArray<LONG> retData(flatSize);
|
---|
467 |
|
---|
468 | for (it = filteredMetrics.begin(); it != filteredMetrics.end(); ++it, ++i)
|
---|
469 | {
|
---|
470 | ULONG *values, length, sequenceNumber;
|
---|
471 | /* @todo We may want to revise the query method to get rid of excessive alloc/memcpy calls. */
|
---|
472 | (*it)->query(&values, &length, &sequenceNumber);
|
---|
473 | LogFlow (("PerformanceCollector::QueryMetricsData() querying metric %s "
|
---|
474 | "returned %d values.\n", (*it)->getName(), length));
|
---|
475 | memcpy(retData.raw() + flatIndex, values, length * sizeof(*values));
|
---|
476 | Bstr tmp((*it)->getName());
|
---|
477 | tmp.detachTo(&retNames[i]);
|
---|
478 | (*it)->getObject().queryInterfaceTo(&retObjects[i]);
|
---|
479 | tmp = (*it)->getUnit();
|
---|
480 | tmp.detachTo(&retUnits[i]);
|
---|
481 | retScales[i] = (*it)->getScale();
|
---|
482 | retSequenceNumbers[i] = sequenceNumber;
|
---|
483 | retLengths[i] = length;
|
---|
484 | retIndices[i] = (ULONG)flatIndex;
|
---|
485 | flatIndex += length;
|
---|
486 | }
|
---|
487 |
|
---|
488 | retNames.detachTo(ComSafeArrayOutArg(outMetricNames));
|
---|
489 | retObjects.detachTo(ComSafeArrayOutArg(outObjects));
|
---|
490 | retUnits.detachTo(ComSafeArrayOutArg(outUnits));
|
---|
491 | retScales.detachTo(ComSafeArrayOutArg(outScales));
|
---|
492 | retSequenceNumbers.detachTo(ComSafeArrayOutArg(outSequenceNumbers));
|
---|
493 | retIndices.detachTo(ComSafeArrayOutArg(outDataIndices));
|
---|
494 | retLengths.detachTo(ComSafeArrayOutArg(outDataLengths));
|
---|
495 | retData.detachTo(ComSafeArrayOutArg(outData));
|
---|
496 | return S_OK;
|
---|
497 | }
|
---|
498 |
|
---|
499 | // public methods for internal purposes
|
---|
500 | ///////////////////////////////////////////////////////////////////////////////
|
---|
501 |
|
---|
502 | void PerformanceCollector::registerBaseMetric(pm::BaseMetric *baseMetric)
|
---|
503 | {
|
---|
504 | //LogFlowThisFuncEnter();
|
---|
505 | AutoCaller autoCaller(this);
|
---|
506 | if (!SUCCEEDED(autoCaller.rc())) return;
|
---|
507 |
|
---|
508 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
509 | LogAleksey(("{%p} " LOG_FN_FMT ": obj=%p name=%s\n", this, __PRETTY_FUNCTION__, (void *)baseMetric->getObject(), baseMetric->getName()));
|
---|
510 | m.baseMetrics.push_back (baseMetric);
|
---|
511 | //LogFlowThisFuncLeave();
|
---|
512 | }
|
---|
513 |
|
---|
514 | void PerformanceCollector::registerMetric(pm::Metric *metric)
|
---|
515 | {
|
---|
516 | //LogFlowThisFuncEnter();
|
---|
517 | AutoCaller autoCaller(this);
|
---|
518 | if (!SUCCEEDED(autoCaller.rc())) return;
|
---|
519 |
|
---|
520 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
521 | LogAleksey(("{%p} " LOG_FN_FMT ": obj=%p name=%s\n", this, __PRETTY_FUNCTION__, (void *)metric->getObject(), metric->getName()));
|
---|
522 | m.metrics.push_back (metric);
|
---|
523 | //LogFlowThisFuncLeave();
|
---|
524 | }
|
---|
525 |
|
---|
526 | void PerformanceCollector::unregisterBaseMetricsFor(const ComPtr<IUnknown> &aObject)
|
---|
527 | {
|
---|
528 | //LogFlowThisFuncEnter();
|
---|
529 | AutoCaller autoCaller(this);
|
---|
530 | if (!SUCCEEDED(autoCaller.rc())) return;
|
---|
531 |
|
---|
532 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
533 | LogAleksey(("{%p} " LOG_FN_FMT ": before remove_if: m.baseMetrics.size()=%d\n", this, __PRETTY_FUNCTION__, m.baseMetrics.size()));
|
---|
534 | BaseMetricList::iterator it;
|
---|
535 | for (it = m.baseMetrics.begin(); it != m.baseMetrics.end();)
|
---|
536 | if ((*it)->associatedWith(aObject))
|
---|
537 | {
|
---|
538 | delete *it;
|
---|
539 | it = m.baseMetrics.erase(it);
|
---|
540 | }
|
---|
541 | else
|
---|
542 | ++it;
|
---|
543 | LogAleksey(("{%p} " LOG_FN_FMT ": after remove_if: m.baseMetrics.size()=%d\n", this, __PRETTY_FUNCTION__, m.baseMetrics.size()));
|
---|
544 | //LogFlowThisFuncLeave();
|
---|
545 | }
|
---|
546 |
|
---|
547 | void PerformanceCollector::unregisterMetricsFor(const ComPtr<IUnknown> &aObject)
|
---|
548 | {
|
---|
549 | //LogFlowThisFuncEnter();
|
---|
550 | AutoCaller autoCaller(this);
|
---|
551 | if (!SUCCEEDED(autoCaller.rc())) return;
|
---|
552 |
|
---|
553 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
554 | LogAleksey(("{%p} " LOG_FN_FMT ": obj=%p\n", this, __PRETTY_FUNCTION__, (void *)aObject));
|
---|
555 | MetricList::iterator it;
|
---|
556 | for (it = m.metrics.begin(); it != m.metrics.end();)
|
---|
557 | if ((*it)->associatedWith(aObject))
|
---|
558 | {
|
---|
559 | delete *it;
|
---|
560 | it = m.metrics.erase(it);
|
---|
561 | }
|
---|
562 | else
|
---|
563 | ++it;
|
---|
564 | //LogFlowThisFuncLeave();
|
---|
565 | }
|
---|
566 |
|
---|
567 | void PerformanceCollector::suspendSampling()
|
---|
568 | {
|
---|
569 | AutoCaller autoCaller(this);
|
---|
570 | if (!SUCCEEDED(autoCaller.rc())) return;
|
---|
571 |
|
---|
572 | int rc = RTTimerLRStop(m.sampler);
|
---|
573 | AssertRC(rc);
|
---|
574 | }
|
---|
575 |
|
---|
576 | void PerformanceCollector::resumeSampling()
|
---|
577 | {
|
---|
578 | AutoCaller autoCaller(this);
|
---|
579 | if (!SUCCEEDED(autoCaller.rc())) return;
|
---|
580 |
|
---|
581 | int rc = RTTimerLRStart(m.sampler, 0);
|
---|
582 | AssertRC(rc);
|
---|
583 | }
|
---|
584 |
|
---|
585 |
|
---|
586 | // private methods
|
---|
587 | ///////////////////////////////////////////////////////////////////////////////
|
---|
588 |
|
---|
589 | /* static */
|
---|
590 | void PerformanceCollector::staticSamplerCallback(RTTIMERLR hTimerLR, void *pvUser,
|
---|
591 | uint64_t iTick)
|
---|
592 | {
|
---|
593 | AssertReturnVoid (pvUser != NULL);
|
---|
594 | PerformanceCollector *collector = static_cast <PerformanceCollector *> (pvUser);
|
---|
595 | Assert(collector->mMagic == MAGIC);
|
---|
596 | if (collector->mMagic == MAGIC)
|
---|
597 | collector->samplerCallback(iTick);
|
---|
598 |
|
---|
599 | NOREF (hTimerLR);
|
---|
600 | }
|
---|
601 |
|
---|
602 | void PerformanceCollector::samplerCallback(uint64_t iTick)
|
---|
603 | {
|
---|
604 | Log4(("{%p} " LOG_FN_FMT ": ENTER\n", this, __PRETTY_FUNCTION__));
|
---|
605 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
606 |
|
---|
607 | pm::CollectorHints hints;
|
---|
608 | uint64_t timestamp = RTTimeMilliTS();
|
---|
609 | BaseMetricList toBeCollected;
|
---|
610 | BaseMetricList::iterator it;
|
---|
611 | /* Compose the list of metrics being collected at this moment */
|
---|
612 | for (it = m.baseMetrics.begin(); it != m.baseMetrics.end(); it++)
|
---|
613 | if ((*it)->collectorBeat(timestamp))
|
---|
614 | {
|
---|
615 | (*it)->preCollect(hints, iTick);
|
---|
616 | toBeCollected.push_back(*it);
|
---|
617 | }
|
---|
618 |
|
---|
619 | if (toBeCollected.size() == 0)
|
---|
620 | return;
|
---|
621 |
|
---|
622 | /* Let know the platform specific code what is being collected */
|
---|
623 | m.hal->preCollect(hints, iTick);
|
---|
624 |
|
---|
625 | /* Finally, collect the data */
|
---|
626 | std::for_each (toBeCollected.begin(), toBeCollected.end(),
|
---|
627 | std::mem_fun (&pm::BaseMetric::collect));
|
---|
628 | Log4(("{%p} " LOG_FN_FMT ": LEAVE\n", this, __PRETTY_FUNCTION__));
|
---|
629 | }
|
---|
630 |
|
---|
631 | ////////////////////////////////////////////////////////////////////////////////
|
---|
632 | // PerformanceMetric class
|
---|
633 | ////////////////////////////////////////////////////////////////////////////////
|
---|
634 |
|
---|
635 | // constructor / destructor
|
---|
636 | ////////////////////////////////////////////////////////////////////////////////
|
---|
637 |
|
---|
638 | PerformanceMetric::PerformanceMetric()
|
---|
639 | {
|
---|
640 | }
|
---|
641 |
|
---|
642 | PerformanceMetric::~PerformanceMetric()
|
---|
643 | {
|
---|
644 | }
|
---|
645 |
|
---|
646 | HRESULT PerformanceMetric::FinalConstruct()
|
---|
647 | {
|
---|
648 | LogFlowThisFunc(("\n"));
|
---|
649 |
|
---|
650 | return BaseFinalConstruct();
|
---|
651 | }
|
---|
652 |
|
---|
653 | void PerformanceMetric::FinalRelease()
|
---|
654 | {
|
---|
655 | LogFlowThisFunc(("\n"));
|
---|
656 |
|
---|
657 | uninit ();
|
---|
658 |
|
---|
659 | BaseFinalRelease();
|
---|
660 | }
|
---|
661 |
|
---|
662 | // public initializer/uninitializer for internal purposes only
|
---|
663 | ////////////////////////////////////////////////////////////////////////////////
|
---|
664 |
|
---|
665 | HRESULT PerformanceMetric::init(pm::Metric *aMetric)
|
---|
666 | {
|
---|
667 | m.name = aMetric->getName();
|
---|
668 | m.object = aMetric->getObject();
|
---|
669 | m.description = aMetric->getDescription();
|
---|
670 | m.period = aMetric->getPeriod();
|
---|
671 | m.count = aMetric->getLength();
|
---|
672 | m.unit = aMetric->getUnit();
|
---|
673 | m.min = aMetric->getMinValue();
|
---|
674 | m.max = aMetric->getMaxValue();
|
---|
675 | return S_OK;
|
---|
676 | }
|
---|
677 |
|
---|
678 | HRESULT PerformanceMetric::init(pm::BaseMetric *aMetric)
|
---|
679 | {
|
---|
680 | m.name = aMetric->getName();
|
---|
681 | m.object = aMetric->getObject();
|
---|
682 | m.description = "";
|
---|
683 | m.period = aMetric->getPeriod();
|
---|
684 | m.count = aMetric->getLength();
|
---|
685 | m.unit = aMetric->getUnit();
|
---|
686 | m.min = aMetric->getMinValue();
|
---|
687 | m.max = aMetric->getMaxValue();
|
---|
688 | return S_OK;
|
---|
689 | }
|
---|
690 |
|
---|
691 | void PerformanceMetric::uninit()
|
---|
692 | {
|
---|
693 | }
|
---|
694 |
|
---|
695 | STDMETHODIMP PerformanceMetric::COMGETTER(MetricName)(BSTR *aMetricName)
|
---|
696 | {
|
---|
697 | /// @todo (r=dmik) why do all these getters not do AutoCaller and
|
---|
698 | /// AutoReadLock? Is the underlying metric a constant object?
|
---|
699 |
|
---|
700 | m.name.cloneTo(aMetricName);
|
---|
701 | return S_OK;
|
---|
702 | }
|
---|
703 |
|
---|
704 | STDMETHODIMP PerformanceMetric::COMGETTER(Object)(IUnknown **anObject)
|
---|
705 | {
|
---|
706 | m.object.queryInterfaceTo(anObject);
|
---|
707 | return S_OK;
|
---|
708 | }
|
---|
709 |
|
---|
710 | STDMETHODIMP PerformanceMetric::COMGETTER(Description)(BSTR *aDescription)
|
---|
711 | {
|
---|
712 | m.description.cloneTo(aDescription);
|
---|
713 | return S_OK;
|
---|
714 | }
|
---|
715 |
|
---|
716 | STDMETHODIMP PerformanceMetric::COMGETTER(Period)(ULONG *aPeriod)
|
---|
717 | {
|
---|
718 | *aPeriod = m.period;
|
---|
719 | return S_OK;
|
---|
720 | }
|
---|
721 |
|
---|
722 | STDMETHODIMP PerformanceMetric::COMGETTER(Count)(ULONG *aCount)
|
---|
723 | {
|
---|
724 | *aCount = m.count;
|
---|
725 | return S_OK;
|
---|
726 | }
|
---|
727 |
|
---|
728 | STDMETHODIMP PerformanceMetric::COMGETTER(Unit)(BSTR *aUnit)
|
---|
729 | {
|
---|
730 | m.unit.cloneTo(aUnit);
|
---|
731 | return S_OK;
|
---|
732 | }
|
---|
733 |
|
---|
734 | STDMETHODIMP PerformanceMetric::COMGETTER(MinimumValue)(LONG *aMinValue)
|
---|
735 | {
|
---|
736 | *aMinValue = m.min;
|
---|
737 | return S_OK;
|
---|
738 | }
|
---|
739 |
|
---|
740 | STDMETHODIMP PerformanceMetric::COMGETTER(MaximumValue)(LONG *aMaxValue)
|
---|
741 | {
|
---|
742 | *aMaxValue = m.max;
|
---|
743 | return S_OK;
|
---|
744 | }
|
---|
745 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|