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