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