[RISC-V][GC] Use 128gb for regions_range on RISCV64 (#84797)
authort-mustafin <66252296+t-mustafin@users.noreply.github.com>
Fri, 5 May 2023 15:18:41 +0000 (18:18 +0300)
committerGitHub <noreply@github.com>
Fri, 5 May 2023 15:18:41 +0000 (17:18 +0200)
* Set virtual memory limit for RISCV64 SV39

Use 128gb for regions_range cause RISCV64 SV39 memory layout allows to use only 256gb of virtual memory: https://docs.kernel.org/riscv/vm-layout.html#risc-v-linux-kernel-sv39.

* PR feedback

src/coreclr/gc/gc.cpp
src/coreclr/gc/unix/gcenv.unix.cpp
src/coreclr/pal/src/misc/sysinfo.cpp

index 820d49d..f01b7ac 100644 (file)
@@ -45747,8 +45747,8 @@ HRESULT GCHeap::Initialize()
         }
         else
         {
-            // If no hard_limit is configured the reservation size is max of 256gb or 2x physical limit
-            gc_heap::regions_range = max(((size_t)256 * 1024 * 1024 * 1024), (size_t)(2 * gc_heap::total_physical_mem));
+            // If no hard_limit is configured the reservation size is min of 1/2 GetVirtualMemoryLimit() or max of 256Gb or 2x physical limit.
+            gc_heap::regions_range = min(GCToOSInterface::GetVirtualMemoryLimit()/2, max((size_t)256 * 1024 * 1024 * 1024, (size_t)(2 * gc_heap::total_physical_mem)));
         }
         gc_heap::regions_range = align_on_page(gc_heap::regions_range);
     }
index 399e9a3..a024d3f 100644 (file)
@@ -1080,12 +1080,18 @@ const AffinitySet* GCToOSInterface::SetGCThreadsAffinitySet(uintptr_t configAffi
 size_t GCToOSInterface::GetVirtualMemoryLimit()
 {
 #ifdef HOST_64BIT
+#ifndef TARGET_RISCV64
     // There is no API to get the total virtual address space size on
     // Unix, so we use a constant value representing 128TB, which is
     // the approximate size of total user virtual address space on
     // the currently supported Unix systems.
     static const uint64_t _128TB = (1ull << 47);
     return _128TB;
+#else // TARGET_RISCV64
+    // For RISC-V Linux Kernel SV39 virtual memory limit is 256gb.
+    static const uint64_t _256GB = (1ull << 38);
+    return _256GB;
+#endif // TARGET_RISCV64
 #else
     return (size_t)-1;
 #endif
index 829100f..cd7f030 100644 (file)
@@ -470,12 +470,17 @@ GlobalMemoryStatusEx(
 #endif // __APPLE__
     }
 
+#ifndef TARGET_RISCV64
     // There is no API to get the total virtual address space size on
     // Unix, so we use a constant value representing 128TB, which is
     // the approximate size of total user virtual address space on
     // the currently supported Unix systems.
-    static const UINT64 _128TB = (1ull << 47);
-    lpBuffer->ullTotalVirtual = _128TB;
+    static const UINT64 VMSize = (1ull << 47);
+#else // TARGET_RISCV64
+    // For RISC-V Linux Kernel SV39 virtual memory limit is 256gb.
+    static const UINT64 VMSize = (1ull << 38);
+#endif // TARGET_RISCV64
+    lpBuffer->ullTotalVirtual = VMSize;
     lpBuffer->ullAvailVirtual = lpBuffer->ullAvailPhys;
 
     LOGEXIT("GlobalMemoryStatusEx returns %d\n", fRetVal);