VirtualBox

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

最後變更 在這個檔案從43507是 43507,由 vboxsync 提交於 12 年 前

Main/Metrics: Alternative way to get link speed for old kernels (#6345)

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette