8
8
#endif
9
9
#include < wincrypt.h>
10
10
#include < VersionHelpers.h>
11
-
11
+ #ifdef __APPLE__
12
+ #include < sys/sysctl.h> // sysctl*
13
+ #elif defined(__linux__)
14
+ #include < unistd.h> // sysconf
15
+ #endif
12
16
// Initialization order
13
17
// AB AutoSystemInfo
14
18
// AD PerfCounter
@@ -119,9 +123,9 @@ AutoSystemInfo::Initialize()
119
123
bool
120
124
AutoSystemInfo::InitPhysicalProcessorCount ()
121
125
{
126
+ DWORD countPhysicalProcessor = 0 ;
122
127
#ifdef _WIN32
123
128
DWORD size = 0 ;
124
- DWORD countPhysicalProcessor = 0 ;
125
129
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pBufferCurrent;
126
130
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pBufferStart;
127
131
BOOL bResult;
@@ -133,8 +137,7 @@ AutoSystemInfo::InitPhysicalProcessorCount()
133
137
134
138
this ->dwNumberOfPhysicalProcessors = this ->dwNumberOfProcessors ;
135
139
136
- // xplat-todo: figure out #physical_cores
137
- #ifdef _WIN32
140
+ #if defined(_WIN32)
138
141
bResult = GetLogicalProcessorInformation (NULL , &size);
139
142
140
143
if (bResult || GetLastError () != ERROR_INSUFFICIENT_BUFFER || !size)
@@ -143,7 +146,6 @@ AutoSystemInfo::InitPhysicalProcessorCount()
143
146
}
144
147
145
148
DWORD count = (size) / sizeof (SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
146
-
147
149
if (size != count * sizeof (SYSTEM_LOGICAL_PROCESSOR_INFORMATION))
148
150
{
149
151
Assert (false );
@@ -172,9 +174,29 @@ AutoSystemInfo::InitPhysicalProcessorCount()
172
174
}
173
175
174
176
NoCheckHeapDeleteArray (count, pBufferStart);
177
+ #elif defined(__APPLE__)
178
+ std::size_t szCount = sizeof (countPhysicalProcessor);
179
+ sysctlbyname (" hw.physicalcpu" , &countPhysicalProcessor, &szCount, nullptr , 0 );
175
180
181
+ if (countPhysicalProcessor < 1 )
182
+ {
183
+ int nMIB[2 ] = {CTL_HW, HW_NCPU}; // fallback. Depracated on latest OS
184
+ sysctl (nMIB, 2 , &countPhysicalProcessor, &szCount, nullptr , 0 );
185
+ if (countPhysicalProcessor < 1 )
186
+ {
187
+ countPhysicalProcessor = 1 ;
188
+ }
189
+ }
190
+ #elif defined(__linux__)
191
+ countPhysicalProcessor = sysconf (_SC_NPROCESSORS_ONLN);
192
+ #else
193
+ // implementation for __linux__ should work for some others.
194
+ // same applies to __APPLE__ implementation
195
+ // instead of reimplementing, add corresponding preprocessors above
196
+ #error "NOT Implemented"
197
+ #endif
176
198
this ->dwNumberOfPhysicalProcessors = countPhysicalProcessor;
177
- # endif // _WIN32
199
+
178
200
return true ;
179
201
}
180
202
0 commit comments