1 | /* $Id: tstRTMp-1.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - RTMp.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/mp.h>
|
---|
42 | #include <iprt/cpuset.h>
|
---|
43 | #include <iprt/errcore.h>
|
---|
44 | #include <iprt/string.h>
|
---|
45 | #include <iprt/test.h>
|
---|
46 | #ifdef VBOX
|
---|
47 | # include <VBox/sup.h>
|
---|
48 | #endif
|
---|
49 |
|
---|
50 |
|
---|
51 |
|
---|
52 | int main(int argc, char **argv)
|
---|
53 | {
|
---|
54 | RTTEST hTest;
|
---|
55 | RTEXITCODE rcExit = RTTestInitAndCreate("tstRTMp-1", &hTest);
|
---|
56 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
57 | return rcExit;
|
---|
58 | RTTestBanner(hTest);
|
---|
59 |
|
---|
60 | NOREF(argc); NOREF(argv);
|
---|
61 | #ifdef VBOX
|
---|
62 | if (argc > 1)
|
---|
63 | SUPR3Init(NULL);
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * Present and possible CPUs.
|
---|
68 | */
|
---|
69 | RTCPUID cCpus = RTMpGetCount();
|
---|
70 | if (cCpus > 0)
|
---|
71 | RTTestIPrintf(RTTESTLVL_ALWAYS, "RTMpGetCount -> %u\n", cCpus);
|
---|
72 | else
|
---|
73 | {
|
---|
74 | RTTestIFailed("RTMpGetCount returned zero");
|
---|
75 | cCpus = 1;
|
---|
76 | }
|
---|
77 |
|
---|
78 | RTCPUID cCoreCpus = RTMpGetCoreCount();
|
---|
79 | if (cCoreCpus > 0)
|
---|
80 | RTTestIPrintf(RTTESTLVL_ALWAYS, "RTMpGetCoreCount -> %d\n", (int)cCoreCpus);
|
---|
81 | else
|
---|
82 | {
|
---|
83 | RTTestIFailed("RTMpGetCoreCount returned zero");
|
---|
84 | cCoreCpus = 1;
|
---|
85 | }
|
---|
86 | RTTESTI_CHECK(cCoreCpus <= cCpus);
|
---|
87 |
|
---|
88 | RTCPUSET Set;
|
---|
89 | PRTCPUSET pSet = RTMpGetSet(&Set);
|
---|
90 | RTTESTI_CHECK(pSet == &Set);
|
---|
91 | if (pSet == &Set)
|
---|
92 | {
|
---|
93 | RTTESTI_CHECK((RTCPUID)RTCpuSetCount(&Set) == cCpus);
|
---|
94 |
|
---|
95 | RTTestIPrintf(RTTESTLVL_ALWAYS, "Possible CPU mask:\n");
|
---|
96 | for (int iCpu = 0; iCpu < RTCPUSET_MAX_CPUS; iCpu++)
|
---|
97 | {
|
---|
98 | RTCPUID idCpu = RTMpCpuIdFromSetIndex(iCpu);
|
---|
99 | if (RTCpuSetIsMemberByIndex(&Set, iCpu))
|
---|
100 | {
|
---|
101 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%2d - id %d: %u/%u MHz", iCpu, (int)idCpu,
|
---|
102 | RTMpGetCurFrequency(idCpu), RTMpGetMaxFrequency(idCpu));
|
---|
103 | if (RTMpIsCpuPresent(idCpu))
|
---|
104 | RTTestIPrintf(RTTESTLVL_ALWAYS, RTMpIsCpuOnline(idCpu) ? " online\n" : " offline\n");
|
---|
105 | else
|
---|
106 | {
|
---|
107 | if (!RTMpIsCpuOnline(idCpu))
|
---|
108 | RTTestIPrintf(RTTESTLVL_ALWAYS, " absent\n");
|
---|
109 | else
|
---|
110 | {
|
---|
111 | RTTestIPrintf(RTTESTLVL_ALWAYS, " online but absent!\n");
|
---|
112 | RTTestIFailed("Cpu with index %d is report as !RTIsCpuPresent while RTIsCpuOnline returns true!\n", iCpu);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | if (!RTMpIsCpuPossible(idCpu))
|
---|
116 | RTTestIFailed("Cpu with index %d is returned by RTCpuSet but not RTMpIsCpuPossible!\n", iCpu);
|
---|
117 | }
|
---|
118 | else if (RTMpIsCpuPossible(idCpu))
|
---|
119 | RTTestIFailed("Cpu with index %d is returned by RTMpIsCpuPossible but not RTCpuSet!\n", iCpu);
|
---|
120 | else if (RTMpGetCurFrequency(idCpu) != 0)
|
---|
121 | RTTestIFailed("RTMpGetCurFrequency(%d[idx=%d]) didn't return 0 as it should\n", (int)idCpu, iCpu);
|
---|
122 | else if (RTMpGetMaxFrequency(idCpu) != 0)
|
---|
123 | RTTestIFailed("RTMpGetMaxFrequency(%d[idx=%d]) didn't return 0 as it should\n", (int)idCpu, iCpu);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | else
|
---|
127 | {
|
---|
128 | RTCpuSetEmpty(&Set);
|
---|
129 | RTCpuSetAdd(&Set, RTMpCpuIdFromSetIndex(0));
|
---|
130 | }
|
---|
131 |
|
---|
132 | /*
|
---|
133 | * Online CPUs.
|
---|
134 | */
|
---|
135 | RTCPUID cCpusOnline = RTMpGetOnlineCount();
|
---|
136 | if (cCpusOnline > 0)
|
---|
137 | {
|
---|
138 | if (cCpusOnline <= cCpus)
|
---|
139 | RTTestIPrintf(RTTESTLVL_ALWAYS, "RTMpGetOnlineCount -> %d\n", (int)cCpusOnline);
|
---|
140 | else
|
---|
141 | {
|
---|
142 | RTTestIFailed("RTMpGetOnlineCount -> %d, expected <= %d\n", (int)cCpusOnline, (int)cCpus);
|
---|
143 | cCpusOnline = cCpus;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | else
|
---|
147 | {
|
---|
148 | RTTestIFailed("RTMpGetOnlineCount -> %d\n", (int)cCpusOnline);
|
---|
149 | cCpusOnline = 1;
|
---|
150 | }
|
---|
151 |
|
---|
152 | RTCPUID cCoresOnline = RTMpGetOnlineCoreCount();
|
---|
153 | if (cCoresOnline > 0)
|
---|
154 | RTTestIPrintf(RTTESTLVL_ALWAYS, "RTMpGetOnlineCoreCount -> %d\n", (int)cCoresOnline);
|
---|
155 | else
|
---|
156 | {
|
---|
157 | RTTestIFailed("RTMpGetOnlineCoreCount -> %d, expected <= %d\n", (int)cCoresOnline, (int)cCpusOnline);
|
---|
158 | cCoresOnline = 1;
|
---|
159 | }
|
---|
160 | RTTESTI_CHECK(cCoresOnline <= cCpusOnline);
|
---|
161 |
|
---|
162 | RTCPUSET SetOnline;
|
---|
163 | pSet = RTMpGetOnlineSet(&SetOnline);
|
---|
164 | if (pSet == &SetOnline)
|
---|
165 | {
|
---|
166 | if (RTCpuSetCount(&SetOnline) <= 0)
|
---|
167 | RTTestIFailed("RTMpGetOnlineSet returned an empty set!\n");
|
---|
168 | else if ((RTCPUID)RTCpuSetCount(&SetOnline) > cCpus)
|
---|
169 | RTTestIFailed("RTMpGetOnlineSet returned a too high value; %d, expected <= %d\n",
|
---|
170 | RTCpuSetCount(&SetOnline), cCpus);
|
---|
171 | RTTestIPrintf(RTTESTLVL_ALWAYS, "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 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%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 | RTTestIFailed("online cpu with index %2d is not a member of the possible cpu set!\n", iCpu);
|
---|
180 | }
|
---|
181 |
|
---|
182 | /* There isn't any sane way of testing RTMpIsCpuOnline really... :-/ */
|
---|
183 | }
|
---|
184 | else
|
---|
185 | RTTestIFailed("RTMpGetOnlineSet -> %p, expected %p\n", pSet, &Set);
|
---|
186 |
|
---|
187 | /*
|
---|
188 | * Present CPUs.
|
---|
189 | */
|
---|
190 | RTCPUID cCpusPresent = RTMpGetPresentCount();
|
---|
191 | if (cCpusPresent > 0)
|
---|
192 | {
|
---|
193 | if ( cCpusPresent <= cCpus
|
---|
194 | && cCpusPresent >= cCpusOnline)
|
---|
195 | RTTestIPrintf(RTTESTLVL_ALWAYS, "RTMpGetPresentCount -> %d\n", (int)cCpusPresent);
|
---|
196 | else
|
---|
197 | RTTestIFailed("RTMpGetPresentCount -> %d, expected <= %d and >= %d\n",
|
---|
198 | (int)cCpusPresent, (int)cCpus, (int)cCpusOnline);
|
---|
199 | }
|
---|
200 | else
|
---|
201 | {
|
---|
202 | RTTestIFailed("RTMpGetPresentCount -> %d\n", (int)cCpusPresent);
|
---|
203 | cCpusPresent = 1;
|
---|
204 | }
|
---|
205 |
|
---|
206 | RTCPUSET SetPresent;
|
---|
207 | pSet = RTMpGetPresentSet(&SetPresent);
|
---|
208 | if (pSet == &SetPresent)
|
---|
209 | {
|
---|
210 | if (RTCpuSetCount(&SetPresent) <= 0)
|
---|
211 | RTTestIFailed("RTMpGetPresentSet returned an empty set!\n");
|
---|
212 | else if ((RTCPUID)RTCpuSetCount(&SetPresent) != cCpusPresent)
|
---|
213 | RTTestIFailed("RTMpGetPresentSet returned a bad value; %d, expected = %d\n",
|
---|
214 | RTCpuSetCount(&SetPresent), cCpusPresent);
|
---|
215 | RTTestIPrintf(RTTESTLVL_ALWAYS, "Present CPU mask:\n");
|
---|
216 | for (int iCpu = 0; iCpu < RTCPUSET_MAX_CPUS; iCpu++)
|
---|
217 | if (RTCpuSetIsMemberByIndex(&SetPresent, iCpu))
|
---|
218 | {
|
---|
219 | RTCPUID idCpu = RTMpCpuIdFromSetIndex(iCpu);
|
---|
220 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%2d - id %d: %u/%u MHz %s\n", iCpu, (int)idCpu, RTMpGetCurFrequency(idCpu),
|
---|
221 | RTMpGetMaxFrequency(idCpu), RTMpIsCpuPresent(idCpu) ? "present" : "absent");
|
---|
222 | if (!RTCpuSetIsMemberByIndex(&Set, iCpu))
|
---|
223 | RTTestIFailed("online cpu with index %2d is not a member of the possible cpu set!\n", iCpu);
|
---|
224 | }
|
---|
225 |
|
---|
226 | /* There isn't any sane way of testing RTMpIsCpuPresent really... :-/ */
|
---|
227 | }
|
---|
228 | else
|
---|
229 | RTTestIFailed("RTMpGetPresentSet -> %p, expected %p\n", pSet, &Set);
|
---|
230 |
|
---|
231 | /*
|
---|
232 | * Quick test of RTMpGetDescription on the first and last online CPUs.
|
---|
233 | */
|
---|
234 | RTCPUID idCpuOnline;
|
---|
235 | for (idCpuOnline = 0; idCpuOnline < RTCPUSET_MAX_CPUS; idCpuOnline++)
|
---|
236 | if (RTMpIsCpuOnline(idCpuOnline))
|
---|
237 | break;
|
---|
238 | RTCPUID aidCpuIds[2] = { idCpuOnline, idCpuOnline };
|
---|
239 | for (idCpuOnline++; idCpuOnline < RTCPUSET_MAX_CPUS; idCpuOnline++)
|
---|
240 | if (RTMpIsCpuOnline(idCpuOnline))
|
---|
241 | aidCpuIds[1] = idCpuOnline;
|
---|
242 | for (size_t i = 0; i < RT_ELEMENTS(aidCpuIds); i++)
|
---|
243 | {
|
---|
244 | idCpuOnline = aidCpuIds[i];
|
---|
245 | char szBuf[64];
|
---|
246 | int rc = RTMpGetDescription(idCpuOnline, &szBuf[0], sizeof(szBuf));
|
---|
247 | if (RT_SUCCESS(rc))
|
---|
248 | {
|
---|
249 | RTTestIPrintf(RTTESTLVL_ALWAYS, "RTMpGetDescription(%d,,) -> '%s'\n", idCpuOnline, szBuf);
|
---|
250 |
|
---|
251 | size_t cch = strlen(szBuf);
|
---|
252 | rc = RTMpGetDescription(idCpuOnline, &szBuf[0], cch);
|
---|
253 | if (rc != VERR_BUFFER_OVERFLOW)
|
---|
254 | RTTestIFailed("RTMpGetDescription(%d,,) -> %Rrc, expected VERR_BUFFER_OVERFLOW\n", idCpuOnline, rc);
|
---|
255 |
|
---|
256 | rc = RTMpGetDescription(idCpuOnline, &szBuf[0], cch + 1);
|
---|
257 | if (RT_FAILURE(rc))
|
---|
258 | RTTestIFailed("RTMpGetDescription(%d,,) -> %Rrc, expected VINF_SUCCESS\n", idCpuOnline, rc);
|
---|
259 | }
|
---|
260 | else
|
---|
261 | RTTestIFailed("RTMpGetDescription(%d,,) -> %Rrc\n", idCpuOnline, rc);
|
---|
262 | }
|
---|
263 |
|
---|
264 | /* */
|
---|
265 |
|
---|
266 | return RTTestSummaryAndDestroy(hTest);
|
---|
267 | }
|
---|
268 |
|
---|