1 | /* $Id: mp-solaris.cpp 11324 2008-08-11 10:17: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 |
|
---|
40 | #include <iprt/mp.h>
|
---|
41 | #include <iprt/cpuset.h>
|
---|
42 | #include <iprt/assert.h>
|
---|
43 | #include <iprt/string.h>
|
---|
44 | #include <iprt/alloc.h>
|
---|
45 | #include <iprt/log.h>
|
---|
46 | #include <iprt/once.h>
|
---|
47 | #include <iprt/critsect.h>
|
---|
48 |
|
---|
49 |
|
---|
50 | /*******************************************************************************
|
---|
51 | * Global Variables *
|
---|
52 | *******************************************************************************/
|
---|
53 | /** Initialization serializing (rtMpSolarisOnce). */
|
---|
54 | static RTONCE g_MpSolarisOnce = RTONCE_INITIALIZER;
|
---|
55 | /** Critical section serializing access to kstat. */
|
---|
56 | static RTCRITSECT g_MpSolarisCritSect;
|
---|
57 | /** The kstat handle. */
|
---|
58 | static kstat_ctl_t *g_pKsCtl;
|
---|
59 | /** Array pointing to the cpu_info instances. */
|
---|
60 | static kstat_t **g_papCpuInfo;
|
---|
61 | /** The number of entries in g_papCpuInfo */
|
---|
62 | static RTCPUID g_capCpuInfo;
|
---|
63 |
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Run once function that initializes the kstats we need here.
|
---|
67 | *
|
---|
68 | * @returns IPRT status code.
|
---|
69 | * @param pvUser1 Unused.
|
---|
70 | * @param pvUser2 Unused.
|
---|
71 | */
|
---|
72 | static DECLCALLBACK(int) rtMpSolarisOnce(void *pvUser1, void *pvUser2)
|
---|
73 | {
|
---|
74 | int rc = VINF_SUCCESS;
|
---|
75 | NOREF(pvUser1); NOREF(pvUser2);
|
---|
76 |
|
---|
77 | /*
|
---|
78 | * Open kstat and find the cpu_info entries for each of the CPUs.
|
---|
79 | */
|
---|
80 | g_pKsCtl = kstat_open();
|
---|
81 | if (g_pKsCtl)
|
---|
82 | {
|
---|
83 | g_capCpuInfo = RTMpGetCount();
|
---|
84 | g_papCpuInfo = (kstat_t **)RTMemAlloc(g_capCpuInfo * sizeof(kstat_t *));
|
---|
85 | if (g_papCpuInfo)
|
---|
86 | {
|
---|
87 | rc = RTCritSectInit(&g_MpSolarisCritSect);
|
---|
88 | if (RT_SUCCESS(rc))
|
---|
89 | {
|
---|
90 | RTCPUID i = 0;
|
---|
91 | for (kstat_t *pKsp = g_pKsCtl->kc_chain; pKsp != NULL; pKsp = pKsp->ks_next)
|
---|
92 | {
|
---|
93 | if (!strcmp(pKsp->ks_module, "cpu_info"))
|
---|
94 | {
|
---|
95 | AssertBreak(i < g_capCpuInfo);
|
---|
96 | g_papCpuInfo[i++] = pKsp;
|
---|
97 | /** @todo ks_instance == cpu_id (/usr/src/uts/common/os/cpu.c)? Check this and fix it ASAP. */
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | return VINF_SUCCESS;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /* bail out, we failed. */
|
---|
105 | RTMemFree(g_papCpuInfo);
|
---|
106 | }
|
---|
107 | else
|
---|
108 | rc = VERR_NO_MEMORY;
|
---|
109 | kstat_close(g_pKsCtl);
|
---|
110 | g_pKsCtl = NULL;
|
---|
111 | }
|
---|
112 | else
|
---|
113 | {
|
---|
114 | rc = RTErrConvertFromErrno(errno);
|
---|
115 | if (RT_SUCCESS(rc))
|
---|
116 | rc = VERR_INTERNAL_ERROR;
|
---|
117 | Log(("kstat_open() -> %d (%Rrc)\n", errno, rc));
|
---|
118 | }
|
---|
119 |
|
---|
120 | return rc;
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Worker for RTMpGetCurFrequency and RTMpGetMaxFrequency.
|
---|
126 | *
|
---|
127 | * @returns The desired frequency on success, 0 on failure.
|
---|
128 | *
|
---|
129 | * @param idCpu The CPU ID.
|
---|
130 | * @param pszStatName The cpu_info stat name.
|
---|
131 | */
|
---|
132 | static uint64_t rtMpSolarisGetFrequency(RTCPUID idCpu, char *pszStatName)
|
---|
133 | {
|
---|
134 | uint64_t u64 = 0;
|
---|
135 | int rc = RTOnce(&g_MpSolarisOnce, rtMpSolarisOnce, NULL, NULL);
|
---|
136 | if (RT_SUCCESS(rc))
|
---|
137 | {
|
---|
138 | if ( idCpu < g_capCpuInfo
|
---|
139 | && g_papCpuInfo[idCpu])
|
---|
140 | {
|
---|
141 | rc = RTCritSectEnter(&g_MpSolarisCritSect);
|
---|
142 | AssertRC(rc);
|
---|
143 | if (RT_SUCCESS(rc))
|
---|
144 | {
|
---|
145 | if (kstat_read(g_pKsCtl, g_papCpuInfo[idCpu], 0) != -1)
|
---|
146 | {
|
---|
147 | kstat_named_t *pStat = (kstat_named_t *)kstat_data_lookup(g_papCpuInfo[idCpu], pszStatName);
|
---|
148 | if (pStat)
|
---|
149 | {
|
---|
150 | Assert(pStat->data_type == KSTAT_DATA_UINT64 || pStat->data_type == KSTAT_DATA_LONG);
|
---|
151 | switch (pStat->data_type)
|
---|
152 | {
|
---|
153 | case KSTAT_DATA_UINT64: u64 = pStat->value.ui64; break; /* current_clock_Hz */
|
---|
154 | case KSTAT_DATA_INT32: u64 = pStat->value.i32; break; /* clock_MHz */
|
---|
155 |
|
---|
156 | /* just in case... */
|
---|
157 | case KSTAT_DATA_UINT32: u64 = pStat->value.ui32; break;
|
---|
158 | case KSTAT_DATA_INT64: u64 = pStat->value.i64; break;
|
---|
159 | default:
|
---|
160 | AssertMsgFailed(("%d\n", pStat->data_type));
|
---|
161 | break;
|
---|
162 | }
|
---|
163 | }
|
---|
164 | else
|
---|
165 | Log(("kstat_data_lookup(%s) -> %d\n", pszStatName, errno));
|
---|
166 | }
|
---|
167 | else
|
---|
168 | Log(("kstat_read() -> %d\n", errno));
|
---|
169 | RTCritSectLeave(&g_MpSolarisCritSect);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | else
|
---|
173 | Log(("invalid idCpu: %d (g_capCpuInfo=%d)\n", (int)idCpu, (int)g_capCpuInfo));
|
---|
174 | }
|
---|
175 |
|
---|
176 | return u64;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
|
---|
181 | {
|
---|
182 | return rtMpSolarisGetFrequency(idCpu, "current_clock_Hz") / 1000000;
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
|
---|
187 | {
|
---|
188 | return rtMpSolarisGetFrequency(idCpu, "clock_MHz");
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | RTDECL(RTCPUID) RTMpGetCount(void)
|
---|
193 | {
|
---|
194 | /*
|
---|
195 | * Solaris has sysconf.
|
---|
196 | */
|
---|
197 | int cCpus = sysconf(_SC_NPROCESSORS_MAX);
|
---|
198 | if (cCpus < 0)
|
---|
199 | cCpus = sysconf(_SC_NPROCESSORS_CONF);
|
---|
200 | return cCpus;
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | RTDECL(RTCPUID) RTMpGetOnlineCount(void)
|
---|
205 | {
|
---|
206 | /*
|
---|
207 | * Solaris has sysconf.
|
---|
208 | */
|
---|
209 | return sysconf(_SC_NPROCESSORS_ONLN);
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
|
---|
214 | {
|
---|
215 | /** @todo fix this! */
|
---|
216 | RTCpuSetEmpty(pSet);
|
---|
217 | RTCPUID cMax = RTMpGetOnlineCount();
|
---|
218 | while (cMax-- > 0)
|
---|
219 | RTCpuSetAdd(pSet, cMax);
|
---|
220 | return pSet;
|
---|
221 | }
|
---|
222 |
|
---|