Skip to content

Commit

Permalink
Clean up macro in GetLogicalProcessorCacheSizeFromOS to include cache…
Browse files Browse the repository at this point in the history
…Size as a parameter via a new argument CURRENT_CACHE_SIZE (#100596)

* Clean up macro

* Addressed feedback.

* Removed redundant print

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

Co-authored-by: Jan Kotas <jkotas@microsoft.com>

* Inlined function call

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

Co-authored-by: Jan Kotas <jkotas@microsoft.com>

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

Co-authored-by: Jan Kotas <jkotas@microsoft.com>

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

Co-authored-by: Jan Kotas <jkotas@microsoft.com>

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

Co-authored-by: Jan Kotas <jkotas@microsoft.com>

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

---------

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
  • Loading branch information
mrsharm and jkotas committed Apr 4, 2024
1 parent 96d5a66 commit 09e1418
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions src/coreclr/gc/unix/gcenv.unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,31 +862,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 > ((long)cacheSize)) { cacheSize = NEW_CACHE_SIZE; cacheLevel = NEW_CACHE_LEVEL; }

static size_t GetLogicalProcessorCacheSizeFromOS()
{
size_t cacheLevel = 0;
size_t cacheSize = 0;
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 = sysconf(_SC_LEVEL1_DCACHE_SIZE);
UPDATE_CACHE_SIZE_AND_LEVEL(size, 1)
#endif
#ifdef _SC_LEVEL2_CACHE_SIZE
size = sysconf(_SC_LEVEL2_CACHE_SIZE);
UPDATE_CACHE_SIZE_AND_LEVEL(size, 2)
#endif
#ifdef _SC_LEVEL3_CACHE_SIZE
size = sysconf(_SC_LEVEL3_CACHE_SIZE);
UPDATE_CACHE_SIZE_AND_LEVEL(size, 3)
#endif
#ifdef _SC_LEVEL4_CACHE_SIZE
size = sysconf(_SC_LEVEL4_CACHE_SIZE);
UPDATE_CACHE_SIZE_AND_LEVEL(size, 4)
#if defined(_SC_LEVEL1_DCACHE_SIZE) || defined(_SC_LEVEL2_CACHE_SIZE) || defined(_SC_LEVEL3_CACHE_SIZE) || defined(_SC_LEVEL4_CACHE_SIZE)
const int cacheLevelNames[] =
{
_SC_LEVEL1_DCACHE_SIZE,
_SC_LEVEL2_CACHE_SIZE,
_SC_LEVEL3_CACHE_SIZE,
_SC_LEVEL4_CACHE_SIZE,
};

for (int i = ARRAY_SIZE(cacheLevelNames) - 1; i >= 0; i--)
{
long size = sysconf(cacheLevelNames[i]);
if (size > 0)
{
cacheSize = (size_t)size;
cacheLevel = i + 1;
break;
}
}
#endif

#if defined(TARGET_LINUX) && !defined(HOST_ARM) && !defined(HOST_X86)
Expand All @@ -912,18 +911,12 @@ static size_t GetLogicalProcessorCacheSizeFromOS()

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);
cacheSize = std::max(cacheSize, (size_t)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((long)cacheSize, size);
cacheLevel = level;
}
}
}
Expand Down Expand Up @@ -975,7 +968,7 @@ static size_t GetLogicalProcessorCacheSizeFromOS()
if (success)
{
assert(cacheSizeFromSysctl > 0);
cacheSize = ( size_t) cacheSizeFromSysctl;
cacheSize = (size_t) cacheSizeFromSysctl;
}
}
#endif
Expand Down

0 comments on commit 09e1418

Please sign in to comment.