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