VirtualBox

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

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

Main/Metrics: Host network metrics, linux only (#6345)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.1 KB
Line 
1/* $Id: tstCollector.cpp 43445 2012-09-27 08:28:59Z 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
144int testNetwork(pm::CollectorHAL *collector)
145{
146 pm::CollectorHints hints;
147 uint64_t hostRxStart, hostTxStart, speedStart;
148 uint64_t hostRxStop, hostTxStop, speedStop;
149
150 RTPrintf("\ntstCollector: TESTING - Network load, sleeping for 5 sec...\n");
151
152 int rc = collector->preCollect(hints, 0);
153 if (RT_FAILURE(rc))
154 {
155 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
156 return 1;
157 }
158 rc = collector->getRawHostNetworkLoad("eth0", &hostRxStart, &hostTxStart, &speedStart);
159 if (RT_FAILURE(rc))
160 {
161 RTPrintf("tstCollector: getRawHostNetworkLoad() -> %Rrc\n", rc);
162 return 1;
163 }
164
165 RTThreadSleep(5000); // Sleep for five seconds
166
167 rc = collector->preCollect(hints, 0);
168 if (RT_FAILURE(rc))
169 {
170 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
171 return 1;
172 }
173 rc = collector->getRawHostNetworkLoad("eth0", &hostRxStop, &hostTxStop, &speedStop);
174 if (RT_FAILURE(rc))
175 {
176 RTPrintf("tstCollector: getRawHostNetworkLoad() -> %Rrc\n", rc);
177 return 1;
178 }
179 if (speedStart != speedStop)
180 RTPrintf("tstCollector: getRawHostNetworkLoad() returned different bandwidth (%llu != %llu)\n", speedStart, speedStop);
181 RTPrintf("tstCollector: host network speed = %llu bytes/sec (%llu mbit/sec)\n",
182 speedStop, speedStop/(1000000/8));
183 RTPrintf("tstCollector: host network rx = %llu bytes/sec (%llu mbit/sec, %d %%*100)\n",
184 (hostRxStop - hostRxStart)/5, (hostRxStop - hostRxStart)/(5000000/8),
185 (hostRxStop - hostRxStart) * 10000 / (speedStop * 5));
186 RTPrintf("tstCollector: host network tx = %llu bytes/sec (%llu mbit/sec, %d %%*100)\n",
187 (hostTxStop - hostTxStart)/5, (hostTxStop - hostTxStart)/(5000000/8),
188 (hostTxStop - hostTxStart) * 10000 / (speedStop * 5));
189
190 return 0;
191}
192
193int main(int argc, char *argv[])
194{
195 /*
196 * Initialize the VBox runtime without loading
197 * the support driver.
198 */
199 int rc = RTR3InitExe(argc, &argv, 0);
200 if (RT_FAILURE(rc))
201 {
202 RTPrintf("tstCollector: RTR3InitExe() -> %d\n", rc);
203 return 1;
204 }
205 if (argc > 1 && !strcmp(argv[1], "-child"))
206 {
207 /* We have spawned ourselves as a child process -- scratch the leg */
208 RTThreadSleep(1000000);
209 return 1;
210 }
211#ifdef RT_OS_WINDOWS
212 HRESULT hRes = CoInitialize(NULL);
213 /*
214 * Need to initialize security to access performance enumerators.
215 */
216 hRes = CoInitializeSecurity(
217 NULL,
218 -1,
219 NULL,
220 NULL,
221 RPC_C_AUTHN_LEVEL_NONE,
222 RPC_C_IMP_LEVEL_IMPERSONATE,
223 NULL, EOAC_NONE, 0);
224#endif
225
226 pm::CollectorHAL *collector = pm::createHAL();
227 if (!collector)
228 {
229 RTPrintf("tstCollector: createMetricFactory() failed\n", rc);
230 return 1;
231 }
232#if 1
233 pm::CollectorHints hints;
234 hints.collectHostCpuLoad();
235 hints.collectHostRamUsage();
236 hints.collectProcessCpuLoad(RTProcSelf());
237 hints.collectProcessRamUsage(RTProcSelf());
238
239 uint64_t start;
240
241 uint64_t hostUserStart, hostKernelStart, hostIdleStart;
242 uint64_t hostUserStop, hostKernelStop, hostIdleStop, hostTotal;
243
244 uint64_t processUserStart, processKernelStart, processTotalStart;
245 uint64_t processUserStop, processKernelStop, processTotalStop;
246
247 RTPrintf("tstCollector: TESTING - CPU load, sleeping for 5 sec\n");
248
249 rc = collector->preCollect(hints, 0);
250 if (RT_FAILURE(rc))
251 {
252 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
253 return 1;
254 }
255 rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
256 if (RT_FAILURE(rc))
257 {
258 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
259 return 1;
260 }
261 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
262 if (RT_FAILURE(rc))
263 {
264 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
265 return 1;
266 }
267
268 RTThreadSleep(5000); // Sleep for 5 seconds
269
270 rc = collector->preCollect(hints, 0);
271 if (RT_FAILURE(rc))
272 {
273 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
274 return 1;
275 }
276 rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
277 if (RT_FAILURE(rc))
278 {
279 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
280 return 1;
281 }
282 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
283 if (RT_FAILURE(rc))
284 {
285 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
286 return 1;
287 }
288 hostTotal = hostUserStop - hostUserStart
289 + hostKernelStop - hostKernelStart
290 + hostIdleStop - hostIdleStart;
291 /*printf("tstCollector: host cpu user = %f sec\n", (hostUserStop - hostUserStart) / 10000000.);
292 printf("tstCollector: host cpu kernel = %f sec\n", (hostKernelStop - hostKernelStart) / 10000000.);
293 printf("tstCollector: host cpu idle = %f sec\n", (hostIdleStop - hostIdleStart) / 10000000.);
294 printf("tstCollector: host cpu total = %f sec\n", hostTotal / 10000000.);*/
295 RTPrintf("tstCollector: host cpu user = %llu %%\n", (hostUserStop - hostUserStart) * 100 / hostTotal);
296 RTPrintf("tstCollector: host cpu kernel = %llu %%\n", (hostKernelStop - hostKernelStart) * 100 / hostTotal);
297 RTPrintf("tstCollector: host cpu idle = %llu %%\n", (hostIdleStop - hostIdleStart) * 100 / hostTotal);
298 RTPrintf("tstCollector: process cpu user = %llu %%\n", (processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart));
299 RTPrintf("tstCollector: process cpu kernel = %llu %%\n\n", (processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart));
300
301 RTPrintf("tstCollector: TESTING - CPU load, looping for 5 sec\n");
302 rc = collector->preCollect(hints, 0);
303 if (RT_FAILURE(rc))
304 {
305 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
306 return 1;
307 }
308 rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
309 if (RT_FAILURE(rc))
310 {
311 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
312 return 1;
313 }
314 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
315 if (RT_FAILURE(rc))
316 {
317 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
318 return 1;
319 }
320 start = RTTimeMilliTS();
321 while(RTTimeMilliTS() - start < 5000)
322 ; // Loop for 5 seconds
323 rc = collector->preCollect(hints, 0);
324 if (RT_FAILURE(rc))
325 {
326 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
327 return 1;
328 }
329 rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
330 if (RT_FAILURE(rc))
331 {
332 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
333 return 1;
334 }
335 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
336 if (RT_FAILURE(rc))
337 {
338 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
339 return 1;
340 }
341 hostTotal = hostUserStop - hostUserStart
342 + hostKernelStop - hostKernelStart
343 + hostIdleStop - hostIdleStart;
344 RTPrintf("tstCollector: host cpu user = %llu %%\n", (hostUserStop - hostUserStart) * 100 / hostTotal);
345 RTPrintf("tstCollector: host cpu kernel = %llu %%\n", (hostKernelStop - hostKernelStart) * 100 / hostTotal);
346 RTPrintf("tstCollector: host cpu idle = %llu %%\n", (hostIdleStop - hostIdleStart) * 100 / hostTotal);
347 RTPrintf("tstCollector: process cpu user = %llu %%\n", (processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart));
348 RTPrintf("tstCollector: process cpu kernel = %llu %%\n\n", (processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart));
349
350 RTPrintf("tstCollector: TESTING - Memory usage\n");
351
352 ULONG total, used, available, processUsed;
353
354 rc = collector->getHostMemoryUsage(&total, &used, &available);
355 if (RT_FAILURE(rc))
356 {
357 RTPrintf("tstCollector: getHostMemoryUsage() -> %Rrc\n", rc);
358 return 1;
359 }
360 rc = collector->getProcessMemoryUsage(RTProcSelf(), &processUsed);
361 if (RT_FAILURE(rc))
362 {
363 RTPrintf("tstCollector: getProcessMemoryUsage() -> %Rrc\n", rc);
364 return 1;
365 }
366 RTPrintf("tstCollector: host mem total = %lu kB\n", total);
367 RTPrintf("tstCollector: host mem used = %lu kB\n", used);
368 RTPrintf("tstCollector: host mem available = %lu kB\n", available);
369 RTPrintf("tstCollector: process mem used = %lu kB\n", processUsed);
370#endif
371#if 1
372 rc = testNetwork(collector);
373#endif
374#if 0
375 RTPrintf("\ntstCollector: TESTING - Performance\n\n");
376
377 measurePerformance(collector, argv[0], 100);
378#endif
379
380 delete collector;
381
382 printf ("\ntstCollector FINISHED.\n");
383
384 return rc;
385}
386
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