1 | /* $Id: Performance.cpp 30764 2010-07-09 14:12:12Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | *
|
---|
5 | * VBox Performance Classes implementation.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2008 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 | * @todo list:
|
---|
22 | *
|
---|
23 | * 1) Detection of erroneous metric names
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef VBOX_COLLECTOR_TEST_CASE
|
---|
27 | #include "VirtualBoxImpl.h"
|
---|
28 | #include "MachineImpl.h"
|
---|
29 | #endif
|
---|
30 | #include "Performance.h"
|
---|
31 |
|
---|
32 | #include <VBox/com/array.h>
|
---|
33 | #include <VBox/com/ptr.h>
|
---|
34 | #include <VBox/com/string.h>
|
---|
35 | #include <VBox/err.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/mem.h>
|
---|
38 | #include <iprt/cpuset.h>
|
---|
39 |
|
---|
40 | #include <algorithm>
|
---|
41 |
|
---|
42 | #include "Logging.h"
|
---|
43 |
|
---|
44 | using namespace pm;
|
---|
45 |
|
---|
46 | // Stubs for non-pure virtual methods
|
---|
47 |
|
---|
48 | int CollectorHAL::getHostCpuLoad(ULONG * /* user */, ULONG * /* kernel */, ULONG * /* idle */)
|
---|
49 | {
|
---|
50 | return E_NOTIMPL;
|
---|
51 | }
|
---|
52 |
|
---|
53 | int CollectorHAL::getProcessCpuLoad(RTPROCESS /* process */, ULONG * /* user */, ULONG * /* kernel */)
|
---|
54 | {
|
---|
55 | return E_NOTIMPL;
|
---|
56 | }
|
---|
57 |
|
---|
58 | int CollectorHAL::getRawHostCpuLoad(uint64_t * /* user */, uint64_t * /* kernel */, uint64_t * /* idle */)
|
---|
59 | {
|
---|
60 | return E_NOTIMPL;
|
---|
61 | }
|
---|
62 |
|
---|
63 | int CollectorHAL::getRawProcessCpuLoad(RTPROCESS /* process */, uint64_t * /* user */, uint64_t * /* kernel */, uint64_t * /* total */)
|
---|
64 | {
|
---|
65 | return E_NOTIMPL;
|
---|
66 | }
|
---|
67 |
|
---|
68 | int CollectorHAL::getHostMemoryUsage(ULONG * /* total */, ULONG * /* used */, ULONG * /* available */)
|
---|
69 | {
|
---|
70 | return E_NOTIMPL;
|
---|
71 | }
|
---|
72 |
|
---|
73 | int CollectorHAL::getProcessMemoryUsage(RTPROCESS /* process */, ULONG * /* used */)
|
---|
74 | {
|
---|
75 | return E_NOTIMPL;
|
---|
76 | }
|
---|
77 |
|
---|
78 | int CollectorHAL::enable()
|
---|
79 | {
|
---|
80 | return E_NOTIMPL;
|
---|
81 | }
|
---|
82 |
|
---|
83 | int CollectorHAL::disable()
|
---|
84 | {
|
---|
85 | return E_NOTIMPL;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /* Generic implementations */
|
---|
89 |
|
---|
90 | int CollectorHAL::getHostCpuMHz(ULONG *mhz)
|
---|
91 | {
|
---|
92 | unsigned cCpus = 0;
|
---|
93 | uint64_t u64TotalMHz = 0;
|
---|
94 | RTCPUSET OnlineSet;
|
---|
95 | RTMpGetOnlineSet(&OnlineSet);
|
---|
96 | for (RTCPUID iCpu = 0; iCpu < RTCPUSET_MAX_CPUS; iCpu++)
|
---|
97 | {
|
---|
98 | LogAleksey(("{%p} " LOG_FN_FMT ": Checking if CPU %d is member of online set...\n",
|
---|
99 | this, __PRETTY_FUNCTION__, (int)iCpu));
|
---|
100 | if (RTCpuSetIsMemberByIndex(&OnlineSet, iCpu))
|
---|
101 | {
|
---|
102 | LogAleksey(("{%p} " LOG_FN_FMT ": Getting frequency for CPU %d...\n",
|
---|
103 | this, __PRETTY_FUNCTION__, (int)iCpu));
|
---|
104 | uint32_t uMHz = RTMpGetCurFrequency(RTMpCpuIdFromSetIndex(iCpu));
|
---|
105 | if (uMHz != 0)
|
---|
106 | {
|
---|
107 | LogAleksey(("{%p} " LOG_FN_FMT ": CPU %d %u MHz\n",
|
---|
108 | this, __PRETTY_FUNCTION__, (int)iCpu, uMHz));
|
---|
109 | u64TotalMHz += uMHz;
|
---|
110 | cCpus++;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | AssertReturn(cCpus, VERR_NOT_IMPLEMENTED);
|
---|
116 | *mhz = (ULONG)(u64TotalMHz / cCpus);
|
---|
117 |
|
---|
118 | return VINF_SUCCESS;
|
---|
119 | }
|
---|
120 |
|
---|
121 | #ifndef VBOX_COLLECTOR_TEST_CASE
|
---|
122 | CollectorGuestHAL::~CollectorGuestHAL()
|
---|
123 | {
|
---|
124 | Assert(!cEnabled);
|
---|
125 | }
|
---|
126 |
|
---|
127 | int CollectorGuestHAL::enable()
|
---|
128 | {
|
---|
129 | HRESULT ret = S_OK;
|
---|
130 |
|
---|
131 | if (ASMAtomicIncU32(&cEnabled) == 1)
|
---|
132 | {
|
---|
133 | ComPtr<IInternalSessionControl> directControl;
|
---|
134 |
|
---|
135 | ret = mMachine->getDirectControl(&directControl);
|
---|
136 | if (ret != S_OK)
|
---|
137 | return ret;
|
---|
138 |
|
---|
139 | /* get the associated console; this is a remote call (!) */
|
---|
140 | ret = directControl->GetRemoteConsole(mConsole.asOutParam());
|
---|
141 | if (ret != S_OK)
|
---|
142 | return ret;
|
---|
143 |
|
---|
144 | ret = mConsole->COMGETTER(Guest)(mGuest.asOutParam());
|
---|
145 | if (ret == S_OK)
|
---|
146 | mGuest->COMSETTER(StatisticsUpdateInterval)(1 /* 1 sec */);
|
---|
147 | }
|
---|
148 | return ret;
|
---|
149 | }
|
---|
150 |
|
---|
151 | int CollectorGuestHAL::disable()
|
---|
152 | {
|
---|
153 | if (ASMAtomicDecU32(&cEnabled) == 0)
|
---|
154 | {
|
---|
155 | Assert(mGuest && mConsole);
|
---|
156 | mGuest->COMSETTER(StatisticsUpdateInterval)(0 /* off */);
|
---|
157 | }
|
---|
158 | return S_OK;
|
---|
159 | }
|
---|
160 |
|
---|
161 | int CollectorGuestHAL::preCollect(const CollectorHints& /* hints */, uint64_t iTick)
|
---|
162 | {
|
---|
163 | if ( mGuest
|
---|
164 | && iTick != mLastTick)
|
---|
165 | {
|
---|
166 | ULONG ulMemAllocTotal, ulMemFreeTotal, ulMemBalloonTotal, ulMemSharedTotal;
|
---|
167 |
|
---|
168 | mGuest->InternalGetStatistics(&mCpuUser, &mCpuKernel, &mCpuIdle,
|
---|
169 | &mMemTotal, &mMemFree, &mMemBalloon, &mMemShared, &mMemCache,
|
---|
170 | &mPageTotal, &ulMemAllocTotal, &ulMemFreeTotal, &ulMemBalloonTotal, &ulMemSharedTotal);
|
---|
171 |
|
---|
172 | if (mHostHAL)
|
---|
173 | mHostHAL->setMemHypervisorStats(ulMemAllocTotal, ulMemFreeTotal, ulMemBalloonTotal, ulMemSharedTotal);
|
---|
174 |
|
---|
175 | mLastTick = iTick;
|
---|
176 | }
|
---|
177 | return S_OK;
|
---|
178 | }
|
---|
179 |
|
---|
180 | #endif /* !VBOX_COLLECTOR_TEST_CASE */
|
---|
181 |
|
---|
182 | bool BaseMetric::collectorBeat(uint64_t nowAt)
|
---|
183 | {
|
---|
184 | if (isEnabled())
|
---|
185 | {
|
---|
186 | if (nowAt - mLastSampleTaken >= mPeriod * 1000)
|
---|
187 | {
|
---|
188 | mLastSampleTaken = nowAt;
|
---|
189 | Log4(("{%p} " LOG_FN_FMT ": Collecting %s for obj(%p)...\n",
|
---|
190 | this, __PRETTY_FUNCTION__, getName(), (void *)mObject));
|
---|
191 | return true;
|
---|
192 | }
|
---|
193 | }
|
---|
194 | return false;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /*bool BaseMetric::associatedWith(ComPtr<IUnknown> object)
|
---|
198 | {
|
---|
199 | LogFlowThisFunc(("mObject(%p) == object(%p) is %s.\n", mObject, object, mObject == object ? "true" : "false"));
|
---|
200 | return mObject == object;
|
---|
201 | }*/
|
---|
202 |
|
---|
203 | void HostCpuLoad::init(ULONG period, ULONG length)
|
---|
204 | {
|
---|
205 | mPeriod = period;
|
---|
206 | mLength = length;
|
---|
207 | mUser->init(mLength);
|
---|
208 | mKernel->init(mLength);
|
---|
209 | mIdle->init(mLength);
|
---|
210 | }
|
---|
211 |
|
---|
212 | void HostCpuLoad::collect()
|
---|
213 | {
|
---|
214 | ULONG user, kernel, idle;
|
---|
215 | int rc = mHAL->getHostCpuLoad(&user, &kernel, &idle);
|
---|
216 | if (RT_SUCCESS(rc))
|
---|
217 | {
|
---|
218 | mUser->put(user);
|
---|
219 | mKernel->put(kernel);
|
---|
220 | mIdle->put(idle);
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | void HostCpuLoadRaw::preCollect(CollectorHints& hints, uint64_t /* iTick */)
|
---|
225 | {
|
---|
226 | hints.collectHostCpuLoad();
|
---|
227 | }
|
---|
228 |
|
---|
229 | void HostCpuLoadRaw::collect()
|
---|
230 | {
|
---|
231 | uint64_t user, kernel, idle;
|
---|
232 | uint64_t userDiff, kernelDiff, idleDiff, totalDiff;
|
---|
233 |
|
---|
234 | int rc = mHAL->getRawHostCpuLoad(&user, &kernel, &idle);
|
---|
235 | if (RT_SUCCESS(rc))
|
---|
236 | {
|
---|
237 | userDiff = user - mUserPrev;
|
---|
238 | kernelDiff = kernel - mKernelPrev;
|
---|
239 | idleDiff = idle - mIdlePrev;
|
---|
240 | totalDiff = userDiff + kernelDiff + idleDiff;
|
---|
241 |
|
---|
242 | if (totalDiff == 0)
|
---|
243 | {
|
---|
244 | /* This is only possible if none of counters has changed! */
|
---|
245 | LogFlowThisFunc(("Impossible! User, kernel and idle raw "
|
---|
246 | "counters has not changed since last sample.\n" ));
|
---|
247 | mUser->put(0);
|
---|
248 | mKernel->put(0);
|
---|
249 | mIdle->put(0);
|
---|
250 | }
|
---|
251 | else
|
---|
252 | {
|
---|
253 | mUser->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * userDiff / totalDiff));
|
---|
254 | mKernel->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * kernelDiff / totalDiff));
|
---|
255 | mIdle->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * idleDiff / totalDiff));
|
---|
256 | }
|
---|
257 |
|
---|
258 | mUserPrev = user;
|
---|
259 | mKernelPrev = kernel;
|
---|
260 | mIdlePrev = idle;
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | void HostCpuMhz::init(ULONG period, ULONG length)
|
---|
265 | {
|
---|
266 | mPeriod = period;
|
---|
267 | mLength = length;
|
---|
268 | mMHz->init(mLength);
|
---|
269 | }
|
---|
270 |
|
---|
271 | void HostCpuMhz::collect()
|
---|
272 | {
|
---|
273 | ULONG mhz;
|
---|
274 | int rc = mHAL->getHostCpuMHz(&mhz);
|
---|
275 | if (RT_SUCCESS(rc))
|
---|
276 | mMHz->put(mhz);
|
---|
277 | }
|
---|
278 |
|
---|
279 | void HostRamUsage::init(ULONG period, ULONG length)
|
---|
280 | {
|
---|
281 | mPeriod = period;
|
---|
282 | mLength = length;
|
---|
283 | mTotal->init(mLength);
|
---|
284 | mUsed->init(mLength);
|
---|
285 | mAvailable->init(mLength);
|
---|
286 | mAllocVMM->init(mLength);
|
---|
287 | mFreeVMM->init(mLength);
|
---|
288 | mBalloonVMM->init(mLength);
|
---|
289 | mSharedVMM->init(mLength);
|
---|
290 | }
|
---|
291 |
|
---|
292 | void HostRamUsage::preCollect(CollectorHints& hints, uint64_t /* iTick */)
|
---|
293 | {
|
---|
294 | hints.collectHostRamUsage();
|
---|
295 | }
|
---|
296 |
|
---|
297 | void HostRamUsage::collect()
|
---|
298 | {
|
---|
299 | ULONG total, used, available;
|
---|
300 | int rc = mHAL->getHostMemoryUsage(&total, &used, &available);
|
---|
301 | if (RT_SUCCESS(rc))
|
---|
302 | {
|
---|
303 | mTotal->put(total);
|
---|
304 | mUsed->put(used);
|
---|
305 | mAvailable->put(available);
|
---|
306 |
|
---|
307 | }
|
---|
308 | ULONG allocVMM, freeVMM, balloonVMM, sharedVMM;
|
---|
309 |
|
---|
310 | mHAL->getMemHypervisorStats(&allocVMM, &freeVMM, &balloonVMM, &sharedVMM);
|
---|
311 | mAllocVMM->put(allocVMM);
|
---|
312 | mFreeVMM->put(freeVMM);
|
---|
313 | mBalloonVMM->put(balloonVMM);
|
---|
314 | mSharedVMM->put(sharedVMM);
|
---|
315 | }
|
---|
316 |
|
---|
317 |
|
---|
318 |
|
---|
319 | void MachineCpuLoad::init(ULONG period, ULONG length)
|
---|
320 | {
|
---|
321 | mPeriod = period;
|
---|
322 | mLength = length;
|
---|
323 | mUser->init(mLength);
|
---|
324 | mKernel->init(mLength);
|
---|
325 | }
|
---|
326 |
|
---|
327 | void MachineCpuLoad::collect()
|
---|
328 | {
|
---|
329 | ULONG user, kernel;
|
---|
330 | int rc = mHAL->getProcessCpuLoad(mProcess, &user, &kernel);
|
---|
331 | if (RT_SUCCESS(rc))
|
---|
332 | {
|
---|
333 | mUser->put(user);
|
---|
334 | mKernel->put(kernel);
|
---|
335 | }
|
---|
336 | }
|
---|
337 |
|
---|
338 | void MachineCpuLoadRaw::preCollect(CollectorHints& hints, uint64_t /* iTick */)
|
---|
339 | {
|
---|
340 | hints.collectProcessCpuLoad(mProcess);
|
---|
341 | }
|
---|
342 |
|
---|
343 | void MachineCpuLoadRaw::collect()
|
---|
344 | {
|
---|
345 | uint64_t processUser, processKernel, hostTotal;
|
---|
346 |
|
---|
347 | int rc = mHAL->getRawProcessCpuLoad(mProcess, &processUser, &processKernel, &hostTotal);
|
---|
348 | if (RT_SUCCESS(rc))
|
---|
349 | {
|
---|
350 | if (hostTotal == mHostTotalPrev)
|
---|
351 | {
|
---|
352 | /* Nearly impossible, but... */
|
---|
353 | mUser->put(0);
|
---|
354 | mKernel->put(0);
|
---|
355 | }
|
---|
356 | else
|
---|
357 | {
|
---|
358 | mUser->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * (processUser - mProcessUserPrev) / (hostTotal - mHostTotalPrev)));
|
---|
359 | mKernel->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * (processKernel - mProcessKernelPrev ) / (hostTotal - mHostTotalPrev)));
|
---|
360 | }
|
---|
361 |
|
---|
362 | mHostTotalPrev = hostTotal;
|
---|
363 | mProcessUserPrev = processUser;
|
---|
364 | mProcessKernelPrev = processKernel;
|
---|
365 | }
|
---|
366 | }
|
---|
367 |
|
---|
368 | void MachineRamUsage::init(ULONG period, ULONG length)
|
---|
369 | {
|
---|
370 | mPeriod = period;
|
---|
371 | mLength = length;
|
---|
372 | mUsed->init(mLength);
|
---|
373 | }
|
---|
374 |
|
---|
375 | void MachineRamUsage::preCollect(CollectorHints& hints, uint64_t /* iTick */)
|
---|
376 | {
|
---|
377 | hints.collectProcessRamUsage(mProcess);
|
---|
378 | }
|
---|
379 |
|
---|
380 | void MachineRamUsage::collect()
|
---|
381 | {
|
---|
382 | ULONG used;
|
---|
383 | int rc = mHAL->getProcessMemoryUsage(mProcess, &used);
|
---|
384 | if (RT_SUCCESS(rc))
|
---|
385 | mUsed->put(used);
|
---|
386 | }
|
---|
387 |
|
---|
388 |
|
---|
389 | void GuestCpuLoad::init(ULONG period, ULONG length)
|
---|
390 | {
|
---|
391 | mPeriod = period;
|
---|
392 | mLength = length;
|
---|
393 |
|
---|
394 | mUser->init(mLength);
|
---|
395 | mKernel->init(mLength);
|
---|
396 | mIdle->init(mLength);
|
---|
397 | }
|
---|
398 |
|
---|
399 | void GuestCpuLoad::preCollect(CollectorHints& hints, uint64_t iTick)
|
---|
400 | {
|
---|
401 | mHAL->preCollect(hints, iTick);
|
---|
402 | }
|
---|
403 |
|
---|
404 | void GuestCpuLoad::collect()
|
---|
405 | {
|
---|
406 | ULONG CpuUser = 0, CpuKernel = 0, CpuIdle = 0;
|
---|
407 |
|
---|
408 | mGuestHAL->getGuestCpuLoad(&CpuUser, &CpuKernel, &CpuIdle);
|
---|
409 | mUser->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * CpuUser) / 100);
|
---|
410 | mKernel->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * CpuKernel) / 100);
|
---|
411 | mIdle->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * CpuIdle) / 100);
|
---|
412 | }
|
---|
413 |
|
---|
414 | void GuestRamUsage::init(ULONG period, ULONG length)
|
---|
415 | {
|
---|
416 | mPeriod = period;
|
---|
417 | mLength = length;
|
---|
418 |
|
---|
419 | mTotal->init(mLength);
|
---|
420 | mFree->init(mLength);
|
---|
421 | mBallooned->init(mLength);
|
---|
422 | mShared->init(mLength);
|
---|
423 | mCache->init(mLength);
|
---|
424 | mPagedTotal->init(mLength);
|
---|
425 | }
|
---|
426 |
|
---|
427 | void GuestRamUsage::preCollect(CollectorHints& hints, uint64_t iTick)
|
---|
428 | {
|
---|
429 | mHAL->preCollect(hints, iTick);
|
---|
430 | }
|
---|
431 |
|
---|
432 | void GuestRamUsage::collect()
|
---|
433 | {
|
---|
434 | ULONG ulMemTotal = 0, ulMemFree = 0, ulMemBalloon = 0, ulMemShared = 0, ulMemCache = 0, ulPageTotal = 0;
|
---|
435 |
|
---|
436 | mGuestHAL->getGuestMemLoad(&ulMemTotal, &ulMemFree, &ulMemBalloon, &ulMemShared, &ulMemCache, &ulPageTotal);
|
---|
437 | mTotal->put(ulMemTotal);
|
---|
438 | mFree->put(ulMemFree);
|
---|
439 | mBallooned->put(ulMemBalloon);
|
---|
440 | mShared->put(ulMemShared);
|
---|
441 | mCache->put(ulMemCache);
|
---|
442 | mPagedTotal->put(ulPageTotal);
|
---|
443 | }
|
---|
444 |
|
---|
445 | void CircularBuffer::init(ULONG ulLength)
|
---|
446 | {
|
---|
447 | if (mData)
|
---|
448 | RTMemFree(mData);
|
---|
449 | mLength = ulLength;
|
---|
450 | if (mLength)
|
---|
451 | mData = (ULONG*)RTMemAllocZ(ulLength * sizeof(ULONG));
|
---|
452 | else
|
---|
453 | mData = NULL;
|
---|
454 | mWrapped = false;
|
---|
455 | mEnd = 0;
|
---|
456 | mSequenceNumber = 0;
|
---|
457 | }
|
---|
458 |
|
---|
459 | ULONG CircularBuffer::length()
|
---|
460 | {
|
---|
461 | return mWrapped ? mLength : mEnd;
|
---|
462 | }
|
---|
463 |
|
---|
464 | void CircularBuffer::put(ULONG value)
|
---|
465 | {
|
---|
466 | if (mData)
|
---|
467 | {
|
---|
468 | mData[mEnd++] = value;
|
---|
469 | if (mEnd >= mLength)
|
---|
470 | {
|
---|
471 | mEnd = 0;
|
---|
472 | mWrapped = true;
|
---|
473 | }
|
---|
474 | ++mSequenceNumber;
|
---|
475 | }
|
---|
476 | }
|
---|
477 |
|
---|
478 | void CircularBuffer::copyTo(ULONG *data)
|
---|
479 | {
|
---|
480 | if (mWrapped)
|
---|
481 | {
|
---|
482 | memcpy(data, mData + mEnd, (mLength - mEnd) * sizeof(ULONG));
|
---|
483 | // Copy the wrapped part
|
---|
484 | if (mEnd)
|
---|
485 | memcpy(data + (mLength - mEnd), mData, mEnd * sizeof(ULONG));
|
---|
486 | }
|
---|
487 | else
|
---|
488 | memcpy(data, mData, mEnd * sizeof(ULONG));
|
---|
489 | }
|
---|
490 |
|
---|
491 | void SubMetric::query(ULONG *data)
|
---|
492 | {
|
---|
493 | copyTo(data);
|
---|
494 | }
|
---|
495 |
|
---|
496 | void Metric::query(ULONG **data, ULONG *count, ULONG *sequenceNumber)
|
---|
497 | {
|
---|
498 | ULONG length;
|
---|
499 | ULONG *tmpData;
|
---|
500 |
|
---|
501 | length = mSubMetric->length();
|
---|
502 | *sequenceNumber = mSubMetric->getSequenceNumber() - length;
|
---|
503 | if (length)
|
---|
504 | {
|
---|
505 | tmpData = (ULONG*)RTMemAlloc(sizeof(*tmpData)*length);
|
---|
506 | mSubMetric->query(tmpData);
|
---|
507 | if (mAggregate)
|
---|
508 | {
|
---|
509 | *count = 1;
|
---|
510 | *data = (ULONG*)RTMemAlloc(sizeof(**data));
|
---|
511 | **data = mAggregate->compute(tmpData, length);
|
---|
512 | RTMemFree(tmpData);
|
---|
513 | }
|
---|
514 | else
|
---|
515 | {
|
---|
516 | *count = length;
|
---|
517 | *data = tmpData;
|
---|
518 | }
|
---|
519 | }
|
---|
520 | else
|
---|
521 | {
|
---|
522 | *count = 0;
|
---|
523 | *data = 0;
|
---|
524 | }
|
---|
525 | }
|
---|
526 |
|
---|
527 | ULONG AggregateAvg::compute(ULONG *data, ULONG length)
|
---|
528 | {
|
---|
529 | uint64_t tmp = 0;
|
---|
530 | for (ULONG i = 0; i < length; ++i)
|
---|
531 | tmp += data[i];
|
---|
532 | return (ULONG)(tmp / length);
|
---|
533 | }
|
---|
534 |
|
---|
535 | const char * AggregateAvg::getName()
|
---|
536 | {
|
---|
537 | return "avg";
|
---|
538 | }
|
---|
539 |
|
---|
540 | ULONG AggregateMin::compute(ULONG *data, ULONG length)
|
---|
541 | {
|
---|
542 | ULONG tmp = *data;
|
---|
543 | for (ULONG i = 0; i < length; ++i)
|
---|
544 | if (data[i] < tmp)
|
---|
545 | tmp = data[i];
|
---|
546 | return tmp;
|
---|
547 | }
|
---|
548 |
|
---|
549 | const char * AggregateMin::getName()
|
---|
550 | {
|
---|
551 | return "min";
|
---|
552 | }
|
---|
553 |
|
---|
554 | ULONG AggregateMax::compute(ULONG *data, ULONG length)
|
---|
555 | {
|
---|
556 | ULONG tmp = *data;
|
---|
557 | for (ULONG i = 0; i < length; ++i)
|
---|
558 | if (data[i] > tmp)
|
---|
559 | tmp = data[i];
|
---|
560 | return tmp;
|
---|
561 | }
|
---|
562 |
|
---|
563 | const char * AggregateMax::getName()
|
---|
564 | {
|
---|
565 | return "max";
|
---|
566 | }
|
---|
567 |
|
---|
568 | Filter::Filter(ComSafeArrayIn(IN_BSTR, metricNames),
|
---|
569 | ComSafeArrayIn(IUnknown *, objects))
|
---|
570 | {
|
---|
571 | /*
|
---|
572 | * Let's work around null/empty safe array mess. I am not sure there is
|
---|
573 | * a way to pass null arrays via webservice, I haven't found one. So I
|
---|
574 | * guess the users will be forced to use empty arrays instead. Constructing
|
---|
575 | * an empty SafeArray is a bit awkward, so what we do in this method is
|
---|
576 | * actually convert null arrays to empty arrays and pass them down to
|
---|
577 | * init() method. If someone knows how to do it better, please be my guest,
|
---|
578 | * fix it.
|
---|
579 | */
|
---|
580 | if (ComSafeArrayInIsNull(metricNames))
|
---|
581 | {
|
---|
582 | com::SafeArray<BSTR> nameArray;
|
---|
583 | if (ComSafeArrayInIsNull(objects))
|
---|
584 | {
|
---|
585 | com::SafeIfaceArray<IUnknown> objectArray;
|
---|
586 | objectArray.reset(0);
|
---|
587 | init(ComSafeArrayAsInParam(nameArray),
|
---|
588 | ComSafeArrayAsInParam(objectArray));
|
---|
589 | }
|
---|
590 | else
|
---|
591 | {
|
---|
592 | com::SafeIfaceArray<IUnknown> objectArray(ComSafeArrayInArg(objects));
|
---|
593 | init(ComSafeArrayAsInParam(nameArray),
|
---|
594 | ComSafeArrayAsInParam(objectArray));
|
---|
595 | }
|
---|
596 | }
|
---|
597 | else
|
---|
598 | {
|
---|
599 | com::SafeArray<IN_BSTR> nameArray(ComSafeArrayInArg(metricNames));
|
---|
600 | if (ComSafeArrayInIsNull(objects))
|
---|
601 | {
|
---|
602 | com::SafeIfaceArray<IUnknown> objectArray;
|
---|
603 | objectArray.reset(0);
|
---|
604 | init(ComSafeArrayAsInParam(nameArray),
|
---|
605 | ComSafeArrayAsInParam(objectArray));
|
---|
606 | }
|
---|
607 | else
|
---|
608 | {
|
---|
609 | com::SafeIfaceArray<IUnknown> objectArray(ComSafeArrayInArg(objects));
|
---|
610 | init(ComSafeArrayAsInParam(nameArray),
|
---|
611 | ComSafeArrayAsInParam(objectArray));
|
---|
612 | }
|
---|
613 | }
|
---|
614 | }
|
---|
615 |
|
---|
616 | void Filter::init(ComSafeArrayIn(IN_BSTR, metricNames),
|
---|
617 | ComSafeArrayIn(IUnknown *, objects))
|
---|
618 | {
|
---|
619 | com::SafeArray<IN_BSTR> nameArray(ComSafeArrayInArg(metricNames));
|
---|
620 | com::SafeIfaceArray<IUnknown> objectArray(ComSafeArrayInArg(objects));
|
---|
621 |
|
---|
622 | if (!objectArray.size())
|
---|
623 | {
|
---|
624 | if (nameArray.size())
|
---|
625 | {
|
---|
626 | for (size_t i = 0; i < nameArray.size(); ++i)
|
---|
627 | processMetricList(com::Utf8Str(nameArray[i]), ComPtr<IUnknown>());
|
---|
628 | }
|
---|
629 | else
|
---|
630 | processMetricList("*", ComPtr<IUnknown>());
|
---|
631 | }
|
---|
632 | else
|
---|
633 | {
|
---|
634 | for (size_t i = 0; i < objectArray.size(); ++i)
|
---|
635 | switch (nameArray.size())
|
---|
636 | {
|
---|
637 | case 0:
|
---|
638 | processMetricList("*", objectArray[i]);
|
---|
639 | break;
|
---|
640 | case 1:
|
---|
641 | processMetricList(com::Utf8Str(nameArray[0]), objectArray[i]);
|
---|
642 | break;
|
---|
643 | default:
|
---|
644 | processMetricList(com::Utf8Str(nameArray[i]), objectArray[i]);
|
---|
645 | break;
|
---|
646 | }
|
---|
647 | }
|
---|
648 | }
|
---|
649 |
|
---|
650 | void Filter::processMetricList(const com::Utf8Str &name, const ComPtr<IUnknown> object)
|
---|
651 | {
|
---|
652 | size_t startPos = 0;
|
---|
653 |
|
---|
654 | for (size_t pos = name.find(",");
|
---|
655 | pos != com::Utf8Str::npos;
|
---|
656 | pos = name.find(",", startPos))
|
---|
657 | {
|
---|
658 | mElements.push_back(std::make_pair(object, iprt::MiniString(name.substr(startPos, pos - startPos).c_str())));
|
---|
659 | startPos = pos + 1;
|
---|
660 | }
|
---|
661 | mElements.push_back(std::make_pair(object, iprt::MiniString(name.substr(startPos).c_str())));
|
---|
662 | }
|
---|
663 |
|
---|
664 | /**
|
---|
665 | * The following method was borrowed from stamR3Match (VMM/STAM.cpp) and
|
---|
666 | * modified to handle the special case of trailing colon in the pattern.
|
---|
667 | *
|
---|
668 | * @returns True if matches, false if not.
|
---|
669 | * @param pszPat Pattern.
|
---|
670 | * @param pszName Name to match against the pattern.
|
---|
671 | * @param fSeenColon Seen colon (':').
|
---|
672 | */
|
---|
673 | bool Filter::patternMatch(const char *pszPat, const char *pszName,
|
---|
674 | bool fSeenColon)
|
---|
675 | {
|
---|
676 | /* ASSUMES ASCII */
|
---|
677 | for (;;)
|
---|
678 | {
|
---|
679 | char chPat = *pszPat;
|
---|
680 | switch (chPat)
|
---|
681 | {
|
---|
682 | default:
|
---|
683 | if (*pszName != chPat)
|
---|
684 | return false;
|
---|
685 | break;
|
---|
686 |
|
---|
687 | case '*':
|
---|
688 | {
|
---|
689 | while ((chPat = *++pszPat) == '*' || chPat == '?')
|
---|
690 | /* nothing */;
|
---|
691 |
|
---|
692 | /* Handle a special case, the mask terminating with a colon. */
|
---|
693 | if (chPat == ':')
|
---|
694 | {
|
---|
695 | if (!fSeenColon && !pszPat[1])
|
---|
696 | return !strchr(pszName, ':');
|
---|
697 | fSeenColon = true;
|
---|
698 | }
|
---|
699 |
|
---|
700 | for (;;)
|
---|
701 | {
|
---|
702 | char ch = *pszName++;
|
---|
703 | if ( ch == chPat
|
---|
704 | && ( !chPat
|
---|
705 | || patternMatch(pszPat + 1, pszName, fSeenColon)))
|
---|
706 | return true;
|
---|
707 | if (!ch)
|
---|
708 | return false;
|
---|
709 | }
|
---|
710 | /* won't ever get here */
|
---|
711 | break;
|
---|
712 | }
|
---|
713 |
|
---|
714 | case '?':
|
---|
715 | if (!*pszName)
|
---|
716 | return false;
|
---|
717 | break;
|
---|
718 |
|
---|
719 | /* Handle a special case, the mask terminating with a colon. */
|
---|
720 | case ':':
|
---|
721 | if (!fSeenColon && !pszPat[1])
|
---|
722 | return !*pszName;
|
---|
723 | if (*pszName != ':')
|
---|
724 | return false;
|
---|
725 | fSeenColon = true;
|
---|
726 | break;
|
---|
727 |
|
---|
728 | case '\0':
|
---|
729 | return !*pszName;
|
---|
730 | }
|
---|
731 | pszName++;
|
---|
732 | pszPat++;
|
---|
733 | }
|
---|
734 | return true;
|
---|
735 | }
|
---|
736 |
|
---|
737 | bool Filter::match(const ComPtr<IUnknown> object, const iprt::MiniString &name) const
|
---|
738 | {
|
---|
739 | ElementList::const_iterator it;
|
---|
740 |
|
---|
741 | LogAleksey(("Filter::match(%p, %s)\n", static_cast<const IUnknown*> (object), name.c_str()));
|
---|
742 | for (it = mElements.begin(); it != mElements.end(); it++)
|
---|
743 | {
|
---|
744 | LogAleksey(("...matching against(%p, %s)\n", static_cast<const IUnknown*> ((*it).first), (*it).second.c_str()));
|
---|
745 | if ((*it).first.isNull() || (*it).first == object)
|
---|
746 | {
|
---|
747 | // Objects match, compare names
|
---|
748 | if (patternMatch((*it).second.c_str(), name.c_str()))
|
---|
749 | {
|
---|
750 | LogFlowThisFunc(("...found!\n"));
|
---|
751 | return true;
|
---|
752 | }
|
---|
753 | }
|
---|
754 | }
|
---|
755 | LogAleksey(("...no matches!\n"));
|
---|
756 | return false;
|
---|
757 | }
|
---|
758 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|