VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/solaris/mp-solaris.cpp@ 47968

Last change on this file since 47968 was 47968, checked in by vboxsync, 11 years ago

Runtime/r3/solaris/mp-solaris: Explicit cast to char*, Solaris need to fix their API to be const correct.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.4 KB
Line 
1/* $Id: mp-solaris.cpp 47968 2013-08-21 13:29:38Z vboxsync $ */
2/** @file
3 * IPRT - Multiprocessor, Solaris.
4 */
5
6/*
7 * Copyright (C) 2008-2012 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* Header Files *
29*******************************************************************************/
30#define LOG_GROUP RTLOGGROUP_DEFAULT
31#include <unistd.h>
32#include <stdio.h>
33#include <errno.h>
34#include <kstat.h>
35#include <sys/processor.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* Global Variables *
49*******************************************************************************/
50/** Initialization serializing (rtMpSolarisOnce). */
51static RTONCE g_MpSolarisOnce = RTONCE_INITIALIZER;
52/** Critical section serializing access to kstat. */
53static RTCRITSECT g_MpSolarisCritSect;
54/** The kstat handle. */
55static kstat_ctl_t *g_pKsCtl;
56/** Array pointing to the cpu_info instances. */
57static kstat_t **g_papCpuInfo;
58/** The number of entries in g_papCpuInfo */
59static RTCPUID g_capCpuInfo;
60
61
62/**
63 * Run once function that initializes the kstats we need here.
64 *
65 * @returns IPRT status code.
66 * @param pvUser Unused.
67 */
68static DECLCALLBACK(int) rtMpSolarisOnce(void *pvUser)
69{
70 int rc = VINF_SUCCESS;
71 NOREF(pvUser);
72
73 /*
74 * Open kstat and find the cpu_info entries for each of the CPUs.
75 */
76 g_pKsCtl = kstat_open();
77 if (g_pKsCtl)
78 {
79 g_capCpuInfo = RTMpGetCount();
80 g_papCpuInfo = (kstat_t **)RTMemAllocZ(g_capCpuInfo * sizeof(kstat_t *));
81 if (g_papCpuInfo)
82 {
83 rc = RTCritSectInit(&g_MpSolarisCritSect);
84 if (RT_SUCCESS(rc))
85 {
86 RTCPUID i = 0;
87 for (kstat_t *pKsp = g_pKsCtl->kc_chain; pKsp != NULL; pKsp = pKsp->ks_next)
88 {
89 if (!strcmp(pKsp->ks_module, "cpu_info"))
90 {
91 AssertBreak(i < g_capCpuInfo);
92 g_papCpuInfo[i++] = pKsp;
93 /** @todo ks_instance == cpu_id (/usr/src/uts/common/os/cpu.c)? Check this and fix it ASAP. */
94 }
95 }
96
97 return VINF_SUCCESS;
98 }
99
100 /* bail out, we failed. */
101 RTMemFree(g_papCpuInfo);
102 }
103 else
104 rc = VERR_NO_MEMORY;
105 kstat_close(g_pKsCtl);
106 g_pKsCtl = NULL;
107 }
108 else
109 {
110 rc = RTErrConvertFromErrno(errno);
111 if (RT_SUCCESS(rc))
112 rc = VERR_INTERNAL_ERROR;
113 Log(("kstat_open() -> %d (%Rrc)\n", errno, rc));
114 }
115
116 return rc;
117}
118
119
120/**
121 * Worker for RTMpGetCurFrequency and RTMpGetMaxFrequency.
122 *
123 * @returns The desired frequency on success, 0 on failure.
124 *
125 * @param idCpu The CPU ID.
126 * @param pszStatName The cpu_info stat name.
127 */
128static uint64_t rtMpSolarisGetFrequency(RTCPUID idCpu, const char *pszStatName)
129{
130 uint64_t u64 = 0;
131 int rc = RTOnce(&g_MpSolarisOnce, rtMpSolarisOnce, NULL);
132 if (RT_SUCCESS(rc))
133 {
134 if ( idCpu < g_capCpuInfo
135 && g_papCpuInfo[idCpu])
136 {
137 rc = RTCritSectEnter(&g_MpSolarisCritSect);
138 AssertRC(rc);
139 if (RT_SUCCESS(rc))
140 {
141 if (kstat_read(g_pKsCtl, g_papCpuInfo[idCpu], 0) != -1)
142 {
143 kstat_named_t *pStat = (kstat_named_t *)kstat_data_lookup(g_papCpuInfo[idCpu], (char*)pszStatName);
144 if (pStat)
145 {
146 Assert(pStat->data_type == KSTAT_DATA_UINT64 || pStat->data_type == KSTAT_DATA_LONG);
147 switch (pStat->data_type)
148 {
149 case KSTAT_DATA_UINT64: u64 = pStat->value.ui64; break; /* current_clock_Hz */
150 case KSTAT_DATA_INT32: u64 = pStat->value.i32; break; /* clock_MHz */
151
152 /* just in case... */
153 case KSTAT_DATA_UINT32: u64 = pStat->value.ui32; break;
154 case KSTAT_DATA_INT64: u64 = pStat->value.i64; break;
155 default:
156 AssertMsgFailed(("%d\n", pStat->data_type));
157 break;
158 }
159 }
160 else
161 Log(("kstat_data_lookup(%s) -> %d\n", pszStatName, errno));
162 }
163 else
164 Log(("kstat_read() -> %d\n", errno));
165 RTCritSectLeave(&g_MpSolarisCritSect);
166 }
167 }
168 else
169 Log(("invalid idCpu: %d (g_capCpuInfo=%d)\n", (int)idCpu, (int)g_capCpuInfo));
170 }
171
172 return u64;
173}
174
175
176RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
177{
178 /* Solaris really need to fix their APIs. */
179 return rtMpSolarisGetFrequency(idCpu, "current_clock_Hz") / 1000000;
180}
181
182
183RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
184{
185 return rtMpSolarisGetFrequency(idCpu, "clock_MHz");
186}
187
188
189#if defined(RT_ARCH_SPARC) || defined(RT_ARCH_SPARC64)
190RTDECL(RTCPUID) RTMpCpuId(void)
191{
192 /** @todo implement RTMpCpuId on solaris/r3! */
193 return NIL_RTCPUID;
194}
195#endif
196
197
198RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
199{
200 return idCpu < RTCPUSET_MAX_CPUS ? (int)idCpu : -1;
201}
202
203
204RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
205{
206 return (unsigned)iCpu < RTCPUSET_MAX_CPUS ? iCpu : NIL_RTCPUID;
207}
208
209
210RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
211{
212 return RTMpGetCount() - 1;
213}
214
215
216RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
217{
218 return idCpu != NIL_RTCPUID
219 && idCpu < (RTCPUID)RTMpGetCount();
220}
221
222
223RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
224{
225 int iStatus = p_online(idCpu, P_STATUS);
226 return iStatus == P_ONLINE
227 || iStatus == P_NOINTR;
228}
229
230
231RTDECL(bool) RTMpIsCpuPresent(RTCPUID idCpu)
232{
233 int iStatus = p_online(idCpu, P_STATUS);
234 return iStatus != -1;
235}
236
237
238RTDECL(RTCPUID) RTMpGetCount(void)
239{
240 /*
241 * Solaris has sysconf.
242 */
243 int cCpus = sysconf(_SC_NPROCESSORS_MAX);
244 if (cCpus < 0)
245 cCpus = sysconf(_SC_NPROCESSORS_CONF);
246 return cCpus;
247}
248
249
250RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
251{
252 RTCpuSetEmpty(pSet);
253 int idCpu = RTMpGetCount();
254 while (idCpu-- > 0)
255 RTCpuSetAdd(pSet, idCpu);
256 return pSet;
257}
258
259
260RTDECL(RTCPUID) RTMpGetOnlineCount(void)
261{
262 /*
263 * Solaris has sysconf.
264 */
265 return sysconf(_SC_NPROCESSORS_ONLN);
266}
267
268
269RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
270{
271 RTCpuSetEmpty(pSet);
272 RTCPUID cCpus = RTMpGetCount();
273 for (RTCPUID idCpu = 0; idCpu < cCpus; idCpu++)
274 if (RTMpIsCpuOnline(idCpu))
275 RTCpuSetAdd(pSet, idCpu);
276 return pSet;
277}
278
279
280RTDECL(PRTCPUSET) RTMpGetPresentSet(PRTCPUSET pSet)
281{
282#ifdef RT_STRICT
283 long cCpusPresent = 0;
284#endif
285 RTCpuSetEmpty(pSet);
286 RTCPUID cCpus = RTMpGetCount();
287 for (RTCPUID idCpu = 0; idCpu < cCpus; idCpu++)
288 if (RTMpIsCpuPresent(idCpu))
289 {
290 RTCpuSetAdd(pSet, idCpu);
291#ifdef RT_STRICT
292 cCpusPresent++;
293#endif
294 }
295 Assert(cCpusPresent == RTMpGetPresentCount());
296 return pSet;
297}
298
299
300RTDECL(RTCPUID) RTMpGetPresentCount(void)
301{
302 /*
303 * Solaris has sysconf.
304 */
305 return sysconf(_SC_NPROCESSORS_CONF);
306}
307
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