1 | /* $Id: Performance.cpp 10868 2008-07-24 18:34:35Z 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) Solaris backend
|
---|
28 | * 2) Linux backend
|
---|
29 | * 3) Detection of erroneous metric names
|
---|
30 | * 4) Min/max ranges for metrics
|
---|
31 | * 5) Darwin backend
|
---|
32 | * 6) [OS/2 backend]
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <VBox/com/array.h>
|
---|
36 | #include <VBox/com/ptr.h>
|
---|
37 | #include <VBox/com/string.h>
|
---|
38 | #include <VBox/err.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include <iprt/mem.h>
|
---|
41 |
|
---|
42 | #include "Logging.h"
|
---|
43 | #include "Performance.h"
|
---|
44 |
|
---|
45 | using namespace pm;
|
---|
46 |
|
---|
47 | // Default factory
|
---|
48 |
|
---|
49 | BaseMetric *MetricFactory::createHostCpuLoad(ComPtr<IUnknown> object, SubMetric *user, SubMetric *kernel, SubMetric *idle)
|
---|
50 | {
|
---|
51 | Assert(mHAL);
|
---|
52 | return new HostCpuLoadRaw(mHAL, object, user, kernel, idle);
|
---|
53 | }
|
---|
54 | BaseMetric *MetricFactory::createHostCpuMHz(ComPtr<IUnknown> object, SubMetric *mhz)
|
---|
55 | {
|
---|
56 | Assert(mHAL);
|
---|
57 | return new HostCpuMhz(mHAL, object, mhz);
|
---|
58 | }
|
---|
59 | BaseMetric *MetricFactory::createHostRamUsage(ComPtr<IUnknown> object, SubMetric *total, SubMetric *used, SubMetric *available)
|
---|
60 | {
|
---|
61 | Assert(mHAL);
|
---|
62 | return new HostRamUsage(mHAL, object, total, used, available);
|
---|
63 | }
|
---|
64 | BaseMetric *MetricFactory::createMachineCpuLoad(ComPtr<IUnknown> object, RTPROCESS process, SubMetric *user, SubMetric *kernel)
|
---|
65 | {
|
---|
66 | Assert(mHAL);
|
---|
67 | return new MachineCpuLoadRaw(mHAL, object, process, user, kernel);
|
---|
68 | }
|
---|
69 | BaseMetric *MetricFactory::createMachineRamUsage(ComPtr<IUnknown> object, RTPROCESS process, SubMetric *used)
|
---|
70 | {
|
---|
71 | Assert(mHAL);
|
---|
72 | return new MachineRamUsage(mHAL, object, process, used);
|
---|
73 | }
|
---|
74 |
|
---|
75 | // Stubs for non-pure virtual methods
|
---|
76 |
|
---|
77 | int CollectorHAL::getHostCpuLoad(unsigned long *user, unsigned long *kernel, unsigned long *idle)
|
---|
78 | {
|
---|
79 | return E_NOTIMPL;
|
---|
80 | }
|
---|
81 |
|
---|
82 | int CollectorHAL::getProcessCpuLoad(RTPROCESS process, unsigned long *user, unsigned long *kernel)
|
---|
83 | {
|
---|
84 | return E_NOTIMPL;
|
---|
85 | }
|
---|
86 |
|
---|
87 | int CollectorHAL::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
|
---|
88 | {
|
---|
89 | return E_NOTIMPL;
|
---|
90 | }
|
---|
91 |
|
---|
92 | int CollectorHAL::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
|
---|
93 | {
|
---|
94 | return E_NOTIMPL;
|
---|
95 | }
|
---|
96 |
|
---|
97 | void BaseMetric::collectorBeat(uint64_t nowAt)
|
---|
98 | {
|
---|
99 | if (isEnabled())
|
---|
100 | {
|
---|
101 | if (nowAt - mLastSampleTaken >= mPeriod * 1000)
|
---|
102 | {
|
---|
103 | mLastSampleTaken = nowAt;
|
---|
104 | LogFlowThisFunc (("Collecting data for obj(%p)...\n", mObject));
|
---|
105 | collect();
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | /*bool BaseMetric::associatedWith(ComPtr<IUnknown> object)
|
---|
111 | {
|
---|
112 | LogFlowThisFunc (("mObject(%p) == object(%p) is %s.\n", mObject, object, mObject == object ? "true" : "false"));
|
---|
113 | return mObject == object;
|
---|
114 | }*/
|
---|
115 |
|
---|
116 | void HostCpuLoad::init(unsigned long period, unsigned long length)
|
---|
117 | {
|
---|
118 | mPeriod = period;
|
---|
119 | mLength = length;
|
---|
120 | mUser->init(mLength);
|
---|
121 | mKernel->init(mLength);
|
---|
122 | mIdle->init(mLength);
|
---|
123 | }
|
---|
124 |
|
---|
125 | void HostCpuLoad::collect()
|
---|
126 | {
|
---|
127 | unsigned long user, kernel, idle;
|
---|
128 | int rc = mHAL->getHostCpuLoad(&user, &kernel, &idle);
|
---|
129 | if (RT_SUCCESS(rc))
|
---|
130 | {
|
---|
131 | mUser->put(user);
|
---|
132 | mKernel->put(kernel);
|
---|
133 | mIdle->put(idle);
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | void HostCpuLoadRaw::collect()
|
---|
138 | {
|
---|
139 | uint64_t user, kernel, idle;
|
---|
140 | uint64_t userDiff, kernelDiff, idleDiff, totalDiff;
|
---|
141 |
|
---|
142 | int rc = mHAL->getRawHostCpuLoad(&user, &kernel, &idle);
|
---|
143 | if (RT_SUCCESS(rc))
|
---|
144 | {
|
---|
145 | userDiff = user - mUserPrev;
|
---|
146 | kernelDiff = kernel - mKernelPrev;
|
---|
147 | idleDiff = idle - mIdlePrev;
|
---|
148 | totalDiff = userDiff + kernelDiff + idleDiff;
|
---|
149 |
|
---|
150 | if (totalDiff == 0)
|
---|
151 | {
|
---|
152 | /* This is only possible if none of counters has changed! */
|
---|
153 | LogFlowThisFunc (("Impossible! User, kernel and idle raw "
|
---|
154 | "counters has not changed since last sample.\n" ));
|
---|
155 | mUser->put(0);
|
---|
156 | mKernel->put(0);
|
---|
157 | mIdle->put(0);
|
---|
158 | }
|
---|
159 | else
|
---|
160 | {
|
---|
161 | mUser->put((unsigned long)(PM_CPU_LOAD_MULTIPLIER * userDiff / totalDiff));
|
---|
162 | mKernel->put((unsigned long)(PM_CPU_LOAD_MULTIPLIER * kernelDiff / totalDiff));
|
---|
163 | mIdle->put((unsigned long)(PM_CPU_LOAD_MULTIPLIER * idleDiff / totalDiff));
|
---|
164 | }
|
---|
165 |
|
---|
166 | mUserPrev = user;
|
---|
167 | mKernelPrev = kernel;
|
---|
168 | mIdlePrev = idle;
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | void HostCpuMhz::init(unsigned long period, unsigned long length)
|
---|
173 | {
|
---|
174 | mPeriod = period;
|
---|
175 | mLength = length;
|
---|
176 | mMHz->init(mLength);
|
---|
177 | }
|
---|
178 |
|
---|
179 | void HostCpuMhz::collect()
|
---|
180 | {
|
---|
181 | unsigned long mhz;
|
---|
182 | int rc = mHAL->getHostCpuMHz(&mhz);
|
---|
183 | if (RT_SUCCESS(rc))
|
---|
184 | mMHz->put(mhz);
|
---|
185 | }
|
---|
186 |
|
---|
187 | void HostRamUsage::init(unsigned long period, unsigned long length)
|
---|
188 | {
|
---|
189 | mPeriod = period;
|
---|
190 | mLength = length;
|
---|
191 | mTotal->init(mLength);
|
---|
192 | mUsed->init(mLength);
|
---|
193 | mAvailable->init(mLength);
|
---|
194 | }
|
---|
195 |
|
---|
196 | void HostRamUsage::collect()
|
---|
197 | {
|
---|
198 | unsigned long total, used, available;
|
---|
199 | int rc = mHAL->getHostMemoryUsage(&total, &used, &available);
|
---|
200 | if (RT_SUCCESS(rc))
|
---|
201 | {
|
---|
202 | mTotal->put(total);
|
---|
203 | mUsed->put(used);
|
---|
204 | mAvailable->put(available);
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 |
|
---|
210 | void MachineCpuLoad::init(unsigned long period, unsigned long length)
|
---|
211 | {
|
---|
212 | mPeriod = period;
|
---|
213 | mLength = length;
|
---|
214 | mUser->init(mLength);
|
---|
215 | mKernel->init(mLength);
|
---|
216 | }
|
---|
217 |
|
---|
218 | void MachineCpuLoad::collect()
|
---|
219 | {
|
---|
220 | unsigned long user, kernel;
|
---|
221 | int rc = mHAL->getProcessCpuLoad(mProcess, &user, &kernel);
|
---|
222 | if (RT_SUCCESS(rc))
|
---|
223 | {
|
---|
224 | mUser->put(user);
|
---|
225 | mKernel->put(kernel);
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | void MachineCpuLoadRaw::collect()
|
---|
230 | {
|
---|
231 | uint64_t processUser, processKernel, hostTotal;
|
---|
232 |
|
---|
233 | int rc = mHAL->getRawProcessCpuLoad(mProcess, &processUser, &processKernel, &hostTotal);
|
---|
234 | if (RT_SUCCESS(rc))
|
---|
235 | {
|
---|
236 | if (hostTotal == mHostTotalPrev)
|
---|
237 | {
|
---|
238 | /* Nearly impossible, but... */
|
---|
239 | mUser->put(0);
|
---|
240 | mKernel->put(0);
|
---|
241 | }
|
---|
242 | else
|
---|
243 | {
|
---|
244 | mUser->put((unsigned long)(PM_CPU_LOAD_MULTIPLIER * (processUser - mProcessUserPrev) / (hostTotal - mHostTotalPrev)));
|
---|
245 | mKernel->put((unsigned long)(PM_CPU_LOAD_MULTIPLIER * (processKernel - mProcessKernelPrev ) / (hostTotal - mHostTotalPrev)));
|
---|
246 | }
|
---|
247 |
|
---|
248 | mHostTotalPrev = hostTotal;
|
---|
249 | mProcessUserPrev = processUser;
|
---|
250 | mProcessKernelPrev = processKernel;
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | void MachineRamUsage::init(unsigned long period, unsigned long length)
|
---|
255 | {
|
---|
256 | mPeriod = period;
|
---|
257 | mLength = length;
|
---|
258 | mUsed->init(mLength);
|
---|
259 | }
|
---|
260 |
|
---|
261 | void MachineRamUsage::collect()
|
---|
262 | {
|
---|
263 | unsigned long used;
|
---|
264 | int rc = mHAL->getProcessMemoryUsage(mProcess, &used);
|
---|
265 | if (RT_SUCCESS(rc))
|
---|
266 | mUsed->put(used);
|
---|
267 | }
|
---|
268 |
|
---|
269 | void CircularBuffer::init(unsigned long length)
|
---|
270 | {
|
---|
271 | if (mData)
|
---|
272 | RTMemFree(mData);
|
---|
273 | mLength = length;
|
---|
274 | mData = (unsigned long *)RTMemAllocZ(length * sizeof(unsigned long));
|
---|
275 | mWrapped = false;
|
---|
276 | mEnd = 0;
|
---|
277 | }
|
---|
278 |
|
---|
279 | unsigned long CircularBuffer::length()
|
---|
280 | {
|
---|
281 | return mWrapped ? mLength : mEnd;
|
---|
282 | }
|
---|
283 |
|
---|
284 | void CircularBuffer::put(unsigned long value)
|
---|
285 | {
|
---|
286 | if (mData)
|
---|
287 | {
|
---|
288 | mData[mEnd++] = value;
|
---|
289 | if (mEnd >= mLength)
|
---|
290 | {
|
---|
291 | mEnd = 0;
|
---|
292 | mWrapped = true;
|
---|
293 | }
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | void CircularBuffer::copyTo(unsigned long *data)
|
---|
298 | {
|
---|
299 | if (mWrapped)
|
---|
300 | {
|
---|
301 | memcpy(data, mData + mEnd, (mLength - mEnd) * sizeof(unsigned long));
|
---|
302 | // Copy the wrapped part
|
---|
303 | if (mEnd)
|
---|
304 | memcpy(data + (mLength - mEnd), mData, mEnd * sizeof(unsigned long));
|
---|
305 | }
|
---|
306 | else
|
---|
307 | memcpy(data, mData, mEnd * sizeof(unsigned long));
|
---|
308 | }
|
---|
309 |
|
---|
310 | void SubMetric::query(unsigned long *data)
|
---|
311 | {
|
---|
312 | copyTo(data);
|
---|
313 | }
|
---|
314 |
|
---|
315 | void Metric::query(unsigned long **data, unsigned long *count)
|
---|
316 | {
|
---|
317 | unsigned long length;
|
---|
318 | unsigned long *tmpData;
|
---|
319 |
|
---|
320 | length = mSubMetric->length();
|
---|
321 | if (length)
|
---|
322 | {
|
---|
323 | tmpData = (unsigned long*)RTMemAlloc(sizeof(*tmpData)*length);
|
---|
324 | mSubMetric->query(tmpData);
|
---|
325 | if (mAggregate)
|
---|
326 | {
|
---|
327 | *count = 1;
|
---|
328 | *data = (unsigned long*)RTMemAlloc(sizeof(**data));
|
---|
329 | **data = mAggregate->compute(tmpData, length);
|
---|
330 | RTMemFree(tmpData);
|
---|
331 | }
|
---|
332 | else
|
---|
333 | {
|
---|
334 | *count = length;
|
---|
335 | *data = tmpData;
|
---|
336 | }
|
---|
337 | }
|
---|
338 | else
|
---|
339 | {
|
---|
340 | *count = 0;
|
---|
341 | *data = 0;
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|
345 | unsigned long AggregateAvg::compute(unsigned long *data, unsigned long length)
|
---|
346 | {
|
---|
347 | uint64_t tmp = 0;
|
---|
348 | for (unsigned long i = 0; i < length; ++i)
|
---|
349 | tmp += data[i];
|
---|
350 | return (unsigned long)(tmp / length);
|
---|
351 | }
|
---|
352 |
|
---|
353 | const char * AggregateAvg::getName()
|
---|
354 | {
|
---|
355 | return "avg";
|
---|
356 | }
|
---|
357 |
|
---|
358 | unsigned long AggregateMin::compute(unsigned long *data, unsigned long length)
|
---|
359 | {
|
---|
360 | unsigned long tmp = *data;
|
---|
361 | for (unsigned long i = 0; i < length; ++i)
|
---|
362 | if (data[i] < tmp)
|
---|
363 | tmp = data[i];
|
---|
364 | return tmp;
|
---|
365 | }
|
---|
366 |
|
---|
367 | const char * AggregateMin::getName()
|
---|
368 | {
|
---|
369 | return "min";
|
---|
370 | }
|
---|
371 |
|
---|
372 | unsigned long AggregateMax::compute(unsigned long *data, unsigned long length)
|
---|
373 | {
|
---|
374 | unsigned long tmp = *data;
|
---|
375 | for (unsigned long i = 0; i < length; ++i)
|
---|
376 | if (data[i] > tmp)
|
---|
377 | tmp = data[i];
|
---|
378 | return tmp;
|
---|
379 | }
|
---|
380 |
|
---|
381 | const char * AggregateMax::getName()
|
---|
382 | {
|
---|
383 | return "max";
|
---|
384 | }
|
---|
385 |
|
---|
386 | Filter::Filter(ComSafeArrayIn(INPTR BSTR, metricNames),
|
---|
387 | ComSafeArrayIn(IUnknown *, objects))
|
---|
388 | {
|
---|
389 | com::SafeIfaceArray <IUnknown> objectArray(ComSafeArrayInArg(objects));
|
---|
390 | com::SafeArray <INPTR BSTR> nameArray(ComSafeArrayInArg(metricNames));
|
---|
391 | if (objectArray.isNull())
|
---|
392 | {
|
---|
393 | if (nameArray.size())
|
---|
394 | {
|
---|
395 | for (size_t i = 0; i < nameArray.size(); ++i)
|
---|
396 | processMetricList(std::string(com::Utf8Str(nameArray[i])), ComPtr<IUnknown>());
|
---|
397 | }
|
---|
398 | else
|
---|
399 | processMetricList(std::string("*"), ComPtr<IUnknown>());
|
---|
400 | }
|
---|
401 | else
|
---|
402 | {
|
---|
403 | for (size_t i = 0; i < objectArray.size(); ++i)
|
---|
404 | switch (nameArray.size())
|
---|
405 | {
|
---|
406 | case 0:
|
---|
407 | processMetricList(std::string("*"), objectArray[i]);
|
---|
408 | break;
|
---|
409 | case 1:
|
---|
410 | processMetricList(std::string(com::Utf8Str(nameArray[0])), objectArray[i]);
|
---|
411 | break;
|
---|
412 | default:
|
---|
413 | processMetricList(std::string(com::Utf8Str(nameArray[i])), objectArray[i]);
|
---|
414 | break;
|
---|
415 | }
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | void Filter::processMetricList(const std::string &name, const ComPtr<IUnknown> object)
|
---|
420 | {
|
---|
421 | std::string::size_type startPos = 0;
|
---|
422 |
|
---|
423 | for (std::string::size_type pos = name.find(",");
|
---|
424 | pos != std::string::npos;
|
---|
425 | pos = name.find(",", startPos))
|
---|
426 | {
|
---|
427 | mElements.push_back(std::make_pair(object, name.substr(startPos, pos - startPos)));
|
---|
428 | startPos = pos + 1;
|
---|
429 | }
|
---|
430 | mElements.push_back(std::make_pair(object, name.substr(startPos)));
|
---|
431 | }
|
---|
432 |
|
---|
433 | bool Filter::match(const ComPtr<IUnknown> object, const std::string &name) const
|
---|
434 | {
|
---|
435 | ElementList::const_iterator it;
|
---|
436 |
|
---|
437 | printf("Filter::match(%p, %s)\n", static_cast<const IUnknown*> (object), name.c_str());
|
---|
438 | for (it = mElements.begin(); it != mElements.end(); it++)
|
---|
439 | {
|
---|
440 | printf("...matching against(%p, %s)\n", static_cast<const IUnknown*> ((*it).first), (*it).second.c_str());
|
---|
441 | if ((*it).first.isNull() || (*it).first == object)
|
---|
442 | {
|
---|
443 | // Objects match, compare names
|
---|
444 | if ((*it).second == "*" || (*it).second == name)
|
---|
445 | {
|
---|
446 | printf("...found!\n");
|
---|
447 | return true;
|
---|
448 | }
|
---|
449 | }
|
---|
450 | }
|
---|
451 | printf("...no matches!\n");
|
---|
452 | return false;
|
---|
453 | }
|
---|