Skip to content

Commit d055d03

Browse files
get proper number of physical cpu cores on macOS (#6835)
* get proper number of physical cpu cores on macOS macOS has an easy way to get the physical cpu count so let's use it. Also with Apple Silicon there are performance and efficiency cores so we need to make sure we ignore those low-perf cores when getting the count. * improve error handling
1 parent 4c80ad7 commit d055d03

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

code/utils/threading.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#ifdef WIN32
1313
#include <windows.h>
14+
#elif defined(__APPLE__)
15+
#include <sys/sysctl.h>
1416
#endif
1517

1618
namespace threading {
@@ -85,6 +87,27 @@ namespace threading {
8587
return num_cores;
8688
}
8789
}
90+
#elif defined __APPLE__
91+
static size_t get_number_of_physical_cores() {
92+
int rval = 0;
93+
int num = 0;
94+
size_t numSize = sizeof(num);
95+
96+
// apple silicon (performance cores only)
97+
rval = sysctlbyname("hw.perflevel0.physicalcpu", &num, &numSize, nullptr, 0);
98+
99+
// intel
100+
if (rval != 0) {
101+
rval = sysctlbyname("hw.physicalcpu", &num, &numSize, nullptr, 0);
102+
}
103+
104+
if (rval == 0 && num > 0) {
105+
return num;
106+
} else {
107+
// invalid results, try fallback
108+
return get_number_of_physical_cores_fallback();
109+
}
110+
}
88111
#elif defined SCP_UNIX
89112
static size_t get_number_of_physical_cores() {
90113
try {
@@ -170,4 +193,4 @@ namespace threading {
170193
size_t get_num_workers() {
171194
return worker_threads.size();
172195
}
173-
}
196+
}

0 commit comments

Comments
 (0)