VirtualBox

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

Last change on this file since 19167 was 13836, checked in by vboxsync, 16 years ago

s/ELEMENTS/RT_ELEMENTS/g - retiring ELEMENTS (finally).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1/* $Id: mp-darwin.cpp 13836 2008-11-05 02:42:54Z vboxsync $ */
2/** @file
3 * IPRT - Multiprocessor, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#define LOG_GROUP RTLOGGROUP_SYSTEM
36#include <iprt/types.h>
37
38#include <unistd.h>
39#include <stdio.h>
40#include <sys/sysctl.h>
41#include <sys/stat.h>
42#include <sys/fcntl.h>
43#include <errno.h>
44#include <mach/mach.h>
45
46#include <iprt/mp.h>
47#include <iprt/cpuset.h>
48#include <iprt/assert.h>
49#include <iprt/string.h>
50
51
52/**
53 * Internal worker that determins the max possible CPU count.
54 *
55 * @returns Max cpus.
56 */
57static RTCPUID rtMpDarwinMaxCpus(void)
58{
59 int aiMib[2];
60 aiMib[0] = CTL_HW;
61 aiMib[1] = HW_NCPU;
62 int cCpus = -1;
63 size_t cb = sizeof(cCpus);
64 int rc = sysctl(aiMib, RT_ELEMENTS(aiMib), &cCpus, &cb, NULL, 0);
65 if (rc != -1 && cCpus >= 1)
66 return cCpus;
67 AssertFailed();
68 return 1;
69}
70
71
72/** @todo RTmpCpuId(). */
73
74RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
75{
76 return idCpu < rtMpDarwinMaxCpus() ? idCpu : -1;
77}
78
79
80RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
81{
82 return (unsigned)iCpu < rtMpDarwinMaxCpus() ? iCpu : NIL_RTCPUID;
83}
84
85
86RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
87{
88 return rtMpDarwinMaxCpus() - 1;
89}
90
91
92RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
93{
94#if 0
95 return RTMpIsCpuPossible(idCpu);
96#else
97 /** @todo proper ring-3 support on darwin, see #3014. */
98 natural_t nCpus;
99 processor_basic_info_t pinfo;
100 mach_msg_type_number_t count;
101 kern_return_t krc = host_processor_info(mach_host_self(),
102 PROCESSOR_BASIC_INFO, &nCpus, (processor_info_array_t*)&pinfo, &count);
103 AssertReturn (krc == KERN_SUCCESS, true);
104 bool isOnline = idCpu < nCpus ? pinfo[idCpu].running : true;
105 vm_deallocate(mach_task_self(), (vm_address_t)pinfo, count * sizeof(*pinfo));
106 return isOnline;
107#endif
108}
109
110
111RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
112{
113 return idCpu != NIL_RTCPUID
114 && idCpu < rtMpDarwinMaxCpus();
115}
116
117
118RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
119{
120#if 0
121 RTCPUID cCpus = rtMpDarwinMaxCpus();
122 return RTCpuSetFromU64(RT_BIT_64(cCpus) - 1);
123
124#else
125 RTCpuSetEmpty(pSet);
126 RTCPUID cMax = rtMpDarwinMaxCpus();
127 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
128 if (RTMpIsCpuPossible(idCpu))
129 RTCpuSetAdd(pSet, idCpu);
130 return pSet;
131#endif
132}
133
134
135RTDECL(RTCPUID) RTMpGetCount(void)
136{
137 return rtMpDarwinMaxCpus();
138}
139
140
141RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
142{
143#if 0
144 return RTMpGetSet(pSet);
145#else
146 RTCpuSetEmpty(pSet);
147 RTCPUID cMax = rtMpDarwinMaxCpus();
148 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
149 if (RTMpIsCpuOnline(idCpu))
150 RTCpuSetAdd(pSet, idCpu);
151 return pSet;
152#endif
153}
154
155
156RTDECL(RTCPUID) RTMpGetOnlineCount(void)
157{
158 RTCPUSET Set;
159 RTMpGetOnlineSet(&Set);
160 return RTCpuSetCount(&Set);
161}
162
163
164RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
165{
166 /** @todo figure out how to get the current cpu speed on darwin. Have to check what powermanagement does. */
167 return 0;
168}
169
170
171RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
172{
173 if (!RTMpIsCpuOnline(idCpu))
174 return 0;
175
176 /*
177 * Try the 'hw.cpufrequency_max' one.
178 */
179 uint64_t CpuFrequencyMax = 0;
180 size_t cb = sizeof(CpuFrequencyMax);
181 int rc = sysctlbyname("hw.cpufrequency_max", &CpuFrequencyMax, &cb, NULL, 0);
182 if (!rc)
183 return (CpuFrequencyMax + 999999) / 1000000;
184
185 /*
186 * Use the depricated one.
187 */
188 int aiMib[2];
189 aiMib[0] = CTL_HW;
190 aiMib[1] = HW_CPU_FREQ;
191 int cCpus = -1;
192 cb = sizeof(cCpus);
193 rc = sysctl(aiMib, RT_ELEMENTS(aiMib), &cCpus, &cb, NULL, 0);
194 if (rc != -1 && cCpus >= 1)
195 return cCpus;
196 AssertFailed();
197 return 0;
198}
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