VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstMp-1.cpp@ 46144

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

Runtime: RTMpGetCoreCount() for Linux

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 10.2 KB
 
1/* $Id: tstMp-1.cpp 46144 2013-05-17 14:46:57Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTMp.
4 */
5
6/*
7 * Copyright (C) 2008-2011 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include <iprt/mp.h>
31#include <iprt/cpuset.h>
32#include <iprt/err.h>
33#include <iprt/initterm.h>
34#include <iprt/stream.h>
35#include <iprt/string.h>
36
37
38/*******************************************************************************
39* Global Variables *
40*******************************************************************************/
41static unsigned g_cErrors = 0;
42
43
44int main()
45{
46 RTR3InitExeNoArguments(0);
47 RTPrintf("tstMp-1: TESTING...\n");
48
49 /*
50 * Present and possible CPUs.
51 */
52 RTCPUID cCpus = RTMpGetCount();
53 if (cCpus > 0)
54 RTPrintf("tstMp-1: RTMpGetCount -> %d\n", (int)cCpus);
55 else
56 {
57 RTPrintf("tstMp-1: FAILURE: RTMpGetCount -> %d\n", (int)cCpus);
58 g_cErrors++;
59 cCpus = 1;
60 }
61
62 RTCPUID cCoreCpus = RTMpGetCoreCount();
63 if (cCoreCpus > 0)
64 RTPrintf("tstMp-1: RTMpGetCoreCount -> %d\n", (int)cCoreCpus);
65 else
66 {
67 RTPrintf("tstMp-1: FAILURE: RTMpGetCoreCount -> %d\n", (int)cCoreCpus);
68 g_cErrors++;
69 cCoreCpus = 1;
70 }
71
72 RTCPUSET Set;
73 PRTCPUSET pSet = RTMpGetSet(&Set);
74 if (pSet == &Set)
75 {
76 if ((RTCPUID)RTCpuSetCount(&Set) != cCpus)
77 {
78 RTPrintf("tstMp-1: FAILURE: RTMpGetSet returned a set with a different cpu count; %d, expected %d\n",
79 RTCpuSetCount(&Set), cCpus);
80 g_cErrors++;
81 }
82 RTPrintf("tstMp-1: Possible CPU mask:\n");
83 for (int iCpu = 0; iCpu < RTCPUSET_MAX_CPUS; iCpu++)
84 {
85 RTCPUID idCpu = RTMpCpuIdFromSetIndex(iCpu);
86 if (RTCpuSetIsMemberByIndex(&Set, iCpu))
87 {
88 RTPrintf("tstMp-1: %2d - id %d: %u/%u MHz", iCpu, (int)idCpu,
89 RTMpGetCurFrequency(idCpu), RTMpGetMaxFrequency(idCpu));
90 if (RTMpIsCpuPresent(idCpu))
91 RTPrintf(RTMpIsCpuOnline(idCpu) ? " online\n" : " offline\n");
92 else
93 {
94 if (!RTMpIsCpuOnline(idCpu))
95 RTPrintf(" absent\n");
96 else
97 {
98 RTPrintf(" online but absent!\n");
99 RTPrintf("tstMp-1: FAILURE: Cpu with index %d is report as !RTIsCpuPresent while RTIsCpuOnline returns true!\n", iCpu);
100 g_cErrors++;
101 }
102 }
103 if (!RTMpIsCpuPossible(idCpu))
104 {
105 RTPrintf("tstMp-1: FAILURE: Cpu with index %d is returned by RTCpuSet but not RTMpIsCpuPossible!\n", iCpu);
106 g_cErrors++;
107 }
108 }
109 else if (RTMpIsCpuPossible(idCpu))
110 {
111 RTPrintf("tstMp-1: FAILURE: Cpu with index %d is returned by RTMpIsCpuPossible but not RTCpuSet!\n", iCpu);
112 g_cErrors++;
113 }
114 else if (RTMpGetCurFrequency(idCpu) != 0)
115 {
116 RTPrintf("tstMp-1: FAILURE: RTMpGetCurFrequency(%d[idx=%d]) didn't return 0 as it should\n", (int)idCpu, iCpu);
117 g_cErrors++;
118 }
119 else if (RTMpGetMaxFrequency(idCpu) != 0)
120 {
121 RTPrintf("tstMp-1: FAILURE: RTMpGetMaxFrequency(%d[idx=%d]) didn't return 0 as it should\n", (int)idCpu, iCpu);
122 g_cErrors++;
123 }
124 }
125 }
126 else
127 {
128 RTPrintf("tstMp-1: FAILURE: RTMpGetSet -> %p, expected %p\n", pSet, &Set);
129 g_cErrors++;
130 RTCpuSetEmpty(&Set);
131 RTCpuSetAdd(&Set, RTMpCpuIdFromSetIndex(0));
132 }
133
134 /*
135 * Online CPUs.
136 */
137 RTCPUID cCpusOnline = RTMpGetOnlineCount();
138 if (cCpusOnline > 0)
139 {
140 if (cCpusOnline <= cCpus)
141 RTPrintf("tstMp-1: RTMpGetOnlineCount -> %d\n", (int)cCpusOnline);
142 else
143 {
144 RTPrintf("tstMp-1: FAILURE: RTMpGetOnlineCount -> %d, expected <= %d\n", (int)cCpusOnline, (int)cCpus);
145 g_cErrors++;
146 cCpusOnline = cCpus;
147 }
148 }
149 else
150 {
151 RTPrintf("tstMp-1: FAILURE: RTMpGetOnlineCount -> %d\n", (int)cCpusOnline);
152 g_cErrors++;
153 cCpusOnline = 1;
154 }
155
156 RTCPUSET SetOnline;
157 pSet = RTMpGetOnlineSet(&SetOnline);
158 if (pSet == &SetOnline)
159 {
160 if (RTCpuSetCount(&SetOnline) <= 0)
161 {
162 RTPrintf("tstMp-1: FAILURE: RTMpGetOnlineSet returned an empty set!\n");
163 g_cErrors++;
164 }
165 else if ((RTCPUID)RTCpuSetCount(&SetOnline) > cCpus)
166 {
167 RTPrintf("tstMp-1: FAILURE: RTMpGetOnlineSet returned a too high value; %d, expected <= %d\n",
168 RTCpuSetCount(&SetOnline), cCpus);
169 g_cErrors++;
170 }
171 RTPrintf("tstMp-1: Online CPU mask:\n");
172 for (int iCpu = 0; iCpu < RTCPUSET_MAX_CPUS; iCpu++)
173 if (RTCpuSetIsMemberByIndex(&SetOnline, iCpu))
174 {
175 RTCPUID idCpu = RTMpCpuIdFromSetIndex(iCpu);
176 RTPrintf("tstMp-1: %2d - id %d: %u/%u MHz %s\n", iCpu, (int)idCpu, RTMpGetCurFrequency(idCpu),
177 RTMpGetMaxFrequency(idCpu), RTMpIsCpuOnline(idCpu) ? "online" : "offline");
178 if (!RTCpuSetIsMemberByIndex(&Set, iCpu))
179 {
180 RTPrintf("tstMp-1: FAILURE: online cpu with index %2d is not a member of the possible cpu set!\n", iCpu);
181 g_cErrors++;
182 }
183 }
184
185 /* There isn't any sane way of testing RTMpIsCpuOnline really... :-/ */
186 }
187 else
188 {
189 RTPrintf("tstMp-1: FAILURE: RTMpGetOnlineSet -> %p, expected %p\n", pSet, &Set);
190 g_cErrors++;
191 }
192
193 /*
194 * Present CPUs.
195 */
196 RTCPUID cCpusPresent = RTMpGetPresentCount();
197 if (cCpusPresent > 0)
198 {
199 if ( cCpusPresent <= cCpus
200 && cCpusPresent >= cCpusOnline)
201 RTPrintf("tstMp-1: RTMpGetPresentCount -> %d\n", (int)cCpusPresent);
202 else
203 {
204 RTPrintf("tstMp-1: FAILURE: RTMpGetPresentCount -> %d, expected <= %d and >= %d\n", (int)cCpusPresent, (int)cCpus, (int)cCpusOnline);
205 g_cErrors++;
206 }
207 }
208 else
209 {
210 RTPrintf("tstMp-1: FAILURE: RTMpGetPresentCount -> %d\n", (int)cCpusPresent);
211 g_cErrors++;
212 cCpusPresent = 1;
213 }
214
215 RTCPUSET SetPresent;
216 pSet = RTMpGetPresentSet(&SetPresent);
217 if (pSet == &SetPresent)
218 {
219 if (RTCpuSetCount(&SetPresent) <= 0)
220 {
221 RTPrintf("tstMp-1: FAILURE: RTMpGetPresentSet returned an empty set!\n");
222 g_cErrors++;
223 }
224 else if ((RTCPUID)RTCpuSetCount(&SetPresent) != cCpusPresent)
225 {
226 RTPrintf("tstMp-1: FAILURE: RTMpGetPresentSet returned a bad value; %d, expected = %d\n",
227 RTCpuSetCount(&SetPresent), cCpusPresent);
228 g_cErrors++;
229 }
230 RTPrintf("tstMp-1: Present CPU mask:\n");
231 for (int iCpu = 0; iCpu < RTCPUSET_MAX_CPUS; iCpu++)
232 if (RTCpuSetIsMemberByIndex(&SetPresent, iCpu))
233 {
234 RTCPUID idCpu = RTMpCpuIdFromSetIndex(iCpu);
235 RTPrintf("tstMp-1: %2d - id %d: %u/%u MHz %s\n", iCpu, (int)idCpu, RTMpGetCurFrequency(idCpu),
236 RTMpGetMaxFrequency(idCpu), RTMpIsCpuPresent(idCpu) ? "present" : "absent");
237 if (!RTCpuSetIsMemberByIndex(&Set, iCpu))
238 {
239 RTPrintf("tstMp-1: FAILURE: online cpu with index %2d is not a member of the possible cpu set!\n", iCpu);
240 g_cErrors++;
241 }
242 }
243
244 /* There isn't any sane way of testing RTMpIsCpuPresent really... :-/ */
245 }
246 else
247 {
248 RTPrintf("tstMp-1: FAILURE: RTMpGetPresentSet -> %p, expected %p\n", pSet, &Set);
249 g_cErrors++;
250 }
251
252
253 /* Find an online cpu for the next test. */
254 RTCPUID idCpuOnline;
255 for (idCpuOnline = 0; idCpuOnline < RTCPUSET_MAX_CPUS; idCpuOnline++)
256 if (RTMpIsCpuOnline(idCpuOnline))
257 break;
258
259 /*
260 * Quick test of RTMpGetDescription.
261 */
262 char szBuf[64];
263 int rc = RTMpGetDescription(idCpuOnline, &szBuf[0], sizeof(szBuf));
264 if (RT_SUCCESS(rc))
265 {
266 RTPrintf("tstMp-1: RTMpGetDescription -> '%s'\n", szBuf);
267
268 size_t cch = strlen(szBuf);
269 rc = RTMpGetDescription(idCpuOnline, &szBuf[0], cch);
270 if (rc != VERR_BUFFER_OVERFLOW)
271 {
272 RTPrintf("tstMp-1: FAILURE: RTMpGetDescription -> %Rrc, expected VERR_BUFFER_OVERFLOW\n", rc);
273 g_cErrors++;
274 }
275 rc = RTMpGetDescription(idCpuOnline, &szBuf[0], cch + 1);
276 if (RT_FAILURE(rc))
277 {
278 RTPrintf("tstMp-1: FAILURE: RTMpGetDescription -> %Rrc, expected VINF_SUCCESS\n", rc);
279 g_cErrors++;
280 }
281 }
282 else
283 {
284 RTPrintf("tstMp-1: FAILURE: RTMpGetDescription -> %Rrc\n", rc);
285 g_cErrors++;
286 }
287
288
289 if (!g_cErrors)
290 RTPrintf("tstMp-1: SUCCESS\n", g_cErrors);
291 else
292 RTPrintf("tstMp-1: FAILURE - %d errors\n", g_cErrors);
293 return !!g_cErrors;
294}
295
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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