1 | /* $Id: tstCollector.cpp 13908 2008-11-06 11:53:47Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | *
|
---|
5 | * Collector classes test cases.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
20 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
21 | * additional information or have any questions.
|
---|
22 | */
|
---|
23 |
|
---|
24 | #ifdef RT_OS_SOLARIS
|
---|
25 | #include "../solaris/PerformanceSolaris.cpp"
|
---|
26 | #endif
|
---|
27 | #ifdef RT_OS_LINUX
|
---|
28 | #include "../linux/PerformanceLinux.cpp"
|
---|
29 | #endif
|
---|
30 | #ifdef RT_OS_WINDOWS
|
---|
31 | #define _WIN32_DCOM
|
---|
32 | #include <objidl.h>
|
---|
33 | #include <objbase.h>
|
---|
34 | #include "../win/PerformanceWin.cpp"
|
---|
35 | #endif
|
---|
36 | #ifdef RT_OS_OS2
|
---|
37 | #include "../os2/PerformanceOS2.cpp"
|
---|
38 | #endif
|
---|
39 | #ifdef RT_OS_DARWIN
|
---|
40 | #include "../darwin/PerformanceDarwin.cpp"
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #include <iprt/runtime.h>
|
---|
44 | #include <iprt/stream.h>
|
---|
45 | #include <iprt/env.h>
|
---|
46 | #include <iprt/err.h>
|
---|
47 | #include <iprt/process.h>
|
---|
48 |
|
---|
49 | #define RUN_TIME_MS 1000
|
---|
50 |
|
---|
51 | #define N_CALLS(n, fn) \
|
---|
52 | for (int call = 0; call < n; ++call) \
|
---|
53 | rc = collector->fn; \
|
---|
54 | if (RT_FAILURE(rc)) \
|
---|
55 | RTPrintf("tstCollector: "#fn" -> %Rrc\n", rc)
|
---|
56 |
|
---|
57 | #define CALLS_PER_SECOND(fn) \
|
---|
58 | nCalls = 0; \
|
---|
59 | start = RTTimeMilliTS(); \
|
---|
60 | do { \
|
---|
61 | rc = collector->fn; \
|
---|
62 | if (RT_FAILURE(rc)) \
|
---|
63 | break; \
|
---|
64 | ++nCalls; \
|
---|
65 | } while(RTTimeMilliTS() - start < RUN_TIME_MS); \
|
---|
66 | if (RT_FAILURE(rc)) \
|
---|
67 | { \
|
---|
68 | RTPrintf("tstCollector: "#fn" -> %Rrc\n", rc); \
|
---|
69 | } \
|
---|
70 | else \
|
---|
71 | RTPrintf("%70s -- %u calls per second\n", #fn, nCalls)
|
---|
72 |
|
---|
73 | void measurePerformance(pm::CollectorHAL *collector, const char *pszName, int cVMs)
|
---|
74 | {
|
---|
75 |
|
---|
76 | static const char * const args[] = { pszName, "-child", NULL };
|
---|
77 | pm::CollectorHints hints;
|
---|
78 | std::vector<RTPROCESS> processes;
|
---|
79 |
|
---|
80 | hints.collectHostCpuLoad();
|
---|
81 | hints.collectHostRamUsage();
|
---|
82 | /* Start fake VMs */
|
---|
83 | for (int i = 0; i < cVMs; ++i)
|
---|
84 | {
|
---|
85 | RTPROCESS pid;
|
---|
86 | int rc = RTProcCreate(pszName, args, RTENV_DEFAULT, 0, &pid);
|
---|
87 | if (RT_FAILURE(rc))
|
---|
88 | {
|
---|
89 | hints.getProcesses(processes);
|
---|
90 | std::for_each(processes.begin(), processes.end(), std::ptr_fun(RTProcTerminate));
|
---|
91 | RTPrintf("tstCollector: RTProcCreate() -> %Rrc\n", rc);
|
---|
92 | return;
|
---|
93 | }
|
---|
94 | hints.collectProcessCpuLoad(pid);
|
---|
95 | hints.collectProcessRamUsage(pid);
|
---|
96 | }
|
---|
97 |
|
---|
98 | hints.getProcesses(processes);
|
---|
99 | RTThreadSleep(30000); // Let children settle for half a minute
|
---|
100 |
|
---|
101 | int rc;
|
---|
102 | ULONG tmp;
|
---|
103 | uint64_t tmp64;
|
---|
104 | uint64_t start;
|
---|
105 | unsigned int nCalls;
|
---|
106 | /* Pre-collect */
|
---|
107 | CALLS_PER_SECOND(preCollect(hints));
|
---|
108 | /* Host CPU load */
|
---|
109 | CALLS_PER_SECOND(getRawHostCpuLoad(&tmp64, &tmp64, &tmp64));
|
---|
110 | /* Process CPU load */
|
---|
111 | CALLS_PER_SECOND(getRawProcessCpuLoad(processes[nCalls%cVMs], &tmp64, &tmp64, &tmp64));
|
---|
112 | /* Host CPU speed */
|
---|
113 | CALLS_PER_SECOND(getHostCpuMHz(&tmp));
|
---|
114 | /* Host RAM usage */
|
---|
115 | CALLS_PER_SECOND(getHostMemoryUsage(&tmp, &tmp, &tmp));
|
---|
116 | /* Process RAM usage */
|
---|
117 | CALLS_PER_SECOND(getProcessMemoryUsage(processes[nCalls%cVMs], &tmp));
|
---|
118 |
|
---|
119 | start = RTTimeNanoTS();
|
---|
120 |
|
---|
121 | int times;
|
---|
122 | for (times = 0; times < 100; times++)
|
---|
123 | {
|
---|
124 | /* Pre-collect */
|
---|
125 | N_CALLS(1, preCollect(hints));
|
---|
126 | /* Host CPU load */
|
---|
127 | N_CALLS(1, getRawHostCpuLoad(&tmp64, &tmp64, &tmp64));
|
---|
128 | /* Host CPU speed */
|
---|
129 | N_CALLS(1, getHostCpuMHz(&tmp));
|
---|
130 | /* Host RAM usage */
|
---|
131 | N_CALLS(1, getHostMemoryUsage(&tmp, &tmp, &tmp));
|
---|
132 | /* Process CPU load */
|
---|
133 | N_CALLS(cVMs, getRawProcessCpuLoad(processes[call], &tmp64, &tmp64, &tmp64));
|
---|
134 | /* Process RAM usage */
|
---|
135 | N_CALLS(cVMs, getProcessMemoryUsage(processes[call], &tmp));
|
---|
136 | }
|
---|
137 | printf("\n%u VMs -- %.2f%% of CPU time\n", cVMs, (RTTimeNanoTS() - start) / 10000000. / times);
|
---|
138 |
|
---|
139 | /* Shut down fake VMs */
|
---|
140 | std::for_each(processes.begin(), processes.end(), std::ptr_fun(RTProcTerminate));
|
---|
141 | }
|
---|
142 |
|
---|
143 | int main(int argc, char *argv[])
|
---|
144 | {
|
---|
145 | /*
|
---|
146 | * Initialize the VBox runtime without loading
|
---|
147 | * the support driver.
|
---|
148 | */
|
---|
149 | int rc = RTR3Init();
|
---|
150 | if (RT_FAILURE(rc))
|
---|
151 | {
|
---|
152 | RTPrintf("tstCollector: RTR3Init() -> %d\n", rc);
|
---|
153 | return 1;
|
---|
154 | }
|
---|
155 | if (argc > 1 && !strcmp(argv[1], "-child"))
|
---|
156 | {
|
---|
157 | /* We have spawned ourselves as a child process -- scratch the leg */
|
---|
158 | RTThreadSleep(1000000);
|
---|
159 | return 1;
|
---|
160 | }
|
---|
161 | #ifdef RT_OS_WINDOWS
|
---|
162 | HRESULT hRes = CoInitialize(NULL);
|
---|
163 | /*
|
---|
164 | * Need to initialize security to access performance enumerators.
|
---|
165 | */
|
---|
166 | hRes = CoInitializeSecurity(
|
---|
167 | NULL,
|
---|
168 | -1,
|
---|
169 | NULL,
|
---|
170 | NULL,
|
---|
171 | RPC_C_AUTHN_LEVEL_NONE,
|
---|
172 | RPC_C_IMP_LEVEL_IMPERSONATE,
|
---|
173 | NULL, EOAC_NONE, 0);
|
---|
174 | #endif
|
---|
175 |
|
---|
176 | pm::CollectorHAL *collector = pm::createHAL();
|
---|
177 | if (!collector)
|
---|
178 | {
|
---|
179 | RTPrintf("tstCollector: createMetricFactory() failed\n", rc);
|
---|
180 | return 1;
|
---|
181 | }
|
---|
182 | #if 1
|
---|
183 | pm::CollectorHints hints;
|
---|
184 | hints.collectHostCpuLoad();
|
---|
185 | hints.collectHostRamUsage();
|
---|
186 | hints.collectProcessCpuLoad(RTProcSelf());
|
---|
187 | hints.collectProcessRamUsage(RTProcSelf());
|
---|
188 |
|
---|
189 | uint64_t start;
|
---|
190 |
|
---|
191 | uint64_t hostUserStart, hostKernelStart, hostIdleStart;
|
---|
192 | uint64_t hostUserStop, hostKernelStop, hostIdleStop, hostTotal;
|
---|
193 |
|
---|
194 | uint64_t processUserStart, processKernelStart, processTotalStart;
|
---|
195 | uint64_t processUserStop, processKernelStop, processTotalStop;
|
---|
196 |
|
---|
197 | RTPrintf("tstCollector: TESTING - CPU load, sleeping for 5 sec\n");
|
---|
198 |
|
---|
199 | rc = collector->preCollect(hints);
|
---|
200 | if (RT_FAILURE(rc))
|
---|
201 | {
|
---|
202 | RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
|
---|
203 | return 1;
|
---|
204 | }
|
---|
205 | rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
|
---|
206 | if (RT_FAILURE(rc))
|
---|
207 | {
|
---|
208 | RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
|
---|
209 | return 1;
|
---|
210 | }
|
---|
211 | rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
|
---|
212 | if (RT_FAILURE(rc))
|
---|
213 | {
|
---|
214 | RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
|
---|
215 | return 1;
|
---|
216 | }
|
---|
217 |
|
---|
218 | RTThreadSleep(5000); // Sleep for 5 seconds
|
---|
219 |
|
---|
220 | rc = collector->preCollect(hints);
|
---|
221 | if (RT_FAILURE(rc))
|
---|
222 | {
|
---|
223 | RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
|
---|
224 | return 1;
|
---|
225 | }
|
---|
226 | rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
|
---|
227 | if (RT_FAILURE(rc))
|
---|
228 | {
|
---|
229 | RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
|
---|
230 | return 1;
|
---|
231 | }
|
---|
232 | rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
|
---|
233 | if (RT_FAILURE(rc))
|
---|
234 | {
|
---|
235 | RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
|
---|
236 | return 1;
|
---|
237 | }
|
---|
238 | hostTotal = hostUserStop - hostUserStart
|
---|
239 | + hostKernelStop - hostKernelStart
|
---|
240 | + hostIdleStop - hostIdleStart;
|
---|
241 | /*printf("tstCollector: host cpu user = %f sec\n", (hostUserStop - hostUserStart) / 10000000.);
|
---|
242 | printf("tstCollector: host cpu kernel = %f sec\n", (hostKernelStop - hostKernelStart) / 10000000.);
|
---|
243 | printf("tstCollector: host cpu idle = %f sec\n", (hostIdleStop - hostIdleStart) / 10000000.);
|
---|
244 | printf("tstCollector: host cpu total = %f sec\n", hostTotal / 10000000.);*/
|
---|
245 | RTPrintf("tstCollector: host cpu user = %llu %%\n", (hostUserStop - hostUserStart) * 100 / hostTotal);
|
---|
246 | RTPrintf("tstCollector: host cpu kernel = %llu %%\n", (hostKernelStop - hostKernelStart) * 100 / hostTotal);
|
---|
247 | RTPrintf("tstCollector: host cpu idle = %llu %%\n", (hostIdleStop - hostIdleStart) * 100 / hostTotal);
|
---|
248 | RTPrintf("tstCollector: process cpu user = %llu %%\n", (processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart));
|
---|
249 | RTPrintf("tstCollector: process cpu kernel = %llu %%\n\n", (processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart));
|
---|
250 |
|
---|
251 | RTPrintf("tstCollector: TESTING - CPU load, looping for 5 sec\n");
|
---|
252 | rc = collector->preCollect(hints);
|
---|
253 | if (RT_FAILURE(rc))
|
---|
254 | {
|
---|
255 | RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
|
---|
256 | return 1;
|
---|
257 | }
|
---|
258 | rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
|
---|
259 | if (RT_FAILURE(rc))
|
---|
260 | {
|
---|
261 | RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
|
---|
262 | return 1;
|
---|
263 | }
|
---|
264 | rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
|
---|
265 | if (RT_FAILURE(rc))
|
---|
266 | {
|
---|
267 | RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
|
---|
268 | return 1;
|
---|
269 | }
|
---|
270 | start = RTTimeMilliTS();
|
---|
271 | while(RTTimeMilliTS() - start < 5000)
|
---|
272 | ; // Loop for 5 seconds
|
---|
273 | rc = collector->preCollect(hints);
|
---|
274 | if (RT_FAILURE(rc))
|
---|
275 | {
|
---|
276 | RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
|
---|
277 | return 1;
|
---|
278 | }
|
---|
279 | rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
|
---|
280 | if (RT_FAILURE(rc))
|
---|
281 | {
|
---|
282 | RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
|
---|
283 | return 1;
|
---|
284 | }
|
---|
285 | rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
|
---|
286 | if (RT_FAILURE(rc))
|
---|
287 | {
|
---|
288 | RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
|
---|
289 | return 1;
|
---|
290 | }
|
---|
291 | hostTotal = hostUserStop - hostUserStart
|
---|
292 | + hostKernelStop - hostKernelStart
|
---|
293 | + hostIdleStop - hostIdleStart;
|
---|
294 | RTPrintf("tstCollector: host cpu user = %llu %%\n", (hostUserStop - hostUserStart) * 100 / hostTotal);
|
---|
295 | RTPrintf("tstCollector: host cpu kernel = %llu %%\n", (hostKernelStop - hostKernelStart) * 100 / hostTotal);
|
---|
296 | RTPrintf("tstCollector: host cpu idle = %llu %%\n", (hostIdleStop - hostIdleStart) * 100 / hostTotal);
|
---|
297 | RTPrintf("tstCollector: process cpu user = %llu %%\n", (processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart));
|
---|
298 | RTPrintf("tstCollector: process cpu kernel = %llu %%\n\n", (processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart));
|
---|
299 |
|
---|
300 | RTPrintf("tstCollector: TESTING - Memory usage\n");
|
---|
301 |
|
---|
302 | ULONG total, used, available, processUsed;
|
---|
303 |
|
---|
304 | rc = collector->getHostMemoryUsage(&total, &used, &available);
|
---|
305 | if (RT_FAILURE(rc))
|
---|
306 | {
|
---|
307 | RTPrintf("tstCollector: getHostMemoryUsage() -> %Rrc\n", rc);
|
---|
308 | return 1;
|
---|
309 | }
|
---|
310 | rc = collector->getProcessMemoryUsage(RTProcSelf(), &processUsed);
|
---|
311 | if (RT_FAILURE(rc))
|
---|
312 | {
|
---|
313 | RTPrintf("tstCollector: getProcessMemoryUsage() -> %Rrc\n", rc);
|
---|
314 | return 1;
|
---|
315 | }
|
---|
316 | RTPrintf("tstCollector: host mem total = %lu kB\n", total);
|
---|
317 | RTPrintf("tstCollector: host mem used = %lu kB\n", used);
|
---|
318 | RTPrintf("tstCollector: host mem available = %lu kB\n", available);
|
---|
319 | RTPrintf("tstCollector: process mem used = %lu kB\n", processUsed);
|
---|
320 | #endif
|
---|
321 | RTPrintf("\ntstCollector: TESTING - Performance\n\n");
|
---|
322 |
|
---|
323 | measurePerformance(collector, argv[0], 100);
|
---|
324 |
|
---|
325 | delete collector;
|
---|
326 |
|
---|
327 | printf ("\ntstCollector FINISHED.\n");
|
---|
328 |
|
---|
329 | return rc;
|
---|
330 | }
|
---|
331 |
|
---|