VirtualBox

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

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

tstCollector: skip tests if the function is not implemented

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.5 KB
Line 
1/* $Id: tstCollector.cpp 48008 2013-08-23 07:36:35Z 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.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#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 (rc == VERR_NOT_IMPLEMENTED)
166 RTPrintf("tstCollector: getRawHostNetworkLoad() not implemented, skipping\n");
167 else
168 {
169 if (RT_FAILURE(rc))
170 {
171 RTPrintf("tstCollector: getRawHostNetworkLoad() -> %Rrc\n", rc);
172 return 1;
173 }
174
175 RTThreadSleep(5000); // Sleep for five seconds
176
177 rc = collector->preCollect(hints, 0);
178 if (RT_FAILURE(rc))
179 {
180 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
181 return 1;
182 }
183 hostRxStop = hostRxStart;
184 hostTxStop = hostTxStart;
185 rc = collector->getRawHostNetworkLoad(NETIFNAME, &hostRxStop, &hostTxStop);
186 if (RT_FAILURE(rc))
187 {
188 RTPrintf("tstCollector: getRawHostNetworkLoad() -> %Rrc\n", rc);
189 return 1;
190 }
191 RTPrintf("tstCollector: host network speed = %llu bytes/sec (%llu mbit/sec)\n",
192 speed, speed/(1000000/8));
193 RTPrintf("tstCollector: host network rx = %llu bytes/sec (%llu mbit/sec, %u.%u %%)\n",
194 (hostRxStop - hostRxStart)/5, (hostRxStop - hostRxStart)/(5000000/8),
195 (hostRxStop - hostRxStart) * 100 / (speed * 5),
196 (hostRxStop - hostRxStart) * 10000 / (speed * 5) % 100);
197 RTPrintf("tstCollector: host network tx = %llu bytes/sec (%llu mbit/sec, %u.%u %%)\n\n",
198 (hostTxStop - hostTxStart)/5, (hostTxStop - hostTxStart)/(5000000/8),
199 (hostTxStop - hostTxStart) * 100 / (speed * 5),
200 (hostTxStop - hostTxStart) * 10000 / (speed * 5) % 100);
201 }
202
203 return 0;
204}
205
206#define FSNAME "/"
207int testFsUsage(pm::CollectorHAL *collector)
208{
209 RTPrintf("tstCollector: TESTING - File system usage\n");
210
211 ULONG total, used, available;
212
213 int rc = collector->getHostFilesystemUsage(FSNAME, &total, &used, &available);
214 if (RT_FAILURE(rc))
215 {
216 RTPrintf("tstCollector: getHostFilesystemUsage() -> %Rrc\n", rc);
217 return 1;
218 }
219 RTPrintf("tstCollector: host root fs total = %lu mB\n", total);
220 RTPrintf("tstCollector: host root fs used = %lu mB\n", used);
221 RTPrintf("tstCollector: host root fs available = %lu mB\n\n", available);
222 return 0;
223}
224
225int testDisk(pm::CollectorHAL *collector)
226{
227 pm::CollectorHints hints;
228 uint64_t diskMsStart, totalMsStart;
229 uint64_t diskMsStop, totalMsStop;
230
231 pm::DiskList disksUsage, disksLoad;
232 int rc = collector->getDiskListByFs(FSNAME, disksUsage, disksLoad);
233 if (rc == VERR_NOT_IMPLEMENTED)
234 RTPrintf("tstCollector: getDiskListByFs() not implemented, skipping\n");
235 else
236 {
237 if (RT_FAILURE(rc))
238 {
239 RTPrintf("tstCollector: getDiskListByFs(%s) -> %Rrc\n", FSNAME, rc);
240 return 1;
241 }
242 if (disksUsage.empty())
243 {
244 RTPrintf("tstCollector: getDiskListByFs(%s) returned empty usage list\n", FSNAME);
245 return 1;
246 }
247 if (disksLoad.empty())
248 {
249 RTPrintf("tstCollector: getDiskListByFs(%s) returned empty usage list\n", FSNAME);
250 return 1;
251 }
252
253 pm::DiskList::iterator it;
254 for (it = disksUsage.begin(); it != disksUsage.end(); ++it)
255 {
256 uint64_t diskSize = 0;
257 rc = collector->getHostDiskSize(it->c_str(), &diskSize);
258 RTPrintf("tstCollector: TESTING - Disk size (%s) = %llu\n", it->c_str(), diskSize);
259 if (RT_FAILURE(rc))
260 {
261 RTPrintf("tstCollector: getHostDiskSize() -> %Rrc\n", rc);
262 return 1;
263 }
264 }
265
266 for (it = disksLoad.begin(); it != disksLoad.end(); ++it)
267 {
268 RTPrintf("tstCollector: TESTING - Disk utilization (%s), sleeping for 5 sec...\n", it->c_str());
269
270 hints.collectHostCpuLoad();
271 rc = collector->preCollect(hints, 0);
272 if (RT_FAILURE(rc))
273 {
274 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
275 return 1;
276 }
277 rc = collector->getRawHostDiskLoad(it->c_str(), &diskMsStart, &totalMsStart);
278 if (RT_FAILURE(rc))
279 {
280 RTPrintf("tstCollector: getRawHostDiskLoad() -> %Rrc\n", rc);
281 return 1;
282 }
283
284 RTThreadSleep(5000); // Sleep for five seconds
285
286 rc = collector->preCollect(hints, 0);
287 if (RT_FAILURE(rc))
288 {
289 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
290 return 1;
291 }
292 rc = collector->getRawHostDiskLoad(it->c_str(), &diskMsStop, &totalMsStop);
293 if (RT_FAILURE(rc))
294 {
295 RTPrintf("tstCollector: getRawHostDiskLoad() -> %Rrc\n", rc);
296 return 1;
297 }
298 RTPrintf("tstCollector: host disk util = %llu msec (%u.%u %%), total = %llu msec\n\n",
299 (diskMsStop - diskMsStart),
300 (unsigned)((diskMsStop - diskMsStart) * 100 / (totalMsStop - totalMsStart)),
301 (unsigned)((diskMsStop - diskMsStart) * 10000 / (totalMsStop - totalMsStart) % 100),
302 totalMsStop - totalMsStart);
303 }
304 }
305
306 return 0;
307}
308
309
310
311int main(int argc, char *argv[])
312{
313 bool cpuTest, ramTest, netTest, diskTest, fsTest, perfTest;
314 cpuTest = ramTest = netTest = diskTest = fsTest = perfTest = false;
315 /*
316 * Initialize the VBox runtime without loading
317 * the support driver.
318 */
319 int rc = RTR3InitExe(argc, &argv, 0);
320 if (RT_FAILURE(rc))
321 {
322 RTPrintf("tstCollector: RTR3InitExe() -> %d\n", rc);
323 return 1;
324 }
325 if (argc > 1)
326 {
327 if (!strcmp(argv[1], "-child"))
328 {
329 /* We have spawned ourselves as a child process -- scratch the leg */
330 RTThreadSleep(1000000);
331 return 1;
332 }
333 for (int i = 1; i < argc; i++)
334 {
335 if (!strcmp(argv[i], "-cpu"))
336 cpuTest = true;
337 else if (!strcmp(argv[i], "-ram"))
338 ramTest = true;
339 else if (!strcmp(argv[i], "-net"))
340 netTest = true;
341 else if (!strcmp(argv[i], "-disk"))
342 diskTest = true;
343 else if (!strcmp(argv[i], "-fs"))
344 fsTest = true;
345 else if (!strcmp(argv[i], "-perf"))
346 perfTest = true;
347 else
348 {
349 RTPrintf("tstCollector: Unknown option: %s\n", argv[i]);
350 return 2;
351 }
352 }
353 }
354 else
355 cpuTest = ramTest = netTest = diskTest = fsTest = perfTest = true;
356
357#ifdef RT_OS_WINDOWS
358 HRESULT hRes = CoInitialize(NULL);
359 /*
360 * Need to initialize security to access performance enumerators.
361 */
362 hRes = CoInitializeSecurity(
363 NULL,
364 -1,
365 NULL,
366 NULL,
367 RPC_C_AUTHN_LEVEL_NONE,
368 RPC_C_IMP_LEVEL_IMPERSONATE,
369 NULL, EOAC_NONE, 0);
370#endif
371
372 pm::CollectorHAL *collector = pm::createHAL();
373 if (!collector)
374 {
375 RTPrintf("tstCollector: createMetricFactory() failed\n", rc);
376 return 1;
377 }
378
379 pm::CollectorHints hints;
380 if (cpuTest)
381 {
382 hints.collectHostCpuLoad();
383 hints.collectProcessCpuLoad(RTProcSelf());
384 }
385 if (ramTest)
386 {
387 hints.collectHostRamUsage();
388 hints.collectProcessRamUsage(RTProcSelf());
389 }
390
391 uint64_t start;
392
393 uint64_t hostUserStart, hostKernelStart, hostIdleStart;
394 uint64_t hostUserStop, hostKernelStop, hostIdleStop, hostTotal;
395
396 uint64_t processUserStart, processKernelStart, processTotalStart;
397 uint64_t processUserStop, processKernelStop, processTotalStop;
398
399 rc = collector->preCollect(hints, 0);
400 if (RT_FAILURE(rc))
401 {
402 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
403 return 1;
404 }
405 if (cpuTest)
406 {
407 RTPrintf("tstCollector: TESTING - CPU load, sleeping for 5 sec\n");
408
409 rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
410 if (RT_FAILURE(rc))
411 {
412 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
413 return 1;
414 }
415 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
416 if (RT_FAILURE(rc))
417 {
418 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
419 return 1;
420 }
421
422 RTThreadSleep(5000); // Sleep for 5 seconds
423
424 rc = collector->preCollect(hints, 0);
425 if (RT_FAILURE(rc))
426 {
427 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
428 return 1;
429 }
430 rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
431 if (RT_FAILURE(rc))
432 {
433 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
434 return 1;
435 }
436 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
437 if (RT_FAILURE(rc))
438 {
439 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
440 return 1;
441 }
442 hostTotal = hostUserStop - hostUserStart
443 + hostKernelStop - hostKernelStart
444 + hostIdleStop - hostIdleStart;
445 RTPrintf("tstCollector: host cpu user = %u.%u %%\n",
446 (unsigned)((hostUserStop - hostUserStart) * 100 / hostTotal),
447 (unsigned)((hostUserStop - hostUserStart) * 10000 / hostTotal % 100));
448 RTPrintf("tstCollector: host cpu kernel = %u.%u %%\n",
449 (unsigned)((hostKernelStop - hostKernelStart) * 100 / hostTotal),
450 (unsigned)((hostKernelStop - hostKernelStart) * 10000 / hostTotal % 100));
451 RTPrintf("tstCollector: host cpu idle = %u.%u %%\n",
452 (unsigned)((hostIdleStop - hostIdleStart) * 100 / hostTotal),
453 (unsigned)((hostIdleStop - hostIdleStart) * 10000 / hostTotal % 100));
454 RTPrintf("tstCollector: process cpu user = %u.%u %%\n",
455 (unsigned)((processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart)),
456 (unsigned)((processUserStop - processUserStart) * 10000 / (processTotalStop - processTotalStart) % 100));
457 RTPrintf("tstCollector: process cpu kernel = %u.%u %%\n\n",
458 (unsigned)((processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart)),
459 (unsigned)((processKernelStop - processKernelStart) * 10000 / (processTotalStop - processTotalStart) % 100));
460
461 RTPrintf("tstCollector: TESTING - CPU load, looping for 5 sec\n");
462 rc = collector->preCollect(hints, 0);
463 if (RT_FAILURE(rc))
464 {
465 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
466 return 1;
467 }
468 rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
469 if (RT_FAILURE(rc))
470 {
471 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
472 return 1;
473 }
474 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
475 if (RT_FAILURE(rc))
476 {
477 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
478 return 1;
479 }
480 start = RTTimeMilliTS();
481 while (RTTimeMilliTS() - start < 5000)
482 ; // Loop for 5 seconds
483 rc = collector->preCollect(hints, 0);
484 if (RT_FAILURE(rc))
485 {
486 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
487 return 1;
488 }
489 rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
490 if (RT_FAILURE(rc))
491 {
492 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
493 return 1;
494 }
495 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
496 if (RT_FAILURE(rc))
497 {
498 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
499 return 1;
500 }
501 hostTotal = hostUserStop - hostUserStart
502 + hostKernelStop - hostKernelStart
503 + hostIdleStop - hostIdleStart;
504 RTPrintf("tstCollector: host cpu user = %u.%u %%\n",
505 (unsigned)((hostUserStop - hostUserStart) * 100 / hostTotal),
506 (unsigned)((hostUserStop - hostUserStart) * 10000 / hostTotal % 100));
507 RTPrintf("tstCollector: host cpu kernel = %u.%u %%\n",
508 (unsigned)((hostKernelStop - hostKernelStart) * 100 / hostTotal),
509 (unsigned)((hostKernelStop - hostKernelStart) * 10000 / hostTotal % 100));
510 RTPrintf("tstCollector: host cpu idle = %u.%u %%\n",
511 (unsigned)((hostIdleStop - hostIdleStart) * 100 / hostTotal),
512 (unsigned)((hostIdleStop - hostIdleStart) * 10000 / hostTotal % 100));
513 RTPrintf("tstCollector: process cpu user = %u.%u %%\n",
514 (unsigned)((processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart)),
515 (unsigned)((processUserStop - processUserStart) * 10000 / (processTotalStop - processTotalStart) % 100));
516 RTPrintf("tstCollector: process cpu kernel = %u.%u %%\n\n",
517 (unsigned)((processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart)),
518 (unsigned)((processKernelStop - processKernelStart) * 10000 / (processTotalStop - processTotalStart) % 100));
519 }
520
521 if (ramTest)
522 {
523 RTPrintf("tstCollector: TESTING - Memory usage\n");
524
525 ULONG total, used, available, processUsed;
526
527 rc = collector->getHostMemoryUsage(&total, &used, &available);
528 if (RT_FAILURE(rc))
529 {
530 RTPrintf("tstCollector: getHostMemoryUsage() -> %Rrc\n", rc);
531 return 1;
532 }
533 rc = collector->getProcessMemoryUsage(RTProcSelf(), &processUsed);
534 if (RT_FAILURE(rc))
535 {
536 RTPrintf("tstCollector: getProcessMemoryUsage() -> %Rrc\n", rc);
537 return 1;
538 }
539 RTPrintf("tstCollector: host mem total = %lu kB\n", total);
540 RTPrintf("tstCollector: host mem used = %lu kB\n", used);
541 RTPrintf("tstCollector: host mem available = %lu kB\n", available);
542 RTPrintf("tstCollector: process mem used = %lu kB\n\n", processUsed);
543 }
544
545 if (netTest)
546 rc = testNetwork(collector);
547 if (fsTest)
548 rc = testFsUsage(collector);
549 if (diskTest)
550 rc = testDisk(collector);
551 if (perfTest)
552 {
553 RTPrintf("tstCollector: TESTING - Performance\n\n");
554
555 measurePerformance(collector, argv[0], 100);
556 }
557
558 delete collector;
559
560 printf ("\ntstCollector FINISHED.\n");
561
562 return rc;
563}
564
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