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