VirtualBox

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

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

Main/Metrics: Minor improvements in tstCollector

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.6 KB
Line 
1/* $Id: tstCollector.cpp 45012 2013-03-13 07:57:24Z 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 pm::DiskList 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 pm::DiskList::iterator it;
240 for (it = disks.begin(); it != disks.end(); ++it)
241 {
242 uint64_t diskSize = 0;
243 rc = collector->getHostDiskSize(disks.front().c_str(), &diskSize);
244 RTPrintf("tstCollector: TESTING - Disk size (%s) = %llu\n", disks.front().c_str(), diskSize);
245 if (RT_FAILURE(rc))
246 {
247 RTPrintf("tstCollector: getHostDiskSize() -> %Rrc\n", rc);
248 return 1;
249 }
250
251 RTPrintf("tstCollector: TESTING - Disk utilization (%s), sleeping for 5 sec...\n", disks.front().c_str());
252
253 hints.collectHostCpuLoad();
254 rc = collector->preCollect(hints, 0);
255 if (RT_FAILURE(rc))
256 {
257 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
258 return 1;
259 }
260 rc = collector->getRawHostDiskLoad(disks.front().c_str(), &diskMsStart, &totalMsStart);
261 if (RT_FAILURE(rc))
262 {
263 RTPrintf("tstCollector: getRawHostDiskLoad() -> %Rrc\n", rc);
264 return 1;
265 }
266
267 RTThreadSleep(5000); // Sleep for five seconds
268
269 rc = collector->preCollect(hints, 0);
270 if (RT_FAILURE(rc))
271 {
272 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
273 return 1;
274 }
275 rc = collector->getRawHostDiskLoad(disks.front().c_str(), &diskMsStop, &totalMsStop);
276 if (RT_FAILURE(rc))
277 {
278 RTPrintf("tstCollector: getRawHostDiskLoad() -> %Rrc\n", rc);
279 return 1;
280 }
281 RTPrintf("tstCollector: host disk util = %llu msec (%u.%u %%), total = %llu msec\n\n",
282 (diskMsStop - diskMsStart),
283 (unsigned)((diskMsStop - diskMsStart) * 100 / (totalMsStop - totalMsStart)),
284 (unsigned)((diskMsStop - diskMsStart) * 10000 / (totalMsStop - totalMsStart) % 100),
285 totalMsStop - totalMsStart);
286 }
287
288 return 0;
289}
290
291
292
293int main(int argc, char *argv[])
294{
295 bool cpuTest, ramTest, netTest, diskTest, fsTest, perfTest;
296 cpuTest = ramTest = netTest = diskTest = fsTest = perfTest = false;
297 /*
298 * Initialize the VBox runtime without loading
299 * the support driver.
300 */
301 int rc = RTR3InitExe(argc, &argv, 0);
302 if (RT_FAILURE(rc))
303 {
304 RTPrintf("tstCollector: RTR3InitExe() -> %d\n", rc);
305 return 1;
306 }
307 if (argc > 1)
308 {
309 if (!strcmp(argv[1], "-child"))
310 {
311 /* We have spawned ourselves as a child process -- scratch the leg */
312 RTThreadSleep(1000000);
313 return 1;
314 }
315 for (int i = 1; i < argc; i++)
316 {
317 if (!strcmp(argv[i], "-cpu"))
318 cpuTest = true;
319 else if (!strcmp(argv[i], "-ram"))
320 ramTest = true;
321 else if (!strcmp(argv[i], "-net"))
322 netTest = true;
323 else if (!strcmp(argv[i], "-disk"))
324 diskTest = true;
325 else if (!strcmp(argv[i], "-fs"))
326 fsTest = true;
327 else if (!strcmp(argv[i], "-perf"))
328 perfTest = true;
329 else
330 {
331 RTPrintf("tstCollector: Unknown option: %s\n", argv[i]);
332 return 2;
333 }
334 }
335 }
336 else
337 cpuTest = ramTest = netTest = diskTest = fsTest = perfTest = true;
338
339#ifdef RT_OS_WINDOWS
340 HRESULT hRes = CoInitialize(NULL);
341 /*
342 * Need to initialize security to access performance enumerators.
343 */
344 hRes = CoInitializeSecurity(
345 NULL,
346 -1,
347 NULL,
348 NULL,
349 RPC_C_AUTHN_LEVEL_NONE,
350 RPC_C_IMP_LEVEL_IMPERSONATE,
351 NULL, EOAC_NONE, 0);
352#endif
353
354 pm::CollectorHAL *collector = pm::createHAL();
355 if (!collector)
356 {
357 RTPrintf("tstCollector: createMetricFactory() failed\n", rc);
358 return 1;
359 }
360
361 pm::CollectorHints hints;
362 if (cpuTest)
363 {
364 hints.collectHostCpuLoad();
365 hints.collectProcessCpuLoad(RTProcSelf());
366 }
367 if (ramTest)
368 {
369 hints.collectHostRamUsage();
370 hints.collectProcessRamUsage(RTProcSelf());
371 }
372
373 uint64_t start;
374
375 uint64_t hostUserStart, hostKernelStart, hostIdleStart;
376 uint64_t hostUserStop, hostKernelStop, hostIdleStop, hostTotal;
377
378 uint64_t processUserStart, processKernelStart, processTotalStart;
379 uint64_t processUserStop, processKernelStop, processTotalStop;
380
381 rc = collector->preCollect(hints, 0);
382 if (RT_FAILURE(rc))
383 {
384 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
385 return 1;
386 }
387 if (cpuTest)
388 {
389 RTPrintf("tstCollector: TESTING - CPU load, sleeping for 5 sec\n");
390
391 rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
392 if (RT_FAILURE(rc))
393 {
394 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
395 return 1;
396 }
397 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
398 if (RT_FAILURE(rc))
399 {
400 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
401 return 1;
402 }
403
404 RTThreadSleep(5000); // Sleep for 5 seconds
405
406 rc = collector->preCollect(hints, 0);
407 if (RT_FAILURE(rc))
408 {
409 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
410 return 1;
411 }
412 rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
413 if (RT_FAILURE(rc))
414 {
415 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
416 return 1;
417 }
418 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
419 if (RT_FAILURE(rc))
420 {
421 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
422 return 1;
423 }
424 hostTotal = hostUserStop - hostUserStart
425 + hostKernelStop - hostKernelStart
426 + hostIdleStop - hostIdleStart;
427 RTPrintf("tstCollector: host cpu user = %u.%u %%\n",
428 (unsigned)((hostUserStop - hostUserStart) * 100 / hostTotal),
429 (unsigned)((hostUserStop - hostUserStart) * 10000 / hostTotal % 100));
430 RTPrintf("tstCollector: host cpu kernel = %u.%u %%\n",
431 (unsigned)((hostKernelStop - hostKernelStart) * 100 / hostTotal),
432 (unsigned)((hostKernelStop - hostKernelStart) * 10000 / hostTotal % 100));
433 RTPrintf("tstCollector: host cpu idle = %u.%u %%\n",
434 (unsigned)((hostIdleStop - hostIdleStart) * 100 / hostTotal),
435 (unsigned)((hostIdleStop - hostIdleStart) * 10000 / hostTotal % 100));
436 RTPrintf("tstCollector: process cpu user = %u.%u %%\n",
437 (unsigned)((processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart)),
438 (unsigned)((processUserStop - processUserStart) * 10000 / (processTotalStop - processTotalStart) % 100));
439 RTPrintf("tstCollector: process cpu kernel = %u.%u %%\n\n",
440 (unsigned)((processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart)),
441 (unsigned)((processKernelStop - processKernelStart) * 10000 / (processTotalStop - processTotalStart) % 100));
442
443 RTPrintf("tstCollector: TESTING - CPU load, looping for 5 sec\n");
444 rc = collector->preCollect(hints, 0);
445 if (RT_FAILURE(rc))
446 {
447 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
448 return 1;
449 }
450 rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
451 if (RT_FAILURE(rc))
452 {
453 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
454 return 1;
455 }
456 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
457 if (RT_FAILURE(rc))
458 {
459 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
460 return 1;
461 }
462 start = RTTimeMilliTS();
463 while(RTTimeMilliTS() - start < 5000)
464 ; // Loop for 5 seconds
465 rc = collector->preCollect(hints, 0);
466 if (RT_FAILURE(rc))
467 {
468 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
469 return 1;
470 }
471 rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
472 if (RT_FAILURE(rc))
473 {
474 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
475 return 1;
476 }
477 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
478 if (RT_FAILURE(rc))
479 {
480 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
481 return 1;
482 }
483 hostTotal = hostUserStop - hostUserStart
484 + hostKernelStop - hostKernelStart
485 + hostIdleStop - hostIdleStart;
486 RTPrintf("tstCollector: host cpu user = %u.%u %%\n",
487 (unsigned)((hostUserStop - hostUserStart) * 100 / hostTotal),
488 (unsigned)((hostUserStop - hostUserStart) * 10000 / hostTotal % 100));
489 RTPrintf("tstCollector: host cpu kernel = %u.%u %%\n",
490 (unsigned)((hostKernelStop - hostKernelStart) * 100 / hostTotal),
491 (unsigned)((hostKernelStop - hostKernelStart) * 10000 / hostTotal % 100));
492 RTPrintf("tstCollector: host cpu idle = %u.%u %%\n",
493 (unsigned)((hostIdleStop - hostIdleStart) * 100 / hostTotal),
494 (unsigned)((hostIdleStop - hostIdleStart) * 10000 / hostTotal % 100));
495 RTPrintf("tstCollector: process cpu user = %u.%u %%\n",
496 (unsigned)((processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart)),
497 (unsigned)((processUserStop - processUserStart) * 10000 / (processTotalStop - processTotalStart) % 100));
498 RTPrintf("tstCollector: process cpu kernel = %u.%u %%\n\n",
499 (unsigned)((processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart)),
500 (unsigned)((processKernelStop - processKernelStart) * 10000 / (processTotalStop - processTotalStart) % 100));
501 }
502
503 if (ramTest)
504 {
505 RTPrintf("tstCollector: TESTING - Memory usage\n");
506
507 ULONG total, used, available, processUsed;
508
509 rc = collector->getHostMemoryUsage(&total, &used, &available);
510 if (RT_FAILURE(rc))
511 {
512 RTPrintf("tstCollector: getHostMemoryUsage() -> %Rrc\n", rc);
513 return 1;
514 }
515 rc = collector->getProcessMemoryUsage(RTProcSelf(), &processUsed);
516 if (RT_FAILURE(rc))
517 {
518 RTPrintf("tstCollector: getProcessMemoryUsage() -> %Rrc\n", rc);
519 return 1;
520 }
521 RTPrintf("tstCollector: host mem total = %lu kB\n", total);
522 RTPrintf("tstCollector: host mem used = %lu kB\n", used);
523 RTPrintf("tstCollector: host mem available = %lu kB\n", available);
524 RTPrintf("tstCollector: process mem used = %lu kB\n\n", processUsed);
525 }
526
527 if (netTest)
528 rc = testNetwork(collector);
529 if (fsTest)
530 rc = testFsUsage(collector);
531 if (diskTest)
532 rc = testDisk(collector);
533 if (perfTest)
534 {
535 RTPrintf("tstCollector: TESTING - Performance\n\n");
536
537 measurePerformance(collector, argv[0], 100);
538 }
539
540 delete collector;
541
542 printf ("\ntstCollector FINISHED.\n");
543
544 return rc;
545}
546
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