Skip to content

Commit 9bdf473

Browse files
mrsharmRuihan-Yin
authored andcommitted
Guard against -1 Returned from sysconf for the Cache Sizes Causing Large Gen0 Sizes and Budgets for Certain Linux Distributions. (dotnet#100502)
* Logging. * Fixed comparison check * Fix logical operations * Completely guard against the cacheSize as UINTMAX_MAX * Fix for right macro * Ensure we are guarded against all cases where cacheSize == SIZE_MAX * Added an extra guard and removed redundant case * Comment clean * Added some additional asserts * Removed unnecessary checks for cacheSize == SIZE_MAX * Cleaned up logic * Fix type casting comparison * Removed redundant comment * Removed one more unneccesary guard
1 parent bc38029 commit 9bdf473

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/coreclr/gc/unix/gcenv.unix.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -868,28 +868,30 @@ bool ReadMemoryValueFromFile(const char* filename, uint64_t* val)
868868
return result;
869869
}
870870

871-
#define UPDATE_CACHE_SIZE_AND_LEVEL(NEW_CACHE_SIZE, NEW_CACHE_LEVEL) if (NEW_CACHE_SIZE > cacheSize) { cacheSize = NEW_CACHE_SIZE; cacheLevel = NEW_CACHE_LEVEL; }
871+
#define UPDATE_CACHE_SIZE_AND_LEVEL(NEW_CACHE_SIZE, NEW_CACHE_LEVEL) if (NEW_CACHE_SIZE > ((long)cacheSize)) { cacheSize = NEW_CACHE_SIZE; cacheLevel = NEW_CACHE_LEVEL; }
872872

873873
static size_t GetLogicalProcessorCacheSizeFromOS()
874874
{
875875
size_t cacheLevel = 0;
876876
size_t cacheSize = 0;
877-
size_t size;
877+
long size;
878878

879+
// sysconf can return -1 if the cache size is unavailable in some distributions and 0 in others.
880+
// UPDATE_CACHE_SIZE_AND_LEVEL should handle both the cases by not updating cacheSize if either of cases are met.
879881
#ifdef _SC_LEVEL1_DCACHE_SIZE
880-
size = ( size_t) sysconf(_SC_LEVEL1_DCACHE_SIZE);
882+
size = sysconf(_SC_LEVEL1_DCACHE_SIZE);
881883
UPDATE_CACHE_SIZE_AND_LEVEL(size, 1)
882884
#endif
883885
#ifdef _SC_LEVEL2_CACHE_SIZE
884-
size = ( size_t) sysconf(_SC_LEVEL2_CACHE_SIZE);
886+
size = sysconf(_SC_LEVEL2_CACHE_SIZE);
885887
UPDATE_CACHE_SIZE_AND_LEVEL(size, 2)
886888
#endif
887889
#ifdef _SC_LEVEL3_CACHE_SIZE
888-
size = ( size_t) sysconf(_SC_LEVEL3_CACHE_SIZE);
890+
size = sysconf(_SC_LEVEL3_CACHE_SIZE);
889891
UPDATE_CACHE_SIZE_AND_LEVEL(size, 3)
890892
#endif
891893
#ifdef _SC_LEVEL4_CACHE_SIZE
892-
size = ( size_t) sysconf(_SC_LEVEL4_CACHE_SIZE);
894+
size = sysconf(_SC_LEVEL4_CACHE_SIZE);
893895
UPDATE_CACHE_SIZE_AND_LEVEL(size, 4)
894896
#endif
895897

@@ -912,17 +914,22 @@ static size_t GetLogicalProcessorCacheSizeFromOS()
912914
{
913915
path_to_size_file[index] = (char)(48 + i);
914916

915-
if (ReadMemoryValueFromFile(path_to_size_file, &size))
917+
uint64_t cache_size_from_sys_file = 0;
918+
919+
if (ReadMemoryValueFromFile(path_to_size_file, &cache_size_from_sys_file))
916920
{
921+
// uint64_t to long conversion as ReadMemoryValueFromFile takes a uint64_t* as an argument for the val argument.
922+
size = (long)cache_size_from_sys_file;
917923
path_to_level_file[index] = (char)(48 + i);
918924

919925
if (ReadMemoryValueFromFile(path_to_level_file, &level))
920926
{
921927
UPDATE_CACHE_SIZE_AND_LEVEL(size, level)
922928
}
929+
923930
else
924931
{
925-
cacheSize = std::max(cacheSize, size);
932+
cacheSize = std::max((long)cacheSize, size);
926933
}
927934
}
928935
}

0 commit comments

Comments
 (0)