VirtualBox

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

Last change on this file since 68246 was 62477, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1/* $Id: mp-freebsd.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
2/** @file
3 * IPRT - Multiprocessor, FreeBSD.
4 */
5
6/*
7 * Copyright (C) 2008-2016 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 <unistd.h>
33#include <stdio.h>
34#include <errno.h>
35#include <sys/sysctl.h>
36
37#include <iprt/mp.h>
38#include <iprt/cpuset.h>
39#include <iprt/assert.h>
40#include <iprt/string.h>
41#include <iprt/alloc.h>
42#include <iprt/log.h>
43#include <iprt/once.h>
44#include <iprt/critsect.h>
45
46
47/**
48 * Internal worker that determines the max possible CPU count.
49 *
50 * @returns Max cpus.
51 */
52static RTCPUID rtMpFreeBsdMaxCpus(void)
53{
54 int aiMib[2];
55 aiMib[0] = CTL_HW;
56 aiMib[1] = HW_NCPU;
57 int cCpus = -1;
58 size_t cb = sizeof(cCpus);
59 int rc = sysctl(aiMib, RT_ELEMENTS(aiMib), &cCpus, &cb, NULL, 0);
60 if (rc != -1 && cCpus >= 1)
61 return cCpus;
62 AssertFailed();
63 return 1;
64}
65
66
67RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
68{
69 return idCpu < RTCPUSET_MAX_CPUS && idCpu < rtMpFreeBsdMaxCpus() ? idCpu : -1;
70}
71
72
73RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
74{
75 return (unsigned)iCpu < rtMpFreeBsdMaxCpus() ? iCpu : NIL_RTCPUID;
76}
77
78
79RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
80{
81 return rtMpFreeBsdMaxCpus() - 1;
82}
83
84
85RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
86{
87 /*
88 * FreeBSD doesn't support CPU hotplugging so every CPU which appears
89 * in the tree is also online.
90 */
91 char szName[40];
92 RTStrPrintf(szName, sizeof(szName), "dev.cpu.%d.%%driver", (int)idCpu);
93
94 char szDriver[10];
95 size_t cbDriver = sizeof(szDriver);
96 RT_ZERO(szDriver); /* this shouldn't be necessary. */
97 int rcBsd = sysctlbyname(szName, szDriver, &cbDriver, NULL, 0);
98 if (rcBsd == 0)
99 return true;
100
101 return false;
102}
103
104
105RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
106{
107 return idCpu != NIL_RTCPUID
108 && idCpu < rtMpFreeBsdMaxCpus();
109}
110
111
112RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
113{
114 RTCpuSetEmpty(pSet);
115 RTCPUID cMax = rtMpFreeBsdMaxCpus();
116 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
117 if (RTMpIsCpuPossible(idCpu))
118 RTCpuSetAdd(pSet, idCpu);
119 return pSet;
120}
121
122
123RTDECL(RTCPUID) RTMpGetCount(void)
124{
125 return rtMpFreeBsdMaxCpus();
126}
127
128
129RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
130{
131 RTCpuSetEmpty(pSet);
132 RTCPUID cMax = rtMpFreeBsdMaxCpus();
133 for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
134 if (RTMpIsCpuOnline(idCpu))
135 RTCpuSetAdd(pSet, idCpu);
136 return pSet;
137}
138
139
140RTDECL(RTCPUID) RTMpGetOnlineCount(void)
141{
142 /*
143 * FreeBSD has sysconf.
144 */
145 return sysconf(_SC_NPROCESSORS_ONLN);
146}
147
148
149RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
150{
151 int uFreqCurr = 0;
152 size_t cbParameter = sizeof(uFreqCurr);
153
154 if (!RTMpIsCpuOnline(idCpu))
155 return 0;
156
157 /* CPU's have a common frequency. */
158 int rc = sysctlbyname("dev.cpu.0.freq", &uFreqCurr, &cbParameter, NULL, 0);
159 if (rc)
160 return 0;
161
162 return (uint32_t)uFreqCurr;
163}
164
165
166RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
167{
168 char szFreqLevels[20]; /* Should be enough to get the highest level which is always the first. */
169 size_t cbFreqLevels = sizeof(szFreqLevels);
170
171 if (!RTMpIsCpuOnline(idCpu))
172 return 0;
173
174 memset(szFreqLevels, 0, sizeof(szFreqLevels));
175
176 /*
177 * CPU 0 has the freq levels entry. ENOMEM is ok as we don't need all supported
178 * levels but only the first one.
179 */
180 int rc = sysctlbyname("dev.cpu.0.freq_levels", szFreqLevels, &cbFreqLevels, NULL, 0);
181 if ( (rc && (errno != ENOMEM))
182 || (cbFreqLevels == 0))
183 return 0;
184
185 /* Clear everything starting from the '/' */
186 unsigned i = 0;
187
188 do
189 {
190 if (szFreqLevels[i] == '/')
191 {
192 memset(&szFreqLevels[i], 0, sizeof(szFreqLevels) - i);
193 break;
194 }
195 i++;
196 } while (i < sizeof(szFreqLevels));
197
198 /* Returns 0 on failure. */
199 return RTStrToUInt32(szFreqLevels);
200}
201
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