1 | /* $Id: Performance.cpp 29225 2010-05-07 16:01:34Z 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, uMemShared;
|
---|
167 |
|
---|
168 | /** todo shared stats. */
|
---|
169 | mGuest->InternalGetStatistics(&mCpuUser, &mCpuKernel, &mCpuIdle,
|
---|
170 | &mMemTotal, &mMemFree, &mMemBalloon, &mMemCache, &uMemShared,
|
---|
171 | &mPageTotal, &ulMemAllocTotal, &ulMemFreeTotal, &ulMemBalloonTotal, &ulMemSharedTotal);
|
---|
172 |
|
---|
173 | if (mHostHAL)
|
---|
174 | mHostHAL->setMemHypervisorStats(ulMemAllocTotal, ulMemFreeTotal, ulMemBalloonTotal);
|
---|
175 |
|
---|
176 | mLastTick = iTick;
|
---|
177 | }
|
---|
178 | return S_OK;
|
---|
179 | }
|
---|
180 |
|
---|
181 | #endif /* !VBOX_COLLECTOR_TEST_CASE */
|
---|
182 |
|
---|
183 | bool BaseMetric::collectorBeat(uint64_t nowAt)
|
---|
184 | {
|
---|
185 | if (isEnabled())
|
---|
186 | {
|
---|
187 | if (nowAt - mLastSampleTaken >= mPeriod * 1000)
|
---|
188 | {
|
---|
189 | mLastSampleTaken = nowAt;
|
---|
190 | Log4(("{%p} " LOG_FN_FMT ": Collecting %s for obj(%p)...\n",
|
---|
191 | this, __PRETTY_FUNCTION__, getName(), (void *)mObject));
|
---|
192 | return true;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | return false;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /*bool BaseMetric::associatedWith(ComPtr<IUnknown> object)
|
---|
199 | {
|
---|
200 | LogFlowThisFunc(("mObject(%p) == object(%p) is %s.\n", mObject, object, mObject == object ? "true" : "false"));
|
---|
201 | return mObject == object;
|
---|
202 | }*/
|
---|
203 |
|
---|
204 | void HostCpuLoad::init(ULONG period, ULONG length)
|
---|
205 | {
|
---|
206 | mPeriod = period;
|
---|
207 | mLength = length;
|
---|
208 | mUser->init(mLength);
|
---|
209 | mKernel->init(mLength);
|
---|
210 | mIdle->init(mLength);
|
---|
211 | }
|
---|
212 |
|
---|
213 | void HostCpuLoad::collect()
|
---|
214 | {
|
---|
215 | ULONG user, kernel, idle;
|
---|
216 | int rc = mHAL->getHostCpuLoad(&user, &kernel, &idle);
|
---|
217 | if (RT_SUCCESS(rc))
|
---|
218 | {
|
---|
219 | mUser->put(user);
|
---|
220 | mKernel->put(kernel);
|
---|
221 | mIdle->put(idle);
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | void HostCpuLoadRaw::preCollect(CollectorHints& hints, uint64_t /* iTick */)
|
---|
226 | {
|
---|
227 | hints.collectHostCpuLoad();
|
---|
228 | }
|
---|
229 |
|
---|
230 | void HostCpuLoadRaw::collect()
|
---|
231 | {
|
---|
232 | uint64_t user, kernel, idle;
|
---|
233 | uint64_t userDiff, kernelDiff, idleDiff, totalDiff;
|
---|
234 |
|
---|
235 | int rc = mHAL->getRawHostCpuLoad(&user, &kernel, &idle);
|
---|
236 | if (RT_SUCCESS(rc))
|
---|
237 | {
|
---|
238 | userDiff = user - mUserPrev;
|
---|
239 | kernelDiff = kernel - mKernelPrev;
|
---|
240 | idleDiff = idle - mIdlePrev;
|
---|
241 | totalDiff = userDiff + kernelDiff + idleDiff;
|
---|
242 |
|
---|
243 | if (totalDiff == 0)
|
---|
244 | {
|
---|
245 | /* This is only possible if none of counters has changed! */
|
---|
246 | LogFlowThisFunc(("Impossible! User, kernel and idle raw "
|
---|
247 | "counters has not changed since last sample.\n" ));
|
---|
248 | mUser->put(0);
|
---|
249 | mKernel->put(0);
|
---|
250 | mIdle->put(0);
|
---|
251 | }
|
---|
252 | else
|
---|
253 | {
|
---|
254 | mUser->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * userDiff / totalDiff));
|
---|
255 | mKernel->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * kernelDiff / totalDiff));
|
---|
256 | mIdle->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * idleDiff / totalDiff));
|
---|
257 | }
|
---|
258 |
|
---|
259 | mUserPrev = user;
|
---|
260 | mKernelPrev = kernel;
|
---|
261 | mIdlePrev = idle;
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | void HostCpuMhz::init(ULONG period, ULONG length)
|
---|
266 | {
|
---|
267 | mPeriod = period;
|
---|
268 | mLength = length;
|
---|
269 | mMHz->init(mLength);
|
---|
270 | }
|
---|
271 |
|
---|
272 | void HostCpuMhz::collect()
|
---|
273 | {
|
---|
274 | ULONG mhz;
|
---|
275 | int rc = mHAL->getHostCpuMHz(&mhz);
|
---|
276 | if (RT_SUCCESS(rc))
|
---|
277 | mMHz->put(mhz);
|
---|
278 | }
|
---|
279 |
|
---|
280 | void HostRamUsage::init(ULONG period, ULONG length)
|
---|
281 | {
|
---|
282 | mPeriod = period;
|
---|
283 | mLength = length;
|
---|
284 | mTotal->init(mLength);
|
---|
285 | mUsed->init(mLength);
|
---|
286 | mAvailable->init(mLength);
|
---|
287 | mAllocVMM->init(mLength);
|
---|
288 | mFreeVMM->init(mLength);
|
---|
289 | mBalloonVMM->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;
|
---|
309 |
|
---|
310 | mHAL->getMemHypervisorStats(&allocVMM, &freeVMM, &balloonVMM);
|
---|
311 | mAllocVMM->put(allocVMM);
|
---|
312 | mFreeVMM->put(freeVMM);
|
---|
313 | mBalloonVMM->put(balloonVMM);
|
---|
314 | }
|
---|
315 |
|
---|
316 |
|
---|
317 |
|
---|
318 | void MachineCpuLoad::init(ULONG period, ULONG length)
|
---|
319 | {
|
---|
320 | mPeriod = period;
|
---|
321 | mLength = length;
|
---|
322 | mUser->init(mLength);
|
---|
323 | mKernel->init(mLength);
|
---|
324 | }
|
---|
325 |
|
---|
326 | void MachineCpuLoad::collect()
|
---|
327 | {
|
---|
328 | ULONG user, kernel;
|
---|
329 | int rc = mHAL->getProcessCpuLoad(mProcess, &user, &kernel);
|
---|
330 | if (RT_SUCCESS(rc))
|
---|
331 | {
|
---|
332 | mUser->put(user);
|
---|
333 | mKernel->put(kernel);
|
---|
334 | }
|
---|
335 | }
|
---|
336 |
|
---|
337 | void MachineCpuLoadRaw::preCollect(CollectorHints& hints, uint64_t /* iTick */)
|
---|
338 | {
|
---|
339 | hints.collectProcessCpuLoad(mProcess);
|
---|
340 | }
|
---|
341 |
|
---|
342 | void MachineCpuLoadRaw::collect()
|
---|
343 | {
|
---|
344 | uint64_t processUser, processKernel, hostTotal;
|
---|
345 |
|
---|
346 | int rc = mHAL->getRawProcessCpuLoad(mProcess, &processUser, &processKernel, &hostTotal);
|
---|
347 | if (RT_SUCCESS(rc))
|
---|
348 | {
|
---|
349 | if (hostTotal == mHostTotalPrev)
|
---|
350 | {
|
---|
351 | /* Nearly impossible, but... */
|
---|
352 | mUser->put(0);
|
---|
353 | mKernel->put(0);
|
---|
354 | }
|
---|
355 | else
|
---|
356 | {
|
---|
357 | mUser->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * (processUser - mProcessUserPrev) / (hostTotal - mHostTotalPrev)));
|
---|
358 | mKernel->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * (processKernel - mProcessKernelPrev ) / (hostTotal - mHostTotalPrev)));
|
---|
359 | }
|
---|
360 |
|
---|
361 | mHostTotalPrev = hostTotal;
|
---|
362 | mProcessUserPrev = processUser;
|
---|
363 | mProcessKernelPrev = processKernel;
|
---|
364 | }
|
---|
365 | }
|
---|
366 |
|
---|
367 | void MachineRamUsage::init(ULONG period, ULONG length)
|
---|
368 | {
|
---|
369 | mPeriod = period;
|
---|
370 | mLength = length;
|
---|
371 | mUsed->init(mLength);
|
---|
372 | }
|
---|
373 |
|
---|
374 | void MachineRamUsage::preCollect(CollectorHints& hints, uint64_t /* iTick */)
|
---|
375 | {
|
---|
376 | hints.collectProcessRamUsage(mProcess);
|
---|
377 | }
|
---|
378 |
|
---|
379 | void MachineRamUsage::collect()
|
---|
380 | {
|
---|
381 | ULONG used;
|
---|
382 | int rc = mHAL->getProcessMemoryUsage(mProcess, &used);
|
---|
383 | if (RT_SUCCESS(rc))
|
---|
384 | mUsed->put(used);
|
---|
385 | }
|
---|
386 |
|
---|
387 |
|
---|
388 | void GuestCpuLoad::init(ULONG period, ULONG length)
|
---|
389 | {
|
---|
390 | mPeriod = period;
|
---|
391 | mLength = length;
|
---|
392 |
|
---|
393 | mUser->init(mLength);
|
---|
394 | mKernel->init(mLength);
|
---|
395 | mIdle->init(mLength);
|
---|
396 | }
|
---|
397 |
|
---|
398 | void GuestCpuLoad::preCollect(CollectorHints& hints, uint64_t iTick)
|
---|
399 | {
|
---|
400 | mHAL->preCollect(hints, iTick);
|
---|
401 | }
|
---|
402 |
|
---|
403 | void GuestCpuLoad::collect()
|
---|
404 | {
|
---|
405 | ULONG CpuUser = 0, CpuKernel = 0, CpuIdle = 0;
|
---|
406 |
|
---|
407 | mGuestHAL->getGuestCpuLoad(&CpuUser, &CpuKernel, &CpuIdle);
|
---|
408 | mUser->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * CpuUser) / 100);
|
---|
409 | mKernel->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * CpuKernel) / 100);
|
---|
410 | mIdle->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * CpuIdle) / 100);
|
---|
411 | }
|
---|
412 |
|
---|
413 | void GuestRamUsage::init(ULONG period, ULONG length)
|
---|
414 | {
|
---|
415 | mPeriod = period;
|
---|
416 | mLength = length;
|
---|
417 |
|
---|
418 | mTotal->init(mLength);
|
---|
419 | mFree->init(mLength);
|
---|
420 | mBallooned->init(mLength);
|
---|
421 | mCache->init(mLength);
|
---|
422 | mPagedTotal->init(mLength);
|
---|
423 | }
|
---|
424 |
|
---|
425 | void GuestRamUsage::preCollect(CollectorHints& hints, uint64_t iTick)
|
---|
426 | {
|
---|
427 | mHAL->preCollect(hints, iTick);
|
---|
428 | }
|
---|
429 |
|
---|
430 | void GuestRamUsage::collect()
|
---|
431 | {
|
---|
432 | ULONG ulMemTotal = 0, ulMemFree = 0, ulMemBalloon = 0, ulMemCache = 0, ulPageTotal = 0;
|
---|
433 |
|
---|
434 | mGuestHAL->getGuestMemLoad(&ulMemTotal, &ulMemFree, &ulMemBalloon, &ulMemCache, &ulPageTotal);
|
---|
435 | mTotal->put(ulMemTotal);
|
---|
436 | mFree->put(ulMemFree);
|
---|
437 | mBallooned->put(ulMemBalloon);
|
---|
438 | mCache->put(ulMemCache);
|
---|
439 | mPagedTotal->put(ulPageTotal);
|
---|
440 | }
|
---|
441 |
|
---|
442 | void CircularBuffer::init(ULONG ulLength)
|
---|
443 | {
|
---|
444 | if (mData)
|
---|
445 | RTMemFree(mData);
|
---|
446 | mLength = ulLength;
|
---|
447 | if (mLength)
|
---|
448 | mData = (ULONG*)RTMemAllocZ(ulLength * sizeof(ULONG));
|
---|
449 | else
|
---|
450 | mData = NULL;
|
---|
451 | mWrapped = false;
|
---|
452 | mEnd = 0;
|
---|
453 | mSequenceNumber = 0;
|
---|
454 | }
|
---|
455 |
|
---|
456 | ULONG CircularBuffer::length()
|
---|
457 | {
|
---|
458 | return mWrapped ? mLength : mEnd;
|
---|
459 | }
|
---|
460 |
|
---|
461 | void CircularBuffer::put(ULONG value)
|
---|
462 | {
|
---|
463 | if (mData)
|
---|
464 | {
|
---|
465 | mData[mEnd++] = value;
|
---|
466 | if (mEnd >= mLength)
|
---|
467 | {
|
---|
468 | mEnd = 0;
|
---|
469 | mWrapped = true;
|
---|
470 | }
|
---|
471 | ++mSequenceNumber;
|
---|
472 | }
|
---|
473 | }
|
---|
474 |
|
---|
475 | void CircularBuffer::copyTo(ULONG *data)
|
---|
476 | {
|
---|
477 | if (mWrapped)
|
---|
478 | {
|
---|
479 | memcpy(data, mData + mEnd, (mLength - mEnd) * sizeof(ULONG));
|
---|
480 | // Copy the wrapped part
|
---|
481 | if (mEnd)
|
---|
482 | memcpy(data + (mLength - mEnd), mData, mEnd * sizeof(ULONG));
|
---|
483 | }
|
---|
484 | else
|
---|
485 | memcpy(data, mData, mEnd * sizeof(ULONG));
|
---|
486 | }
|
---|
487 |
|
---|
488 | void SubMetric::query(ULONG *data)
|
---|
489 | {
|
---|
490 | copyTo(data);
|
---|
491 | }
|
---|
492 |
|
---|
493 | void Metric::query(ULONG **data, ULONG *count, ULONG *sequenceNumber)
|
---|
494 | {
|
---|
495 | ULONG length;
|
---|
496 | ULONG *tmpData;
|
---|
497 |
|
---|
498 | length = mSubMetric->length();
|
---|
499 | *sequenceNumber = mSubMetric->getSequenceNumber() - length;
|
---|
500 | if (length)
|
---|
501 | {
|
---|
502 | tmpData = (ULONG*)RTMemAlloc(sizeof(*tmpData)*length);
|
---|
503 | mSubMetric->query(tmpData);
|
---|
504 | if (mAggregate)
|
---|
505 | {
|
---|
506 | *count = 1;
|
---|
507 | *data = (ULONG*)RTMemAlloc(sizeof(**data));
|
---|
508 | **data = mAggregate->compute(tmpData, length);
|
---|
509 | RTMemFree(tmpData);
|
---|
510 | }
|
---|
511 | else
|
---|
512 | {
|
---|
513 | *count = length;
|
---|
514 | *data = tmpData;
|
---|
515 | }
|
---|
516 | }
|
---|
517 | else
|
---|
518 | {
|
---|
519 | *count = 0;
|
---|
520 | *data = 0;
|
---|
521 | }
|
---|
522 | }
|
---|
523 |
|
---|
524 | ULONG AggregateAvg::compute(ULONG *data, ULONG length)
|
---|
525 | {
|
---|
526 | uint64_t tmp = 0;
|
---|
527 | for (ULONG i = 0; i < length; ++i)
|
---|
528 | tmp += data[i];
|
---|
529 | return (ULONG)(tmp / length);
|
---|
530 | }
|
---|
531 |
|
---|
532 | const char * AggregateAvg::getName()
|
---|
533 | {
|
---|
534 | return "avg";
|
---|
535 | }
|
---|
536 |
|
---|
537 | ULONG AggregateMin::compute(ULONG *data, ULONG length)
|
---|
538 | {
|
---|
539 | ULONG tmp = *data;
|
---|
540 | for (ULONG i = 0; i < length; ++i)
|
---|
541 | if (data[i] < tmp)
|
---|
542 | tmp = data[i];
|
---|
543 | return tmp;
|
---|
544 | }
|
---|
545 |
|
---|
546 | const char * AggregateMin::getName()
|
---|
547 | {
|
---|
548 | return "min";
|
---|
549 | }
|
---|
550 |
|
---|
551 | ULONG AggregateMax::compute(ULONG *data, ULONG length)
|
---|
552 | {
|
---|
553 | ULONG tmp = *data;
|
---|
554 | for (ULONG i = 0; i < length; ++i)
|
---|
555 | if (data[i] > tmp)
|
---|
556 | tmp = data[i];
|
---|
557 | return tmp;
|
---|
558 | }
|
---|
559 |
|
---|
560 | const char * AggregateMax::getName()
|
---|
561 | {
|
---|
562 | return "max";
|
---|
563 | }
|
---|
564 |
|
---|
565 | Filter::Filter(ComSafeArrayIn(IN_BSTR, metricNames),
|
---|
566 | ComSafeArrayIn(IUnknown *, objects))
|
---|
567 | {
|
---|
568 | /*
|
---|
569 | * Let's work around null/empty safe array mess. I am not sure there is
|
---|
570 | * a way to pass null arrays via webservice, I haven't found one. So I
|
---|
571 | * guess the users will be forced to use empty arrays instead. Constructing
|
---|
572 | * an empty SafeArray is a bit awkward, so what we do in this method is
|
---|
573 | * actually convert null arrays to empty arrays and pass them down to
|
---|
574 | * init() method. If someone knows how to do it better, please be my guest,
|
---|
575 | * fix it.
|
---|
576 | */
|
---|
577 | if (ComSafeArrayInIsNull(metricNames))
|
---|
578 | {
|
---|
579 | com::SafeArray<BSTR> nameArray;
|
---|
580 | if (ComSafeArrayInIsNull(objects))
|
---|
581 | {
|
---|
582 | com::SafeIfaceArray<IUnknown> objectArray;
|
---|
583 | objectArray.reset(0);
|
---|
584 | init(ComSafeArrayAsInParam(nameArray),
|
---|
585 | ComSafeArrayAsInParam(objectArray));
|
---|
586 | }
|
---|
587 | else
|
---|
588 | {
|
---|
589 | com::SafeIfaceArray<IUnknown> objectArray(ComSafeArrayInArg(objects));
|
---|
590 | init(ComSafeArrayAsInParam(nameArray),
|
---|
591 | ComSafeArrayAsInParam(objectArray));
|
---|
592 | }
|
---|
593 | }
|
---|
594 | else
|
---|
595 | {
|
---|
596 | com::SafeArray<IN_BSTR> nameArray(ComSafeArrayInArg(metricNames));
|
---|
597 | if (ComSafeArrayInIsNull(objects))
|
---|
598 | {
|
---|
599 | com::SafeIfaceArray<IUnknown> objectArray;
|
---|
600 | objectArray.reset(0);
|
---|
601 | init(ComSafeArrayAsInParam(nameArray),
|
---|
602 | ComSafeArrayAsInParam(objectArray));
|
---|
603 | }
|
---|
604 | else
|
---|
605 | {
|
---|
606 | com::SafeIfaceArray<IUnknown> objectArray(ComSafeArrayInArg(objects));
|
---|
607 | init(ComSafeArrayAsInParam(nameArray),
|
---|
608 | ComSafeArrayAsInParam(objectArray));
|
---|
609 | }
|
---|
610 | }
|
---|
611 | }
|
---|
612 |
|
---|
613 | void Filter::init(ComSafeArrayIn(IN_BSTR, metricNames),
|
---|
614 | ComSafeArrayIn(IUnknown *, objects))
|
---|
615 | {
|
---|
616 | com::SafeArray<IN_BSTR> nameArray(ComSafeArrayInArg(metricNames));
|
---|
617 | com::SafeIfaceArray<IUnknown> objectArray(ComSafeArrayInArg(objects));
|
---|
618 |
|
---|
619 | if (!objectArray.size())
|
---|
620 | {
|
---|
621 | if (nameArray.size())
|
---|
622 | {
|
---|
623 | for (size_t i = 0; i < nameArray.size(); ++i)
|
---|
624 | processMetricList(com::Utf8Str(nameArray[i]), ComPtr<IUnknown>());
|
---|
625 | }
|
---|
626 | else
|
---|
627 | processMetricList("*", ComPtr<IUnknown>());
|
---|
628 | }
|
---|
629 | else
|
---|
630 | {
|
---|
631 | for (size_t i = 0; i < objectArray.size(); ++i)
|
---|
632 | switch (nameArray.size())
|
---|
633 | {
|
---|
634 | case 0:
|
---|
635 | processMetricList("*", objectArray[i]);
|
---|
636 | break;
|
---|
637 | case 1:
|
---|
638 | processMetricList(com::Utf8Str(nameArray[0]), objectArray[i]);
|
---|
639 | break;
|
---|
640 | default:
|
---|
641 | processMetricList(com::Utf8Str(nameArray[i]), objectArray[i]);
|
---|
642 | break;
|
---|
643 | }
|
---|
644 | }
|
---|
645 | }
|
---|
646 |
|
---|
647 | void Filter::processMetricList(const com::Utf8Str &name, const ComPtr<IUnknown> object)
|
---|
648 | {
|
---|
649 | size_t startPos = 0;
|
---|
650 |
|
---|
651 | for (size_t pos = name.find(",");
|
---|
652 | pos != com::Utf8Str::npos;
|
---|
653 | pos = name.find(",", startPos))
|
---|
654 | {
|
---|
655 | mElements.push_back(std::make_pair(object, iprt::MiniString(name.substr(startPos, pos - startPos).c_str())));
|
---|
656 | startPos = pos + 1;
|
---|
657 | }
|
---|
658 | mElements.push_back(std::make_pair(object, iprt::MiniString(name.substr(startPos).c_str())));
|
---|
659 | }
|
---|
660 |
|
---|
661 | /**
|
---|
662 | * The following method was borrowed from stamR3Match (VMM/STAM.cpp) and
|
---|
663 | * modified to handle the special case of trailing colon in the pattern.
|
---|
664 | *
|
---|
665 | * @returns True if matches, false if not.
|
---|
666 | * @param pszPat Pattern.
|
---|
667 | * @param pszName Name to match against the pattern.
|
---|
668 | * @param fSeenColon Seen colon (':').
|
---|
669 | */
|
---|
670 | bool Filter::patternMatch(const char *pszPat, const char *pszName,
|
---|
671 | bool fSeenColon)
|
---|
672 | {
|
---|
673 | /* ASSUMES ASCII */
|
---|
674 | for (;;)
|
---|
675 | {
|
---|
676 | char chPat = *pszPat;
|
---|
677 | switch (chPat)
|
---|
678 | {
|
---|
679 | default:
|
---|
680 | if (*pszName != chPat)
|
---|
681 | return false;
|
---|
682 | break;
|
---|
683 |
|
---|
684 | case '*':
|
---|
685 | {
|
---|
686 | while ((chPat = *++pszPat) == '*' || chPat == '?')
|
---|
687 | /* nothing */;
|
---|
688 |
|
---|
689 | /* Handle a special case, the mask terminating with a colon. */
|
---|
690 | if (chPat == ':')
|
---|
691 | {
|
---|
692 | if (!fSeenColon && !pszPat[1])
|
---|
693 | return !strchr(pszName, ':');
|
---|
694 | fSeenColon = true;
|
---|
695 | }
|
---|
696 |
|
---|
697 | for (;;)
|
---|
698 | {
|
---|
699 | char ch = *pszName++;
|
---|
700 | if ( ch == chPat
|
---|
701 | && ( !chPat
|
---|
702 | || patternMatch(pszPat + 1, pszName, fSeenColon)))
|
---|
703 | return true;
|
---|
704 | if (!ch)
|
---|
705 | return false;
|
---|
706 | }
|
---|
707 | /* won't ever get here */
|
---|
708 | break;
|
---|
709 | }
|
---|
710 |
|
---|
711 | case '?':
|
---|
712 | if (!*pszName)
|
---|
713 | return false;
|
---|
714 | break;
|
---|
715 |
|
---|
716 | /* Handle a special case, the mask terminating with a colon. */
|
---|
717 | case ':':
|
---|
718 | if (!fSeenColon && !pszPat[1])
|
---|
719 | return !*pszName;
|
---|
720 | if (*pszName != ':')
|
---|
721 | return false;
|
---|
722 | fSeenColon = true;
|
---|
723 | break;
|
---|
724 |
|
---|
725 | case '\0':
|
---|
726 | return !*pszName;
|
---|
727 | }
|
---|
728 | pszName++;
|
---|
729 | pszPat++;
|
---|
730 | }
|
---|
731 | return true;
|
---|
732 | }
|
---|
733 |
|
---|
734 | bool Filter::match(const ComPtr<IUnknown> object, const iprt::MiniString &name) const
|
---|
735 | {
|
---|
736 | ElementList::const_iterator it;
|
---|
737 |
|
---|
738 | LogAleksey(("Filter::match(%p, %s)\n", static_cast<const IUnknown*> (object), name.c_str()));
|
---|
739 | for (it = mElements.begin(); it != mElements.end(); it++)
|
---|
740 | {
|
---|
741 | LogAleksey(("...matching against(%p, %s)\n", static_cast<const IUnknown*> ((*it).first), (*it).second.c_str()));
|
---|
742 | if ((*it).first.isNull() || (*it).first == object)
|
---|
743 | {
|
---|
744 | // Objects match, compare names
|
---|
745 | if (patternMatch((*it).second.c_str(), name.c_str()))
|
---|
746 | {
|
---|
747 | LogFlowThisFunc(("...found!\n"));
|
---|
748 | return true;
|
---|
749 | }
|
---|
750 | }
|
---|
751 | }
|
---|
752 | LogAleksey(("...no matches!\n"));
|
---|
753 | return false;
|
---|
754 | }
|
---|
755 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|