VirtualBox

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

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

Runtime/r3/mp-win, Runtime/Makefile: Support for getting physical core count for Windows.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.5 KB
Line 
1/* $Id: mp-win.cpp 46616 2013-06-18 10:48:34Z 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#include <iprt/mp.h>
34#include <iprt/cpuset.h>
35#include <iprt/assert.h>
36
37
38AssertCompile(MAXIMUM_PROCESSORS <= RTCPUSET_MAX_CPUS);
39
40
41/** @todo RTmpCpuId(). */
42
43RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
44{
45 return idCpu < MAXIMUM_PROCESSORS ? idCpu : -1;
46}
47
48
49RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
50{
51 return (unsigned)iCpu < MAXIMUM_PROCESSORS ? iCpu : NIL_RTCPUID;
52}
53
54
55RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
56{
57 return MAXIMUM_PROCESSORS - 1;
58}
59
60
61RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
62{
63 RTCPUSET Set;
64 return RTCpuSetIsMember(RTMpGetOnlineSet(&Set), idCpu);
65}
66
67
68RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
69{
70 RTCPUSET Set;
71 return RTCpuSetIsMember(RTMpGetSet(&Set), idCpu);
72}
73
74
75RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
76{
77 RTCPUID idCpu = RTMpGetCount();
78 RTCpuSetEmpty(pSet);
79 while (idCpu-- > 0)
80 RTCpuSetAdd(pSet, idCpu);
81 return pSet;
82}
83
84
85RTDECL(RTCPUID) RTMpGetCount(void)
86{
87 SYSTEM_INFO SysInfo;
88 GetSystemInfo(&SysInfo);
89 Assert((RTCPUID)SysInfo.dwNumberOfProcessors == SysInfo.dwNumberOfProcessors);
90 return SysInfo.dwNumberOfProcessors;
91}
92
93RTDECL(RTCPUID) RTMpGetCoreCount(void)
94{
95 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pSysInfo = NULL;
96 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pSysInfoTmp = NULL;
97 DWORD sysProcInfoLen = 0;
98 DWORD offset = 0;
99 DWORD coreCount = 0;
100 BOOL rc;
101 BOOL (WINAPI *pfnGetLogicalProcInfo)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
102
103 pfnGetLogicalProcInfo = (BOOL (WINAPI *)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD))
104 GetProcAddress(GetModuleHandle("KERNEL32.DLL"), "GetLogicalProcessorInformation");
105 /* 0 represent error condition. Can't return VERR* error codes as caller expects a unsigned value of core count.*/
106 if (!pfnGetLogicalProcInfo)
107 return 0;
108
109 rc = pfnGetLogicalProcInfo(pSysInfo, &sysProcInfoLen);
110 if (rc == FALSE)
111 {
112 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
113 {
114 RTMemFree(pSysInfo);
115 pSysInfo = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)RTMemAlloc(sysProcInfoLen);
116 if (!pSysInfo)
117 return 0;
118 }
119 else
120 return 0;
121 }
122
123 rc = pfnGetLogicalProcInfo(pSysInfo, &sysProcInfoLen);
124 if (rc == FALSE)
125 {
126 RTMemFree(pSysInfo);
127 return 0;
128 }
129 pSysInfoTmp = pSysInfo;
130 while (offset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= sysProcInfoLen)
131 {
132 switch (pSysInfoTmp->Relationship)
133 {
134 case RelationProcessorCore:
135 coreCount++;
136 break;
137 case RelationCache:
138 case RelationNumaNode:
139 case RelationProcessorPackage:
140 default:
141 ;
142 }
143 offset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
144 pSysInfoTmp++;
145 }
146 RTMemFree(pSysInfo);
147 return coreCount;
148}
149
150RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
151{
152 SYSTEM_INFO SysInfo;
153 GetSystemInfo(&SysInfo);
154/** @todo port to W2K8 / W7 w/ > 64 CPUs & grouping. */
155 return RTCpuSetFromU64(pSet, SysInfo.dwActiveProcessorMask);
156}
157
158
159RTDECL(RTCPUID) RTMpGetOnlineCount(void)
160{
161 RTCPUSET Set;
162 RTMpGetOnlineSet(&Set);
163 return RTCpuSetCount(&Set);
164}
165
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