VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/Performance.cpp@ 35964

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

Metrics: Introduced RAM/VMM base metric

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.2 KB
Line 
1/* $Id: Performance.cpp 35964 2011-02-14 17:06:48Z 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.215389.xyz. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20/*
21 * @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
44using namespace pm;
45
46// Stubs for non-pure virtual methods
47
48int CollectorHAL::getHostCpuLoad(ULONG * /* user */, ULONG * /* kernel */, ULONG * /* idle */)
49{
50 return E_NOTIMPL;
51}
52
53int CollectorHAL::getProcessCpuLoad(RTPROCESS /* process */, ULONG * /* user */, ULONG * /* kernel */)
54{
55 return E_NOTIMPL;
56}
57
58int CollectorHAL::getRawHostCpuLoad(uint64_t * /* user */, uint64_t * /* kernel */, uint64_t * /* idle */)
59{
60 return E_NOTIMPL;
61}
62
63int CollectorHAL::getRawProcessCpuLoad(RTPROCESS /* process */, uint64_t * /* user */, uint64_t * /* kernel */, uint64_t * /* total */)
64{
65 return E_NOTIMPL;
66}
67
68int CollectorHAL::getHostMemoryUsage(ULONG * /* total */, ULONG * /* used */, ULONG * /* available */)
69{
70 return E_NOTIMPL;
71}
72
73int CollectorHAL::getProcessMemoryUsage(RTPROCESS /* process */, ULONG * /* used */)
74{
75 return E_NOTIMPL;
76}
77
78int CollectorHAL::enable()
79{
80 return E_NOTIMPL;
81}
82
83int CollectorHAL::disable()
84{
85 return E_NOTIMPL;
86}
87
88/* Generic implementations */
89
90int 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
123uint32_t CollectorGuestHAL::cVMsEnabled = 0;
124
125CollectorGuestHAL::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
136CollectorGuestHAL::~CollectorGuestHAL()
137{
138 /* cannot use ComObjPtr<Machine> in Performance.h, do it manually */
139 mMachine->Release();
140 Assert(!cEnabled);
141}
142
143int 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
177int 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
192int 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
213bool 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
234void 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
243void 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
255void HostCpuLoadRaw::preCollect(CollectorHints& hints, uint64_t /* iTick */)
256{
257 hints.collectHostCpuLoad();
258}
259
260void 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
295void HostCpuMhz::init(ULONG period, ULONG length)
296{
297 mPeriod = period;
298 mLength = length;
299 mMHz->init(mLength);
300}
301
302void HostCpuMhz::collect()
303{
304 ULONG mhz;
305 int rc = mHAL->getHostCpuMHz(&mhz);
306 if (RT_SUCCESS(rc))
307 mMHz->put(mhz);
308}
309
310void 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}
318
319void HostRamUsage::preCollect(CollectorHints& hints, uint64_t /* iTick */)
320{
321 hints.collectHostRamUsage();
322}
323
324void HostRamUsage::collect()
325{
326 ULONG total, used, available;
327 int rc = mHAL->getHostMemoryUsage(&total, &used, &available);
328 if (RT_SUCCESS(rc))
329 {
330 mTotal->put(total);
331 mUsed->put(used);
332 mAvailable->put(available);
333
334 }
335}
336
337void HostRamVmm::init(ULONG period, ULONG length)
338{
339 mPeriod = period;
340 mLength = length;
341 mAllocVMM->init(mLength);
342 mFreeVMM->init(mLength);
343 mBalloonVMM->init(mLength);
344 mSharedVMM->init(mLength);
345}
346
347void HostRamVmm::preCollect(CollectorHints& hints, uint64_t /* iTick */)
348{
349 /* Guest RAM metrics do not use hints */
350}
351
352void HostRamVmm::collect()
353{
354 ULONG allocVMM, freeVMM, balloonVMM, sharedVMM;
355
356 mHAL->getMemHypervisorStats(&allocVMM, &freeVMM, &balloonVMM, &sharedVMM);
357 mAllocVMM->put(allocVMM);
358 mFreeVMM->put(freeVMM);
359 mBalloonVMM->put(balloonVMM);
360 mSharedVMM->put(sharedVMM);
361}
362
363
364
365void MachineCpuLoad::init(ULONG period, ULONG length)
366{
367 mPeriod = period;
368 mLength = length;
369 mUser->init(mLength);
370 mKernel->init(mLength);
371}
372
373void MachineCpuLoad::collect()
374{
375 ULONG user, kernel;
376 int rc = mHAL->getProcessCpuLoad(mProcess, &user, &kernel);
377 if (RT_SUCCESS(rc))
378 {
379 mUser->put(user);
380 mKernel->put(kernel);
381 }
382}
383
384void MachineCpuLoadRaw::preCollect(CollectorHints& hints, uint64_t /* iTick */)
385{
386 hints.collectProcessCpuLoad(mProcess);
387}
388
389void MachineCpuLoadRaw::collect()
390{
391 uint64_t processUser, processKernel, hostTotal;
392
393 int rc = mHAL->getRawProcessCpuLoad(mProcess, &processUser, &processKernel, &hostTotal);
394 if (RT_SUCCESS(rc))
395 {
396 if (hostTotal == mHostTotalPrev)
397 {
398 /* Nearly impossible, but... */
399 mUser->put(0);
400 mKernel->put(0);
401 }
402 else
403 {
404 mUser->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * (processUser - mProcessUserPrev) / (hostTotal - mHostTotalPrev)));
405 mKernel->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * (processKernel - mProcessKernelPrev ) / (hostTotal - mHostTotalPrev)));
406 }
407
408 mHostTotalPrev = hostTotal;
409 mProcessUserPrev = processUser;
410 mProcessKernelPrev = processKernel;
411 }
412}
413
414void MachineRamUsage::init(ULONG period, ULONG length)
415{
416 mPeriod = period;
417 mLength = length;
418 mUsed->init(mLength);
419}
420
421void MachineRamUsage::preCollect(CollectorHints& hints, uint64_t /* iTick */)
422{
423 hints.collectProcessRamUsage(mProcess);
424}
425
426void MachineRamUsage::collect()
427{
428 ULONG used;
429 int rc = mHAL->getProcessMemoryUsage(mProcess, &used);
430 if (RT_SUCCESS(rc))
431 mUsed->put(used);
432}
433
434
435void GuestCpuLoad::init(ULONG period, ULONG length)
436{
437 mPeriod = period;
438 mLength = length;
439
440 mUser->init(mLength);
441 mKernel->init(mLength);
442 mIdle->init(mLength);
443}
444
445void GuestCpuLoad::preCollect(CollectorHints& hints, uint64_t iTick)
446{
447 mHAL->preCollect(hints, iTick);
448}
449
450void GuestCpuLoad::collect()
451{
452 ULONG CpuUser = 0, CpuKernel = 0, CpuIdle = 0;
453
454 mGuestHAL->getGuestCpuLoad(&CpuUser, &CpuKernel, &CpuIdle);
455 mUser->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * CpuUser) / 100);
456 mKernel->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * CpuKernel) / 100);
457 mIdle->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * CpuIdle) / 100);
458}
459
460void GuestRamUsage::init(ULONG period, ULONG length)
461{
462 mPeriod = period;
463 mLength = length;
464
465 mTotal->init(mLength);
466 mFree->init(mLength);
467 mBallooned->init(mLength);
468 mShared->init(mLength);
469 mCache->init(mLength);
470 mPagedTotal->init(mLength);
471}
472
473void GuestRamUsage::preCollect(CollectorHints& hints, uint64_t iTick)
474{
475 mHAL->preCollect(hints, iTick);
476}
477
478void GuestRamUsage::collect()
479{
480 ULONG ulMemTotal = 0, ulMemFree = 0, ulMemBalloon = 0, ulMemShared = 0, ulMemCache = 0, ulPageTotal = 0;
481
482 mGuestHAL->getGuestMemLoad(&ulMemTotal, &ulMemFree, &ulMemBalloon, &ulMemShared, &ulMemCache, &ulPageTotal);
483 mTotal->put(ulMemTotal);
484 mFree->put(ulMemFree);
485 mBallooned->put(ulMemBalloon);
486 mShared->put(ulMemShared);
487 mCache->put(ulMemCache);
488 mPagedTotal->put(ulPageTotal);
489}
490
491void CircularBuffer::init(ULONG ulLength)
492{
493 if (mData)
494 RTMemFree(mData);
495 mLength = ulLength;
496 if (mLength)
497 mData = (ULONG*)RTMemAllocZ(ulLength * sizeof(ULONG));
498 else
499 mData = NULL;
500 mWrapped = false;
501 mEnd = 0;
502 mSequenceNumber = 0;
503}
504
505ULONG CircularBuffer::length()
506{
507 return mWrapped ? mLength : mEnd;
508}
509
510void CircularBuffer::put(ULONG value)
511{
512 if (mData)
513 {
514 mData[mEnd++] = value;
515 if (mEnd >= mLength)
516 {
517 mEnd = 0;
518 mWrapped = true;
519 }
520 ++mSequenceNumber;
521 }
522}
523
524void CircularBuffer::copyTo(ULONG *data)
525{
526 if (mWrapped)
527 {
528 memcpy(data, mData + mEnd, (mLength - mEnd) * sizeof(ULONG));
529 // Copy the wrapped part
530 if (mEnd)
531 memcpy(data + (mLength - mEnd), mData, mEnd * sizeof(ULONG));
532 }
533 else
534 memcpy(data, mData, mEnd * sizeof(ULONG));
535}
536
537void SubMetric::query(ULONG *data)
538{
539 copyTo(data);
540}
541
542void Metric::query(ULONG **data, ULONG *count, ULONG *sequenceNumber)
543{
544 ULONG length;
545 ULONG *tmpData;
546
547 length = mSubMetric->length();
548 *sequenceNumber = mSubMetric->getSequenceNumber() - length;
549 if (length)
550 {
551 tmpData = (ULONG*)RTMemAlloc(sizeof(*tmpData)*length);
552 mSubMetric->query(tmpData);
553 if (mAggregate)
554 {
555 *count = 1;
556 *data = (ULONG*)RTMemAlloc(sizeof(**data));
557 **data = mAggregate->compute(tmpData, length);
558 RTMemFree(tmpData);
559 }
560 else
561 {
562 *count = length;
563 *data = tmpData;
564 }
565 }
566 else
567 {
568 *count = 0;
569 *data = 0;
570 }
571}
572
573ULONG AggregateAvg::compute(ULONG *data, ULONG length)
574{
575 uint64_t tmp = 0;
576 for (ULONG i = 0; i < length; ++i)
577 tmp += data[i];
578 return (ULONG)(tmp / length);
579}
580
581const char * AggregateAvg::getName()
582{
583 return "avg";
584}
585
586ULONG AggregateMin::compute(ULONG *data, ULONG length)
587{
588 ULONG tmp = *data;
589 for (ULONG i = 0; i < length; ++i)
590 if (data[i] < tmp)
591 tmp = data[i];
592 return tmp;
593}
594
595const char * AggregateMin::getName()
596{
597 return "min";
598}
599
600ULONG AggregateMax::compute(ULONG *data, ULONG length)
601{
602 ULONG tmp = *data;
603 for (ULONG i = 0; i < length; ++i)
604 if (data[i] > tmp)
605 tmp = data[i];
606 return tmp;
607}
608
609const char * AggregateMax::getName()
610{
611 return "max";
612}
613
614Filter::Filter(ComSafeArrayIn(IN_BSTR, metricNames),
615 ComSafeArrayIn(IUnknown *, objects))
616{
617 /*
618 * Let's work around null/empty safe array mess. I am not sure there is
619 * a way to pass null arrays via webservice, I haven't found one. So I
620 * guess the users will be forced to use empty arrays instead. Constructing
621 * an empty SafeArray is a bit awkward, so what we do in this method is
622 * actually convert null arrays to empty arrays and pass them down to
623 * init() method. If someone knows how to do it better, please be my guest,
624 * fix it.
625 */
626 if (ComSafeArrayInIsNull(metricNames))
627 {
628 com::SafeArray<BSTR> nameArray;
629 if (ComSafeArrayInIsNull(objects))
630 {
631 com::SafeIfaceArray<IUnknown> objectArray;
632 objectArray.reset(0);
633 init(ComSafeArrayAsInParam(nameArray),
634 ComSafeArrayAsInParam(objectArray));
635 }
636 else
637 {
638 com::SafeIfaceArray<IUnknown> objectArray(ComSafeArrayInArg(objects));
639 init(ComSafeArrayAsInParam(nameArray),
640 ComSafeArrayAsInParam(objectArray));
641 }
642 }
643 else
644 {
645 com::SafeArray<IN_BSTR> nameArray(ComSafeArrayInArg(metricNames));
646 if (ComSafeArrayInIsNull(objects))
647 {
648 com::SafeIfaceArray<IUnknown> objectArray;
649 objectArray.reset(0);
650 init(ComSafeArrayAsInParam(nameArray),
651 ComSafeArrayAsInParam(objectArray));
652 }
653 else
654 {
655 com::SafeIfaceArray<IUnknown> objectArray(ComSafeArrayInArg(objects));
656 init(ComSafeArrayAsInParam(nameArray),
657 ComSafeArrayAsInParam(objectArray));
658 }
659 }
660}
661
662void Filter::init(ComSafeArrayIn(IN_BSTR, metricNames),
663 ComSafeArrayIn(IUnknown *, objects))
664{
665 com::SafeArray<IN_BSTR> nameArray(ComSafeArrayInArg(metricNames));
666 com::SafeIfaceArray<IUnknown> objectArray(ComSafeArrayInArg(objects));
667
668 if (!objectArray.size())
669 {
670 if (nameArray.size())
671 {
672 for (size_t i = 0; i < nameArray.size(); ++i)
673 processMetricList(com::Utf8Str(nameArray[i]), ComPtr<IUnknown>());
674 }
675 else
676 processMetricList("*", ComPtr<IUnknown>());
677 }
678 else
679 {
680 for (size_t i = 0; i < objectArray.size(); ++i)
681 switch (nameArray.size())
682 {
683 case 0:
684 processMetricList("*", objectArray[i]);
685 break;
686 case 1:
687 processMetricList(com::Utf8Str(nameArray[0]), objectArray[i]);
688 break;
689 default:
690 processMetricList(com::Utf8Str(nameArray[i]), objectArray[i]);
691 break;
692 }
693 }
694}
695
696void Filter::processMetricList(const com::Utf8Str &name, const ComPtr<IUnknown> object)
697{
698 size_t startPos = 0;
699
700 for (size_t pos = name.find(",");
701 pos != com::Utf8Str::npos;
702 pos = name.find(",", startPos))
703 {
704 mElements.push_back(std::make_pair(object, iprt::MiniString(name.substr(startPos, pos - startPos).c_str())));
705 startPos = pos + 1;
706 }
707 mElements.push_back(std::make_pair(object, iprt::MiniString(name.substr(startPos).c_str())));
708}
709
710/**
711 * The following method was borrowed from stamR3Match (VMM/STAM.cpp) and
712 * modified to handle the special case of trailing colon in the pattern.
713 *
714 * @returns True if matches, false if not.
715 * @param pszPat Pattern.
716 * @param pszName Name to match against the pattern.
717 * @param fSeenColon Seen colon (':').
718 */
719bool Filter::patternMatch(const char *pszPat, const char *pszName,
720 bool fSeenColon)
721{
722 /* ASSUMES ASCII */
723 for (;;)
724 {
725 char chPat = *pszPat;
726 switch (chPat)
727 {
728 default:
729 if (*pszName != chPat)
730 return false;
731 break;
732
733 case '*':
734 {
735 while ((chPat = *++pszPat) == '*' || chPat == '?')
736 /* nothing */;
737
738 /* Handle a special case, the mask terminating with a colon. */
739 if (chPat == ':')
740 {
741 if (!fSeenColon && !pszPat[1])
742 return !strchr(pszName, ':');
743 fSeenColon = true;
744 }
745
746 for (;;)
747 {
748 char ch = *pszName++;
749 if ( ch == chPat
750 && ( !chPat
751 || patternMatch(pszPat + 1, pszName, fSeenColon)))
752 return true;
753 if (!ch)
754 return false;
755 }
756 /* won't ever get here */
757 break;
758 }
759
760 case '?':
761 if (!*pszName)
762 return false;
763 break;
764
765 /* Handle a special case, the mask terminating with a colon. */
766 case ':':
767 if (!fSeenColon && !pszPat[1])
768 return !*pszName;
769 if (*pszName != ':')
770 return false;
771 fSeenColon = true;
772 break;
773
774 case '\0':
775 return !*pszName;
776 }
777 pszName++;
778 pszPat++;
779 }
780 return true;
781}
782
783bool Filter::match(const ComPtr<IUnknown> object, const iprt::MiniString &name) const
784{
785 ElementList::const_iterator it;
786
787 LogAleksey(("Filter::match(%p, %s)\n", static_cast<const IUnknown*> (object), name.c_str()));
788 for (it = mElements.begin(); it != mElements.end(); it++)
789 {
790 LogAleksey(("...matching against(%p, %s)\n", static_cast<const IUnknown*> ((*it).first), (*it).second.c_str()));
791 if ((*it).first.isNull() || (*it).first == object)
792 {
793 // Objects match, compare names
794 if (patternMatch((*it).second.c_str(), name.c_str()))
795 {
796 LogFlowThisFunc(("...found!\n"));
797 return true;
798 }
799 }
800 }
801 LogAleksey(("...no matches!\n"));
802 return false;
803}
804/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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