Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions src/coreclr/gc/unix/gcenv.unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -965,17 +965,38 @@ static size_t GetLogicalProcessorCacheSizeFromOS()
#if HAVE_SYSCTLBYNAME
if (cacheSize == 0)
{
bool success = false;
int64_t cacheSizeFromSysctl = 0;
size_t sz = sizeof(cacheSizeFromSysctl);
const bool success = false
// macOS: Since macOS 12.0, Apple added ".perflevelX." to determinate cache sizes for efficiency
// and performance cores separately. "perflevel0" stands for "performance"
|| sysctlbyname("hw.perflevel0.l3cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0
|| sysctlbyname("hw.perflevel0.l2cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0
// macOS: Since macOS 12.0, Apple added ".perflevelX." to determinate cache sizes for efficiency
// and performance cores separately. "perflevel0" stands for "performance"
if (sysctlbyname("hw.perflevel0.l3cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0)
{
success = true;
}
else if (sysctlbyname("hw.perflevel0.l2cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0)
{
int64_t cpusMax;
int64_t cpusPerL2;
size_t szCpusMax = sizeof(cpusMax);
size_t szCpusPerL2 = sizeof(cpusPerL2);
// macOS: The reported L2 cache size is actually not total but rather per CPU "cluster".
// Therefore, we need to account for CPU count per "cluster" to calculate total size
if (sysctlbyname("hw.perflevel0.physicalcpu_max", &cpusMax, &szCpusMax, nullptr, 0) == 0 &&
sysctlbyname("hw.perflevel0.cpusperl2", &cpusPerL2, &szCpusPerL2, nullptr, 0) == 0)
{
cacheSizeFromSysctl *= (cpusMax / cpusPerL2);
}
success = true;
}
else
{
// macOS: these report cache sizes for efficiency cores only:
|| sysctlbyname("hw.l3cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0
|| sysctlbyname("hw.l2cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0
|| sysctlbyname("hw.l1dcachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0;
success = sysctlbyname("hw.l3cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0
|| sysctlbyname("hw.l2cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0
|| sysctlbyname("hw.l1dcachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0;
}

if (success)
{
assert(cacheSizeFromSysctl > 0);
Expand Down
37 changes: 29 additions & 8 deletions src/coreclr/pal/src/misc/sysinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,17 +633,38 @@ PAL_GetLogicalProcessorCacheSizeFromOS()
#if HAVE_SYSCTLBYNAME
if (cacheSize == 0)
{
bool success = false;
int64_t cacheSizeFromSysctl = 0;
size_t sz = sizeof(cacheSizeFromSysctl);
const bool success = false
// macOS: Since macOS 12.0, Apple added ".perflevelX." to determinate cache sizes for efficiency
// and performance cores separately. "perflevel0" stands for "performance"
|| sysctlbyname("hw.perflevel0.l3cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0
|| sysctlbyname("hw.perflevel0.l2cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0
// macOS: Since macOS 12.0, Apple added ".perflevelX." to determinate cache sizes for efficiency
// and performance cores separately. "perflevel0" stands for "performance"
if (sysctlbyname("hw.perflevel0.l3cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0)
{
success = true;
}
else if (sysctlbyname("hw.perflevel0.l2cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0)
{
int64_t cpusMax;
int64_t cpusPerL2;
size_t szCpusMax = sizeof(cpusMax);
size_t szCpusPerL2 = sizeof(cpusPerL2);
// macOS: The reported L2 cache size is actually not total but rather per CPU "cluster".
// Therefore, we need to account for CPU count per "cluster" to calculate total size
if (sysctlbyname("hw.perflevel0.physicalcpu_max", &cpusMax, &szCpusMax, nullptr, 0) == 0 &&
sysctlbyname("hw.perflevel0.cpusperl2", &cpusPerL2, &szCpusPerL2, nullptr, 0) == 0)
{
cacheSizeFromSysctl *= (cpusMax / cpusPerL2);
}
success = true;
}
else
{
// macOS: these report cache sizes for efficiency cores only:
|| sysctlbyname("hw.l3cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0
|| sysctlbyname("hw.l2cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0
|| sysctlbyname("hw.l1dcachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0;
success = sysctlbyname("hw.l3cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0
|| sysctlbyname("hw.l2cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0
|| sysctlbyname("hw.l1dcachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0;
}

if (success)
{
_ASSERTE(cacheSizeFromSysctl > 0);
Expand Down