-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix the heuristics for L3 cache size for Arm64 #71029
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f6310e4
Get cacheLevel for unix
kunalspathak 45c4138
Get cacheLevel for windows
kunalspathak 8b6862c
Get cacheLevel for os
kunalspathak 363490c
add the macro CHECK_CACHE_SIZE
kunalspathak 29ed6db
fix multiplier
kunalspathak 468dc4a
Review comments
kunalspathak b09e495
Fix linux arm64 build
kunalspathak c7c905a
Change from std:string to char[]
kunalspathak 0bf5ec1
minor nit
kunalspathak 1f7ce1c
opps should be outside the loop
kunalspathak cc72139
Replace assert with _ASSERTE
kunalspathak 5671866
CHECK_CACHE_SIZE -> UPDATE_CACHE_SIZE_AND_LEVEL
kunalspathak aaab0bf
Use GetTotalProcessorCount for windows
kunalspathak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -876,21 +876,29 @@ bool ReadMemoryValueFromFile(const char* filename, uint64_t* val) | |
return result; | ||
} | ||
|
||
#define UPDATE_CACHE_SIZE_AND_LEVEL(CACHE_LEVEL) if (size > cacheSize) { cacheSize = size; cacheLevel = CACHE_LEVEL; } | ||
|
||
static size_t GetLogicalProcessorCacheSizeFromOS() | ||
{ | ||
size_t cacheLevel = 0; | ||
size_t cacheSize = 0; | ||
size_t size; | ||
|
||
#ifdef _SC_LEVEL1_DCACHE_SIZE | ||
cacheSize = std::max(cacheSize, ( size_t) sysconf(_SC_LEVEL1_DCACHE_SIZE)); | ||
size = ( size_t) sysconf(_SC_LEVEL1_DCACHE_SIZE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q (also applies to the old src) - what does sysconf return if there's no cache of that level? I presume it returns 0, not -1 (the doc says it returns -1 if there's an error). |
||
UPDATE_CACHE_SIZE_AND_LEVEL(1) | ||
#endif | ||
#ifdef _SC_LEVEL2_CACHE_SIZE | ||
cacheSize = std::max(cacheSize, ( size_t) sysconf(_SC_LEVEL2_CACHE_SIZE)); | ||
size = ( size_t) sysconf(_SC_LEVEL2_CACHE_SIZE); | ||
UPDATE_CACHE_SIZE_AND_LEVEL(2) | ||
#endif | ||
#ifdef _SC_LEVEL3_CACHE_SIZE | ||
cacheSize = std::max(cacheSize, ( size_t) sysconf(_SC_LEVEL3_CACHE_SIZE)); | ||
size = ( size_t) sysconf(_SC_LEVEL3_CACHE_SIZE); | ||
UPDATE_CACHE_SIZE_AND_LEVEL(3) | ||
#endif | ||
#ifdef _SC_LEVEL4_CACHE_SIZE | ||
cacheSize = std::max(cacheSize, ( size_t) sysconf(_SC_LEVEL4_CACHE_SIZE)); | ||
size = ( size_t) sysconf(_SC_LEVEL4_CACHE_SIZE); | ||
UPDATE_CACHE_SIZE_AND_LEVEL(4) | ||
#endif | ||
|
||
#if defined(TARGET_LINUX) && !defined(HOST_ARM) && !defined(HOST_X86) | ||
|
@@ -901,25 +909,39 @@ static size_t GetLogicalProcessorCacheSizeFromOS() | |
// for the platform. Currently musl and arm64 should be only cases to use | ||
// this method to determine cache size. | ||
// | ||
size_t size; | ||
|
||
if (ReadMemoryValueFromFile("/sys/devices/system/cpu/cpu0/cache/index0/size", &size)) | ||
cacheSize = std::max(cacheSize, size); | ||
if (ReadMemoryValueFromFile("/sys/devices/system/cpu/cpu0/cache/index1/size", &size)) | ||
cacheSize = std::max(cacheSize, size); | ||
if (ReadMemoryValueFromFile("/sys/devices/system/cpu/cpu0/cache/index2/size", &size)) | ||
cacheSize = std::max(cacheSize, size); | ||
if (ReadMemoryValueFromFile("/sys/devices/system/cpu/cpu0/cache/index3/size", &size)) | ||
cacheSize = std::max(cacheSize, size); | ||
if (ReadMemoryValueFromFile("/sys/devices/system/cpu/cpu0/cache/index4/size", &size)) | ||
cacheSize = std::max(cacheSize, size); | ||
size_t level; | ||
char path_to_size_file[] = "/sys/devices/system/cpu/cpu0/cache/index-/size"; | ||
char path_to_level_file[] = "/sys/devices/system/cpu/cpu0/cache/index-/level"; | ||
int index = 40; | ||
assert(path_to_size_file[index] == '-'); | ||
assert(path_to_level_file[index] == '-'); | ||
|
||
for (int i = 0; i < 5; i++) | ||
{ | ||
path_to_size_file[index] = (char)(48 + i); | ||
|
||
if (ReadMemoryValueFromFile(path_to_size_file, &size)) | ||
{ | ||
path_to_level_file[index] = (char)(48 + i); | ||
|
||
if (ReadMemoryValueFromFile(path_to_level_file, &level)) | ||
{ | ||
UPDATE_CACHE_SIZE_AND_LEVEL(level) | ||
} | ||
else | ||
{ | ||
cacheSize = std::max(cacheSize, size); | ||
} | ||
} | ||
} | ||
} | ||
#endif | ||
|
||
#if defined(HOST_ARM64) && !defined(TARGET_OSX) | ||
#if (defined(HOST_ARM64) || defined(HOST_LOONGARCH64)) && !defined(TARGET_OSX) | ||
if (cacheSize == 0) | ||
{ | ||
// It is currently expected to be missing cache size info | ||
// We expect to get the L3 cache size for Arm64 but currently expected to be missing that info | ||
// from most of the machines. | ||
// | ||
// _SC_LEVEL*_*CACHE_SIZE is not yet present. Work is in progress to enable this for arm64 | ||
// | ||
|
@@ -964,6 +986,38 @@ static size_t GetLogicalProcessorCacheSizeFromOS() | |
} | ||
#endif | ||
|
||
#if (defined(HOST_ARM64) || defined(HOST_LOONGARCH64)) && !defined(TARGET_OSX) | ||
if (cacheLevel != 3) | ||
{ | ||
// We expect to get the L3 cache size for Arm64 but currently expected to be missing that info | ||
// from most of the machines. | ||
// Hence, just use the following heuristics at best depending on the CPU count | ||
// 1 ~ 4 : 4 MB | ||
// 5 ~ 16 : 8 MB | ||
// 17 ~ 64 : 16 MB | ||
// 65+ : 32 MB | ||
DWORD logicalCPUs = g_totalCpuCount; | ||
if (logicalCPUs < 5) | ||
{ | ||
cacheSize = 4; | ||
} | ||
else if (logicalCPUs < 17) | ||
{ | ||
cacheSize = 8; | ||
} | ||
else if (logicalCPUs < 65) | ||
{ | ||
cacheSize = 16; | ||
} | ||
else | ||
{ | ||
cacheSize = 32; | ||
} | ||
|
||
cacheSize *= (1024 * 1024); | ||
} | ||
#endif | ||
|
||
return cacheSize; | ||
} | ||
|
||
|
@@ -1037,15 +1091,10 @@ size_t GCToOSInterface::GetCacheSizePerLogicalCpu(bool trueSize) | |
size_t maxSize, maxTrueSize; | ||
maxSize = maxTrueSize = GetLogicalProcessorCacheSizeFromOS(); // Returns the size of the highest level processor cache | ||
|
||
#if defined(HOST_ARM64) | ||
// Bigger gen0 size helps arm64 targets | ||
maxSize = maxTrueSize * 3; | ||
#endif | ||
|
||
s_maxSize = maxSize; | ||
s_maxTrueSize = maxTrueSize; | ||
|
||
// printf("GetCacheSizePerLogicalCpu returns %d, adjusted size %d\n", maxSize, maxTrueSize); | ||
// printf("GetCacheSizePerLogicalCpu returns %zu, adjusted size %zu\n", maxSize, maxTrueSize); | ||
return trueSize ? maxTrueSize : maxSize; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last nit - please feel free to do that later if you want to get this in before today's snap for preview 6.
Looking at the usages of this macro, I have realized it would be great to make the
size
a parameter of the macro too. From the usage sites, it is not obvious where it gets the size from (I have to read the macro definition to figure it out).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, will do it in follow-up PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#71209