Skip to content

Commit 2ef8dae

Browse files
Fix up physical memory config for WASM (#3151)
1 parent a9f328d commit 2ef8dae

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ static uint8_t* g_helperPage = 0;
184184
// Mutex to make the FlushProcessWriteBuffersMutex thread safe
185185
static pthread_mutex_t g_flushProcessWriteBuffersMutex;
186186

187+
uint64_t GetTotalPhysicalMemory_Wasm();
188+
187189
size_t GetRestrictedPhysicalMemoryLimit();
188190
bool GetPhysicalMemoryUsed(size_t* val);
189191

@@ -329,7 +331,9 @@ bool GCToOSInterface::Initialize()
329331
#endif
330332

331333
// Get the physical memory size
332-
#if HAVE_SYSCONF && HAVE__SC_PHYS_PAGES
334+
#ifdef TARGET_WASM
335+
g_totalPhysicalMemSize = GetTotalPhysicalMemory_Wasm();
336+
#elif HAVE_SYSCONF && HAVE__SC_PHYS_PAGES
333337
long pages = sysconf(_SC_PHYS_PAGES);
334338
if (pages == -1)
335339
{
@@ -1264,7 +1268,9 @@ uint64_t GetAvailablePhysicalMemory()
12641268
uint64_t available = 0;
12651269

12661270
// Get the physical memory available.
1267-
#if defined(__APPLE__)
1271+
#if defined(TARGET_WASM)
1272+
abort(); // Unreachable.
1273+
#elif defined(__APPLE__)
12681274
uint32_t mem_free = 0;
12691275
size_t mem_free_length = sizeof(uint32_t);
12701276
assert(g_kern_memorystatus_level_mib != NULL);
@@ -1286,8 +1292,6 @@ uint64_t GetAvailablePhysicalMemory()
12861292
sysctlbyname("vm.stats.vm.v_free_count", &free_count, &sz, NULL, 0);
12871293

12881294
available = (inactive_count + laundry_count + free_count) * sysconf(_SC_PAGESIZE);
1289-
#elif defined(TARGET_WASM)
1290-
available = sysconf(SYSCONF_PAGES) * sysconf(_SC_PAGE_SIZE);
12911295
#elif defined(__HAIKU__)
12921296
system_info info;
12931297
if (get_system_info(&info) == B_OK)

src/coreclr/gc/wasm/gcenv.wasm.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#include "common.h"
55
#include "gcenv.h"
66

7+
#ifdef TARGET_BROWSER
8+
#include <emscripten/heap.h>
9+
#endif
10+
711
// Flush write buffers of processors that are executing threads of the current process - a NOP for Wasm
812
void GCToOSInterface::FlushProcessWriteBuffers()
913
{
@@ -119,7 +123,7 @@ bool GCToOSInterface::VirtualReset(void* address, size_t size, bool unlock)
119123
}
120124

121125
//
122-
// CPU and memory limits - not avaliable on WASM (yet).
126+
// CPU and memory limits.
123127
//
124128
void InitializeCGroup()
125129
{
@@ -129,12 +133,31 @@ void CleanupCGroup()
129133
{
130134
}
131135

136+
uint64_t GetTotalPhysicalMemory_Wasm()
137+
{
138+
#ifdef TARGET_BROWSER
139+
// Note that Emscripten takes care not to return a value larger than max<size_t> here.
140+
return emscripten_get_heap_max();
141+
#else // TARGET_WASI
142+
// WASI doesn't have any API that could return the maximum memory size.
143+
// The best we can do here is use the value we set as default --maximum-memory.
144+
return 2 * 1024 * 1024 * 1024ULL; // 2GB.
145+
#endif // TARGET_WASI
146+
}
147+
132148
size_t GetRestrictedPhysicalMemoryLimit()
133149
{
134-
return 0; // 'Unlimited'.
150+
// We must return a valid value here since you can't "overcommit" memory in WASM.
151+
return GetTotalPhysicalMemory_Wasm();
135152
}
136153

137154
bool GetPhysicalMemoryUsed(size_t* val)
138155
{
139-
return false; // 'Unknown'.
156+
*val = __builtin_wasm_memory_size(0) * OS_PAGE_SIZE;
157+
if (*val == 0)
158+
{
159+
// This overflow can happen when all 4GB of memory are in use.
160+
*val = GetTotalPhysicalMemory_Wasm();
161+
}
162+
return true;
140163
}

0 commit comments

Comments
 (0)