VirtualBox

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

Last change on this file since 11527 was 10616, checked in by vboxsync, 17 years ago

comment

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