[1] | 1 | /* $Id: mp-win.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - Multiprocessor, Windows.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[96407] | 7 | * Copyright (C) 2006-2022 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[5999] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[1] | 41 | #define LOG_GROUP RTLOGGROUP_SYSTEM
|
---|
[62592] | 42 | #include <iprt/win/windows.h>
|
---|
[46635] | 43 |
|
---|
[7362] | 44 | #include <iprt/mp.h>
|
---|
[46635] | 45 | #include "internal/iprt.h"
|
---|
| 46 |
|
---|
[95766] | 47 | #include <iprt/alloca.h>
|
---|
[46635] | 48 | #include <iprt/assert.h>
|
---|
[7362] | 49 | #include <iprt/cpuset.h>
|
---|
[46635] | 50 | #include <iprt/ldr.h>
|
---|
[46617] | 51 | #include <iprt/mem.h>
|
---|
[64220] | 52 | #include <iprt/once.h>
|
---|
[64281] | 53 | #include <iprt/param.h>
|
---|
[64220] | 54 | #if defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64)
|
---|
| 55 | # include <iprt/asm-amd64-x86.h>
|
---|
| 56 | #endif
|
---|
[64283] | 57 | #if defined(VBOX) && !defined(IN_GUEST) && !defined(IN_RT_STATIC)
|
---|
[64281] | 58 | # include <VBox/sup.h>
|
---|
| 59 | # define IPRT_WITH_GIP_MP_INFO
|
---|
| 60 | #else
|
---|
| 61 | # undef IPRT_WITH_GIP_MP_INFO
|
---|
| 62 | #endif
|
---|
[1] | 63 |
|
---|
[64220] | 64 | #include "internal-r3-win.h"
|
---|
[1] | 65 |
|
---|
[36232] | 66 |
|
---|
[64220] | 67 | /*********************************************************************************************************************************
|
---|
| 68 | * Defined Constants And Macros *
|
---|
| 69 | *********************************************************************************************************************************/
|
---|
[64293] | 70 | /** @def RTMPWIN_UPDATE_GIP_GLOBALS
|
---|
[64281] | 71 | * Does lazy (re-)initialization using information provieded by GIP. */
|
---|
| 72 | #ifdef IPRT_WITH_GIP_MP_INFO
|
---|
[64293] | 73 | # define RTMPWIN_UPDATE_GIP_GLOBALS() \
|
---|
[64281] | 74 | do { RTMPWIN_UPDATE_GIP_GLOBALS_AND_GET_PGIP(); } while (0)
|
---|
| 75 | #else
|
---|
[64293] | 76 | # define RTMPWIN_UPDATE_GIP_GLOBALS() do { } while (0)
|
---|
[64281] | 77 | #endif
|
---|
| 78 |
|
---|
| 79 | /** @def RTMPWIN_UPDATE_GIP_GLOBALS_AND_GET_PGIP
|
---|
| 80 | * Does lazy (re-)initialization using information provieded by GIP and
|
---|
| 81 | * declare and initalize a pGip local variable. */
|
---|
| 82 | #ifdef IPRT_WITH_GIP_MP_INFO
|
---|
| 83 | #define RTMPWIN_UPDATE_GIP_GLOBALS_AND_GET_PGIP() \
|
---|
| 84 | PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage; \
|
---|
| 85 | if (pGip) \
|
---|
| 86 | { \
|
---|
| 87 | if ( pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC \
|
---|
| 88 | && RTOnce(&g_MpInitOnceGip, rtMpWinInitOnceGip, NULL) == VINF_SUCCESS) \
|
---|
| 89 | { \
|
---|
| 90 | if (g_cRtMpWinActiveCpus >= pGip->cOnlineCpus) \
|
---|
| 91 | { /* likely */ } \
|
---|
| 92 | else \
|
---|
| 93 | rtMpWinRefreshGip(); \
|
---|
| 94 | } \
|
---|
| 95 | else \
|
---|
| 96 | pGip = NULL; \
|
---|
| 97 | } else do { } while (0)
|
---|
| 98 | #else
|
---|
| 99 | # define RTMPWIN_UPDATE_GIP_GLOBALS_AND_GET_PGIP() do { } while (0)
|
---|
| 100 | #endif
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 | /*********************************************************************************************************************************
|
---|
| 104 | * Global Variables *
|
---|
| 105 | *********************************************************************************************************************************/
|
---|
[64220] | 106 | /** Initialize once. */
|
---|
| 107 | static RTONCE g_MpInitOnce = RTONCE_INITIALIZER;
|
---|
[64281] | 108 | #ifdef IPRT_WITH_GIP_MP_INFO
|
---|
| 109 | /** Initialize once using GIP. */
|
---|
| 110 | static RTONCE g_MpInitOnceGip = RTONCE_INITIALIZER;
|
---|
| 111 | #endif
|
---|
| 112 |
|
---|
| 113 | static decltype(GetMaximumProcessorCount) *g_pfnGetMaximumProcessorCount;
|
---|
| 114 | //static decltype(GetActiveProcessorCount) *g_pfnGetActiveProcessorCount;
|
---|
[64220] | 115 | static decltype(GetCurrentProcessorNumber) *g_pfnGetCurrentProcessorNumber;
|
---|
| 116 | static decltype(GetCurrentProcessorNumberEx) *g_pfnGetCurrentProcessorNumberEx;
|
---|
| 117 | static decltype(GetLogicalProcessorInformation) *g_pfnGetLogicalProcessorInformation;
|
---|
| 118 | static decltype(GetLogicalProcessorInformationEx) *g_pfnGetLogicalProcessorInformationEx;
|
---|
[1] | 119 |
|
---|
[64220] | 120 |
|
---|
| 121 | /** The required buffer size for getting group relations. */
|
---|
| 122 | static uint32_t g_cbRtMpWinGrpRelBuf;
|
---|
| 123 | /** The max number of CPUs (RTMpGetCount). */
|
---|
| 124 | static uint32_t g_cRtMpWinMaxCpus;
|
---|
| 125 | /** The max number of CPU cores (RTMpGetCoreCount). */
|
---|
| 126 | static uint32_t g_cRtMpWinMaxCpuCores;
|
---|
| 127 | /** The max number of groups. */
|
---|
| 128 | static uint32_t g_cRtMpWinMaxCpuGroups;
|
---|
[64281] | 129 | /** The number of active CPUs the last time we checked. */
|
---|
| 130 | static uint32_t volatile g_cRtMpWinActiveCpus;
|
---|
| 131 | /** Static per group info.
|
---|
[81096] | 132 | * @remarks With 256 entries this takes up 33KB.
|
---|
[64281] | 133 | * @sa g_aRtMpNtCpuGroups */
|
---|
[64220] | 134 | static struct
|
---|
| 135 | {
|
---|
| 136 | /** The max CPUs in the group. */
|
---|
| 137 | uint16_t cMaxCpus;
|
---|
[64281] | 138 | /** The number of active CPUs at the time of initialization. */
|
---|
| 139 | uint16_t cActiveCpus;
|
---|
| 140 | /** CPU set indexes for each CPU in the group. */
|
---|
| 141 | int16_t aidxCpuSetMembers[64];
|
---|
[81096] | 142 | } g_aRtMpWinCpuGroups[256];
|
---|
[64281] | 143 | /** Maps CPU set indexes to RTCPUID.
|
---|
| 144 | * @sa g_aidRtMpNtByCpuSetIdx */
|
---|
| 145 | RTCPUID g_aidRtMpWinByCpuSetIdx[RTCPUSET_MAX_CPUS];
|
---|
[64220] | 146 |
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
[64281] | 149 | * @callback_method_impl{FNRTONCE,
|
---|
| 150 | * Resolves dynamic imports and initializes globals.}
|
---|
[64220] | 151 | */
|
---|
| 152 | static DECLCALLBACK(int32_t) rtMpWinInitOnce(void *pvUser)
|
---|
| 153 | {
|
---|
| 154 | RT_NOREF(pvUser);
|
---|
| 155 |
|
---|
| 156 | Assert(g_WinOsInfoEx.dwOSVersionInfoSize != 0);
|
---|
| 157 | Assert(g_hModKernel32 != NULL);
|
---|
| 158 |
|
---|
| 159 | /*
|
---|
| 160 | * Resolve dynamic APIs.
|
---|
| 161 | */
|
---|
| 162 | #define RESOLVE_API(a_szMod, a_FnName) \
|
---|
| 163 | do { \
|
---|
| 164 | RT_CONCAT(g_pfn,a_FnName) = (decltype(a_FnName) *)GetProcAddress(g_hModKernel32, #a_FnName); \
|
---|
| 165 | } while (0)
|
---|
[64281] | 166 | RESOLVE_API("kernel32.dll", GetMaximumProcessorCount);
|
---|
| 167 | //RESOLVE_API("kernel32.dll", GetActiveProcessorCount); - slow :/
|
---|
[64220] | 168 | RESOLVE_API("kernel32.dll", GetCurrentProcessorNumber);
|
---|
| 169 | RESOLVE_API("kernel32.dll", GetCurrentProcessorNumberEx);
|
---|
| 170 | RESOLVE_API("kernel32.dll", GetLogicalProcessorInformation);
|
---|
| 171 | RESOLVE_API("kernel32.dll", GetLogicalProcessorInformationEx);
|
---|
| 172 |
|
---|
| 173 | /*
|
---|
[64281] | 174 | * Reset globals.
|
---|
| 175 | */
|
---|
| 176 | for (unsigned i = 0; i < RT_ELEMENTS(g_aidRtMpWinByCpuSetIdx); i++)
|
---|
| 177 | g_aidRtMpWinByCpuSetIdx[i] = NIL_RTCPUID;
|
---|
| 178 | for (unsigned idxGroup = 0; idxGroup < RT_ELEMENTS(g_aRtMpWinCpuGroups); idxGroup++)
|
---|
| 179 | {
|
---|
| 180 | g_aRtMpWinCpuGroups[idxGroup].cMaxCpus = 0;
|
---|
| 181 | g_aRtMpWinCpuGroups[idxGroup].cActiveCpus = 0;
|
---|
| 182 | for (unsigned idxMember = 0; idxMember < RT_ELEMENTS(g_aRtMpWinCpuGroups[idxGroup].aidxCpuSetMembers); idxMember++)
|
---|
| 183 | g_aRtMpWinCpuGroups[idxGroup].aidxCpuSetMembers[idxMember] = -1;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /*
|
---|
| 187 | * Query group information, partitioning CPU IDs and CPU set indexes.
|
---|
[64220] | 188 | *
|
---|
[64531] | 189 | * We ASSUME the GroupInfo index is the same as the group number.
|
---|
[64234] | 190 | *
|
---|
[64281] | 191 | * We CANNOT ASSUME that the kernel CPU indexes are assigned in any given
|
---|
| 192 | * way, though they usually are in group order by active processor. So,
|
---|
| 193 | * we do that to avoid trouble. We must use information provided thru GIP
|
---|
| 194 | * if we want the kernel CPU set indexes. Even there, the inactive CPUs
|
---|
| 195 | * wont have sensible indexes. Sigh.
|
---|
[64220] | 196 | *
|
---|
[64281] | 197 | * We try to assign IDs to inactive CPUs in the same manner as mp-r0drv-nt.cpp
|
---|
[64234] | 198 | *
|
---|
[64281] | 199 | * Note! We will die (AssertFatal) if there are too many processors!
|
---|
[64220] | 200 | */
|
---|
| 201 | union
|
---|
| 202 | {
|
---|
| 203 | SYSTEM_INFO SysInfo;
|
---|
| 204 | SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX Info;
|
---|
| 205 | uint8_t abPaddingG[ sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)
|
---|
[81096] | 206 | + sizeof(PROCESSOR_GROUP_INFO) * 256];
|
---|
[64220] | 207 | uint8_t abPaddingC[ sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)
|
---|
| 208 | + (sizeof(PROCESSOR_RELATIONSHIP) + sizeof(GROUP_AFFINITY))
|
---|
| 209 | * RTCPUSET_MAX_CPUS];
|
---|
| 210 | } uBuf;
|
---|
| 211 | if (g_pfnGetLogicalProcessorInformationEx)
|
---|
| 212 | {
|
---|
| 213 | /* Query the information. */
|
---|
| 214 | DWORD cbData = sizeof(uBuf);
|
---|
| 215 | AssertFatalMsg(g_pfnGetLogicalProcessorInformationEx(RelationGroup, &uBuf.Info, &cbData) != FALSE,
|
---|
| 216 | ("last error = %u, cbData = %u (in %u)\n", GetLastError(), cbData, sizeof(uBuf)));
|
---|
| 217 | AssertFatalMsg(uBuf.Info.Relationship == RelationGroup,
|
---|
| 218 | ("Relationship = %u, expected %u!\n", uBuf.Info.Relationship, RelationGroup));
|
---|
| 219 | AssertFatalMsg(uBuf.Info.Group.MaximumGroupCount <= RT_ELEMENTS(g_aRtMpWinCpuGroups),
|
---|
| 220 | ("MaximumGroupCount is %u, we only support up to %u!\n",
|
---|
| 221 | uBuf.Info.Group.MaximumGroupCount, RT_ELEMENTS(g_aRtMpWinCpuGroups)));
|
---|
| 222 |
|
---|
| 223 | AssertMsg(uBuf.Info.Group.MaximumGroupCount == uBuf.Info.Group.ActiveGroupCount, /* 2nd assumption mentioned above. */
|
---|
| 224 | ("%u vs %u\n", uBuf.Info.Group.MaximumGroupCount, uBuf.Info.Group.ActiveGroupCount));
|
---|
| 225 | AssertFatal(uBuf.Info.Group.MaximumGroupCount >= uBuf.Info.Group.ActiveGroupCount);
|
---|
| 226 |
|
---|
[64281] | 227 | g_cRtMpWinMaxCpuGroups = uBuf.Info.Group.MaximumGroupCount;
|
---|
[64220] | 228 |
|
---|
[64281] | 229 | /* Count max cpus (see mp-r0drv0-nt.cpp) why we don't use GetMaximumProcessorCount(ALL). */
|
---|
| 230 | uint32_t idxGroup;
|
---|
| 231 | g_cRtMpWinMaxCpus = 0;
|
---|
| 232 | for (idxGroup = 0; idxGroup < uBuf.Info.Group.ActiveGroupCount; idxGroup++)
|
---|
| 233 | g_cRtMpWinMaxCpus += uBuf.Info.Group.GroupInfo[idxGroup].MaximumProcessorCount;
|
---|
| 234 |
|
---|
[64220] | 235 | /* Process the active groups. */
|
---|
[64281] | 236 | uint32_t cActive = 0;
|
---|
| 237 | uint32_t cInactive = 0;
|
---|
| 238 | uint32_t idxCpu = 0;
|
---|
| 239 | uint32_t idxCpuSetNextInactive = g_cRtMpWinMaxCpus - 1;
|
---|
| 240 | for (idxGroup = 0; idxGroup < uBuf.Info.Group.ActiveGroupCount; idxGroup++)
|
---|
[64220] | 241 | {
|
---|
[64281] | 242 | PROCESSOR_GROUP_INFO const *pGroupInfo = &uBuf.Info.Group.GroupInfo[idxGroup];
|
---|
| 243 | g_aRtMpWinCpuGroups[idxGroup].cMaxCpus = pGroupInfo->MaximumProcessorCount;
|
---|
| 244 | g_aRtMpWinCpuGroups[idxGroup].cActiveCpus = pGroupInfo->ActiveProcessorCount;
|
---|
| 245 | for (uint32_t idxMember = 0; idxMember < pGroupInfo->MaximumProcessorCount; idxMember++)
|
---|
| 246 | {
|
---|
| 247 | if (pGroupInfo->ActiveProcessorMask & RT_BIT_64(idxMember))
|
---|
| 248 | {
|
---|
| 249 | g_aRtMpWinCpuGroups[idxGroup].aidxCpuSetMembers[idxMember] = idxCpu;
|
---|
| 250 | g_aidRtMpWinByCpuSetIdx[idxCpu] = idxCpu;
|
---|
| 251 | idxCpu++;
|
---|
| 252 | cActive++;
|
---|
| 253 | }
|
---|
| 254 | else
|
---|
| 255 | {
|
---|
| 256 | if (idxCpuSetNextInactive >= idxCpu)
|
---|
| 257 | {
|
---|
| 258 | g_aRtMpWinCpuGroups[idxGroup].aidxCpuSetMembers[idxMember] = idxCpuSetNextInactive;
|
---|
| 259 | g_aidRtMpWinByCpuSetIdx[idxCpuSetNextInactive] = idxCpuSetNextInactive;
|
---|
| 260 | idxCpuSetNextInactive--;
|
---|
| 261 | }
|
---|
| 262 | cInactive++;
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
[64220] | 265 | }
|
---|
[64281] | 266 | g_cRtMpWinActiveCpus = cActive;
|
---|
| 267 | Assert(cActive + cInactive <= g_cRtMpWinMaxCpus);
|
---|
| 268 | Assert(idxCpu <= idxCpuSetNextInactive + 1);
|
---|
| 269 | Assert(idxCpu <= g_cRtMpWinMaxCpus);
|
---|
[64220] | 270 |
|
---|
| 271 | /* Just in case the 2nd assumption doesn't hold true and there are inactive groups. */
|
---|
| 272 | for (; idxGroup < uBuf.Info.Group.MaximumGroupCount; idxGroup++)
|
---|
| 273 | {
|
---|
[64281] | 274 | DWORD cMaxMembers = g_pfnGetMaximumProcessorCount(idxGroup);
|
---|
| 275 | g_aRtMpWinCpuGroups[idxGroup].cMaxCpus = cMaxMembers;
|
---|
| 276 | g_aRtMpWinCpuGroups[idxGroup].cActiveCpus = 0;
|
---|
| 277 | for (uint32_t idxMember = 0; idxMember < cMaxMembers; idxMember++)
|
---|
| 278 | {
|
---|
| 279 | if (idxCpuSetNextInactive >= idxCpu)
|
---|
| 280 | {
|
---|
| 281 | g_aRtMpWinCpuGroups[idxGroup].aidxCpuSetMembers[idxMember] = idxCpuSetNextInactive;
|
---|
| 282 | g_aidRtMpWinByCpuSetIdx[idxCpuSetNextInactive] = idxCpuSetNextInactive;
|
---|
| 283 | idxCpuSetNextInactive--;
|
---|
| 284 | }
|
---|
| 285 | cInactive++;
|
---|
| 286 | }
|
---|
[64220] | 287 | }
|
---|
[64281] | 288 | Assert(cActive + cInactive <= g_cRtMpWinMaxCpus);
|
---|
| 289 | Assert(idxCpu <= idxCpuSetNextInactive + 1);
|
---|
[64220] | 290 | }
|
---|
| 291 | else
|
---|
| 292 | {
|
---|
| 293 | /* Legacy: */
|
---|
| 294 | GetSystemInfo(&uBuf.SysInfo);
|
---|
[64281] | 295 | g_cRtMpWinMaxCpuGroups = 1;
|
---|
[64220] | 296 | g_cRtMpWinMaxCpus = uBuf.SysInfo.dwNumberOfProcessors;
|
---|
| 297 | g_aRtMpWinCpuGroups[0].cMaxCpus = uBuf.SysInfo.dwNumberOfProcessors;
|
---|
[64281] | 298 | g_aRtMpWinCpuGroups[0].cActiveCpus = uBuf.SysInfo.dwNumberOfProcessors;
|
---|
| 299 |
|
---|
| 300 | for (uint32_t idxMember = 0; idxMember < uBuf.SysInfo.dwNumberOfProcessors; idxMember++)
|
---|
| 301 | {
|
---|
| 302 | g_aRtMpWinCpuGroups[0].aidxCpuSetMembers[idxMember] = idxMember;
|
---|
| 303 | g_aidRtMpWinByCpuSetIdx[idxMember] = idxMember;
|
---|
| 304 | }
|
---|
[64220] | 305 | }
|
---|
| 306 |
|
---|
| 307 | AssertFatalMsg(g_cRtMpWinMaxCpus <= RTCPUSET_MAX_CPUS,
|
---|
| 308 | ("g_cRtMpWinMaxCpus=%u (%#x); RTCPUSET_MAX_CPUS=%u\n", g_cRtMpWinMaxCpus, g_cRtMpWinMaxCpus, RTCPUSET_MAX_CPUS));
|
---|
| 309 |
|
---|
| 310 | g_cbRtMpWinGrpRelBuf = sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)
|
---|
| 311 | + (g_cRtMpWinMaxCpuGroups + 2) * sizeof(PROCESSOR_GROUP_INFO);
|
---|
| 312 |
|
---|
| 313 | /*
|
---|
| 314 | * Get information about cores.
|
---|
| 315 | *
|
---|
| 316 | * Note! This will only give us info about active processors according to
|
---|
| 317 | * MSDN, we'll just have to hope that CPUs aren't hotplugged after we
|
---|
| 318 | * initialize here (or that the API consumers doesn't care too much).
|
---|
| 319 | */
|
---|
| 320 | /** @todo A hot CPU plug event would be nice. */
|
---|
| 321 | g_cRtMpWinMaxCpuCores = g_cRtMpWinMaxCpus;
|
---|
| 322 | if (g_pfnGetLogicalProcessorInformationEx)
|
---|
| 323 | {
|
---|
| 324 | /* Query the information. */
|
---|
| 325 | DWORD cbData = sizeof(uBuf);
|
---|
| 326 | AssertFatalMsg(g_pfnGetLogicalProcessorInformationEx(RelationProcessorCore, &uBuf.Info, &cbData) != FALSE,
|
---|
| 327 | ("last error = %u, cbData = %u (in %u)\n", GetLastError(), cbData, sizeof(uBuf)));
|
---|
| 328 | g_cRtMpWinMaxCpuCores = 0;
|
---|
| 329 | for (uint32_t off = 0; off < cbData; )
|
---|
| 330 | {
|
---|
| 331 | SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *pCur = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)&uBuf.abPaddingG[off];
|
---|
| 332 | AssertFatalMsg(pCur->Relationship == RelationProcessorCore,
|
---|
| 333 | ("off = %#x, Relationship = %u, expected %u!\n", off, pCur->Relationship, RelationProcessorCore));
|
---|
| 334 | g_cRtMpWinMaxCpuCores++;
|
---|
| 335 | off += pCur->Size;
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | #if ARCH_BITS == 32
|
---|
| 339 | if (g_cRtMpWinMaxCpuCores > g_cRtMpWinMaxCpus)
|
---|
| 340 | {
|
---|
| 341 | /** @todo WOW64 trouble where the emulation environment has folded the high
|
---|
| 342 | * processor masks (63..32) into the low (31..0), hiding some
|
---|
| 343 | * processors from us. Currently we don't deal with that. */
|
---|
| 344 | g_cRtMpWinMaxCpuCores = g_cRtMpWinMaxCpus;
|
---|
| 345 | }
|
---|
| 346 | else
|
---|
| 347 | AssertStmt(g_cRtMpWinMaxCpuCores > 0, g_cRtMpWinMaxCpuCores = g_cRtMpWinMaxCpus);
|
---|
| 348 | #else
|
---|
| 349 | AssertStmt(g_cRtMpWinMaxCpuCores > 0 && g_cRtMpWinMaxCpuCores <= g_cRtMpWinMaxCpus,
|
---|
| 350 | g_cRtMpWinMaxCpuCores = g_cRtMpWinMaxCpus);
|
---|
| 351 | #endif
|
---|
| 352 | }
|
---|
| 353 | else
|
---|
| 354 | {
|
---|
| 355 | /*
|
---|
| 356 | * Sadly, on XP and Server 2003, even if the API is present, it does not tell us
|
---|
| 357 | * how many physical cores there are (any package will look like a single core).
|
---|
| 358 | * That is worse than not using the API at all, so just skip it unless it's Vista+.
|
---|
| 359 | */
|
---|
| 360 | if ( g_pfnGetLogicalProcessorInformation
|
---|
| 361 | && g_WinOsInfoEx.dwPlatformId == VER_PLATFORM_WIN32_NT
|
---|
| 362 | && g_WinOsInfoEx.dwMajorVersion >= 6)
|
---|
| 363 | {
|
---|
| 364 | /* Query the info. */
|
---|
| 365 | DWORD cbSysProcInfo = _4K;
|
---|
| 366 | PSYSTEM_LOGICAL_PROCESSOR_INFORMATION paSysInfo = NULL;
|
---|
| 367 | BOOL fRc = FALSE;
|
---|
| 368 | do
|
---|
| 369 | {
|
---|
| 370 | cbSysProcInfo = RT_ALIGN_32(cbSysProcInfo, 256);
|
---|
| 371 | void *pv = RTMemRealloc(paSysInfo, cbSysProcInfo);
|
---|
| 372 | if (!pv)
|
---|
| 373 | break;
|
---|
| 374 | paSysInfo = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)pv;
|
---|
| 375 | fRc = g_pfnGetLogicalProcessorInformation(paSysInfo, &cbSysProcInfo);
|
---|
| 376 | } while (!fRc && GetLastError() == ERROR_INSUFFICIENT_BUFFER);
|
---|
| 377 | if (fRc)
|
---|
| 378 | {
|
---|
| 379 | /* Count the cores in the result. */
|
---|
| 380 | g_cRtMpWinMaxCpuCores = 0;
|
---|
| 381 | uint32_t i = cbSysProcInfo / sizeof(paSysInfo[0]);
|
---|
| 382 | while (i-- > 0)
|
---|
| 383 | if (paSysInfo[i].Relationship == RelationProcessorCore)
|
---|
| 384 | g_cRtMpWinMaxCpuCores++;
|
---|
| 385 |
|
---|
| 386 | AssertStmt(g_cRtMpWinMaxCpuCores > 0 && g_cRtMpWinMaxCpuCores <= g_cRtMpWinMaxCpus,
|
---|
| 387 | g_cRtMpWinMaxCpuCores = g_cRtMpWinMaxCpus);
|
---|
| 388 | }
|
---|
| 389 | RTMemFree(paSysInfo);
|
---|
| 390 | }
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | return VINF_SUCCESS;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 |
|
---|
[64281] | 397 | #ifdef IPRT_WITH_GIP_MP_INFO
|
---|
| 398 | /**
|
---|
| 399 | * @callback_method_impl{FNRTONCE, Updates globals with information from GIP.}
|
---|
| 400 | */
|
---|
| 401 | static DECLCALLBACK(int32_t) rtMpWinInitOnceGip(void *pvUser)
|
---|
[64220] | 402 | {
|
---|
[64281] | 403 | RT_NOREF(pvUser);
|
---|
[64220] | 404 | RTOnce(&g_MpInitOnce, rtMpWinInitOnce, NULL);
|
---|
| 405 |
|
---|
[64281] | 406 | PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
|
---|
| 407 | if ( pGip
|
---|
| 408 | && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC)
|
---|
[64220] | 409 | {
|
---|
[64281] | 410 | /*
|
---|
| 411 | * Update globals.
|
---|
| 412 | */
|
---|
| 413 | if (g_cRtMpWinMaxCpus != pGip->cPossibleCpus)
|
---|
| 414 | g_cRtMpWinMaxCpus = pGip->cPossibleCpus;
|
---|
| 415 | if (g_cRtMpWinActiveCpus != pGip->cOnlineCpus)
|
---|
| 416 | g_cRtMpWinActiveCpus = pGip->cOnlineCpus;
|
---|
| 417 | Assert(g_cRtMpWinMaxCpuGroups == pGip->cPossibleCpuGroups);
|
---|
| 418 | if (g_cRtMpWinMaxCpuGroups != pGip->cPossibleCpuGroups)
|
---|
| 419 | {
|
---|
| 420 | g_cRtMpWinMaxCpuGroups = pGip->cPossibleCpuGroups;
|
---|
| 421 | g_cbRtMpWinGrpRelBuf = sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)
|
---|
| 422 | + (g_cRtMpWinMaxCpuGroups + 2) * sizeof(PROCESSOR_GROUP_INFO);
|
---|
| 423 | }
|
---|
| 424 |
|
---|
| 425 | /*
|
---|
| 426 | * Update CPU set IDs.
|
---|
| 427 | */
|
---|
| 428 | for (unsigned i = g_cRtMpWinMaxCpus; i < RT_ELEMENTS(g_aidRtMpWinByCpuSetIdx); i++)
|
---|
| 429 | g_aidRtMpWinByCpuSetIdx[i] = NIL_RTCPUID;
|
---|
| 430 |
|
---|
| 431 | unsigned const cbGip = pGip->cPages * PAGE_SIZE;
|
---|
[81114] | 432 | for (uint32_t idxGroup = 0; idxGroup < g_cRtMpWinMaxCpuGroups; idxGroup++)
|
---|
[64281] | 433 | {
|
---|
| 434 | uint32_t idxMember;
|
---|
[81106] | 435 | uint32_t offCpuGroup = pGip->aoffCpuGroup[idxGroup];
|
---|
[64281] | 436 | if (offCpuGroup < cbGip)
|
---|
| 437 | {
|
---|
| 438 | PSUPGIPCPUGROUP pGipCpuGrp = (PSUPGIPCPUGROUP)((uintptr_t)pGip + offCpuGroup);
|
---|
| 439 | uint32_t cMaxMembers = pGipCpuGrp->cMaxMembers;
|
---|
[81114] | 440 | AssertStmt(cMaxMembers <= RT_ELEMENTS(g_aRtMpWinCpuGroups[0].aidxCpuSetMembers),
|
---|
| 441 | cMaxMembers = RT_ELEMENTS(g_aRtMpWinCpuGroups[0].aidxCpuSetMembers));
|
---|
[64281] | 442 | g_aRtMpWinCpuGroups[idxGroup].cMaxCpus = cMaxMembers;
|
---|
| 443 | g_aRtMpWinCpuGroups[idxGroup].cActiveCpus = RT_MIN(pGipCpuGrp->cMembers, cMaxMembers);
|
---|
| 444 |
|
---|
| 445 | for (idxMember = 0; idxMember < cMaxMembers; idxMember++)
|
---|
| 446 | {
|
---|
| 447 | int16_t idxSet = pGipCpuGrp->aiCpuSetIdxs[idxMember];
|
---|
| 448 | g_aRtMpWinCpuGroups[idxGroup].aidxCpuSetMembers[idxMember] = idxSet;
|
---|
| 449 | if ((unsigned)idxSet < RT_ELEMENTS(g_aidRtMpWinByCpuSetIdx))
|
---|
| 450 | # ifdef IPRT_WITH_RTCPUID_AS_GROUP_AND_NUMBER
|
---|
| 451 | g_aidRtMpWinByCpuSetIdx[idxSet] = RTMPCPUID_FROM_GROUP_AND_NUMBER(idxGroup, idxMember);
|
---|
| 452 | # else
|
---|
| 453 | g_aidRtMpWinByCpuSetIdx[idxSet] = idxSet;
|
---|
| 454 | # endif
|
---|
| 455 | }
|
---|
| 456 | }
|
---|
| 457 | else
|
---|
| 458 | idxMember = 0;
|
---|
| 459 | for (; idxMember < RT_ELEMENTS(g_aRtMpWinCpuGroups[0].aidxCpuSetMembers); idxMember++)
|
---|
| 460 | g_aRtMpWinCpuGroups[idxGroup].aidxCpuSetMembers[idxMember] = -1;
|
---|
| 461 | }
|
---|
[64220] | 462 | }
|
---|
| 463 |
|
---|
[64281] | 464 | return VINF_SUCCESS;
|
---|
| 465 | }
|
---|
| 466 |
|
---|
| 467 |
|
---|
| 468 | /**
|
---|
| 469 | * Refreshes globals from GIP after one or more CPUs were added.
|
---|
| 470 | *
|
---|
| 471 | * There are potential races here. We might race other threads and we may race
|
---|
| 472 | * more CPUs being added.
|
---|
| 473 | */
|
---|
| 474 | static void rtMpWinRefreshGip(void)
|
---|
| 475 | {
|
---|
| 476 | PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
|
---|
| 477 | if ( pGip
|
---|
| 478 | && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC)
|
---|
[64220] | 479 | {
|
---|
[64281] | 480 | /*
|
---|
| 481 | * Since CPUs cannot be removed, we only have to update the IDs and
|
---|
| 482 | * indexes of CPUs that we think are inactive and the group member counts.
|
---|
| 483 | */
|
---|
| 484 | for (;;)
|
---|
| 485 | {
|
---|
| 486 | unsigned const cbGip = pGip->cPages * PAGE_SIZE;
|
---|
| 487 | uint32_t const cGipActiveCpus = pGip->cOnlineCpus;
|
---|
| 488 | uint32_t const cMyActiveCpus = ASMAtomicReadU32(&g_cRtMpWinActiveCpus);
|
---|
| 489 | ASMCompilerBarrier();
|
---|
| 490 |
|
---|
[81114] | 491 | for (uint32_t idxGroup = 0; idxGroup < g_cRtMpWinMaxCpuGroups; idxGroup++)
|
---|
[64281] | 492 | {
|
---|
[81106] | 493 | uint32_t offCpuGroup = pGip->aoffCpuGroup[idxGroup];
|
---|
[64281] | 494 | if (offCpuGroup < cbGip)
|
---|
| 495 | {
|
---|
| 496 | PSUPGIPCPUGROUP pGipCpuGrp = (PSUPGIPCPUGROUP)((uintptr_t)pGip + offCpuGroup);
|
---|
| 497 | uint32_t cMaxMembers = pGipCpuGrp->cMaxMembers;
|
---|
[81114] | 498 | AssertStmt(cMaxMembers <= RT_ELEMENTS(g_aRtMpWinCpuGroups[0].aidxCpuSetMembers),
|
---|
| 499 | cMaxMembers = RT_ELEMENTS(g_aRtMpWinCpuGroups[0].aidxCpuSetMembers));
|
---|
[64281] | 500 | for (uint32_t idxMember = g_aRtMpWinCpuGroups[idxGroup].cActiveCpus; idxMember < cMaxMembers; idxMember++)
|
---|
| 501 | {
|
---|
| 502 | int16_t idxSet = pGipCpuGrp->aiCpuSetIdxs[idxMember];
|
---|
| 503 | g_aRtMpWinCpuGroups[idxGroup].aidxCpuSetMembers[idxMember] = idxSet;
|
---|
| 504 | if ((unsigned)idxSet < RT_ELEMENTS(g_aidRtMpWinByCpuSetIdx))
|
---|
| 505 | # ifdef IPRT_WITH_RTCPUID_AS_GROUP_AND_NUMBER
|
---|
| 506 | g_aidRtMpWinByCpuSetIdx[idxSet] = RTMPCPUID_FROM_GROUP_AND_NUMBER(idxGroup, idxMember);
|
---|
| 507 | # else
|
---|
| 508 | g_aidRtMpWinByCpuSetIdx[idxSet] = idxSet;
|
---|
| 509 | # endif
|
---|
| 510 | }
|
---|
| 511 | g_aRtMpWinCpuGroups[idxGroup].cMaxCpus = RT_MIN(pGipCpuGrp->cMembers, cMaxMembers);
|
---|
| 512 | g_aRtMpWinCpuGroups[idxGroup].cActiveCpus = RT_MIN(pGipCpuGrp->cMembers, cMaxMembers);
|
---|
| 513 | }
|
---|
| 514 | else
|
---|
| 515 | Assert(g_aRtMpWinCpuGroups[idxGroup].cActiveCpus == 0);
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | ASMCompilerBarrier();
|
---|
| 519 | if (cGipActiveCpus == pGip->cOnlineCpus)
|
---|
| 520 | if (ASMAtomicCmpXchgU32(&g_cRtMpWinActiveCpus, cGipActiveCpus, cMyActiveCpus))
|
---|
| 521 | break;
|
---|
| 522 | }
|
---|
[64220] | 523 | }
|
---|
| 524 | }
|
---|
| 525 |
|
---|
[64281] | 526 | #endif /* IPRT_WITH_GIP_MP_INFO */
|
---|
[64220] | 527 |
|
---|
[64281] | 528 |
|
---|
| 529 | /*
|
---|
| 530 | * Conversion between CPU ID and set index.
|
---|
| 531 | */
|
---|
| 532 |
|
---|
[7362] | 533 | RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
|
---|
[1] | 534 | {
|
---|
[64220] | 535 | RTOnce(&g_MpInitOnce, rtMpWinInitOnce, NULL);
|
---|
[64293] | 536 | RTMPWIN_UPDATE_GIP_GLOBALS();
|
---|
[64220] | 537 |
|
---|
[64281] | 538 | #ifdef IPRT_WITH_RTCPUID_AS_GROUP_AND_NUMBER
|
---|
| 539 | if (idCpu != NIL_RTCPUID)
|
---|
| 540 | return RTMpSetIndexFromCpuGroupMember(rtMpCpuIdGetGroup(idCpu), rtMpCpuIdGetGroupMember(idCpu));
|
---|
| 541 | return -1;
|
---|
| 542 |
|
---|
| 543 | #else
|
---|
[64220] | 544 | /* 1:1 mapping, just do range checking. */
|
---|
| 545 | return idCpu < g_cRtMpWinMaxCpus ? idCpu : -1;
|
---|
[64281] | 546 | #endif
|
---|
[7362] | 547 | }
|
---|
[1] | 548 |
|
---|
| 549 |
|
---|
[7362] | 550 | RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
|
---|
| 551 | {
|
---|
[64220] | 552 | RTOnce(&g_MpInitOnce, rtMpWinInitOnce, NULL);
|
---|
[64293] | 553 | RTMPWIN_UPDATE_GIP_GLOBALS();
|
---|
[64220] | 554 |
|
---|
[64281] | 555 | if ((unsigned)iCpu < RT_ELEMENTS(g_aidRtMpWinByCpuSetIdx))
|
---|
| 556 | {
|
---|
| 557 | RTCPUID idCpu = g_aidRtMpWinByCpuSetIdx[iCpu];
|
---|
| 558 |
|
---|
| 559 | #if defined(IPRT_WITH_RTCPUID_AS_GROUP_AND_NUMBER) && defined(RT_STRICT)
|
---|
| 560 | /* Check the correctness of the mapping table. */
|
---|
| 561 | RTCPUID idCpuGip = NIL_RTCPUID;
|
---|
| 562 | if ( pGip
|
---|
| 563 | && (unsigned)iCpu < RT_ELEMENTS(pGip->aiCpuFromCpuSetIdx))
|
---|
| 564 | {
|
---|
| 565 | unsigned idxSupCpu = pGip->aiCpuFromCpuSetIdx[idxGuess];
|
---|
| 566 | if (idxSupCpu < pGip->cCpus)
|
---|
| 567 | if (pGip->aCPUs[idxSupCpu].enmState != SUPGIPCPUSTATE_INVALID)
|
---|
| 568 | idCpuGip = pGip->aCPUs[idxSupCpu].idCpu;
|
---|
| 569 | }
|
---|
| 570 | AssertMsg(idCpu == idCpuGip, ("table:%#x gip:%#x\n", idCpu, idCpuGip));
|
---|
| 571 | #endif
|
---|
| 572 |
|
---|
| 573 | return idCpu;
|
---|
| 574 | }
|
---|
| 575 | return NIL_RTCPUID;
|
---|
[1] | 576 | }
|
---|
| 577 |
|
---|
| 578 |
|
---|
[64281] | 579 | RTDECL(int) RTMpSetIndexFromCpuGroupMember(uint32_t idxGroup, uint32_t idxMember)
|
---|
[1] | 580 | {
|
---|
[64293] | 581 | RTOnce(&g_MpInitOnce, rtMpWinInitOnce, NULL);
|
---|
| 582 | RTMPWIN_UPDATE_GIP_GLOBALS();
|
---|
| 583 |
|
---|
[64281] | 584 | if (idxGroup < g_cRtMpWinMaxCpuGroups)
|
---|
| 585 | if (idxMember < g_aRtMpWinCpuGroups[idxGroup].cMaxCpus)
|
---|
| 586 | return g_aRtMpWinCpuGroups[idxGroup].aidxCpuSetMembers[idxMember];
|
---|
| 587 | return -1;
|
---|
| 588 | }
|
---|
| 589 |
|
---|
| 590 |
|
---|
| 591 | RTDECL(uint32_t) RTMpGetCpuGroupCounts(uint32_t idxGroup, uint32_t *pcActive)
|
---|
| 592 | {
|
---|
[64293] | 593 | RTOnce(&g_MpInitOnce, rtMpWinInitOnce, NULL);
|
---|
| 594 | RTMPWIN_UPDATE_GIP_GLOBALS();
|
---|
| 595 |
|
---|
[64281] | 596 | if (idxGroup < g_cRtMpWinMaxCpuGroups)
|
---|
| 597 | {
|
---|
| 598 | if (pcActive)
|
---|
| 599 | *pcActive = g_aRtMpWinCpuGroups[idxGroup].cActiveCpus;
|
---|
| 600 | return g_aRtMpWinCpuGroups[idxGroup].cMaxCpus;
|
---|
| 601 | }
|
---|
| 602 | if (pcActive)
|
---|
| 603 | *pcActive = 0;
|
---|
| 604 | return 0;
|
---|
| 605 | }
|
---|
| 606 |
|
---|
| 607 |
|
---|
| 608 | RTDECL(uint32_t) RTMpGetMaxCpuGroupCount(void)
|
---|
| 609 | {
|
---|
[64293] | 610 | RTOnce(&g_MpInitOnce, rtMpWinInitOnce, NULL);
|
---|
| 611 | RTMPWIN_UPDATE_GIP_GLOBALS();
|
---|
| 612 |
|
---|
[64281] | 613 | return g_cRtMpWinMaxCpuGroups;
|
---|
| 614 | }
|
---|
| 615 |
|
---|
| 616 |
|
---|
| 617 |
|
---|
| 618 | /*
|
---|
| 619 | * Get current CPU.
|
---|
| 620 | */
|
---|
| 621 |
|
---|
| 622 | RTDECL(RTCPUID) RTMpCpuId(void)
|
---|
| 623 | {
|
---|
[64220] | 624 | RTOnce(&g_MpInitOnce, rtMpWinInitOnce, NULL);
|
---|
[64293] | 625 | RTMPWIN_UPDATE_GIP_GLOBALS();
|
---|
[64281] | 626 |
|
---|
| 627 | PROCESSOR_NUMBER ProcNum;
|
---|
| 628 | ProcNum.Group = 0;
|
---|
| 629 | ProcNum.Number = 0xff;
|
---|
| 630 | if (g_pfnGetCurrentProcessorNumberEx)
|
---|
| 631 | g_pfnGetCurrentProcessorNumberEx(&ProcNum);
|
---|
| 632 | else if (g_pfnGetCurrentProcessorNumber)
|
---|
| 633 | {
|
---|
| 634 | DWORD iCpu = g_pfnGetCurrentProcessorNumber();
|
---|
| 635 | Assert(iCpu < g_cRtMpWinMaxCpus);
|
---|
| 636 | ProcNum.Number = iCpu;
|
---|
| 637 | }
|
---|
| 638 | else
|
---|
| 639 | {
|
---|
| 640 | #if defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64)
|
---|
| 641 | ProcNum.Number = ASMGetApicId();
|
---|
| 642 | #else
|
---|
| 643 | # error "Not ported to this architecture."
|
---|
| 644 | return NIL_RTCPUID;
|
---|
| 645 | #endif
|
---|
| 646 | }
|
---|
| 647 |
|
---|
| 648 | #ifdef IPRT_WITH_RTCPUID_AS_GROUP_AND_NUMBER
|
---|
| 649 | return RTMPCPUID_FROM_GROUP_AND_NUMBER(ProcNum.Group, ProcNum.Number);
|
---|
| 650 | #else
|
---|
| 651 | return RTMpSetIndexFromCpuGroupMember(ProcNum.Group, ProcNum.Number);
|
---|
| 652 | #endif
|
---|
[7362] | 653 | }
|
---|
| 654 |
|
---|
| 655 |
|
---|
[64281] | 656 | /*
|
---|
| 657 | * Possible CPUs and cores.
|
---|
| 658 | */
|
---|
| 659 |
|
---|
| 660 | RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
|
---|
[7362] | 661 | {
|
---|
[64220] | 662 | RTOnce(&g_MpInitOnce, rtMpWinInitOnce, NULL);
|
---|
[64293] | 663 | RTMPWIN_UPDATE_GIP_GLOBALS();
|
---|
| 664 |
|
---|
[64281] | 665 | #ifdef IPRT_WITH_RTCPUID_AS_GROUP_AND_NUMBER
|
---|
| 666 | return RTMPCPUID_FROM_GROUP_AND_NUMBER(g_cRtMpWinMaxCpuGroups - 1,
|
---|
| 667 | g_aRtMpWinCpuGroups[g_cRtMpWinMaxCpuGroups - 1].cMaxCpus - 1);
|
---|
| 668 | #else
|
---|
| 669 | return g_cRtMpWinMaxCpus - 1;
|
---|
| 670 | #endif
|
---|
[7362] | 671 | }
|
---|
| 672 |
|
---|
| 673 |
|
---|
[9429] | 674 | RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
|
---|
[7362] | 675 | {
|
---|
[64220] | 676 | RTOnce(&g_MpInitOnce, rtMpWinInitOnce, NULL);
|
---|
[64293] | 677 | RTMPWIN_UPDATE_GIP_GLOBALS();
|
---|
[64281] | 678 |
|
---|
[64220] | 679 | /* Any CPU between 0 and g_cRtMpWinMaxCpus are possible. */
|
---|
| 680 | return idCpu < g_cRtMpWinMaxCpus;
|
---|
[7362] | 681 | }
|
---|
| 682 |
|
---|
| 683 |
|
---|
| 684 | RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
|
---|
| 685 | {
|
---|
[64281] | 686 | RTCPUID iCpu = RTMpGetCount();
|
---|
[7362] | 687 | RTCpuSetEmpty(pSet);
|
---|
[64281] | 688 | while (iCpu-- > 0)
|
---|
| 689 | RTCpuSetAddByIndex(pSet, iCpu);
|
---|
[7362] | 690 | return pSet;
|
---|
| 691 | }
|
---|
| 692 |
|
---|
| 693 |
|
---|
| 694 | RTDECL(RTCPUID) RTMpGetCount(void)
|
---|
| 695 | {
|
---|
[64220] | 696 | RTOnce(&g_MpInitOnce, rtMpWinInitOnce, NULL);
|
---|
[64293] | 697 | RTMPWIN_UPDATE_GIP_GLOBALS();
|
---|
[64281] | 698 |
|
---|
[64220] | 699 | return g_cRtMpWinMaxCpus;
|
---|
[7362] | 700 | }
|
---|
[1] | 701 |
|
---|
[46635] | 702 |
|
---|
[46616] | 703 | RTDECL(RTCPUID) RTMpGetCoreCount(void)
|
---|
| 704 | {
|
---|
[64220] | 705 | RTOnce(&g_MpInitOnce, rtMpWinInitOnce, NULL);
|
---|
[64293] | 706 | RTMPWIN_UPDATE_GIP_GLOBALS();
|
---|
[64281] | 707 |
|
---|
[64220] | 708 | return g_cRtMpWinMaxCpuCores;
|
---|
| 709 | }
|
---|
[7362] | 710 |
|
---|
[54931] | 711 |
|
---|
[64281] | 712 | /*
|
---|
| 713 | * Online CPUs and cores.
|
---|
| 714 | */
|
---|
| 715 |
|
---|
[64220] | 716 | RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
|
---|
| 717 | {
|
---|
| 718 | RTOnce(&g_MpInitOnce, rtMpWinInitOnce, NULL);
|
---|
| 719 |
|
---|
[64281] | 720 | #ifdef IPRT_WITH_GIP_MP_INFO
|
---|
| 721 | RTMPWIN_UPDATE_GIP_GLOBALS_AND_GET_PGIP();
|
---|
| 722 | if (pGip)
|
---|
| 723 | {
|
---|
| 724 | *pSet = pGip->OnlineCpuSet;
|
---|
| 725 | return pSet;
|
---|
| 726 | }
|
---|
| 727 | #endif
|
---|
| 728 |
|
---|
[64220] | 729 | if (g_pfnGetLogicalProcessorInformationEx)
|
---|
[46616] | 730 | {
|
---|
[46635] | 731 | /*
|
---|
[64220] | 732 | * Get the group relation info.
|
---|
| 733 | *
|
---|
| 734 | * In addition to the ASSUMPTIONS that are documented in rtMpWinInitOnce,
|
---|
| 735 | * we ASSUME that PROCESSOR_GROUP_INFO::MaximumProcessorCount gives the
|
---|
| 736 | * active processor mask width.
|
---|
[46635] | 737 | */
|
---|
[64281] | 738 | /** @todo this is not correct for WOW64 */
|
---|
[64220] | 739 | DWORD cbInfo = g_cbRtMpWinGrpRelBuf;
|
---|
| 740 | SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *pInfo = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)alloca(cbInfo);
|
---|
| 741 | AssertFatalMsg(g_pfnGetLogicalProcessorInformationEx(RelationGroup, pInfo, &cbInfo) != FALSE,
|
---|
| 742 | ("last error = %u, cbInfo = %u (in %u)\n", GetLastError(), cbInfo, g_cbRtMpWinGrpRelBuf));
|
---|
| 743 | AssertFatalMsg(pInfo->Relationship == RelationGroup,
|
---|
| 744 | ("Relationship = %u, expected %u!\n", pInfo->Relationship, RelationGroup));
|
---|
| 745 | AssertFatalMsg(pInfo->Group.MaximumGroupCount == g_cRtMpWinMaxCpuGroups,
|
---|
| 746 | ("MaximumGroupCount is %u, expected %u!\n", pInfo->Group.MaximumGroupCount, g_cRtMpWinMaxCpuGroups));
|
---|
| 747 |
|
---|
| 748 | RTCpuSetEmpty(pSet);
|
---|
| 749 | for (uint32_t idxGroup = 0; idxGroup < pInfo->Group.MaximumGroupCount; idxGroup++)
|
---|
[46635] | 750 | {
|
---|
[64220] | 751 | Assert(pInfo->Group.GroupInfo[idxGroup].MaximumProcessorCount == g_aRtMpWinCpuGroups[idxGroup].cMaxCpus);
|
---|
| 752 | Assert(pInfo->Group.GroupInfo[idxGroup].ActiveProcessorCount <= g_aRtMpWinCpuGroups[idxGroup].cMaxCpus);
|
---|
[46635] | 753 |
|
---|
[64220] | 754 | KAFFINITY fActive = pInfo->Group.GroupInfo[idxGroup].ActiveProcessorMask;
|
---|
| 755 | if (fActive != 0)
|
---|
| 756 | {
|
---|
| 757 | #ifdef RT_STRICT
|
---|
| 758 | uint32_t cMembersLeft = pInfo->Group.GroupInfo[idxGroup].ActiveProcessorCount;
|
---|
| 759 | #endif
|
---|
| 760 | int const cMembers = g_aRtMpWinCpuGroups[idxGroup].cMaxCpus;
|
---|
| 761 | for (int idxMember = 0; idxMember < cMembers; idxMember++)
|
---|
| 762 | {
|
---|
| 763 | if (fActive & 1)
|
---|
| 764 | {
|
---|
| 765 | #ifdef RT_STRICT
|
---|
| 766 | cMembersLeft--;
|
---|
| 767 | #endif
|
---|
[64281] | 768 | RTCpuSetAddByIndex(pSet, g_aRtMpWinCpuGroups[idxGroup].aidxCpuSetMembers[idxMember]);
|
---|
[64220] | 769 | fActive >>= 1;
|
---|
| 770 | if (!fActive)
|
---|
| 771 | break;
|
---|
| 772 | }
|
---|
| 773 | else
|
---|
| 774 | {
|
---|
| 775 | fActive >>= 1;
|
---|
| 776 | }
|
---|
| 777 | }
|
---|
| 778 | Assert(cMembersLeft == 0);
|
---|
| 779 | }
|
---|
| 780 | else
|
---|
| 781 | Assert(pInfo->Group.GroupInfo[idxGroup].ActiveProcessorCount == 0);
|
---|
[46616] | 782 | }
|
---|
[46635] | 783 |
|
---|
[64220] | 784 | return pSet;
|
---|
[46616] | 785 | }
|
---|
[46635] | 786 |
|
---|
[64220] | 787 | /*
|
---|
| 788 | * Legacy fallback code.
|
---|
| 789 | */
|
---|
[7362] | 790 | SYSTEM_INFO SysInfo;
|
---|
[1] | 791 | GetSystemInfo(&SysInfo);
|
---|
[7362] | 792 | return RTCpuSetFromU64(pSet, SysInfo.dwActiveProcessorMask);
|
---|
| 793 | }
|
---|
[1] | 794 |
|
---|
[7362] | 795 |
|
---|
[64281] | 796 | RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
|
---|
| 797 | {
|
---|
| 798 | RTCPUSET Set;
|
---|
| 799 | return RTCpuSetIsMember(RTMpGetOnlineSet(&Set), idCpu);
|
---|
| 800 | }
|
---|
| 801 |
|
---|
| 802 |
|
---|
[7362] | 803 | RTDECL(RTCPUID) RTMpGetOnlineCount(void)
|
---|
| 804 | {
|
---|
[64281] | 805 | #ifdef IPRT_WITH_GIP_MP_INFO
|
---|
| 806 | RTMPWIN_UPDATE_GIP_GLOBALS_AND_GET_PGIP();
|
---|
| 807 | if (pGip)
|
---|
| 808 | return pGip->cOnlineCpus;
|
---|
| 809 | #endif
|
---|
| 810 |
|
---|
[7362] | 811 | RTCPUSET Set;
|
---|
| 812 | RTMpGetOnlineSet(&Set);
|
---|
| 813 | return RTCpuSetCount(&Set);
|
---|
[1] | 814 | }
|
---|
| 815 |
|
---|
[46639] | 816 |
|
---|
| 817 | RTDECL(RTCPUID) RTMpGetOnlineCoreCount(void)
|
---|
| 818 | {
|
---|
[64220] | 819 | /** @todo this isn't entirely correct, but whatever. */
|
---|
[46639] | 820 | return RTMpGetCoreCount();
|
---|
| 821 | }
|
---|
| 822 |
|
---|