VirtualBox

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

Last change on this file since 46618 was 46618, checked in by vboxsync, 12 years ago

Runtime/r3/mp-win: coding style

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