VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/freebsd/mp-freebsd.cpp@ 98108

Last change on this file since 98108 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.4 KB
Line 
1/* $Id: mp-freebsd.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Multiprocessor, FreeBSD.
4 */
5
6/*
7 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_SYSTEM
42#include <unistd.h>
43#include <stdio.h>
44#include <errno.h>
45#include <sys/sysctl.h>
46
47#include <iprt/mp.h>
48#include <iprt/cpuset.h>
49#include <iprt/assert.h>
50#include <iprt/string.h>
51#include <iprt/alloc.h>
52#include <iprt/log.h>
53#include <iprt/once.h>
54#include <iprt/critsect.h>
55
56
57/**
58 * Internal worker that determines the max possible CPU count.
59 *
60 * @returns Max cpus.
61 */
62static RTCPUID rtMpFreeBsdMaxCpus(void)
63{
64 int aiMib[2];
65 aiMib[0] = CTL_HW;
66 aiMib[1] = HW_NCPU;
67 int cCpus = -1;
68 size_t cb = sizeof(cCpus);
69 int rc = sysctl(aiMib, RT_ELEMENTS(aiMib), &cCpus, &cb, NULL, 0);
70 if (rc != -1 && cCpus >= 1)
71 return cCpus;
72 AssertFailed();
73 return 1;
74}
75
76
77RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
78{
79 return idCpu < RTCPUSET_MAX_CPUS && idCpu < rtMpFreeBsdMaxCpus() ? idCpu : -1;
80}
81
82
83RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
84{
85 return (unsigned)iCpu < rtMpFreeBsdMaxCpus() ? iCpu : NIL_RTCPUID;
86}
87
88
89RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
90{
91 return rtMpFreeBsdMaxCpus() - 1;
92}
93
94
95RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
96{
97 /*
98 * FreeBSD doesn't support CPU hotplugging so every CPU which appears
99 * in the tree is also online.
100 */
101 char szName[40];
102 RTStrPrintf(szName, sizeof(szName), "dev.cpu.%d.%%driver", (int)idCpu);
103
104 char szDriver[10];
105 size_t cbDriver = sizeof(szDriver);
106 RT_ZERO(szDriver); /* this shouldn't be necessary. */
107 int rcBsd = sysctlbyname(szName, szDriver, &cbDriver, NULL, 0);
108 if (rcBsd == 0)
109 return true;
110
111 return false;
112}
113
114
115RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
116{
117 return idCpu != NIL_RTCPUID
118 && idCpu < rtMpFreeBsdMaxCpus();
119}
120
121
122RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
123{
124 RTCpuSetEmpty(pSet);
125 RTCPUID cMax = rtMpFreeBsdMaxCpus();
126 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
127 if (RTMpIsCpuPossible(idCpu))
128 RTCpuSetAdd(pSet, idCpu);
129 return pSet;
130}
131
132
133RTDECL(RTCPUID) RTMpGetCount(void)
134{
135 return rtMpFreeBsdMaxCpus();
136}
137
138
139RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
140{
141 RTCpuSetEmpty(pSet);
142 RTCPUID cMax = rtMpFreeBsdMaxCpus();
143 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
144 if (RTMpIsCpuOnline(idCpu))
145 RTCpuSetAdd(pSet, idCpu);
146 return pSet;
147}
148
149
150RTDECL(RTCPUID) RTMpGetOnlineCount(void)
151{
152 /*
153 * FreeBSD has sysconf.
154 */
155 return sysconf(_SC_NPROCESSORS_ONLN);
156}
157
158
159RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
160{
161 int uFreqCurr = 0;
162 size_t cbParameter = sizeof(uFreqCurr);
163
164 if (!RTMpIsCpuOnline(idCpu))
165 return 0;
166
167 /* CPU's have a common frequency. */
168 int rc = sysctlbyname("dev.cpu.0.freq", &uFreqCurr, &cbParameter, NULL, 0);
169 if (rc)
170 return 0;
171
172 return (uint32_t)uFreqCurr;
173}
174
175
176RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
177{
178 char szFreqLevels[20]; /* Should be enough to get the highest level which is always the first. */
179 size_t cbFreqLevels = sizeof(szFreqLevels);
180
181 if (!RTMpIsCpuOnline(idCpu))
182 return 0;
183
184 memset(szFreqLevels, 0, sizeof(szFreqLevels));
185
186 /*
187 * CPU 0 has the freq levels entry. ENOMEM is ok as we don't need all supported
188 * levels but only the first one.
189 */
190 int rc = sysctlbyname("dev.cpu.0.freq_levels", szFreqLevels, &cbFreqLevels, NULL, 0);
191 if ( (rc && (errno != ENOMEM))
192 || (cbFreqLevels == 0))
193 return 0;
194
195 /* Clear everything starting from the '/' */
196 unsigned i = 0;
197
198 do
199 {
200 if (szFreqLevels[i] == '/')
201 {
202 memset(&szFreqLevels[i], 0, sizeof(szFreqLevels) - i);
203 break;
204 }
205 i++;
206 } while (i < sizeof(szFreqLevels));
207
208 /* Returns 0 on failure. */
209 return RTStrToUInt32(szFreqLevels);
210}
211
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