VirtualBox

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

Last change on this file since 34244 was 33540, checked in by vboxsync, 14 years ago

*: spelling fixes, thanks Timeless!

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1/* $Id: mp-linux.cpp 33540 2010-10-28 09:27:05Z vboxsync $ */
2/** @file
3 * IPRT - Multiprocessor, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2008 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(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
181{
182 RTCpuSetEmpty(pSet);
183 RTCPUID cMax = rtMpLinuxMaxCpus();
184 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
185 if (RTMpIsCpuOnline(idCpu))
186 RTCpuSetAdd(pSet, idCpu);
187 return pSet;
188}
189
190
191RTDECL(RTCPUID) RTMpGetOnlineCount(void)
192{
193 RTCPUSET Set;
194 RTMpGetOnlineSet(&Set);
195 return RTCpuSetCount(&Set);
196}
197
198
199RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
200{
201 int64_t kHz = RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/cpufreq/cpuinfo_cur_freq", (int)idCpu);
202 if (kHz == -1)
203 {
204 /*
205 * The file may be just unreadable - in that case use plan B, i.e.
206 * /proc/cpuinfo to get the data we want. The assumption is that if
207 * cpuinfo_cur_freq doesn't exist then the speed won't change, and
208 * thus cur == max. If it does exist then cpuinfo contains the
209 * current frequency.
210 */
211 kHz = rtMpLinuxGetFrequency(idCpu) * 1000;
212 }
213 return (kHz + 999) / 1000;
214}
215
216
217RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
218{
219 int64_t kHz = RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", (int)idCpu);
220 if (kHz == -1)
221 {
222 /*
223 * Check if the file isn't there - if it is there, then /proc/cpuinfo
224 * would provide current frequency information, which is wrong.
225 */
226 if (!RTLinuxSysFsExists("devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", (int)idCpu))
227 kHz = rtMpLinuxGetFrequency(idCpu) * 1000;
228 else
229 kHz = 0;
230 }
231 return (kHz + 999) / 1000;
232}
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