1 | /* $Id: mp-solaris.cpp 48935 2013-10-07 21:19:37Z 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). */
|
---|
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 | /** Array of core ids. */
|
---|
61 | static uint64_t *g_pu64CoreIds;
|
---|
62 | /** Number of entries in g_pu64CoreIds. */
|
---|
63 | static size_t g_cu64CoreIds;
|
---|
64 | /** Number of cores in the system. */
|
---|
65 | static size_t g_cCores;
|
---|
66 |
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Helper for getting the core ID for a given CPU/strand/hyperthread.
|
---|
70 | *
|
---|
71 | * @returns The core ID.
|
---|
72 | * @param idCpu The CPU ID instance.
|
---|
73 | */
|
---|
74 | static inline uint64_t rtMpSolarisGetCoreId(RTCPUID idCpu)
|
---|
75 | {
|
---|
76 | kstat_named_t *pStat = (kstat_named_t *)kstat_data_lookup(g_papCpuInfo[idCpu], (char *)"core_id");
|
---|
77 | Assert(pStat->data_type == KSTAT_DATA_LONG);
|
---|
78 | Assert(pStat->value.l >= 0);
|
---|
79 | AssertCompile(sizeof(uint64_t) >= sizeof(long)); /* Paranoia. */
|
---|
80 | return (uint64_t)pStat->value.l;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Populates 'g_pu64CoreIds' array with unique core identifiers in the system.
|
---|
86 | *
|
---|
87 | * @returns VBox status code.
|
---|
88 | */
|
---|
89 | static int rtMpSolarisGetCoreIds(void)
|
---|
90 | {
|
---|
91 | for (RTCPUID idCpu = 0; idCpu < g_capCpuInfo; idCpu++)
|
---|
92 | {
|
---|
93 | if (kstat_read(g_pKsCtl, g_papCpuInfo[idCpu], 0) != -1)
|
---|
94 | {
|
---|
95 | /* Strands/Hyperthreads share the same core ID. */
|
---|
96 | uint64_t u64CoreId = rtMpSolarisGetCoreId(idCpu);
|
---|
97 | bool fAddedCore = false;
|
---|
98 | for (RTCPUID i = 0; i < g_cCores; i++)
|
---|
99 | {
|
---|
100 | if (g_pu64CoreIds[i] == u64CoreId)
|
---|
101 | {
|
---|
102 | fAddedCore = true;
|
---|
103 | break;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (!fAddedCore)
|
---|
108 | {
|
---|
109 | g_pu64CoreIds[g_cCores] = u64CoreId;
|
---|
110 | ++g_cCores;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | else
|
---|
114 | return VERR_INTERNAL_ERROR_2;
|
---|
115 | }
|
---|
116 |
|
---|
117 | return VINF_SUCCESS;
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Run once function that initializes the kstats we need here.
|
---|
123 | *
|
---|
124 | * @returns IPRT status code.
|
---|
125 | * @param pvUser Unused.
|
---|
126 | */
|
---|
127 | static DECLCALLBACK(int) rtMpSolarisOnce(void *pvUser)
|
---|
128 | {
|
---|
129 | int rc = VINF_SUCCESS;
|
---|
130 | NOREF(pvUser);
|
---|
131 |
|
---|
132 | /*
|
---|
133 | * Open kstat and find the cpu_info entries for each of the CPUs.
|
---|
134 | */
|
---|
135 | g_pKsCtl = kstat_open();
|
---|
136 | if (g_pKsCtl)
|
---|
137 | {
|
---|
138 | g_capCpuInfo = RTMpGetCount();
|
---|
139 | g_papCpuInfo = (kstat_t **)RTMemAllocZ(g_capCpuInfo * sizeof(kstat_t *));
|
---|
140 | if (g_papCpuInfo)
|
---|
141 | {
|
---|
142 | g_cu64CoreIds = g_capCpuInfo;
|
---|
143 | g_pu64CoreIds = (uint64_t *)RTMemAllocZ(g_cu64CoreIds * sizeof(uint64_t));
|
---|
144 | if (g_pu64CoreIds)
|
---|
145 | {
|
---|
146 | rc = RTCritSectInit(&g_MpSolarisCritSect);
|
---|
147 | if (RT_SUCCESS(rc))
|
---|
148 | {
|
---|
149 | RTCPUID i = 0;
|
---|
150 | for (kstat_t *pKsp = g_pKsCtl->kc_chain; pKsp != NULL; pKsp = pKsp->ks_next)
|
---|
151 | {
|
---|
152 | if (!RTStrCmp(pKsp->ks_module, "cpu_info"))
|
---|
153 | {
|
---|
154 | AssertBreak(i < g_capCpuInfo);
|
---|
155 | g_papCpuInfo[i++] = pKsp;
|
---|
156 | /** @todo ks_instance == cpu_id (/usr/src/uts/common/os/cpu.c)? Check this and fix it ASAP. */
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | rc = rtMpSolarisGetCoreIds();
|
---|
161 | if (RT_SUCCESS(rc))
|
---|
162 | return VINF_SUCCESS;
|
---|
163 | else
|
---|
164 | Log(("rtMpSolarisGetCoreIds failed. rc=%Rrc\n", rc));
|
---|
165 | }
|
---|
166 |
|
---|
167 | RTMemFree(g_pu64CoreIds);
|
---|
168 | g_pu64CoreIds = NULL;
|
---|
169 | }
|
---|
170 | else
|
---|
171 | rc = VERR_NO_MEMORY;
|
---|
172 |
|
---|
173 | /* bail out, we failed. */
|
---|
174 | RTMemFree(g_papCpuInfo);
|
---|
175 | g_papCpuInfo = NULL;
|
---|
176 | }
|
---|
177 | else
|
---|
178 | rc = VERR_NO_MEMORY;
|
---|
179 | kstat_close(g_pKsCtl);
|
---|
180 | g_pKsCtl = NULL;
|
---|
181 | }
|
---|
182 | else
|
---|
183 | {
|
---|
184 | rc = RTErrConvertFromErrno(errno);
|
---|
185 | if (RT_SUCCESS(rc))
|
---|
186 | rc = VERR_INTERNAL_ERROR;
|
---|
187 | Log(("kstat_open() -> %d (%Rrc)\n", errno, rc));
|
---|
188 | }
|
---|
189 |
|
---|
190 | return rc;
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * RTOnceEx() cleanup function.
|
---|
196 | *
|
---|
197 | * @param pvUser Unused.
|
---|
198 | * @param fLazyCleanUpOk Whether lazy cleanup is okay or not.
|
---|
199 | */
|
---|
200 | static DECLCALLBACK(void) rtMpSolarisCleanUp(void *pvUser, bool fLazyCleanUpOk)
|
---|
201 | {
|
---|
202 | if (g_pKsCtl)
|
---|
203 | kstat_close(g_pKsCtl);
|
---|
204 | RTMemFree(g_pu64CoreIds);
|
---|
205 | RTMemFree(g_papCpuInfo);
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Worker for RTMpGetCurFrequency and RTMpGetMaxFrequency.
|
---|
211 | *
|
---|
212 | * @returns The desired frequency on success, 0 on failure.
|
---|
213 | *
|
---|
214 | * @param idCpu The CPU ID.
|
---|
215 | * @param pszStatName The cpu_info stat name.
|
---|
216 | */
|
---|
217 | static uint64_t rtMpSolarisGetFrequency(RTCPUID idCpu, const char *pszStatName)
|
---|
218 | {
|
---|
219 | uint64_t u64 = 0;
|
---|
220 | int rc = RTOnceEx(&g_MpSolarisOnce, rtMpSolarisOnce, rtMpSolarisCleanUp, NULL /* pvUser */);
|
---|
221 | if (RT_SUCCESS(rc))
|
---|
222 | {
|
---|
223 | if ( idCpu < g_capCpuInfo
|
---|
224 | && g_papCpuInfo[idCpu])
|
---|
225 | {
|
---|
226 | rc = RTCritSectEnter(&g_MpSolarisCritSect);
|
---|
227 | AssertRC(rc);
|
---|
228 | if (RT_SUCCESS(rc))
|
---|
229 | {
|
---|
230 | if (kstat_read(g_pKsCtl, g_papCpuInfo[idCpu], 0) != -1)
|
---|
231 | {
|
---|
232 | /* Solaris really need to fix their APIs. Explicitly cast for now. */
|
---|
233 | kstat_named_t *pStat = (kstat_named_t *)kstat_data_lookup(g_papCpuInfo[idCpu], (char*)pszStatName);
|
---|
234 | if (pStat)
|
---|
235 | {
|
---|
236 | Assert(pStat->data_type == KSTAT_DATA_UINT64 || pStat->data_type == KSTAT_DATA_LONG);
|
---|
237 | switch (pStat->data_type)
|
---|
238 | {
|
---|
239 | case KSTAT_DATA_UINT64: u64 = pStat->value.ui64; break; /* current_clock_Hz */
|
---|
240 | case KSTAT_DATA_INT32: u64 = pStat->value.i32; break; /* clock_MHz */
|
---|
241 |
|
---|
242 | /* just in case... */
|
---|
243 | case KSTAT_DATA_UINT32: u64 = pStat->value.ui32; break;
|
---|
244 | case KSTAT_DATA_INT64: u64 = pStat->value.i64; break;
|
---|
245 | default:
|
---|
246 | AssertMsgFailed(("%d\n", pStat->data_type));
|
---|
247 | break;
|
---|
248 | }
|
---|
249 | }
|
---|
250 | else
|
---|
251 | Log(("kstat_data_lookup(%s) -> %d\n", pszStatName, errno));
|
---|
252 | }
|
---|
253 | else
|
---|
254 | Log(("kstat_read() -> %d\n", errno));
|
---|
255 | RTCritSectLeave(&g_MpSolarisCritSect);
|
---|
256 | }
|
---|
257 | }
|
---|
258 | else
|
---|
259 | Log(("invalid idCpu: %d (g_capCpuInfo=%d)\n", (int)idCpu, (int)g_capCpuInfo));
|
---|
260 | }
|
---|
261 |
|
---|
262 | return u64;
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
|
---|
267 | {
|
---|
268 | return rtMpSolarisGetFrequency(idCpu, "current_clock_Hz") / 1000000;
|
---|
269 | }
|
---|
270 |
|
---|
271 |
|
---|
272 | RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
|
---|
273 | {
|
---|
274 | return rtMpSolarisGetFrequency(idCpu, "clock_MHz");
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|
278 | #if defined(RT_ARCH_SPARC) || defined(RT_ARCH_SPARC64)
|
---|
279 | RTDECL(RTCPUID) RTMpCpuId(void)
|
---|
280 | {
|
---|
281 | /** @todo implement RTMpCpuId on solaris/r3! */
|
---|
282 | return NIL_RTCPUID;
|
---|
283 | }
|
---|
284 | #endif
|
---|
285 |
|
---|
286 |
|
---|
287 | RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
|
---|
288 | {
|
---|
289 | return idCpu < RTCPUSET_MAX_CPUS ? (int)idCpu : -1;
|
---|
290 | }
|
---|
291 |
|
---|
292 |
|
---|
293 | RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
|
---|
294 | {
|
---|
295 | return (unsigned)iCpu < RTCPUSET_MAX_CPUS ? iCpu : NIL_RTCPUID;
|
---|
296 | }
|
---|
297 |
|
---|
298 |
|
---|
299 | RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
|
---|
300 | {
|
---|
301 | return RTMpGetCount() - 1;
|
---|
302 | }
|
---|
303 |
|
---|
304 |
|
---|
305 | RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
|
---|
306 | {
|
---|
307 | return idCpu != NIL_RTCPUID
|
---|
308 | && idCpu < (RTCPUID)RTMpGetCount();
|
---|
309 | }
|
---|
310 |
|
---|
311 |
|
---|
312 | RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
|
---|
313 | {
|
---|
314 | int iStatus = p_online(idCpu, P_STATUS);
|
---|
315 | return iStatus == P_ONLINE
|
---|
316 | || iStatus == P_NOINTR;
|
---|
317 | }
|
---|
318 |
|
---|
319 |
|
---|
320 | RTDECL(bool) RTMpIsCpuPresent(RTCPUID idCpu)
|
---|
321 | {
|
---|
322 | int iStatus = p_online(idCpu, P_STATUS);
|
---|
323 | return iStatus != -1;
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 | RTDECL(RTCPUID) RTMpGetCount(void)
|
---|
328 | {
|
---|
329 | /*
|
---|
330 | * Solaris has sysconf.
|
---|
331 | */
|
---|
332 | int cCpus = sysconf(_SC_NPROCESSORS_MAX);
|
---|
333 | if (cCpus < 0)
|
---|
334 | cCpus = sysconf(_SC_NPROCESSORS_CONF);
|
---|
335 | return cCpus;
|
---|
336 | }
|
---|
337 |
|
---|
338 |
|
---|
339 | RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
|
---|
340 | {
|
---|
341 | RTCpuSetEmpty(pSet);
|
---|
342 | int idCpu = RTMpGetCount();
|
---|
343 | while (idCpu-- > 0)
|
---|
344 | RTCpuSetAdd(pSet, idCpu);
|
---|
345 | return pSet;
|
---|
346 | }
|
---|
347 |
|
---|
348 |
|
---|
349 | RTDECL(RTCPUID) RTMpGetOnlineCount(void)
|
---|
350 | {
|
---|
351 | /*
|
---|
352 | * Solaris has sysconf.
|
---|
353 | */
|
---|
354 | return sysconf(_SC_NPROCESSORS_ONLN);
|
---|
355 | }
|
---|
356 |
|
---|
357 |
|
---|
358 | RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
|
---|
359 | {
|
---|
360 | RTCpuSetEmpty(pSet);
|
---|
361 | RTCPUID cCpus = RTMpGetCount();
|
---|
362 | for (RTCPUID idCpu = 0; idCpu < cCpus; idCpu++)
|
---|
363 | if (RTMpIsCpuOnline(idCpu))
|
---|
364 | RTCpuSetAdd(pSet, idCpu);
|
---|
365 | return pSet;
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 | RTDECL(PRTCPUSET) RTMpGetPresentSet(PRTCPUSET pSet)
|
---|
370 | {
|
---|
371 | #ifdef RT_STRICT
|
---|
372 | RTCPUID cCpusPresent = 0;
|
---|
373 | #endif
|
---|
374 | RTCpuSetEmpty(pSet);
|
---|
375 | RTCPUID cCpus = RTMpGetCount();
|
---|
376 | for (RTCPUID idCpu = 0; idCpu < cCpus; idCpu++)
|
---|
377 | if (RTMpIsCpuPresent(idCpu))
|
---|
378 | {
|
---|
379 | RTCpuSetAdd(pSet, idCpu);
|
---|
380 | #ifdef RT_STRICT
|
---|
381 | cCpusPresent++;
|
---|
382 | #endif
|
---|
383 | }
|
---|
384 | Assert(cCpusPresent == RTMpGetPresentCount());
|
---|
385 | return pSet;
|
---|
386 | }
|
---|
387 |
|
---|
388 |
|
---|
389 | RTDECL(RTCPUID) RTMpGetPresentCount(void)
|
---|
390 | {
|
---|
391 | /*
|
---|
392 | * Solaris has sysconf.
|
---|
393 | */
|
---|
394 | return sysconf(_SC_NPROCESSORS_CONF);
|
---|
395 | }
|
---|
396 |
|
---|
397 |
|
---|
398 | RTDECL(RTCPUID) RTMpGetPresentCoreCount(void)
|
---|
399 | {
|
---|
400 | return RTMpGetCoreCount();
|
---|
401 | }
|
---|
402 |
|
---|
403 |
|
---|
404 | RTDECL(RTCPUID) RTMpGetCoreCount(void)
|
---|
405 | {
|
---|
406 | int rc = RTOnceEx(&g_MpSolarisOnce, rtMpSolarisOnce, rtMpSolarisCleanUp, NULL /* pvUser */);
|
---|
407 | if (RT_SUCCESS(rc))
|
---|
408 | return g_cCores;
|
---|
409 |
|
---|
410 | return 0;
|
---|
411 | }
|
---|
412 |
|
---|
413 |
|
---|
414 | RTDECL(RTCPUID) RTMpGetOnlineCoreCount(void)
|
---|
415 | {
|
---|
416 | RTCPUID uOnlineCores = 0;
|
---|
417 | int rc = RTOnceEx(&g_MpSolarisOnce, rtMpSolarisOnce, rtMpSolarisCleanUp, NULL /* pvUser */);
|
---|
418 | if (RT_SUCCESS(rc))
|
---|
419 | {
|
---|
420 | rc = RTCritSectEnter(&g_MpSolarisCritSect);
|
---|
421 | AssertRC(rc);
|
---|
422 |
|
---|
423 | /*
|
---|
424 | * For each core in the system, count how many are currently online.
|
---|
425 | */
|
---|
426 | for (RTCPUID j = 0; j < g_cCores; j++)
|
---|
427 | {
|
---|
428 | uint64_t u64CoreId = g_pu64CoreIds[j];
|
---|
429 | for (RTCPUID idCpu = 0; idCpu < g_capCpuInfo; idCpu++)
|
---|
430 | {
|
---|
431 | rc = kstat_read(g_pKsCtl, g_papCpuInfo[idCpu], 0);
|
---|
432 | AssertReturn(rc != -1, 0 /* rc */);
|
---|
433 | uint64_t u64ThreadCoreId = rtMpSolarisGetCoreId(idCpu);
|
---|
434 | if (u64ThreadCoreId == u64CoreId)
|
---|
435 | {
|
---|
436 | kstat_named_t *pStat = (kstat_named_t *)kstat_data_lookup(g_papCpuInfo[idCpu], (char *)"state");
|
---|
437 | Assert(pStat->data_type == KSTAT_DATA_CHAR);
|
---|
438 | if( !RTStrNCmp(pStat->value.c, PS_ONLINE, sizeof(PS_ONLINE) - 1)
|
---|
439 | || !RTStrNCmp(pStat->value.c, PS_NOINTR, sizeof(PS_NOINTR) - 1))
|
---|
440 | {
|
---|
441 | uOnlineCores++;
|
---|
442 | break; /* Move to the next core. We have at least 1 hyperthread online in the current core. */
|
---|
443 | }
|
---|
444 | }
|
---|
445 | }
|
---|
446 | }
|
---|
447 |
|
---|
448 | RTCritSectLeave(&g_MpSolarisCritSect);
|
---|
449 | }
|
---|
450 |
|
---|
451 | return uOnlineCores;
|
---|
452 | }
|
---|
453 |
|
---|