1 | /* $Id: tstCollector.cpp 10955 2008-07-29 19:54:14Z 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/err.h>
|
---|
27 | #include <iprt/process.h>
|
---|
28 |
|
---|
29 | #ifdef RT_OS_SOLARIS
|
---|
30 | #include "../solaris/PerformanceSolaris.cpp"
|
---|
31 | #endif
|
---|
32 | #ifdef RT_OS_LINUX
|
---|
33 | #include "../linux/PerformanceLinux.cpp"
|
---|
34 | #endif
|
---|
35 | #ifdef RT_OS_WINDOWS
|
---|
36 | #include "../win/PerformanceWin.cpp"
|
---|
37 | #endif
|
---|
38 | #ifdef RT_OS_OS2
|
---|
39 | #include "../os2/PerformanceOS2.cpp"
|
---|
40 | #endif
|
---|
41 | #ifdef RT_OS_DARWIN
|
---|
42 | #include "../darwin/PerformanceDarwin.cpp"
|
---|
43 | #endif
|
---|
44 |
|
---|
45 | pm::CollectorHAL *createCollector()
|
---|
46 | {
|
---|
47 | #ifdef RT_OS_SOLARIS
|
---|
48 | return new pm::CollectorSolaris();
|
---|
49 | #endif
|
---|
50 | #ifdef RT_OS_LINUX
|
---|
51 | return new pm::CollectorLinux();
|
---|
52 | #endif
|
---|
53 | #ifdef RT_OS_WINDOWS
|
---|
54 | return new pm::CollectorWin();
|
---|
55 | #endif
|
---|
56 | #ifdef RT_OS_OS2
|
---|
57 | return new pm::CollectorOS2();
|
---|
58 | #endif
|
---|
59 | #ifdef RT_OS_DARWIN
|
---|
60 | return new pm::CollectorDarwin();
|
---|
61 | #endif
|
---|
62 | return 0;
|
---|
63 | }
|
---|
64 |
|
---|
65 | int main(int argc, char *argv[])
|
---|
66 | {
|
---|
67 | /*
|
---|
68 | * Initialize the VBox runtime without loading
|
---|
69 | * the support driver.
|
---|
70 | */
|
---|
71 | int rc = RTR3Init(false);
|
---|
72 | if (RT_FAILURE(rc))
|
---|
73 | {
|
---|
74 | RTPrintf("tstCollector: RTR3Init() -> %d\n", rc);
|
---|
75 | return 1;
|
---|
76 | }
|
---|
77 |
|
---|
78 | pm::CollectorHAL *collector = createCollector();
|
---|
79 | if (!collector)
|
---|
80 | {
|
---|
81 | RTPrintf("tstCollector: createMetricFactory() failed\n", rc);
|
---|
82 | return 1;
|
---|
83 | }
|
---|
84 |
|
---|
85 | uint64_t hostUserStart, hostKernelStart, hostIdleStart;
|
---|
86 | uint64_t hostUserStop, hostKernelStop, hostIdleStop, hostTotal;
|
---|
87 |
|
---|
88 | uint64_t processUserStart, processKernelStart, processTotalStart;
|
---|
89 | uint64_t processUserStop, processKernelStop, processTotalStop;
|
---|
90 |
|
---|
91 | RTPrintf("tstCollector: TESTING - CPU load, sleeping for 5 sec\n");
|
---|
92 |
|
---|
93 | rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
|
---|
94 | if (RT_FAILURE(rc))
|
---|
95 | {
|
---|
96 | RTPrintf("tstCollector: getRawHostCpuLoad() -> %Vrc\n", rc);
|
---|
97 | return 1;
|
---|
98 | }
|
---|
99 | rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
|
---|
100 | if (RT_FAILURE(rc))
|
---|
101 | {
|
---|
102 | RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Vrc\n", rc);
|
---|
103 | return 1;
|
---|
104 | }
|
---|
105 |
|
---|
106 | RTThreadSleep(5000); // Sleep for 5 seconds
|
---|
107 |
|
---|
108 | rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
|
---|
109 | if (RT_FAILURE(rc))
|
---|
110 | {
|
---|
111 | RTPrintf("tstCollector: getRawHostCpuLoad() -> %Vrc\n", rc);
|
---|
112 | return 1;
|
---|
113 | }
|
---|
114 | rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
|
---|
115 | if (RT_FAILURE(rc))
|
---|
116 | {
|
---|
117 | RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Vrc\n", rc);
|
---|
118 | return 1;
|
---|
119 | }
|
---|
120 | hostTotal = hostUserStop - hostUserStart
|
---|
121 | + hostKernelStop - hostKernelStart
|
---|
122 | + hostIdleStop - hostIdleStart;
|
---|
123 | RTPrintf("tstCollector: host cpu user = %llu %%\n", (hostUserStop - hostUserStart) * 100 / hostTotal);
|
---|
124 | RTPrintf("tstCollector: host cpu kernel = %llu %%\n", (hostKernelStop - hostKernelStart) * 100 / hostTotal);
|
---|
125 | RTPrintf("tstCollector: host cpu idle = %llu %%\n", (hostIdleStop - hostIdleStart) * 100 / hostTotal);
|
---|
126 | RTPrintf("tstCollector: process cpu user = %llu %%\n", (processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart));
|
---|
127 | RTPrintf("tstCollector: process cpu kernel = %llu %%\n\n", (processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart));
|
---|
128 |
|
---|
129 | RTPrintf("tstCollector: TESTING - CPU load, looping for 5 sec\n");
|
---|
130 | rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
|
---|
131 | if (RT_FAILURE(rc))
|
---|
132 | {
|
---|
133 | RTPrintf("tstCollector: getRawHostCpuLoad() -> %Vrc\n", rc);
|
---|
134 | return 1;
|
---|
135 | }
|
---|
136 | rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
|
---|
137 | if (RT_FAILURE(rc))
|
---|
138 | {
|
---|
139 | RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Vrc\n", rc);
|
---|
140 | return 1;
|
---|
141 | }
|
---|
142 | uint64_t start = RTTimeMilliTS();
|
---|
143 | while(RTTimeMilliTS() - start < 5000); // Loop for 5 seconds
|
---|
144 | rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
|
---|
145 | if (RT_FAILURE(rc))
|
---|
146 | {
|
---|
147 | RTPrintf("tstCollector: getRawHostCpuLoad() -> %Vrc\n", rc);
|
---|
148 | return 1;
|
---|
149 | }
|
---|
150 | rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
|
---|
151 | if (RT_FAILURE(rc))
|
---|
152 | {
|
---|
153 | RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Vrc\n", rc);
|
---|
154 | return 1;
|
---|
155 | }
|
---|
156 | hostTotal = hostUserStop - hostUserStart
|
---|
157 | + hostKernelStop - hostKernelStart
|
---|
158 | + hostIdleStop - hostIdleStart;
|
---|
159 | RTPrintf("tstCollector: host cpu user = %llu %%\n", (hostUserStop - hostUserStart) * 100 / hostTotal);
|
---|
160 | RTPrintf("tstCollector: host cpu kernel = %llu %%\n", (hostKernelStop - hostKernelStart) * 100 / hostTotal);
|
---|
161 | RTPrintf("tstCollector: host cpu idle = %llu %%\n", (hostIdleStop - hostIdleStart) * 100 / hostTotal);
|
---|
162 | RTPrintf("tstCollector: process cpu user = %llu %%\n", (processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart));
|
---|
163 | RTPrintf("tstCollector: process cpu kernel = %llu %%\n\n", (processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart));
|
---|
164 |
|
---|
165 | RTPrintf("tstCollector: TESTING - Memory usage\n");
|
---|
166 |
|
---|
167 | unsigned long total, used, available, processUsed;
|
---|
168 |
|
---|
169 | rc = collector->getHostMemoryUsage(&total, &used, &available);
|
---|
170 | if (RT_FAILURE(rc))
|
---|
171 | {
|
---|
172 | RTPrintf("tstCollector: getHostMemoryUsage() -> %Vrc\n", rc);
|
---|
173 | return 1;
|
---|
174 | }
|
---|
175 | rc = collector->getProcessMemoryUsage(RTProcSelf(), &processUsed);
|
---|
176 | if (RT_FAILURE(rc))
|
---|
177 | {
|
---|
178 | RTPrintf("tstCollector: getProcessMemoryUsage() -> %Vrc\n", rc);
|
---|
179 | return 1;
|
---|
180 | }
|
---|
181 | RTPrintf("tstCollector: host mem total = %lu kB\n", total);
|
---|
182 | RTPrintf("tstCollector: host mem used = %lu kB\n", used);
|
---|
183 | RTPrintf("tstCollector: host mem available = %lu kB\n", available);
|
---|
184 | RTPrintf("tstCollector: process mem used = %lu kB\n", processUsed);
|
---|
185 |
|
---|
186 | delete collector;
|
---|
187 |
|
---|
188 | printf ("tstCollector FINISHED.\n");
|
---|
189 |
|
---|
190 | return rc;
|
---|
191 | }
|
---|
192 |
|
---|