1 | /* $Id: mp-freebsd.cpp 33815 2010-11-05 21:29:08Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiprocessor, FreeBSD.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 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 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #define LOG_GROUP RTLOGGROUP_SYSTEM
|
---|
31 | #include <unistd.h>
|
---|
32 | #include <stdio.h>
|
---|
33 | #include <errno.h>
|
---|
34 | #include <sys/sysctl.h>
|
---|
35 |
|
---|
36 | #include <iprt/mp.h>
|
---|
37 | #include <iprt/cpuset.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include <iprt/alloc.h>
|
---|
41 | #include <iprt/log.h>
|
---|
42 | #include <iprt/once.h>
|
---|
43 | #include <iprt/critsect.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Internal worker that determines the max possible CPU count.
|
---|
48 | *
|
---|
49 | * @returns Max cpus.
|
---|
50 | */
|
---|
51 | static RTCPUID rtMpFreeBsdMaxCpus(void)
|
---|
52 | {
|
---|
53 | int aiMib[2];
|
---|
54 | aiMib[0] = CTL_HW;
|
---|
55 | aiMib[1] = HW_NCPU;
|
---|
56 | int cCpus = -1;
|
---|
57 | size_t cb = sizeof(cCpus);
|
---|
58 | int rc = sysctl(aiMib, RT_ELEMENTS(aiMib), &cCpus, &cb, NULL, 0);
|
---|
59 | if (rc != -1 && cCpus >= 1)
|
---|
60 | return cCpus;
|
---|
61 | AssertFailed();
|
---|
62 | return 1;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
|
---|
67 | {
|
---|
68 | return idCpu < rtMpFreeBsdMaxCpus() ? idCpu : -1;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
|
---|
73 | {
|
---|
74 | return (unsigned)iCpu < rtMpFreeBsdMaxCpus() ? iCpu : NIL_RTCPUID;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
|
---|
79 | {
|
---|
80 | return rtMpFreeBsdMaxCpus() - 1;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
|
---|
85 | {
|
---|
86 | int rc = VINF_SUCCESS;
|
---|
87 | char szName[32];
|
---|
88 | char szDriver[10];
|
---|
89 | size_t cbDriver = sizeof(szDriver);
|
---|
90 |
|
---|
91 | /*
|
---|
92 | * FreeBSD doesn't support CPU hotplugging
|
---|
93 | * so every CPU which appears in the tree is also online.
|
---|
94 | */
|
---|
95 | memset(szDriver, 0, sizeof(szDriver));
|
---|
96 | memset(szName, 0, sizeof(szName));
|
---|
97 |
|
---|
98 | rc = RTStrPrintf(szName, sizeof(szName), "dev.cpu.%d.%%driver", (int)idCpu);
|
---|
99 | if (RT_SUCCESS(rc))
|
---|
100 | {
|
---|
101 | int rcBsd = sysctlbyname(szName, szDriver, &cbDriver, NULL, NULL);
|
---|
102 | if (rcBsd == 0)
|
---|
103 | return true;
|
---|
104 | }
|
---|
105 |
|
---|
106 | return false;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
|
---|
111 | {
|
---|
112 | return idCpu != NIL_RTCPUID
|
---|
113 | && idCpu < rtMpFreeBsdMaxCpus();
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
|
---|
118 | {
|
---|
119 | RTCpuSetEmpty(pSet);
|
---|
120 | RTCPUID cMax = rtMpFreeBsdMaxCpus();
|
---|
121 | for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
|
---|
122 | if (RTMpIsCpuPossible(idCpu))
|
---|
123 | RTCpuSetAdd(pSet, idCpu);
|
---|
124 | return pSet;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | RTDECL(RTCPUID) RTMpGetCount(void)
|
---|
129 | {
|
---|
130 | return rtMpFreeBsdMaxCpus();
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
|
---|
135 | {
|
---|
136 | RTCpuSetEmpty(pSet);
|
---|
137 | RTCPUID cMax = rtMpFreeBsdMaxCpus();
|
---|
138 | for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
|
---|
139 | if (RTMpIsCpuOnline(idCpu))
|
---|
140 | RTCpuSetAdd(pSet, idCpu);
|
---|
141 | return pSet;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | RTDECL(RTCPUID) RTMpGetOnlineCount(void)
|
---|
146 | {
|
---|
147 | /*
|
---|
148 | * FreeBSD has sysconf.
|
---|
149 | */
|
---|
150 | return sysconf(_SC_NPROCESSORS_ONLN);
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
|
---|
155 | {
|
---|
156 | int uFreqCurr = 0;
|
---|
157 | size_t cbParameter = sizeof(uFreqCurr);
|
---|
158 |
|
---|
159 | if (!RTMpIsCpuOnline(idCpu))
|
---|
160 | return 0;
|
---|
161 |
|
---|
162 | /* CPU's have a common frequency. */
|
---|
163 | int rc = sysctlbyname("dev.cpu.0.freq", &uFreqCurr, &cbParameter, NULL, NULL);
|
---|
164 | if (rc)
|
---|
165 | return 0;
|
---|
166 |
|
---|
167 | return (uint32_t)uFreqCurr;
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
|
---|
172 | {
|
---|
173 | char szFreqLevels[20]; /* Should be enough to get the highest level which is always the first. */
|
---|
174 | size_t cbFreqLevels = sizeof(szFreqLevels);
|
---|
175 |
|
---|
176 | if (!RTMpIsCpuOnline(idCpu))
|
---|
177 | return 0;
|
---|
178 |
|
---|
179 | memset(szFreqLevels, 0, sizeof(szFreqLevels));
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * CPU 0 has the freq levels entry. ENOMEM is ok as we don't need all supported
|
---|
183 | * levels but only the first one.
|
---|
184 | */
|
---|
185 | int rc = sysctlbyname("dev.cpu.0.freq_levels", szFreqLevels, &cbFreqLevels, NULL, NULL);
|
---|
186 | if ( (rc && (errno != ENOMEM))
|
---|
187 | || (cbFreqLevels == 0))
|
---|
188 | return 0;
|
---|
189 |
|
---|
190 | /* Clear everything starting from the '/' */
|
---|
191 | unsigned i = 0;
|
---|
192 |
|
---|
193 | do
|
---|
194 | {
|
---|
195 | if (szFreqLevels[i] == '/')
|
---|
196 | {
|
---|
197 | memset(&szFreqLevels[i], 0, sizeof(szFreqLevels) - i);
|
---|
198 | break;
|
---|
199 | }
|
---|
200 | i++;
|
---|
201 | } while (i < sizeof(szFreqLevels));
|
---|
202 |
|
---|
203 | /* Returns 0 on failure. */
|
---|
204 | return RTStrToUInt32(szFreqLevels);
|
---|
205 | }
|
---|
206 |
|
---|