VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/darwin/mp-darwin.cpp@ 86327

Last change on this file since 86327 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 6.3 KB
Line 
1/* $Id: mp-darwin.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IPRT - Multiprocessor, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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 <iprt/types.h>
33
34#include <unistd.h>
35#include <stdio.h>
36#include <sys/sysctl.h>
37#include <sys/stat.h>
38#include <sys/fcntl.h>
39#include <errno.h>
40#include <mach/mach.h>
41
42#include <iprt/mp.h>
43#include <iprt/cpuset.h>
44#include <iprt/assert.h>
45#include <iprt/string.h>
46
47
48/**
49 * Internal worker that determines the max possible logical CPU count (hyperthreads).
50 *
51 * @returns Max cpus.
52 */
53static RTCPUID rtMpDarwinMaxLogicalCpus(void)
54{
55 int cCpus = -1;
56 size_t cb = sizeof(cCpus);
57 int rc = sysctlbyname("hw.logicalcpu_max", &cCpus, &cb, NULL, 0);
58 if (rc != -1 && cCpus >= 1)
59 return cCpus;
60 AssertFailed();
61 return 1;
62}
63
64/**
65 * Internal worker that determines the max possible physical core count.
66 *
67 * @returns Max cpus.
68 */
69static RTCPUID rtMpDarwinMaxPhysicalCpus(void)
70{
71 int cCpus = -1;
72 size_t cb = sizeof(cCpus);
73 int rc = sysctlbyname("hw.physicalcpu_max", &cCpus, &cb, NULL, 0);
74 if (rc != -1 && cCpus >= 1)
75 return cCpus;
76 AssertFailed();
77 return 1;
78}
79
80
81#if 0 /* unused */
82/**
83 * Internal worker that determines the current number of logical CPUs (hyperthreads).
84 *
85 * @returns Max cpus.
86 */
87static RTCPUID rtMpDarwinOnlineLogicalCpus(void)
88{
89 int cCpus = -1;
90 size_t cb = sizeof(cCpus);
91 int rc = sysctlbyname("hw.logicalcpu", &cCpus, &cb, NULL, 0);
92 if (rc != -1 && cCpus >= 1)
93 return cCpus;
94 AssertFailed();
95 return 1;
96}
97#endif /* unused */
98
99
100/**
101 * Internal worker that determines the current number of physical CPUs.
102 *
103 * @returns Max cpus.
104 */
105static RTCPUID rtMpDarwinOnlinePhysicalCpus(void)
106{
107 int cCpus = -1;
108 size_t cb = sizeof(cCpus);
109 int rc = sysctlbyname("hw.physicalcpu", &cCpus, &cb, NULL, 0);
110 if (rc != -1 && cCpus >= 1)
111 return cCpus;
112 AssertFailed();
113 return 1;
114}
115
116
117/** @todo RTmpCpuId(). */
118
119RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
120{
121 return idCpu < RTCPUSET_MAX_CPUS && idCpu < rtMpDarwinMaxLogicalCpus() ? idCpu : -1;
122}
123
124
125RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
126{
127 return (unsigned)iCpu < rtMpDarwinMaxLogicalCpus() ? iCpu : NIL_RTCPUID;
128}
129
130
131RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
132{
133 return rtMpDarwinMaxLogicalCpus() - 1;
134}
135
136
137RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
138{
139#if 0
140 return RTMpIsCpuPossible(idCpu);
141#else
142 /** @todo proper ring-3 support on darwin, see @bugref{3014}. */
143 natural_t nCpus;
144 processor_basic_info_t pinfo;
145 mach_msg_type_number_t count;
146 kern_return_t krc = host_processor_info(mach_host_self(),
147 PROCESSOR_BASIC_INFO, &nCpus, (processor_info_array_t*)&pinfo, &count);
148 AssertReturn (krc == KERN_SUCCESS, true);
149 bool isOnline = idCpu < nCpus ? pinfo[idCpu].running : false;
150 vm_deallocate(mach_task_self(), (vm_address_t)pinfo, count * sizeof(*pinfo));
151 return isOnline;
152#endif
153}
154
155
156RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
157{
158 return idCpu != NIL_RTCPUID
159 && idCpu < rtMpDarwinMaxLogicalCpus();
160}
161
162
163RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
164{
165#if 0
166 RTCPUID cCpus = rtMpDarwinMaxLogicalCpus();
167 return RTCpuSetFromU64(RT_BIT_64(cCpus) - 1);
168
169#else
170 RTCpuSetEmpty(pSet);
171 RTCPUID cMax = rtMpDarwinMaxLogicalCpus();
172 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
173 if (RTMpIsCpuPossible(idCpu))
174 RTCpuSetAdd(pSet, idCpu);
175 return pSet;
176#endif
177}
178
179
180RTDECL(RTCPUID) RTMpGetCount(void)
181{
182 return rtMpDarwinMaxLogicalCpus();
183}
184
185
186RTDECL(RTCPUID) RTMpGetCoreCount(void)
187{
188 return rtMpDarwinMaxPhysicalCpus();
189}
190
191
192RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
193{
194#if 0
195 return RTMpGetSet(pSet);
196#else
197 RTCpuSetEmpty(pSet);
198 RTCPUID cMax = rtMpDarwinMaxLogicalCpus();
199 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
200 if (RTMpIsCpuOnline(idCpu))
201 RTCpuSetAdd(pSet, idCpu);
202 return pSet;
203#endif
204}
205
206
207RTDECL(RTCPUID) RTMpGetOnlineCount(void)
208{
209 RTCPUSET Set;
210 RTMpGetOnlineSet(&Set);
211 return RTCpuSetCount(&Set);
212}
213
214
215RTDECL(RTCPUID) RTMpGetOnlineCoreCount(void)
216{
217 return rtMpDarwinOnlinePhysicalCpus();
218}
219
220
221RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
222{
223 /** @todo figure out how to get the current cpu speed on darwin. Have to check what powermanagement does. */
224 NOREF(idCpu);
225 return 0;
226}
227
228
229RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
230{
231 if (!RTMpIsCpuOnline(idCpu))
232 return 0;
233
234 /*
235 * Try the 'hw.cpufrequency_max' one.
236 */
237 uint64_t CpuFrequencyMax = 0;
238 size_t cb = sizeof(CpuFrequencyMax);
239 int rc = sysctlbyname("hw.cpufrequency_max", &CpuFrequencyMax, &cb, NULL, 0);
240 if (!rc)
241 return (CpuFrequencyMax + 999999) / 1000000;
242
243 /*
244 * Use the deprecated one.
245 */
246 int aiMib[2];
247 aiMib[0] = CTL_HW;
248 aiMib[1] = HW_CPU_FREQ;
249 int cCpus = -1;
250 cb = sizeof(cCpus);
251 rc = sysctl(aiMib, RT_ELEMENTS(aiMib), &cCpus, &cb, NULL, 0);
252 if (rc != -1 && cCpus >= 1)
253 return cCpus;
254 AssertFailed();
255 return 0;
256}
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