VirtualBox

source: vbox/trunk/src/VBox/Main/Performance.cpp@ 27789

Last change on this file since 27789 was 27645, checked in by vboxsync, 15 years ago

Todo removed

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.8 KB
Line 
1/* $Id: Performance.cpp 27645 2010-03-23 16:13:21Z vboxsync $ */
2
3/** @file
4 *
5 * VBox Performance Classes implementation.
6 */
7
8/*
9 * Copyright (C) 2008 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20 * Clara, CA 95054 USA or visit http://www.sun.com if you need
21 * additional information or have any questions.
22 */
23
24/*
25 * @todo list:
26 *
27 * 1) Detection of erroneous metric names
28 */
29
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
68/* Generic implementations */
69
70int CollectorHAL::getHostCpuMHz(ULONG *mhz)
71{
72 unsigned cCpus = 0;
73 uint64_t u64TotalMHz = 0;
74 RTCPUSET OnlineSet;
75 RTMpGetOnlineSet(&OnlineSet);
76 for (RTCPUID iCpu = 0; iCpu < RTCPUSET_MAX_CPUS; iCpu++)
77 {
78 LogAleksey(("{%p} " LOG_FN_FMT ": Checking if CPU %d is member of online set...\n",
79 this, __PRETTY_FUNCTION__, (int)iCpu));
80 if (RTCpuSetIsMemberByIndex(&OnlineSet, iCpu))
81 {
82 LogAleksey(("{%p} " LOG_FN_FMT ": Getting frequency for CPU %d...\n",
83 this, __PRETTY_FUNCTION__, (int)iCpu));
84 uint32_t uMHz = RTMpGetCurFrequency(RTMpCpuIdFromSetIndex(iCpu));
85 if (uMHz != 0)
86 {
87 LogAleksey(("{%p} " LOG_FN_FMT ": CPU %d %u MHz\n",
88 this, __PRETTY_FUNCTION__, (int)iCpu, uMHz));
89 u64TotalMHz += uMHz;
90 cCpus++;
91 }
92 }
93 }
94
95 AssertReturn(cCpus, VERR_NOT_IMPLEMENTED);
96 *mhz = (ULONG)(u64TotalMHz / cCpus);
97
98 return VINF_SUCCESS;
99}
100
101bool BaseMetric::collectorBeat(uint64_t nowAt)
102{
103 if (isEnabled())
104 {
105 if (nowAt - mLastSampleTaken >= mPeriod * 1000)
106 {
107 mLastSampleTaken = nowAt;
108 Log4(("{%p} " LOG_FN_FMT ": Collecting %s for obj(%p)...\n",
109 this, __PRETTY_FUNCTION__, getName(), (void *)mObject));
110 return true;
111 }
112 }
113 return false;
114}
115
116/*bool BaseMetric::associatedWith(ComPtr<IUnknown> object)
117{
118 LogFlowThisFunc(("mObject(%p) == object(%p) is %s.\n", mObject, object, mObject == object ? "true" : "false"));
119 return mObject == object;
120}*/
121
122void HostCpuLoad::init(ULONG period, ULONG length)
123{
124 mPeriod = period;
125 mLength = length;
126 mUser->init(mLength);
127 mKernel->init(mLength);
128 mIdle->init(mLength);
129}
130
131void HostCpuLoad::collect()
132{
133 ULONG user, kernel, idle;
134 int rc = mHAL->getHostCpuLoad(&user, &kernel, &idle);
135 if (RT_SUCCESS(rc))
136 {
137 mUser->put(user);
138 mKernel->put(kernel);
139 mIdle->put(idle);
140 }
141}
142
143void HostCpuLoadRaw::preCollect(CollectorHints& hints)
144{
145 hints.collectHostCpuLoad();
146}
147
148void HostCpuLoadRaw::collect()
149{
150 uint64_t user, kernel, idle;
151 uint64_t userDiff, kernelDiff, idleDiff, totalDiff;
152
153 int rc = mHAL->getRawHostCpuLoad(&user, &kernel, &idle);
154 if (RT_SUCCESS(rc))
155 {
156 userDiff = user - mUserPrev;
157 kernelDiff = kernel - mKernelPrev;
158 idleDiff = idle - mIdlePrev;
159 totalDiff = userDiff + kernelDiff + idleDiff;
160
161 if (totalDiff == 0)
162 {
163 /* This is only possible if none of counters has changed! */
164 LogFlowThisFunc(("Impossible! User, kernel and idle raw "
165 "counters has not changed since last sample.\n" ));
166 mUser->put(0);
167 mKernel->put(0);
168 mIdle->put(0);
169 }
170 else
171 {
172 mUser->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * userDiff / totalDiff));
173 mKernel->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * kernelDiff / totalDiff));
174 mIdle->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * idleDiff / totalDiff));
175 }
176
177 mUserPrev = user;
178 mKernelPrev = kernel;
179 mIdlePrev = idle;
180 }
181}
182
183void HostCpuMhz::init(ULONG period, ULONG length)
184{
185 mPeriod = period;
186 mLength = length;
187 mMHz->init(mLength);
188}
189
190void HostCpuMhz::collect()
191{
192 ULONG mhz;
193 int rc = mHAL->getHostCpuMHz(&mhz);
194 if (RT_SUCCESS(rc))
195 mMHz->put(mhz);
196}
197
198void HostRamUsage::init(ULONG period, ULONG length)
199{
200 mPeriod = period;
201 mLength = length;
202 mTotal->init(mLength);
203 mUsed->init(mLength);
204 mAvailable->init(mLength);
205}
206
207void HostRamUsage::preCollect(CollectorHints& hints)
208{
209 hints.collectHostRamUsage();
210}
211
212void HostRamUsage::collect()
213{
214 ULONG total, used, available;
215 int rc = mHAL->getHostMemoryUsage(&total, &used, &available);
216 if (RT_SUCCESS(rc))
217 {
218 mTotal->put(total);
219 mUsed->put(used);
220 mAvailable->put(available);
221 }
222}
223
224
225
226void MachineCpuLoad::init(ULONG period, ULONG length)
227{
228 mPeriod = period;
229 mLength = length;
230 mUser->init(mLength);
231 mKernel->init(mLength);
232}
233
234void MachineCpuLoad::collect()
235{
236 ULONG user, kernel;
237 int rc = mHAL->getProcessCpuLoad(mProcess, &user, &kernel);
238 if (RT_SUCCESS(rc))
239 {
240 mUser->put(user);
241 mKernel->put(kernel);
242 }
243}
244
245void MachineCpuLoadRaw::preCollect(CollectorHints& hints)
246{
247 hints.collectProcessCpuLoad(mProcess);
248}
249
250void MachineCpuLoadRaw::collect()
251{
252 uint64_t processUser, processKernel, hostTotal;
253
254 int rc = mHAL->getRawProcessCpuLoad(mProcess, &processUser, &processKernel, &hostTotal);
255 if (RT_SUCCESS(rc))
256 {
257 if (hostTotal == mHostTotalPrev)
258 {
259 /* Nearly impossible, but... */
260 mUser->put(0);
261 mKernel->put(0);
262 }
263 else
264 {
265 mUser->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * (processUser - mProcessUserPrev) / (hostTotal - mHostTotalPrev)));
266 mKernel->put((ULONG)(PM_CPU_LOAD_MULTIPLIER * (processKernel - mProcessKernelPrev ) / (hostTotal - mHostTotalPrev)));
267 }
268
269 mHostTotalPrev = hostTotal;
270 mProcessUserPrev = processUser;
271 mProcessKernelPrev = processKernel;
272 }
273}
274
275void MachineRamUsage::init(ULONG period, ULONG length)
276{
277 mPeriod = period;
278 mLength = length;
279 mUsed->init(mLength);
280}
281
282void MachineRamUsage::preCollect(CollectorHints& hints)
283{
284 hints.collectProcessRamUsage(mProcess);
285}
286
287void MachineRamUsage::collect()
288{
289 ULONG used;
290 int rc = mHAL->getProcessMemoryUsage(mProcess, &used);
291 if (RT_SUCCESS(rc))
292 mUsed->put(used);
293}
294
295void CircularBuffer::init(ULONG ulLength)
296{
297 if (mData)
298 RTMemFree(mData);
299 mLength = ulLength;
300 if (mLength)
301 mData = (ULONG*)RTMemAllocZ(ulLength * sizeof(ULONG));
302 else
303 mData = NULL;
304 mWrapped = false;
305 mEnd = 0;
306 mSequenceNumber = 0;
307}
308
309ULONG CircularBuffer::length()
310{
311 return mWrapped ? mLength : mEnd;
312}
313
314void CircularBuffer::put(ULONG value)
315{
316 if (mData)
317 {
318 mData[mEnd++] = value;
319 if (mEnd >= mLength)
320 {
321 mEnd = 0;
322 mWrapped = true;
323 }
324 ++mSequenceNumber;
325 }
326}
327
328void CircularBuffer::copyTo(ULONG *data)
329{
330 if (mWrapped)
331 {
332 memcpy(data, mData + mEnd, (mLength - mEnd) * sizeof(ULONG));
333 // Copy the wrapped part
334 if (mEnd)
335 memcpy(data + (mLength - mEnd), mData, mEnd * sizeof(ULONG));
336 }
337 else
338 memcpy(data, mData, mEnd * sizeof(ULONG));
339}
340
341void SubMetric::query(ULONG *data)
342{
343 copyTo(data);
344}
345
346void Metric::query(ULONG **data, ULONG *count, ULONG *sequenceNumber)
347{
348 ULONG length;
349 ULONG *tmpData;
350
351 length = mSubMetric->length();
352 *sequenceNumber = mSubMetric->getSequenceNumber() - length;
353 if (length)
354 {
355 tmpData = (ULONG*)RTMemAlloc(sizeof(*tmpData)*length);
356 mSubMetric->query(tmpData);
357 if (mAggregate)
358 {
359 *count = 1;
360 *data = (ULONG*)RTMemAlloc(sizeof(**data));
361 **data = mAggregate->compute(tmpData, length);
362 RTMemFree(tmpData);
363 }
364 else
365 {
366 *count = length;
367 *data = tmpData;
368 }
369 }
370 else
371 {
372 *count = 0;
373 *data = 0;
374 }
375}
376
377ULONG AggregateAvg::compute(ULONG *data, ULONG length)
378{
379 uint64_t tmp = 0;
380 for (ULONG i = 0; i < length; ++i)
381 tmp += data[i];
382 return (ULONG)(tmp / length);
383}
384
385const char * AggregateAvg::getName()
386{
387 return "avg";
388}
389
390ULONG AggregateMin::compute(ULONG *data, ULONG length)
391{
392 ULONG tmp = *data;
393 for (ULONG i = 0; i < length; ++i)
394 if (data[i] < tmp)
395 tmp = data[i];
396 return tmp;
397}
398
399const char * AggregateMin::getName()
400{
401 return "min";
402}
403
404ULONG AggregateMax::compute(ULONG *data, ULONG length)
405{
406 ULONG tmp = *data;
407 for (ULONG i = 0; i < length; ++i)
408 if (data[i] > tmp)
409 tmp = data[i];
410 return tmp;
411}
412
413const char * AggregateMax::getName()
414{
415 return "max";
416}
417
418Filter::Filter(ComSafeArrayIn(IN_BSTR, metricNames),
419 ComSafeArrayIn(IUnknown *, objects))
420{
421 /*
422 * Let's work around null/empty safe array mess. I am not sure there is
423 * a way to pass null arrays via webservice, I haven't found one. So I
424 * guess the users will be forced to use empty arrays instead. Constructing
425 * an empty SafeArray is a bit awkward, so what we do in this method is
426 * actually convert null arrays to empty arrays and pass them down to
427 * init() method. If someone knows how to do it better, please be my guest,
428 * fix it.
429 */
430 if (ComSafeArrayInIsNull(metricNames))
431 {
432 com::SafeArray<BSTR> nameArray;
433 if (ComSafeArrayInIsNull(objects))
434 {
435 com::SafeIfaceArray<IUnknown> objectArray;
436 objectArray.reset(0);
437 init(ComSafeArrayAsInParam(nameArray),
438 ComSafeArrayAsInParam(objectArray));
439 }
440 else
441 {
442 com::SafeIfaceArray<IUnknown> objectArray(ComSafeArrayInArg(objects));
443 init(ComSafeArrayAsInParam(nameArray),
444 ComSafeArrayAsInParam(objectArray));
445 }
446 }
447 else
448 {
449 com::SafeArray<IN_BSTR> nameArray(ComSafeArrayInArg(metricNames));
450 if (ComSafeArrayInIsNull(objects))
451 {
452 com::SafeIfaceArray<IUnknown> objectArray;
453 objectArray.reset(0);
454 init(ComSafeArrayAsInParam(nameArray),
455 ComSafeArrayAsInParam(objectArray));
456 }
457 else
458 {
459 com::SafeIfaceArray<IUnknown> objectArray(ComSafeArrayInArg(objects));
460 init(ComSafeArrayAsInParam(nameArray),
461 ComSafeArrayAsInParam(objectArray));
462 }
463 }
464}
465
466void Filter::init(ComSafeArrayIn(IN_BSTR, metricNames),
467 ComSafeArrayIn(IUnknown *, objects))
468{
469 com::SafeArray<IN_BSTR> nameArray(ComSafeArrayInArg(metricNames));
470 com::SafeIfaceArray<IUnknown> objectArray(ComSafeArrayInArg(objects));
471
472 if (!objectArray.size())
473 {
474 if (nameArray.size())
475 {
476 for (size_t i = 0; i < nameArray.size(); ++i)
477 processMetricList(com::Utf8Str(nameArray[i]), ComPtr<IUnknown>());
478 }
479 else
480 processMetricList("*", ComPtr<IUnknown>());
481 }
482 else
483 {
484 for (size_t i = 0; i < objectArray.size(); ++i)
485 switch (nameArray.size())
486 {
487 case 0:
488 processMetricList("*", objectArray[i]);
489 break;
490 case 1:
491 processMetricList(com::Utf8Str(nameArray[0]), objectArray[i]);
492 break;
493 default:
494 processMetricList(com::Utf8Str(nameArray[i]), objectArray[i]);
495 break;
496 }
497 }
498}
499
500void Filter::processMetricList(const com::Utf8Str &name, const ComPtr<IUnknown> object)
501{
502 size_t startPos = 0;
503
504 for (size_t pos = name.find(",");
505 pos != com::Utf8Str::npos;
506 pos = name.find(",", startPos))
507 {
508 mElements.push_back(std::make_pair(object, iprt::MiniString(name.substr(startPos, pos - startPos).c_str())));
509 startPos = pos + 1;
510 }
511 mElements.push_back(std::make_pair(object, iprt::MiniString(name.substr(startPos).c_str())));
512}
513
514/**
515 * The following method was borrowed from stamR3Match (VMM/STAM.cpp) and
516 * modified to handle the special case of trailing colon in the pattern.
517 *
518 * @returns True if matches, false if not.
519 * @param pszPat Pattern.
520 * @param pszName Name to match against the pattern.
521 * @param fSeenColon Seen colon (':').
522 */
523bool Filter::patternMatch(const char *pszPat, const char *pszName,
524 bool fSeenColon)
525{
526 /* ASSUMES ASCII */
527 for (;;)
528 {
529 char chPat = *pszPat;
530 switch (chPat)
531 {
532 default:
533 if (*pszName != chPat)
534 return false;
535 break;
536
537 case '*':
538 {
539 while ((chPat = *++pszPat) == '*' || chPat == '?')
540 /* nothing */;
541
542 /* Handle a special case, the mask terminating with a colon. */
543 if (chPat == ':')
544 {
545 if (!fSeenColon && !pszPat[1])
546 return !strchr(pszName, ':');
547 fSeenColon = true;
548 }
549
550 for (;;)
551 {
552 char ch = *pszName++;
553 if ( ch == chPat
554 && ( !chPat
555 || patternMatch(pszPat + 1, pszName, fSeenColon)))
556 return true;
557 if (!ch)
558 return false;
559 }
560 /* won't ever get here */
561 break;
562 }
563
564 case '?':
565 if (!*pszName)
566 return false;
567 break;
568
569 /* Handle a special case, the mask terminating with a colon. */
570 case ':':
571 if (!fSeenColon && !pszPat[1])
572 return !*pszName;
573 if (*pszName != ':')
574 return false;
575 fSeenColon = true;
576 break;
577
578 case '\0':
579 return !*pszName;
580 }
581 pszName++;
582 pszPat++;
583 }
584 return true;
585}
586
587bool Filter::match(const ComPtr<IUnknown> object, const iprt::MiniString &name) const
588{
589 ElementList::const_iterator it;
590
591 LogAleksey(("Filter::match(%p, %s)\n", static_cast<const IUnknown*> (object), name.c_str()));
592 for (it = mElements.begin(); it != mElements.end(); it++)
593 {
594 LogAleksey(("...matching against(%p, %s)\n", static_cast<const IUnknown*> ((*it).first), (*it).second.c_str()));
595 if ((*it).first.isNull() || (*it).first == object)
596 {
597 // Objects match, compare names
598 if (patternMatch((*it).second.c_str(), name.c_str()))
599 {
600 LogFlowThisFunc(("...found!\n"));
601 return true;
602 }
603 }
604 }
605 LogAleksey(("...no matches!\n"));
606 return false;
607}
608/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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