VirtualBox

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

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

Main/Metrics: Host disk size, linux only (#6345)

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