VirtualBox

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

Last change on this file since 30470 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1/* $Id: mp-darwin.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT - Multiprocessor, Darwin.
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 <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 determins the max possible CPU count.
50 *
51 * @returns Max cpus.
52 */
53static RTCPUID rtMpDarwinMaxCpus(void)
54{
55 int aiMib[2];
56 aiMib[0] = CTL_HW;
57 aiMib[1] = HW_NCPU;
58 int cCpus = -1;
59 size_t cb = sizeof(cCpus);
60 int rc = sysctl(aiMib, RT_ELEMENTS(aiMib), &cCpus, &cb, NULL, 0);
61 if (rc != -1 && cCpus >= 1)
62 return cCpus;
63 AssertFailed();
64 return 1;
65}
66
67
68/** @todo RTmpCpuId(). */
69
70RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
71{
72 return idCpu < rtMpDarwinMaxCpus() ? idCpu : -1;
73}
74
75
76RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
77{
78 return (unsigned)iCpu < rtMpDarwinMaxCpus() ? iCpu : NIL_RTCPUID;
79}
80
81
82RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
83{
84 return rtMpDarwinMaxCpus() - 1;
85}
86
87
88RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
89{
90#if 0
91 return RTMpIsCpuPossible(idCpu);
92#else
93 /** @todo proper ring-3 support on darwin, see #3014. */
94 natural_t nCpus;
95 processor_basic_info_t pinfo;
96 mach_msg_type_number_t count;
97 kern_return_t krc = host_processor_info(mach_host_self(),
98 PROCESSOR_BASIC_INFO, &nCpus, (processor_info_array_t*)&pinfo, &count);
99 AssertReturn (krc == KERN_SUCCESS, true);
100 bool isOnline = idCpu < nCpus ? pinfo[idCpu].running : true;
101 vm_deallocate(mach_task_self(), (vm_address_t)pinfo, count * sizeof(*pinfo));
102 return isOnline;
103#endif
104}
105
106
107RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
108{
109 return idCpu != NIL_RTCPUID
110 && idCpu < rtMpDarwinMaxCpus();
111}
112
113
114RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
115{
116#if 0
117 RTCPUID cCpus = rtMpDarwinMaxCpus();
118 return RTCpuSetFromU64(RT_BIT_64(cCpus) - 1);
119
120#else
121 RTCpuSetEmpty(pSet);
122 RTCPUID cMax = rtMpDarwinMaxCpus();
123 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
124 if (RTMpIsCpuPossible(idCpu))
125 RTCpuSetAdd(pSet, idCpu);
126 return pSet;
127#endif
128}
129
130
131RTDECL(RTCPUID) RTMpGetCount(void)
132{
133 return rtMpDarwinMaxCpus();
134}
135
136
137RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
138{
139#if 0
140 return RTMpGetSet(pSet);
141#else
142 RTCpuSetEmpty(pSet);
143 RTCPUID cMax = rtMpDarwinMaxCpus();
144 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
145 if (RTMpIsCpuOnline(idCpu))
146 RTCpuSetAdd(pSet, idCpu);
147 return pSet;
148#endif
149}
150
151
152RTDECL(RTCPUID) RTMpGetOnlineCount(void)
153{
154 RTCPUSET Set;
155 RTMpGetOnlineSet(&Set);
156 return RTCpuSetCount(&Set);
157}
158
159
160RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
161{
162 /** @todo figure out how to get the current cpu speed on darwin. Have to check what powermanagement does. */
163 return 0;
164}
165
166
167RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
168{
169 if (!RTMpIsCpuOnline(idCpu))
170 return 0;
171
172 /*
173 * Try the 'hw.cpufrequency_max' one.
174 */
175 uint64_t CpuFrequencyMax = 0;
176 size_t cb = sizeof(CpuFrequencyMax);
177 int rc = sysctlbyname("hw.cpufrequency_max", &CpuFrequencyMax, &cb, NULL, 0);
178 if (!rc)
179 return (CpuFrequencyMax + 999999) / 1000000;
180
181 /*
182 * Use the depricated one.
183 */
184 int aiMib[2];
185 aiMib[0] = CTL_HW;
186 aiMib[1] = HW_CPU_FREQ;
187 int cCpus = -1;
188 cb = sizeof(cCpus);
189 rc = sysctl(aiMib, RT_ELEMENTS(aiMib), &cCpus, &cb, NULL, 0);
190 if (rc != -1 && cCpus >= 1)
191 return cCpus;
192 AssertFailed();
193 return 0;
194}
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