Skip to content

Conversation

@mrsharm
Copy link
Member

@mrsharm mrsharm commented Apr 3, 2024

Testing

Added printf after the sysconf calls to discern which cache size is used and found that it correctly resolves to the L3 Cache Size on my machine with the following details after running getconf -a | grep CACHE:

LEVEL1_ICACHE_SIZE 32768
LEVEL1_ICACHE_ASSOC
LEVEL1_ICACHE_LINESIZE 64
LEVEL1_DCACHE_SIZE 32768
LEVEL1_DCACHE_ASSOC 8
LEVEL1_DCACHE_LINESIZE 64
LEVEL2_CACHE_SIZE 524288
LEVEL2_CACHE_ASSOC 8
LEVEL2_CACHE_LINESIZE 64
LEVEL3_CACHE_SIZE 268435456
LEVEL3_CACHE_ASSOC 0
LEVEL3_CACHE_LINESIZE 64
LEVEL4_CACHE_SIZE
LEVEL4_CACHE_ASSOC
LEVEL4_CACHE_LINESIZE

Output of printf( "Last Cache Size: %zu\n", cacheSize);: Last Cache Size: 268435456

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/gc
See info in area-owners.md if you want to be subscribed.

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
@mrsharm mrsharm requested review from janvorli and jkotas April 3, 2024 19:34
mrsharm and others added 3 commits April 3, 2024 16:18
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@mrsharm
Copy link
Member Author

mrsharm commented Apr 3, 2024

Thanks!

Thank you and @janvorli for the great feedback!

@mrsharm
Copy link
Member Author

mrsharm commented Apr 4, 2024

Working on fixing the following compiler errors breaking the build:

  /Users/runner/work/1/s/src/coreclr/gc/unix/gcenv.unix.cpp:873:9: error: use of undeclared identifier '_SC_LEVEL1_DCACHE_SIZE'
          _SC_LEVEL1_DCACHE_SIZE,
          ^
  /Users/runner/work/1/s/src/coreclr/gc/unix/gcenv.unix.cpp:874:9: error: use of undeclared identifier '_SC_LEVEL2_CACHE_SIZE'
          _SC_LEVEL2_CACHE_SIZE,
          ^
  /Users/runner/work/1/s/src/coreclr/gc/unix/gcenv.unix.cpp:875:9: error: use of undeclared identifier '_SC_LEVEL3_CACHE_SIZE'
          _SC_LEVEL3_CACHE_SIZE,
          ^
  /Users/runner/work/1/s/src/coreclr/gc/unix/gcenv.unix.cpp:876:9: error: use of undeclared identifier '_SC_LEVEL4_CACHE_SIZE'
          _SC_LEVEL4_CACHE_SIZE,
          ^
  /Users/runner/work/1/s/src/coreclr/gc/unix/gcenv.unix.cpp:879:18: error: invalid application of 'sizeof' to an incomplete type 'const int[]'
      for (int i = ARRAY_SIZE(cacheLevelNames) - 1; i >= 0; i--)
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  /Users/runner/work/1/s/src/native/minipal/utils.h:7:32: note: expanded from macro 'ARRAY_SIZE'
  #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
                                 ^~~~~
  5 errors generated.

@jkotas
Copy link
Member

jkotas commented Apr 4, 2024

Working on fixing the following compiler errors breaking the build:

Hmm, interesting - I thought that sysconf is going to be always used on Linux, but it actually only used with GLIBC.

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
// 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you are casting it to a size_t, that just lost the benefit of handling the case when this returns -1. we haven't seen this read -1, but then again, we didn't know sysconf could return -1 either. I thought you wanted to handle that scenario?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function cannot set the cache_size_from_sys_file to -1 and still return true. The only theoretical case when it could happen would be if the file contained literally -1. The file is generated by the kernel and it either has a value or it doesn't exist if the corresponding cache level information is not available. The sysconf case was different, it is documented to return -1 in error cases like the case when we ask it for a value that it cannot provide.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't seem worth it to me to depend on theoretical case not happening when you can so trivially handle it and make it consistent from what it's done so far in this function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it consistent from what it's done so far in this function.

It is done in one place in this function that needs it. It is not consistently done in this function.

For example,

cacheSize = (size_t) cacheSizeFromSysctl;
does not protect against bogus values either.

If you would like to protect against bogus cache sizes, it would be best to do it at the very end of the function or even in the calling code in the GC. For example, ignore the cache size if it is more than 1GB.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point is that theoretically, that function could return any value if the kernel generated wrong contents of those virtual files due to some bug. So it doesn't seem to be useful to handle one specific case explicitly. If there was a bug, it can as well contain -2 or 1234567890 or whatever random number. As I've said, it is very different from the sysconf where the -1 is valid and documented return value that we've just forgotten about.

@mrsharm mrsharm merged commit 09e1418 into dotnet:main Apr 4, 2024
matouskozak pushed a commit to matouskozak/runtime that referenced this pull request Apr 30, 2024
…Size as a parameter via a new argument CURRENT_CACHE_SIZE (dotnet#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>
@github-actions github-actions bot locked and limited conversation to collaborators May 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants