1 | /* $Id: tstCollector.cpp 12400 2008-09-11 10:34:58Z 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 | for (int times = 0; times < 100; times++)
|
---|
141 | {
|
---|
142 | /* Pre-collect */
|
---|
143 | N_CALLS(1, preCollect(hints));
|
---|
144 | /* Host CPU load */
|
---|
145 | N_CALLS(1, getRawHostCpuLoad(&tmp64, &tmp64, &tmp64));
|
---|
146 | /* Host CPU speed */
|
---|
147 | N_CALLS(1, getHostCpuMHz(&tmp));
|
---|
148 | /* Host RAM usage */
|
---|
149 | N_CALLS(1, getHostMemoryUsage(&tmp, &tmp, &tmp));
|
---|
150 | /* Process CPU load */
|
---|
151 | N_CALLS(cVMs, getRawProcessCpuLoad(processes[call], &tmp64, &tmp64, &tmp64));
|
---|
152 | /* Process RAM usage */
|
---|
153 | N_CALLS(cVMs, getProcessMemoryUsage(processes[call], &tmp));
|
---|
154 | }
|
---|
155 | printf("\n%u VMs -- %.2f%% of CPU time\n", cVMs, (RTTimeNanoTS() - start) / 10000000. / times);
|
---|
156 |
|
---|
157 | /* Shut down fake VMs */
|
---|
158 | std::for_each(processes.begin(), processes.end(), std::ptr_fun(RTProcTerminate));
|
---|
159 | }
|
---|
160 |
|
---|
161 | int main(int argc, char *argv[])
|
---|
162 | {
|
---|
163 | /*
|
---|
164 | * Initialize the VBox runtime without loading
|
---|
165 | * the support driver.
|
---|
166 | */
|
---|
167 | int rc = RTR3Init();
|
---|
168 | if (RT_FAILURE(rc))
|
---|
169 | {
|
---|
170 | RTPrintf("tstCollector: RTR3Init() -> %d\n", rc);
|
---|
171 | return 1;
|
---|
172 | }
|
---|
173 | if (argc > 1 && !strcmp(argv[1], "-child"))
|
---|
174 | {
|
---|
175 | /* We have spawned ourselves as a child process -- scratch the leg */
|
---|
176 | RTThreadSleep(1000000);
|
---|
177 | return 1;
|
---|
178 | }
|
---|
179 | #ifdef RT_OS_WINDOWS
|
---|
180 | HRESULT hRes = CoInitialize(NULL);
|
---|
181 | /*
|
---|
182 | * Need to initialize security to access performance enumerators.
|
---|
183 | */
|
---|
184 | hRes = CoInitializeSecurity(
|
---|
185 | NULL,
|
---|
186 | -1,
|
---|
187 | NULL,
|
---|
188 | NULL,
|
---|
189 | RPC_C_AUTHN_LEVEL_NONE,
|
---|
190 | RPC_C_IMP_LEVEL_IMPERSONATE,
|
---|
191 | NULL, EOAC_NONE, 0);
|
---|
192 | #endif
|
---|
193 |
|
---|
194 | pm::CollectorHAL *collector = createCollector();
|
---|
195 | if (!collector)
|
---|
196 | {
|
---|
197 | RTPrintf("tstCollector: createMetricFactory() failed\n", rc);
|
---|
198 | return 1;
|
---|
199 | }
|
---|
200 | #if 0
|
---|
201 | uint64_t start;
|
---|
202 |
|
---|
203 | uint64_t hostUserStart, hostKernelStart, hostIdleStart;
|
---|
204 | uint64_t hostUserStop, hostKernelStop, hostIdleStop, hostTotal;
|
---|
205 |
|
---|
206 | uint64_t processUserStart, processKernelStart, processTotalStart;
|
---|
207 | uint64_t processUserStop, processKernelStop, processTotalStop;
|
---|
208 |
|
---|
209 | RTPrintf("tstCollector: TESTING - CPU load, sleeping for 5 sec\n");
|
---|
210 |
|
---|
211 | rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
|
---|
212 | if (RT_FAILURE(rc))
|
---|
213 | {
|
---|
214 | RTPrintf("tstCollector: getRawHostCpuLoad() -> %Vrc\n", rc);
|
---|
215 | return 1;
|
---|
216 | }
|
---|
217 | rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
|
---|
218 | if (RT_FAILURE(rc))
|
---|
219 | {
|
---|
220 | RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Vrc\n", rc);
|
---|
221 | return 1;
|
---|
222 | }
|
---|
223 |
|
---|
224 | RTThreadSleep(5000); // Sleep for 5 seconds
|
---|
225 |
|
---|
226 | rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
|
---|
227 | if (RT_FAILURE(rc))
|
---|
228 | {
|
---|
229 | RTPrintf("tstCollector: getRawHostCpuLoad() -> %Vrc\n", rc);
|
---|
230 | return 1;
|
---|
231 | }
|
---|
232 | rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
|
---|
233 | if (RT_FAILURE(rc))
|
---|
234 | {
|
---|
235 | RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Vrc\n", rc);
|
---|
236 | return 1;
|
---|
237 | }
|
---|
238 | hostTotal = hostUserStop - hostUserStart
|
---|
239 | + hostKernelStop - hostKernelStart
|
---|
240 | + hostIdleStop - hostIdleStart;
|
---|
241 | RTPrintf("tstCollector: host cpu user = %llu %%\n", (hostUserStop - hostUserStart) * 100 / hostTotal);
|
---|
242 | RTPrintf("tstCollector: host cpu kernel = %llu %%\n", (hostKernelStop - hostKernelStart) * 100 / hostTotal);
|
---|
243 | RTPrintf("tstCollector: host cpu idle = %llu %%\n", (hostIdleStop - hostIdleStart) * 100 / hostTotal);
|
---|
244 | RTPrintf("tstCollector: process cpu user = %llu %%\n", (processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart));
|
---|
245 | RTPrintf("tstCollector: process cpu kernel = %llu %%\n\n", (processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart));
|
---|
246 |
|
---|
247 | RTPrintf("tstCollector: TESTING - CPU load, looping for 5 sec\n");
|
---|
248 | rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
|
---|
249 | if (RT_FAILURE(rc))
|
---|
250 | {
|
---|
251 | RTPrintf("tstCollector: getRawHostCpuLoad() -> %Vrc\n", rc);
|
---|
252 | return 1;
|
---|
253 | }
|
---|
254 | rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
|
---|
255 | if (RT_FAILURE(rc))
|
---|
256 | {
|
---|
257 | RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Vrc\n", rc);
|
---|
258 | return 1;
|
---|
259 | }
|
---|
260 | start = RTTimeMilliTS();
|
---|
261 | while(RTTimeMilliTS() - start < 5000); // Loop for 5 seconds
|
---|
262 | rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
|
---|
263 | if (RT_FAILURE(rc))
|
---|
264 | {
|
---|
265 | RTPrintf("tstCollector: getRawHostCpuLoad() -> %Vrc\n", rc);
|
---|
266 | return 1;
|
---|
267 | }
|
---|
268 | rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
|
---|
269 | if (RT_FAILURE(rc))
|
---|
270 | {
|
---|
271 | RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Vrc\n", rc);
|
---|
272 | return 1;
|
---|
273 | }
|
---|
274 | hostTotal = hostUserStop - hostUserStart
|
---|
275 | + hostKernelStop - hostKernelStart
|
---|
276 | + hostIdleStop - hostIdleStart;
|
---|
277 | RTPrintf("tstCollector: host cpu user = %llu %%\n", (hostUserStop - hostUserStart) * 100 / hostTotal);
|
---|
278 | RTPrintf("tstCollector: host cpu kernel = %llu %%\n", (hostKernelStop - hostKernelStart) * 100 / hostTotal);
|
---|
279 | RTPrintf("tstCollector: host cpu idle = %llu %%\n", (hostIdleStop - hostIdleStart) * 100 / hostTotal);
|
---|
280 | RTPrintf("tstCollector: process cpu user = %llu %%\n", (processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart));
|
---|
281 | RTPrintf("tstCollector: process cpu kernel = %llu %%\n\n", (processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart));
|
---|
282 |
|
---|
283 | RTPrintf("tstCollector: TESTING - Memory usage\n");
|
---|
284 |
|
---|
285 | ULONG total, used, available, processUsed;
|
---|
286 |
|
---|
287 | rc = collector->getHostMemoryUsage(&total, &used, &available);
|
---|
288 | if (RT_FAILURE(rc))
|
---|
289 | {
|
---|
290 | RTPrintf("tstCollector: getHostMemoryUsage() -> %Vrc\n", rc);
|
---|
291 | return 1;
|
---|
292 | }
|
---|
293 | rc = collector->getProcessMemoryUsage(RTProcSelf(), &processUsed);
|
---|
294 | if (RT_FAILURE(rc))
|
---|
295 | {
|
---|
296 | RTPrintf("tstCollector: getProcessMemoryUsage() -> %Vrc\n", rc);
|
---|
297 | return 1;
|
---|
298 | }
|
---|
299 | RTPrintf("tstCollector: host mem total = %lu kB\n", total);
|
---|
300 | RTPrintf("tstCollector: host mem used = %lu kB\n", used);
|
---|
301 | RTPrintf("tstCollector: host mem available = %lu kB\n", available);
|
---|
302 | RTPrintf("tstCollector: process mem used = %lu kB\n", processUsed);
|
---|
303 | #endif
|
---|
304 | RTPrintf("\ntstCollector: TESTING - Performance\n\n");
|
---|
305 |
|
---|
306 | measurePerformance(collector, argv[0], 100);
|
---|
307 |
|
---|
308 | delete collector;
|
---|
309 |
|
---|
310 | printf ("\ntstCollector FINISHED.\n");
|
---|
311 |
|
---|
312 | return rc;
|
---|
313 | }
|
---|
314 |
|
---|