VirtualBox

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

Last change on this file since 35437 was 35368, checked in by vboxsync, 14 years ago

Main: source re-org.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.2 KB
Line 
1/* $Id: tstCollector.cpp 35368 2010-12-30 13:38:23Z 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 main(int argc, char *argv[])
145{
146 /*
147 * Initialize the VBox runtime without loading
148 * the support driver.
149 */
150 int rc = RTR3Init();
151 if (RT_FAILURE(rc))
152 {
153 RTPrintf("tstCollector: RTR3Init() -> %d\n", rc);
154 return 1;
155 }
156 if (argc > 1 && !strcmp(argv[1], "-child"))
157 {
158 /* We have spawned ourselves as a child process -- scratch the leg */
159 RTThreadSleep(1000000);
160 return 1;
161 }
162#ifdef RT_OS_WINDOWS
163 HRESULT hRes = CoInitialize(NULL);
164 /*
165 * Need to initialize security to access performance enumerators.
166 */
167 hRes = CoInitializeSecurity(
168 NULL,
169 -1,
170 NULL,
171 NULL,
172 RPC_C_AUTHN_LEVEL_NONE,
173 RPC_C_IMP_LEVEL_IMPERSONATE,
174 NULL, EOAC_NONE, 0);
175#endif
176
177 pm::CollectorHAL *collector = pm::createHAL();
178 if (!collector)
179 {
180 RTPrintf("tstCollector: createMetricFactory() failed\n", rc);
181 return 1;
182 }
183#if 1
184 pm::CollectorHints hints;
185 hints.collectHostCpuLoad();
186 hints.collectHostRamUsage();
187 hints.collectProcessCpuLoad(RTProcSelf());
188 hints.collectProcessRamUsage(RTProcSelf());
189
190 uint64_t start;
191
192 uint64_t hostUserStart, hostKernelStart, hostIdleStart;
193 uint64_t hostUserStop, hostKernelStop, hostIdleStop, hostTotal;
194
195 uint64_t processUserStart, processKernelStart, processTotalStart;
196 uint64_t processUserStop, processKernelStop, processTotalStop;
197
198 RTPrintf("tstCollector: TESTING - CPU load, sleeping for 5 sec\n");
199
200 rc = collector->preCollect(hints, 0);
201 if (RT_FAILURE(rc))
202 {
203 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
204 return 1;
205 }
206 rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
207 if (RT_FAILURE(rc))
208 {
209 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
210 return 1;
211 }
212 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
213 if (RT_FAILURE(rc))
214 {
215 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
216 return 1;
217 }
218
219 RTThreadSleep(5000); // Sleep for 5 seconds
220
221 rc = collector->preCollect(hints, 0);
222 if (RT_FAILURE(rc))
223 {
224 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
225 return 1;
226 }
227 rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
228 if (RT_FAILURE(rc))
229 {
230 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
231 return 1;
232 }
233 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
234 if (RT_FAILURE(rc))
235 {
236 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
237 return 1;
238 }
239 hostTotal = hostUserStop - hostUserStart
240 + hostKernelStop - hostKernelStart
241 + hostIdleStop - hostIdleStart;
242 /*printf("tstCollector: host cpu user = %f sec\n", (hostUserStop - hostUserStart) / 10000000.);
243 printf("tstCollector: host cpu kernel = %f sec\n", (hostKernelStop - hostKernelStart) / 10000000.);
244 printf("tstCollector: host cpu idle = %f sec\n", (hostIdleStop - hostIdleStart) / 10000000.);
245 printf("tstCollector: host cpu total = %f sec\n", hostTotal / 10000000.);*/
246 RTPrintf("tstCollector: host cpu user = %llu %%\n", (hostUserStop - hostUserStart) * 100 / hostTotal);
247 RTPrintf("tstCollector: host cpu kernel = %llu %%\n", (hostKernelStop - hostKernelStart) * 100 / hostTotal);
248 RTPrintf("tstCollector: host cpu idle = %llu %%\n", (hostIdleStop - hostIdleStart) * 100 / hostTotal);
249 RTPrintf("tstCollector: process cpu user = %llu %%\n", (processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart));
250 RTPrintf("tstCollector: process cpu kernel = %llu %%\n\n", (processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart));
251
252 RTPrintf("tstCollector: TESTING - CPU load, looping for 5 sec\n");
253 rc = collector->preCollect(hints, 0);
254 if (RT_FAILURE(rc))
255 {
256 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
257 return 1;
258 }
259 rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
260 if (RT_FAILURE(rc))
261 {
262 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
263 return 1;
264 }
265 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
266 if (RT_FAILURE(rc))
267 {
268 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
269 return 1;
270 }
271 start = RTTimeMilliTS();
272 while(RTTimeMilliTS() - start < 5000)
273 ; // Loop for 5 seconds
274 rc = collector->preCollect(hints, 0);
275 if (RT_FAILURE(rc))
276 {
277 RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
278 return 1;
279 }
280 rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
281 if (RT_FAILURE(rc))
282 {
283 RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
284 return 1;
285 }
286 rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
287 if (RT_FAILURE(rc))
288 {
289 RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
290 return 1;
291 }
292 hostTotal = hostUserStop - hostUserStart
293 + hostKernelStop - hostKernelStart
294 + hostIdleStop - hostIdleStart;
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 - Memory usage\n");
302
303 ULONG total, used, available, processUsed;
304
305 rc = collector->getHostMemoryUsage(&total, &used, &available);
306 if (RT_FAILURE(rc))
307 {
308 RTPrintf("tstCollector: getHostMemoryUsage() -> %Rrc\n", rc);
309 return 1;
310 }
311 rc = collector->getProcessMemoryUsage(RTProcSelf(), &processUsed);
312 if (RT_FAILURE(rc))
313 {
314 RTPrintf("tstCollector: getProcessMemoryUsage() -> %Rrc\n", rc);
315 return 1;
316 }
317 RTPrintf("tstCollector: host mem total = %lu kB\n", total);
318 RTPrintf("tstCollector: host mem used = %lu kB\n", used);
319 RTPrintf("tstCollector: host mem available = %lu kB\n", available);
320 RTPrintf("tstCollector: process mem used = %lu kB\n", processUsed);
321#endif
322 RTPrintf("\ntstCollector: TESTING - Performance\n\n");
323
324 measurePerformance(collector, argv[0], 100);
325
326 delete collector;
327
328 printf ("\ntstCollector FINISHED.\n");
329
330 return rc;
331}
332
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