Skip to content

Commit

Permalink
Guard against -1 Returned from sysconf for the Cache Sizes Causing La…
Browse files Browse the repository at this point in the history
…rge 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
  • Loading branch information
mrsharm authored and Ruihan-Yin committed May 30, 2024
1 parent bc38029 commit 9bdf473
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/coreclr/gc/unix/gcenv.unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -868,28 +868,30 @@ bool ReadMemoryValueFromFile(const char* filename, uint64_t* val)
return result;
}

#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; }
#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; }

static size_t GetLogicalProcessorCacheSizeFromOS()
{
size_t cacheLevel = 0;
size_t cacheSize = 0;
size_t size;
long size;

// sysconf can return -1 if the cache size is unavailable in some distributions and 0 in others.
// UPDATE_CACHE_SIZE_AND_LEVEL should handle both the cases by not updating cacheSize if either of cases are met.
#ifdef _SC_LEVEL1_DCACHE_SIZE
size = ( size_t) sysconf(_SC_LEVEL1_DCACHE_SIZE);
size = sysconf(_SC_LEVEL1_DCACHE_SIZE);
UPDATE_CACHE_SIZE_AND_LEVEL(size, 1)
#endif
#ifdef _SC_LEVEL2_CACHE_SIZE
size = ( size_t) sysconf(_SC_LEVEL2_CACHE_SIZE);
size = sysconf(_SC_LEVEL2_CACHE_SIZE);
UPDATE_CACHE_SIZE_AND_LEVEL(size, 2)
#endif
#ifdef _SC_LEVEL3_CACHE_SIZE
size = ( size_t) sysconf(_SC_LEVEL3_CACHE_SIZE);
size = sysconf(_SC_LEVEL3_CACHE_SIZE);
UPDATE_CACHE_SIZE_AND_LEVEL(size, 3)
#endif
#ifdef _SC_LEVEL4_CACHE_SIZE
size = ( size_t) sysconf(_SC_LEVEL4_CACHE_SIZE);
size = sysconf(_SC_LEVEL4_CACHE_SIZE);
UPDATE_CACHE_SIZE_AND_LEVEL(size, 4)
#endif

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

if (ReadMemoryValueFromFile(path_to_size_file, &size))
uint64_t cache_size_from_sys_file = 0;

if (ReadMemoryValueFromFile(path_to_size_file, &cache_size_from_sys_file))
{
// uint64_t to long conversion as ReadMemoryValueFromFile takes a uint64_t* as an argument for the val argument.
size = (long)cache_size_from_sys_file;
path_to_level_file[index] = (char)(48 + i);

if (ReadMemoryValueFromFile(path_to_level_file, &level))
{
UPDATE_CACHE_SIZE_AND_LEVEL(size, level)
}

else
{
cacheSize = std::max(cacheSize, size);
cacheSize = std::max((long)cacheSize, size);
}
}
}
Expand Down

0 comments on commit 9bdf473

Please sign in to comment.