VirtualBox

source: vbox/trunk/src/VBox/Main/testcase/tstCollector.cpp@ 44535

Last change on this file since 44535 was 44529, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.2 KB
Line 
1/* $Id: tstCollector.cpp 44529 2013-02-04 15:54:15Z vboxsync $ */
2
3/** @file
4 *
5 * Collector classes test cases.
6 */
7
8/*
9 * Copyright (C) 2008-2012 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#ifdef RT_OS_DARWIN
21# include "../src-server/darwin/PerformanceDarwin.cpp"
22#endif
23#ifdef RT_OS_FREEBSD
24# include "../src-server/freebsd/PerformanceFreeBSD.cpp"
25#endif
26#ifdef RT_OS_LINUX
27# include "../src-server/linux/PerformanceLinux.cpp"
28#endif
29#ifdef RT_OS_OS2
30# include "../src-server/os2/PerformanceOS2.cpp"
31#endif
32#ifdef RT_OS_SOLARIS
33# include "../src-server/solaris/PerformanceSolaris.cpp"
34#endif
35#ifdef RT_OS_WINDOWS
36# define _WIN32_DCOM
37# include <objidl.h>
38# include <objbase.h>
39# include "../src-server/win/PerformanceWin.cpp"
40#endif
41
42#include <iprt/initterm.h>
43#include <iprt/stream.h>
44#include <iprt/env.h>
45#include <iprt/err.h>
46#include <iprt/process.h>
47#include <iprt/thread.h>
48#include <iprt/time.h>
49
50#define RUN_TIME_MS 1000
51
52#define N_CALLS(n, fn) \
53 for (int call = 0; call < n; ++call) \
54 rc = collector->fn; \
55 if (RT_FAILURE(rc)) \
56 RTPrintf("tstCollector: "#fn" -> %Rrc\n", rc)
57
58#define CALLS_PER_SECOND(fn) \
59 nCalls = 0; \
60 start = RTTimeMilliTS(); \
61 do { \
62 rc = collector->fn; \
63 if (RT_FAILURE(rc)) \
64 break; \
65 ++nCalls; \
66 } while(RTTimeMilliTS() - start < RUN_TIME_MS); \
67 if (RT_FAILURE(rc)) \
68 { \
69 RTPrintf("tstCollector: "#fn" -> %Rrc\n", rc); \
70 } \
71 else \
72 RTPrintf("%70s -- %u calls per second\n", #fn, nCalls)
73
74void measurePerformance(pm::CollectorHAL *collector, const char *pszName, int cVMs)
75{
76
77 static const char * const args[] = { pszName, "-child", NULL };
78 pm::CollectorHints hints;
79 std::vector<RTPROCESS> processes;
80
81 hints.collectHostCpuLoad();
82 hints.collectHostRamUsage();
83 /* Start fake VMs */
84 for (int i = 0; i < cVMs; ++i)
85 {
86 RTPROCESS pid;
87 int rc = RTProcCreate(pszName, args, RTENV_DEFAULT, 0, &pid);
88 if (RT_FAILURE(rc))
89 {
90 hints.getProcesses(processes);
91 std::for_each(processes.begin(), processes.end(), std::ptr_fun(RTProcTerminate));
92 RTPrintf("tstCollector: RTProcCreate() -> %Rrc\n", rc);
93 return;
94 }
95 hints.collectProcessCpuLoad(pid);
96 hints.collectProcessRamUsage(pid);
97 }
98
99 hints.getProcesses(processes);
100 RTThreadSleep(30000); // Let children settle for half a minute
101
102 int rc;
103 ULONG tmp;
104 uint64_t tmp64;
105 uint64_t start;
106 unsigned int nCalls;
107 /* Pre-collect */
108 CALLS_PER_SECOND(preCollect(hints, 0));
109 /* Host CPU load */
110 CALLS_PER_SECOND(getRawHostCpuLoad(&tmp64, &tmp64, &tmp64));
111 /* Process CPU load */
112 CALLS_PER_SECOND(getRawProcessCpuLoad(processes[nCalls%cVMs], &tmp64, &tmp64, &tmp64));
113 /* Host CPU speed */
114 CALLS_PER_SECOND(getHostCpuMHz(&tmp));
115 /* Host RAM usage */
116 CALLS_PER_SECOND(getHostMemoryUsage(&tmp, &tmp, &tmp));
117 /* Process RAM usage */
118 CALLS_PER_SECOND(getProcessMemoryUsage(processes[nCalls%cVMs], &tmp));
119
120 start = RTTimeNanoTS();
121
122 int times;
123 for (times = 0; times < 100; times++)
124 {
125 /* Pre-collect */
126 N_CALLS(1, preCollect(hints, 0));
127 /* Host CPU load */
128 N_CALLS(1, getRawHostCpuLoad(&tmp64, &tmp64, &tmp64));
129 /* Host CPU speed */
130 N_CALLS(1, getHostCpuMHz(&tmp));
131 /* Host RAM usage */
132 N_CALLS(1, getHostMemoryUsage(&tmp, &tmp, &tmp));
133 /* Process CPU load */
134 N_CALLS(cVMs, getRawProcessCpuLoad(processes[call], &tmp64, &tmp64, &tmp64));
135 /* Process RAM usage */
136 N_CALLS(cVMs, getProcessMemoryUsage(processes[call], &tmp));
137 }
138 printf("\n%u VMs -- %.2f%% of CPU time\n", cVMs, (RTTimeNanoTS() - start) / 10000000. / times);
139
140 /* Shut down fake VMs */
141 std::for_each(processes.begin(), processes.end(), std::ptr_fun(RTProcTerminate));
142}
143
144#ifdef RT_OS_SOLARIS
145#define NETIFNAME "net0"
146#else
147#define NETIFNAME "eth0"
148#endif
149int testNetwork(pm::CollectorHAL *collector)
150{
151 pm::CollectorHints hints;
152 uint64_t hostRxStart, hostTxStart;
153 uint64_t hostRxStop, hostTxStop, speed = 125000000; /* Assume 1Gbit/s */
154
155 RTPrintf("tstCollector: TESTING - Network load, sleeping for 5 sec...\n");
156
157 hostRxStart = hostTxStart = 0;
158 int rc = collector->preCollect(hints, 0);
159 if (RT_FAILURE(rc))
160 {
161 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
162 return 1;
163 }
164 rc = collector->getRawHostNetworkLoad(NETIFNAME, &hostRxStart, &hostTxStart);
165 if (RT_FAILURE(rc))
166 {
167 RTPrintf("tstCollector: getRawHostNetworkLoad() -> %Rrc\n", rc);
168 return 1;
169 }
170
171 RTThreadSleep(5000); // Sleep for five seconds
172
173 rc = collector->preCollect(hints, 0);
174 if (RT_FAILURE(rc))
175 {
176 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
177 return 1;
178 }
179 hostRxStop = hostRxStart;
180 hostTxStop = hostTxStart;
181 rc = collector->getRawHostNetworkLoad(NETIFNAME, &hostRxStop, &hostTxStop);
182 if (RT_FAILURE(rc))
183 {
184 RTPrintf("tstCollector: getRawHostNetworkLoad() -> %Rrc\n", rc);
185 return 1;
186 }
187 RTPrintf("tstCollector: host network speed = %llu bytes/sec (%llu mbit/sec)\n",
188 speed, speed/(1000000/8));
189 RTPrintf("tstCollector: host network rx = %llu bytes/sec (%llu mbit/sec, %u.%u %%)\n",
190 (hostRxStop - hostRxStart)/5, (hostRxStop - hostRxStart)/(5000000/8),
191 (hostRxStop - hostRxStart) * 100 / (speed * 5),
192 (hostRxStop - hostRxStart) * 10000 / (speed * 5) % 100);
193 RTPrintf("tstCollector: host network tx = %llu bytes/sec (%llu mbit/sec, %u.%u %%)\n\n",
194 (hostTxStop - hostTxStart)/5, (hostTxStop - hostTxStart)/(5000000/8),
195 (hostTxStop - hostTxStart) * 100 / (speed * 5),
196 (hostTxStop - hostTxStart) * 10000 / (speed * 5) % 100);
197
198 return 0;
199}
200
201#define FSNAME "/"
202int testFsUsage(pm::CollectorHAL *collector)
203{
204 RTPrintf("tstCollector: TESTING - File system usage\n");
205
206 ULONG total, used, available;
207
208 int rc = collector->getHostFilesystemUsage(FSNAME, &total, &used, &available);
209 if (RT_FAILURE(rc))
210 {
211 RTPrintf("tstCollector: getHostFilesystemUsage() -> %Rrc\n", rc);
212 return 1;
213 }
214 RTPrintf("tstCollector: host root fs total = %lu mB\n", total);
215 RTPrintf("tstCollector: host root fs used = %lu mB\n", used);
216 RTPrintf("tstCollector: host root fs available = %lu mB\n\n", available);
217 return 0;
218}
219
220int testDisk(pm::CollectorHAL *collector)
221{
222 pm::CollectorHints hints;
223 uint64_t diskMsStart, totalMsStart;
224 uint64_t diskMsStop, totalMsStop;
225
226 std::list<RTCString> disks;
227 int rc = collector->getDiskListByFs(FSNAME, disks);
228 if (RT_FAILURE(rc))
229 {
230 RTPrintf("tstCollector: getDiskListByFs(%s) -> %Rrc\n", FSNAME, rc);
231 return 1;
232 }
233 if (disks.empty())
234 {
235 RTPrintf("tstCollector: getDiskListByFs(%s) returned empty list\n", FSNAME);
236 return 1;
237 }
238
239 uint64_t diskSize = 0;
240 rc = collector->getHostDiskSize(disks.front().c_str(), &diskSize);
241 RTPrintf("tstCollector: TESTING - Disk size (%s) = %llu\n", disks.front().c_str(), diskSize);
242 if (RT_FAILURE(rc))
243 {
244 RTPrintf("tstCollector: getHostDiskSize() -> %Rrc\n", rc);
245 return 1;
246 }
247
248 RTPrintf("tstCollector: TESTING - Disk utilization (%s), sleeping for 5 sec...\n", disks.front().c_str());
249
250 hints.collectHostCpuLoad();
251 rc = collector->preCollect(hints, 0);
252 if (RT_FAILURE(rc))
253 {
254 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
255 return 1;
256 }
257 rc = collector->getRawHostDiskLoad(disks.front().c_str(), &diskMsStart, &totalMsStart);
258 if (RT_FAILURE(rc))
259 {
260 RTPrintf("tstCollector: getRawHostDiskLoad() -> %Rrc\n", rc);
261 return 1;
262 }
263
264 RTThreadSleep(5000); // Sleep for five seconds
265
266 rc = collector->preCollect(hints, 0);
267 if (RT_FAILURE(rc))
268 {
269 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
270 return 1;
271 }
272 rc = collector->getRawHostDiskLoad(disks.front().c_str(), &diskMsStop, &totalMsStop);
273 if (RT_FAILURE(rc))
274 {
275 RTPrintf("tstCollector: getRawHostDiskLoad() -> %Rrc\n", rc);
276 return 1;
277 }
278 RTPrintf("tstCollector: host disk util = %llu msec (%u.%u %%), total = %llu msec\n\n",
279 (diskMsStop - diskMsStart),
280 (unsigned)((diskMsStop - diskMsStart) * 100 / (totalMsStop - totalMsStart)),
281 (unsigned)((diskMsStop - diskMsStart) * 10000 / (totalMsStop - totalMsStart) % 100),
282 totalMsStop - totalMsStart);
283
284 return 0;
285}
286
287
288
289int main(int argc, char *argv[])
290{
291 /*
292 * Initialize the VBox runtime without loading
293 * the support driver.
294 */
295 int rc = RTR3InitExe(argc, &argv, 0);
296 if (RT_FAILURE(rc))
297 {
298 RTPrintf("tstCollector: RTR3InitExe() -> %d\n", rc);
299 return 1;
300 }
301 if (argc > 1 && !strcmp(argv[1], "-child"))
302 {
303 /* We have spawned ourselves as a child process -- scratch the leg */
304 RTThreadSleep(1000000);
305 return 1;
306 }
307#ifdef RT_OS_WINDOWS
308 HRESULT hRes = CoInitialize(NULL);
309 /*
310 * Need to initialize security to access performance enumerators.
311 */
312 hRes = CoInitializeSecurity(
313 NULL,
314 -1,
315 NULL,
316 NULL,
317 RPC_C_AUTHN_LEVEL_NONE,
318 RPC_C_IMP_LEVEL_IMPERSONATE,
319 NULL, EOAC_NONE, 0);
320#endif
321
322 pm::CollectorHAL *collector = pm::createHAL();
323 if (!collector)
324 {
325 RTPrintf("tstCollector: createMetricFactory() failed\n", rc);
326 return 1;
327 }
328#if 1
329 pm::CollectorHints hints;
330 hints.collectHostCpuLoad();
331 hints.collectHostRamUsage();
332 hints.collectProcessCpuLoad(RTProcSelf());
333 hints.collectProcessRamUsage(RTProcSelf());
334
335 uint64_t start;
336
337 uint64_t hostUserStart, hostKernelStart, hostIdleStart;
338 uint64_t hostUserStop, hostKernelStop, hostIdleStop, hostTotal;
339
340 uint64_t processUserStart, processKernelStart, processTotalStart;
341 uint64_t processUserStop, processKernelStop, processTotalStop;
342
343 RTPrintf("tstCollector: TESTING - CPU load, sleeping for 5 sec\n");
344
345 rc = collector->preCollect(hints, 0);
346 if (RT_FAILURE(rc))
347 {
348 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
349 return 1;
350 }
351 rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
352 if (RT_FAILURE(rc))
353 {
354 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
355 return 1;
356 }
357 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
358 if (RT_FAILURE(rc))
359 {
360 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
361 return 1;
362 }
363
364 RTThreadSleep(5000); // Sleep for 5 seconds
365
366 rc = collector->preCollect(hints, 0);
367 if (RT_FAILURE(rc))
368 {
369 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
370 return 1;
371 }
372 rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
373 if (RT_FAILURE(rc))
374 {
375 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
376 return 1;
377 }
378 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
379 if (RT_FAILURE(rc))
380 {
381 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
382 return 1;
383 }
384 hostTotal = hostUserStop - hostUserStart
385 + hostKernelStop - hostKernelStart
386 + hostIdleStop - hostIdleStart;
387 /*printf("tstCollector: host cpu user = %f sec\n", (hostUserStop - hostUserStart) / 10000000.);
388 printf("tstCollector: host cpu kernel = %f sec\n", (hostKernelStop - hostKernelStart) / 10000000.);
389 printf("tstCollector: host cpu idle = %f sec\n", (hostIdleStop - hostIdleStart) / 10000000.);
390 printf("tstCollector: host cpu total = %f sec\n", hostTotal / 10000000.);*/
391 RTPrintf("tstCollector: host cpu user = %u.%u %%\n",
392 (unsigned)((hostUserStop - hostUserStart) * 100 / hostTotal),
393 (unsigned)((hostUserStop - hostUserStart) * 10000 / hostTotal % 100));
394 RTPrintf("tstCollector: host cpu kernel = %u.%u %%\n",
395 (unsigned)((hostKernelStop - hostKernelStart) * 100 / hostTotal),
396 (unsigned)((hostKernelStop - hostKernelStart) * 10000 / hostTotal % 100));
397 RTPrintf("tstCollector: host cpu idle = %u.%u %%\n",
398 (unsigned)((hostIdleStop - hostIdleStart) * 100 / hostTotal),
399 (unsigned)((hostIdleStop - hostIdleStart) * 10000 / hostTotal % 100));
400 RTPrintf("tstCollector: process cpu user = %u.%u %%\n",
401 (unsigned)((processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart)),
402 (unsigned)((processUserStop - processUserStart) * 10000 / (processTotalStop - processTotalStart) % 100));
403 RTPrintf("tstCollector: process cpu kernel = %u.%u %%\n\n",
404 (unsigned)((processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart)),
405 (unsigned)((processKernelStop - processKernelStart) * 10000 / (processTotalStop - processTotalStart) % 100));
406
407 RTPrintf("tstCollector: TESTING - CPU load, looping for 5 sec\n");
408 rc = collector->preCollect(hints, 0);
409 if (RT_FAILURE(rc))
410 {
411 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
412 return 1;
413 }
414 rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
415 if (RT_FAILURE(rc))
416 {
417 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
418 return 1;
419 }
420 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
421 if (RT_FAILURE(rc))
422 {
423 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
424 return 1;
425 }
426 start = RTTimeMilliTS();
427 while(RTTimeMilliTS() - start < 5000)
428 ; // Loop for 5 seconds
429 rc = collector->preCollect(hints, 0);
430 if (RT_FAILURE(rc))
431 {
432 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
433 return 1;
434 }
435 rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
436 if (RT_FAILURE(rc))
437 {
438 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
439 return 1;
440 }
441 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
442 if (RT_FAILURE(rc))
443 {
444 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
445 return 1;
446 }
447 hostTotal = hostUserStop - hostUserStart
448 + hostKernelStop - hostKernelStart
449 + hostIdleStop - hostIdleStart;
450 RTPrintf("tstCollector: host cpu user = %u.%u %%\n",
451 (unsigned)((hostUserStop - hostUserStart) * 100 / hostTotal),
452 (unsigned)((hostUserStop - hostUserStart) * 10000 / hostTotal % 100));
453 RTPrintf("tstCollector: host cpu kernel = %u.%u %%\n",
454 (unsigned)((hostKernelStop - hostKernelStart) * 100 / hostTotal),
455 (unsigned)((hostKernelStop - hostKernelStart) * 10000 / hostTotal % 100));
456 RTPrintf("tstCollector: host cpu idle = %u.%u %%\n",
457 (unsigned)((hostIdleStop - hostIdleStart) * 100 / hostTotal),
458 (unsigned)((hostIdleStop - hostIdleStart) * 10000 / hostTotal % 100));
459 RTPrintf("tstCollector: process cpu user = %u.%u %%\n",
460 (unsigned)((processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart)),
461 (unsigned)((processUserStop - processUserStart) * 10000 / (processTotalStop - processTotalStart) % 100));
462 RTPrintf("tstCollector: process cpu kernel = %u.%u %%\n\n",
463 (unsigned)((processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart)),
464 (unsigned)((processKernelStop - processKernelStart) * 10000 / (processTotalStop - processTotalStart) % 100));
465
466 RTPrintf("tstCollector: TESTING - Memory usage\n");
467
468 ULONG total, used, available, processUsed;
469
470 rc = collector->getHostMemoryUsage(&total, &used, &available);
471 if (RT_FAILURE(rc))
472 {
473 RTPrintf("tstCollector: getHostMemoryUsage() -> %Rrc\n", rc);
474 return 1;
475 }
476 rc = collector->getProcessMemoryUsage(RTProcSelf(), &processUsed);
477 if (RT_FAILURE(rc))
478 {
479 RTPrintf("tstCollector: getProcessMemoryUsage() -> %Rrc\n", rc);
480 return 1;
481 }
482 RTPrintf("tstCollector: host mem total = %lu kB\n", total);
483 RTPrintf("tstCollector: host mem used = %lu kB\n", used);
484 RTPrintf("tstCollector: host mem available = %lu kB\n", available);
485 RTPrintf("tstCollector: process mem used = %lu kB\n\n", processUsed);
486#endif
487#if 1
488 rc = testNetwork(collector);
489#endif
490#if 1
491 rc = testFsUsage(collector);
492#endif
493#if 1
494 rc = testDisk(collector);
495#endif
496#if 1
497 RTPrintf("tstCollector: TESTING - Performance\n\n");
498
499 measurePerformance(collector, argv[0], 100);
500#endif
501
502 delete collector;
503
504 printf ("\ntstCollector FINISHED.\n");
505
506 return rc;
507}
508
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