VirtualBox

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

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

iprt: Solaris R3 implementation of RTMp*Present*.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette