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