VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/linux/mp-linux.cpp

Last change on this file was 100194, checked in by vboxsync, 11 months ago

Runtime/mp-linux.cpp: Implement RTMpCpuId() for ARM based on sched_getcpu(), bugref:10457

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 9.7 KB
Line 
1/* $Id: mp-linux.cpp 100194 2023-06-16 08:19:12Z vboxsync $ */
2/** @file
3 * IPRT - Multiprocessor, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2023 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#define LOG_GROUP RTLOGGROUP_SYSTEM
42#include <stdio.h>
43#include <errno.h>
44
45#include <iprt/mp.h>
46#include "internal/iprt.h"
47
48#include <iprt/alloca.h>
49#include <iprt/cpuset.h>
50#include <iprt/assert.h>
51#include <iprt/string.h>
52#include <iprt/linux/sysfs.h>
53
54
55#if defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32)
56# include <sched.h>
57
58RTDECL(RTCPUID) RTMpCpuId(void)
59{
60 int rc = sched_getcpu();
61 if (rc >= 0)
62 return (RTCPUID)rc;
63
64 return NIL_RTCPUID;
65}
66#endif
67
68/**
69 * Internal worker that determines the max possible CPU count.
70 *
71 * @returns Max cpus.
72 */
73static RTCPUID rtMpLinuxMaxCpus(void)
74{
75#if 0 /* this doesn't do the right thing :-/ */
76 int cMax = sysconf(_SC_NPROCESSORS_CONF);
77 Assert(cMax >= 1);
78 return cMax;
79#else
80 static uint32_t s_cMax = 0;
81 if (!s_cMax)
82 {
83 int cMax = 1;
84 for (unsigned iCpu = 0; iCpu < RTCPUSET_MAX_CPUS; iCpu++)
85 if (RTLinuxSysFsExists("devices/system/cpu/cpu%d", iCpu))
86 cMax = iCpu + 1;
87 ASMAtomicUoWriteU32((uint32_t volatile *)&s_cMax, cMax);
88 return cMax;
89 }
90 return s_cMax;
91#endif
92}
93
94/**
95 * Internal worker that picks the processor speed in MHz from /proc/cpuinfo.
96 *
97 * @returns CPU frequency.
98 */
99static uint32_t rtMpLinuxGetFrequency(RTCPUID idCpu)
100{
101 FILE *pFile = fopen("/proc/cpuinfo", "r");
102 if (!pFile)
103 return 0;
104
105 char sz[256];
106 RTCPUID idCpuFound = NIL_RTCPUID;
107 uint32_t Frequency = 0;
108 while (fgets(sz, sizeof(sz), pFile))
109 {
110 char *psz;
111 if ( !strncmp(sz, RT_STR_TUPLE("processor"))
112 && (sz[10] == ' ' || sz[10] == '\t' || sz[10] == ':')
113 && (psz = strchr(sz, ':')))
114 {
115 psz += 2;
116 int64_t iCpu;
117 int rc = RTStrToInt64Ex(psz, NULL, 0, &iCpu);
118 if (RT_SUCCESS(rc))
119 idCpuFound = iCpu;
120 }
121 else if ( idCpu == idCpuFound
122 && !strncmp(sz, RT_STR_TUPLE("cpu MHz"))
123 && (sz[10] == ' ' || sz[10] == '\t' || sz[10] == ':')
124 && (psz = strchr(sz, ':')))
125 {
126 psz += 2;
127 int64_t v;
128 int rc = RTStrToInt64Ex(psz, &psz, 0, &v);
129 if (RT_SUCCESS(rc))
130 {
131 Frequency = v;
132 break;
133 }
134 }
135 }
136 fclose(pFile);
137 return Frequency;
138}
139
140
141/** @todo RTmpCpuId(). */
142
143RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
144{
145 return idCpu < rtMpLinuxMaxCpus() ? (int)idCpu : -1;
146}
147
148
149RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
150{
151 return (unsigned)iCpu < rtMpLinuxMaxCpus() ? iCpu : NIL_RTCPUID;
152}
153
154
155RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
156{
157 return rtMpLinuxMaxCpus() - 1;
158}
159
160
161RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
162{
163 /** @todo check if there is a simpler interface than this... */
164 int64_t i = 0;
165 int rc = RTLinuxSysFsReadIntFile(0, &i, "devices/system/cpu/cpu%d/online", (int)idCpu);
166 if ( RT_FAILURE(rc)
167 && RTLinuxSysFsExists("devices/system/cpu/cpu%d", (int)idCpu))
168 {
169 /** @todo Assert(!RTLinuxSysFsExists("devices/system/cpu/cpu%d/online",
170 * (int)idCpu));
171 * Unfortunately, the online file wasn't always world readable (centos
172 * 2.6.18-164). */
173 i = 1;
174 rc = VINF_SUCCESS;
175 }
176
177 AssertMsg(i == 0 || i == -1 || i == 1, ("i=%d\n", i));
178 return RT_SUCCESS(rc) && i != 0;
179}
180
181
182RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
183{
184 /** @todo check this up with hotplugging! */
185 return RTLinuxSysFsExists("devices/system/cpu/cpu%d", (int)idCpu);
186}
187
188
189RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
190{
191 RTCpuSetEmpty(pSet);
192 RTCPUID cMax = rtMpLinuxMaxCpus();
193 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
194 if (RTMpIsCpuPossible(idCpu))
195 RTCpuSetAdd(pSet, idCpu);
196 return pSet;
197}
198
199
200RTDECL(RTCPUID) RTMpGetCount(void)
201{
202 RTCPUSET Set;
203 RTMpGetSet(&Set);
204 return RTCpuSetCount(&Set);
205}
206
207
208RTDECL(RTCPUID) RTMpGetCoreCount(void)
209{
210 RTCPUID cMax = rtMpLinuxMaxCpus();
211 uint32_t *paidCores = (uint32_t *)alloca(sizeof(paidCores[0]) * (cMax + 1));
212 uint32_t *paidPckgs = (uint32_t *)alloca(sizeof(paidPckgs[0]) * (cMax + 1));
213 uint32_t cCores = 0;
214 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
215 {
216 if (RTMpIsCpuPossible(idCpu))
217 {
218 int64_t idCore = 0;
219 int64_t idPckg = 0;
220
221 int rc = RTLinuxSysFsReadIntFile(0, &idCore, "devices/system/cpu/cpu%d/topology/core_id", (int)idCpu);
222 if (RT_SUCCESS(rc))
223 rc = RTLinuxSysFsReadIntFile(0, &idPckg, "devices/system/cpu/cpu%d/topology/physical_package_id", (int)idCpu);
224
225 if (RT_SUCCESS(rc))
226 {
227 uint32_t i;
228
229 for (i = 0; i < cCores; i++)
230 if ( paidCores[i] == (uint32_t)idCore
231 && paidPckgs[i] == (uint32_t)idPckg)
232 break;
233 if (i >= cCores)
234 {
235 paidCores[cCores] = (uint32_t)idCore;
236 paidPckgs[cCores] = (uint32_t)idPckg;
237 cCores++;
238 }
239 }
240 }
241 }
242 Assert(cCores > 0);
243 return cCores;
244}
245
246
247RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
248{
249 RTCpuSetEmpty(pSet);
250 RTCPUID cMax = rtMpLinuxMaxCpus();
251 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
252 if (RTMpIsCpuOnline(idCpu))
253 RTCpuSetAdd(pSet, idCpu);
254 return pSet;
255}
256
257
258RTDECL(RTCPUID) RTMpGetOnlineCount(void)
259{
260 RTCPUSET Set;
261 RTMpGetOnlineSet(&Set);
262 return RTCpuSetCount(&Set);
263}
264
265
266RTDECL(RTCPUID) RTMpGetOnlineCoreCount(void)
267{
268 RTCPUID cMax = rtMpLinuxMaxCpus();
269 uint32_t *paidCores = (uint32_t *)alloca(sizeof(paidCores[0]) * (cMax + 1));
270 uint32_t *paidPckgs = (uint32_t *)alloca(sizeof(paidPckgs[0]) * (cMax + 1));
271 uint32_t cCores = 0;
272 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
273 {
274 if (RTMpIsCpuOnline(idCpu))
275 {
276 int64_t idCore = 0;
277 int64_t idPckg = 0;
278
279 int rc = RTLinuxSysFsReadIntFile(0, &idCore, "devices/system/cpu/cpu%d/topology/core_id", (int)idCpu);
280 if (RT_SUCCESS(rc))
281 rc = RTLinuxSysFsReadIntFile(0, &idPckg, "devices/system/cpu/cpu%d/topology/physical_package_id", (int)idCpu);
282
283 if (RT_SUCCESS(rc))
284 {
285 uint32_t i;
286
287 for (i = 0; i < cCores; i++)
288 if ( paidCores[i] == idCore
289 && paidPckgs[i] == idPckg)
290 break;
291 if (i >= cCores)
292 {
293 paidCores[cCores] = idCore;
294 paidPckgs[cCores] = idPckg;
295 cCores++;
296 }
297 }
298 }
299 }
300 Assert(cCores > 0);
301 return cCores;
302}
303
304
305
306RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
307{
308 int64_t kHz = 0;
309 int rc = RTLinuxSysFsReadIntFile(0, &kHz, "devices/system/cpu/cpu%d/cpufreq/cpuinfo_cur_freq", (int)idCpu);
310 if (RT_FAILURE(rc))
311 {
312 /*
313 * The file may be just unreadable - in that case use plan B, i.e.
314 * /proc/cpuinfo to get the data we want. The assumption is that if
315 * cpuinfo_cur_freq doesn't exist then the speed won't change, and
316 * thus cur == max. If it does exist then cpuinfo contains the
317 * current frequency.
318 */
319 kHz = rtMpLinuxGetFrequency(idCpu) * 1000;
320 }
321 return (kHz + 999) / 1000;
322}
323
324
325RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
326{
327 int64_t kHz = 0;
328 int rc = RTLinuxSysFsReadIntFile(0, &kHz, "devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", (int)idCpu);
329 if (RT_FAILURE(rc))
330 {
331 /*
332 * Check if the file isn't there - if it is there, then /proc/cpuinfo
333 * would provide current frequency information, which is wrong.
334 */
335 if (!RTLinuxSysFsExists("devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", (int)idCpu))
336 kHz = rtMpLinuxGetFrequency(idCpu) * 1000;
337 else
338 kHz = 0;
339 }
340 return (kHz + 999) / 1000;
341}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use