VirtualBox

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

Last change on this file since 54729 was 48935, checked in by vboxsync, 11 years ago

Runtime: Whitespace and svn:keyword cleanups by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.9 KB
Line 
1/* $Id: mp-win.cpp 48935 2013-10-07 21:19:37Z vboxsync $ */
2/** @file
3 * IPRT - Multiprocessor, Windows.
4 */
5
6/*
7 * Copyright (C) 2006-2011 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 if (s_pfnGetLogicalProcInfo)
110 {
111 /*
112 * Query the information. This unfortunately requires a buffer, so we
113 * start with a guess and let windows advice us if it's too small.
114 */
115 DWORD cbSysProcInfo = _4K;
116 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION paSysInfo = NULL;
117 BOOL fRc = FALSE;
118 do
119 {
120 cbSysProcInfo = RT_ALIGN_32(cbSysProcInfo, 256);
121 void *pv = RTMemRealloc(paSysInfo, cbSysProcInfo);
122 if (!pv)
123 break;
124 paSysInfo = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)pv;
125 fRc = s_pfnGetLogicalProcInfo(paSysInfo, &cbSysProcInfo);
126 } while (!fRc && GetLastError() == ERROR_INSUFFICIENT_BUFFER);
127 if (fRc)
128 {
129 /*
130 * Parse the result.
131 */
132 uint32_t cCores = 0;
133 uint32_t i = cbSysProcInfo / sizeof(paSysInfo[0]);
134 while (i-- > 0)
135 if (paSysInfo[i].Relationship == RelationProcessorCore)
136 cCores++;
137
138 RTMemFree(paSysInfo);
139 Assert(cCores > 0);
140 return cCores;
141 }
142
143 RTMemFree(paSysInfo);
144 }
145
146 /* If we don't have the necessary API or if it failed, return the same
147 value as the generic implementation. */
148 return RTMpGetCount();
149}
150
151
152RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
153{
154 SYSTEM_INFO SysInfo;
155 GetSystemInfo(&SysInfo);
156/** @todo port to W2K8 / W7 w/ > 64 CPUs & grouping. */
157 return RTCpuSetFromU64(pSet, SysInfo.dwActiveProcessorMask);
158}
159
160
161RTDECL(RTCPUID) RTMpGetOnlineCount(void)
162{
163 RTCPUSET Set;
164 RTMpGetOnlineSet(&Set);
165 return RTCpuSetCount(&Set);
166}
167
168
169RTDECL(RTCPUID) RTMpGetOnlineCoreCount(void)
170{
171 /** @todo this isn't entirely correct. */
172 return RTMpGetCoreCount();
173}
174
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette