VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/mp-win.cpp@ 62514

Last change on this file since 62514 was 62477, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.6 KB
Line 
1/* $Id: mp-win.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
2/** @file
3 * IPRT - Multiprocessor, Windows.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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_SYSTEM
32#include <Windows.h>
33
34#include <iprt/mp.h>
35#include "internal/iprt.h"
36
37#include <iprt/assert.h>
38#include <iprt/cpuset.h>
39#include <iprt/ldr.h>
40#include <iprt/mem.h>
41
42
43AssertCompile(MAXIMUM_PROCESSORS <= RTCPUSET_MAX_CPUS);
44
45
46/** @todo RTmpCpuId(). */
47
48RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
49{
50 return idCpu < MAXIMUM_PROCESSORS ? idCpu : -1;
51}
52
53
54RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
55{
56 return (unsigned)iCpu < MAXIMUM_PROCESSORS ? iCpu : NIL_RTCPUID;
57}
58
59
60RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
61{
62 return MAXIMUM_PROCESSORS - 1;
63}
64
65
66RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
67{
68 RTCPUSET Set;
69 return RTCpuSetIsMember(RTMpGetOnlineSet(&Set), idCpu);
70}
71
72
73RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
74{
75 RTCPUSET Set;
76 return RTCpuSetIsMember(RTMpGetSet(&Set), idCpu);
77}
78
79
80RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
81{
82 RTCPUID idCpu = RTMpGetCount();
83 RTCpuSetEmpty(pSet);
84 while (idCpu-- > 0)
85 RTCpuSetAdd(pSet, idCpu);
86 return pSet;
87}
88
89
90RTDECL(RTCPUID) RTMpGetCount(void)
91{
92 SYSTEM_INFO SysInfo;
93 GetSystemInfo(&SysInfo);
94 Assert((RTCPUID)SysInfo.dwNumberOfProcessors == SysInfo.dwNumberOfProcessors);
95 return SysInfo.dwNumberOfProcessors;
96}
97
98
99RTDECL(RTCPUID) RTMpGetCoreCount(void)
100{
101 /*
102 * Resolve the API dynamically (one try) as it requires XP w/ sp3 or later.
103 */
104 typedef BOOL (WINAPI *PFNGETLOGICALPROCINFO)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
105 static PFNGETLOGICALPROCINFO s_pfnGetLogicalProcInfo = (PFNGETLOGICALPROCINFO)~(uintptr_t)0;
106 if (s_pfnGetLogicalProcInfo == (PFNGETLOGICALPROCINFO)~(uintptr_t)0)
107 s_pfnGetLogicalProcInfo = (PFNGETLOGICALPROCINFO)RTLdrGetSystemSymbol("kernel32.dll", "GetLogicalProcessorInformation");
108
109 /*
110 * Sadly, on XP and Server 2003, even if the API is present, it does not tell us
111 * how many physical cores there are (any package will look like a single core).
112 * That is worse than not using the API at all, so just skip it unless it's Vista+.
113 */
114 bool fIsVistaOrLater = false;
115 OSVERSIONINFOEX OSInfoEx = { 0 };
116 OSInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
117 if ( GetVersionEx((LPOSVERSIONINFO) &OSInfoEx)
118 && (OSInfoEx.dwPlatformId == VER_PLATFORM_WIN32_NT)
119 && (OSInfoEx.dwMajorVersion >= 6))
120 fIsVistaOrLater = true;
121
122 if (s_pfnGetLogicalProcInfo && fIsVistaOrLater)
123 {
124 /*
125 * Query the information. This unfortunately requires a buffer, so we
126 * start with a guess and let windows advice us if it's too small.
127 */
128 DWORD cbSysProcInfo = _4K;
129 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION paSysInfo = NULL;
130 BOOL fRc = FALSE;
131 do
132 {
133 cbSysProcInfo = RT_ALIGN_32(cbSysProcInfo, 256);
134 void *pv = RTMemRealloc(paSysInfo, cbSysProcInfo);
135 if (!pv)
136 break;
137 paSysInfo = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)pv;
138 fRc = s_pfnGetLogicalProcInfo(paSysInfo, &cbSysProcInfo);
139 } while (!fRc && GetLastError() == ERROR_INSUFFICIENT_BUFFER);
140 if (fRc)
141 {
142 /*
143 * Parse the result.
144 */
145 uint32_t cCores = 0;
146 uint32_t i = cbSysProcInfo / sizeof(paSysInfo[0]);
147 while (i-- > 0)
148 if (paSysInfo[i].Relationship == RelationProcessorCore)
149 cCores++;
150
151 RTMemFree(paSysInfo);
152 Assert(cCores > 0);
153 return cCores;
154 }
155
156 RTMemFree(paSysInfo);
157 }
158
159 /* If we don't have the necessary API or if it failed, return the same
160 value as the generic implementation. */
161 return RTMpGetCount();
162}
163
164
165RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
166{
167 SYSTEM_INFO SysInfo;
168 GetSystemInfo(&SysInfo);
169/** @todo port to W2K8 / W7 w/ > 64 CPUs & grouping. */
170 return RTCpuSetFromU64(pSet, SysInfo.dwActiveProcessorMask);
171}
172
173
174RTDECL(RTCPUID) RTMpGetOnlineCount(void)
175{
176 RTCPUSET Set;
177 RTMpGetOnlineSet(&Set);
178 return RTCpuSetCount(&Set);
179}
180
181
182RTDECL(RTCPUID) RTMpGetOnlineCoreCount(void)
183{
184 /** @todo this isn't entirely correct. */
185 return RTMpGetCoreCount();
186}
187
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