VirtualBox

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

Last change on this file since 46144 was 46144, checked in by vboxsync, 11 years ago

Runtime: RTMpGetCoreCount() for Linux

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1/* $Id: mp-linux.cpp 46144 2013-05-17 14:46:57Z 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 <iprt/cpuset.h>
37#include <iprt/assert.h>
38#include <iprt/string.h>
39#include <iprt/linux/sysfs.h>
40
41
42/**
43 * Internal worker that determines the max possible CPU count.
44 *
45 * @returns Max cpus.
46 */
47static RTCPUID rtMpLinuxMaxCpus(void)
48{
49#if 0 /* this doesn't do the right thing :-/ */
50 int cMax = sysconf(_SC_NPROCESSORS_CONF);
51 Assert(cMax >= 1);
52 return cMax;
53#else
54 static uint32_t s_cMax = 0;
55 if (!s_cMax)
56 {
57 int cMax = 1;
58 for (unsigned iCpu = 0; iCpu < RTCPUSET_MAX_CPUS; iCpu++)
59 if (RTLinuxSysFsExists("devices/system/cpu/cpu%d", iCpu))
60 cMax = iCpu + 1;
61 ASMAtomicUoWriteU32((uint32_t volatile *)&s_cMax, cMax);
62 return cMax;
63 }
64 return s_cMax;
65#endif
66}
67
68/**
69 * Internal worker that picks the processor speed in MHz from /proc/cpuinfo.
70 *
71 * @returns CPU frequency.
72 */
73static uint32_t rtMpLinuxGetFrequency(RTCPUID idCpu)
74{
75 FILE *pFile = fopen("/proc/cpuinfo", "r");
76 if (!pFile)
77 return 0;
78
79 char sz[256];
80 RTCPUID idCpuFound = NIL_RTCPUID;
81 uint32_t Frequency = 0;
82 while (fgets(sz, sizeof(sz), pFile))
83 {
84 char *psz;
85 if ( !strncmp(sz, "processor", 9)
86 && (sz[10] == ' ' || sz[10] == '\t' || sz[10] == ':')
87 && (psz = strchr(sz, ':')))
88 {
89 psz += 2;
90 int64_t iCpu;
91 int rc = RTStrToInt64Ex(psz, NULL, 0, &iCpu);
92 if (RT_SUCCESS(rc))
93 idCpuFound = iCpu;
94 }
95 else if ( idCpu == idCpuFound
96 && !strncmp(sz, "cpu MHz", 7)
97 && (sz[10] == ' ' || sz[10] == '\t' || sz[10] == ':')
98 && (psz = strchr(sz, ':')))
99 {
100 psz += 2;
101 int64_t v;
102 int rc = RTStrToInt64Ex(psz, &psz, 0, &v);
103 if (RT_SUCCESS(rc))
104 {
105 Frequency = v;
106 break;
107 }
108 }
109 }
110 fclose(pFile);
111 return Frequency;
112}
113
114
115/** @todo RTmpCpuId(). */
116
117RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
118{
119 return idCpu < rtMpLinuxMaxCpus() ? (int)idCpu : -1;
120}
121
122
123RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
124{
125 return (unsigned)iCpu < rtMpLinuxMaxCpus() ? iCpu : NIL_RTCPUID;
126}
127
128
129RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
130{
131 return rtMpLinuxMaxCpus() - 1;
132}
133
134
135RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
136{
137 /** @todo check if there is a simpler interface than this... */
138 int i = RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/online", (int)idCpu);
139 if ( i == -1
140 && RTLinuxSysFsExists("devices/system/cpu/cpu%d", (int)idCpu))
141 {
142 /** @todo Assert(!RTLinuxSysFsExists("devices/system/cpu/cpu%d/online",
143 * (int)idCpu));
144 * Unfortunately, the online file wasn't always world readable (centos
145 * 2.6.18-164). */
146 i = 1;
147 }
148
149 AssertMsg(i == 0 || i == -1 || i == 1, ("i=%d\n", i));
150 return i != 0 && i != -1;
151}
152
153
154RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
155{
156 /** @todo check this up with hotplugging! */
157 return RTLinuxSysFsExists("devices/system/cpu/cpu%d", (int)idCpu);
158}
159
160
161RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
162{
163 RTCpuSetEmpty(pSet);
164 RTCPUID cMax = rtMpLinuxMaxCpus();
165 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
166 if (RTMpIsCpuPossible(idCpu))
167 RTCpuSetAdd(pSet, idCpu);
168 return pSet;
169}
170
171
172RTDECL(RTCPUID) RTMpGetCount(void)
173{
174 RTCPUSET Set;
175 RTMpGetSet(&Set);
176 return RTCpuSetCount(&Set);
177}
178
179
180RTDECL(RTCPUID) RTMpGetCoreCount(void)
181{
182 RTCPUID cMax = rtMpLinuxMaxCpus();
183 uint32_t aCores[256];
184 RT_ZERO(aCores);
185 uint32_t cCores = 0;
186 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
187 {
188 if (RTMpIsCpuPossible(idCpu))
189 {
190 uint32_t idCore =
191 (uint32_t)RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/topology/core_id", (int)idCpu);
192 unsigned i;
193 for (i = 0; i < cCores; i++)
194 if (aCores[i] == idCore)
195 break;
196 if ( i >= cCores
197 && cCores < RT_ELEMENTS(aCores))
198 {
199 aCores[cCores] = idCore;
200 cCores++;
201 }
202 }
203 }
204 return cCores;
205}
206
207
208RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
209{
210 RTCpuSetEmpty(pSet);
211 RTCPUID cMax = rtMpLinuxMaxCpus();
212 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
213 if (RTMpIsCpuOnline(idCpu))
214 RTCpuSetAdd(pSet, idCpu);
215 return pSet;
216}
217
218
219RTDECL(RTCPUID) RTMpGetOnlineCount(void)
220{
221 RTCPUSET Set;
222 RTMpGetOnlineSet(&Set);
223 return RTCpuSetCount(&Set);
224}
225
226
227RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
228{
229 int64_t kHz = RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/cpufreq/cpuinfo_cur_freq", (int)idCpu);
230 if (kHz == -1)
231 {
232 /*
233 * The file may be just unreadable - in that case use plan B, i.e.
234 * /proc/cpuinfo to get the data we want. The assumption is that if
235 * cpuinfo_cur_freq doesn't exist then the speed won't change, and
236 * thus cur == max. If it does exist then cpuinfo contains the
237 * current frequency.
238 */
239 kHz = rtMpLinuxGetFrequency(idCpu) * 1000;
240 }
241 return (kHz + 999) / 1000;
242}
243
244
245RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
246{
247 int64_t kHz = RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", (int)idCpu);
248 if (kHz == -1)
249 {
250 /*
251 * Check if the file isn't there - if it is there, then /proc/cpuinfo
252 * would provide current frequency information, which is wrong.
253 */
254 if (!RTLinuxSysFsExists("devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", (int)idCpu))
255 kHz = rtMpLinuxGetFrequency(idCpu) * 1000;
256 else
257 kHz = 0;
258 }
259 return (kHz + 999) / 1000;
260}
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