VirtualBox

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

Last change on this file since 60686 was 57358, checked in by vboxsync, 10 years ago

*: scm cleanup run.

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