VirtualBox

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

Last change on this file since 48647 was 46640, checked in by vboxsync, 12 years ago

typo.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.8 KB
Line 
1/* $Id: mp-linux.cpp 46640 2013-06-18 17:37:03Z 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 */
50static 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 */
76static 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
120RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
121{
122 return idCpu < rtMpLinuxMaxCpus() ? (int)idCpu : -1;
123}
124
125
126RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
127{
128 return (unsigned)iCpu < rtMpLinuxMaxCpus() ? iCpu : NIL_RTCPUID;
129}
130
131
132RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
133{
134 return rtMpLinuxMaxCpus() - 1;
135}
136
137
138RTDECL(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
157RTDECL(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
164RTDECL(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
175RTDECL(RTCPUID) RTMpGetCount(void)
176{
177 RTCPUSET Set;
178 RTMpGetSet(&Set);
179 return RTCpuSetCount(&Set);
180}
181
182
183RTDECL(RTCPUID) RTMpGetCoreCount(void)
184{
185 RTCPUID cMax = rtMpLinuxMaxCpus();
186 uint32_t *paidCores = (uint32_t *)alloca(sizeof(paidCores[0]) * (cMax + 1));
187 uint32_t cCores = 0;
188 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
189 {
190 if (RTMpIsCpuPossible(idCpu))
191 {
192 uint32_t idCore = (uint32_t)RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/topology/core_id", (int)idCpu);
193 uint32_t i;
194 for (i = 0; i < cCores; i++)
195 if (paidCores[i] == idCore)
196 break;
197 if (i >= cCores)
198 paidCores[cCores++] = idCore;
199 }
200 }
201 Assert(cCores > 0);
202 return cCores;
203}
204
205
206RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
207{
208 RTCpuSetEmpty(pSet);
209 RTCPUID cMax = rtMpLinuxMaxCpus();
210 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
211 if (RTMpIsCpuOnline(idCpu))
212 RTCpuSetAdd(pSet, idCpu);
213 return pSet;
214}
215
216
217RTDECL(RTCPUID) RTMpGetOnlineCount(void)
218{
219 RTCPUSET Set;
220 RTMpGetOnlineSet(&Set);
221 return RTCpuSetCount(&Set);
222}
223
224
225RTDECL(RTCPUID) RTMpGetOnlineCoreCount(void)
226{
227 RTCPUID cMax = rtMpLinuxMaxCpus();
228 uint32_t *paidCores = (uint32_t *)alloca(sizeof(paidCores[0]) * (cMax + 1));
229 uint32_t cCores = 0;
230 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
231 {
232 if (RTMpIsCpuOnline(idCpu))
233 {
234 uint32_t idCore = (uint32_t)RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/topology/core_id", (int)idCpu);
235 uint32_t i;
236 for (i = 0; i < cCores; i++)
237 if (paidCores[i] == idCore)
238 break;
239 if (i >= cCores)
240 paidCores[cCores++] = idCore;
241 }
242 }
243 Assert(cCores > 0);
244 return cCores;
245}
246
247
248
249RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
250{
251 int64_t kHz = RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/cpufreq/cpuinfo_cur_freq", (int)idCpu);
252 if (kHz == -1)
253 {
254 /*
255 * The file may be just unreadable - in that case use plan B, i.e.
256 * /proc/cpuinfo to get the data we want. The assumption is that if
257 * cpuinfo_cur_freq doesn't exist then the speed won't change, and
258 * thus cur == max. If it does exist then cpuinfo contains the
259 * current frequency.
260 */
261 kHz = rtMpLinuxGetFrequency(idCpu) * 1000;
262 }
263 return (kHz + 999) / 1000;
264}
265
266
267RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
268{
269 int64_t kHz = RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", (int)idCpu);
270 if (kHz == -1)
271 {
272 /*
273 * Check if the file isn't there - if it is there, then /proc/cpuinfo
274 * would provide current frequency information, which is wrong.
275 */
276 if (!RTLinuxSysFsExists("devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", (int)idCpu))
277 kHz = rtMpLinuxGetFrequency(idCpu) * 1000;
278 else
279 kHz = 0;
280 }
281 return (kHz + 999) / 1000;
282}
Note: See TracBrowser for help on using the repository browser.

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