Skip to content

Commit a5bf5df

Browse files
committed
Reflect PR feeback and move total memory computation to init
1 parent 449d29c commit a5bf5df

File tree

1 file changed

+51
-53
lines changed

1 file changed

+51
-53
lines changed

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

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ AffinitySet g_processAffinitySet;
220220
extern "C" int g_highestNumaNode;
221221
extern "C" bool g_numaAvailable;
222222

223+
static int64_t g_totalPhysicalMemSize = 0;
224+
223225
#ifdef TARGET_APPLE
224226
static int *g_kern_memorystatus_level_mib = NULL;
225227
static size_t g_kern_memorystatus_level_mib_length = 0;
@@ -230,7 +232,6 @@ static size_t g_kern_memorystatus_level_mib_length = 0;
230232
// true if it has succeeded, false if it has failed
231233
bool GCToOSInterface::Initialize()
232234
{
233-
bool success = true;
234235
int pageSize = sysconf( _SC_PAGE_SIZE );
235236

236237
g_pageSizeUnixInl = uint32_t((pageSize > 0) ? pageSize : 0x1000);
@@ -326,21 +327,53 @@ bool GCToOSInterface::Initialize()
326327
#ifdef TARGET_APPLE
327328
const char* mem_free_name = "kern.memorystatus_level";
328329
int rc = sysctlnametomib(mem_free_name, NULL, &g_kern_memorystatus_level_mib_length);
329-
if (rc == 0)
330+
if (rc != 0)
330331
{
331-
g_kern_memorystatus_level_mib = (int*)malloc(g_kern_memorystatus_level_mib_length * sizeof(int));
332-
rc = sysctlnametomib(mem_free_name, g_kern_memorystatus_level_mib, &g_kern_memorystatus_level_mib_length);
333-
if (rc != 0)
334-
{
335-
free(g_kern_memorystatus_level_mib);
336-
g_kern_memorystatus_level_mib = NULL;
337-
g_kern_memorystatus_level_mib_length = 0;
338-
success = false;
339-
}
332+
return false;
333+
}
334+
335+
g_kern_memorystatus_level_mib = (int*)malloc(g_kern_memorystatus_level_mib_length * sizeof(int));
336+
if (g_kern_memorystatus_level_mib == NULL)
337+
{
338+
return false;
339+
}
340+
341+
rc = sysctlnametomib(mem_free_name, g_kern_memorystatus_level_mib, &g_kern_memorystatus_level_mib_length);
342+
if (rc != 0)
343+
{
344+
free(g_kern_memorystatus_level_mib);
345+
g_kern_memorystatus_level_mib = NULL;
346+
g_kern_memorystatus_level_mib_length = 0;
347+
return false;
340348
}
341349
#endif
342350

343-
return success;
351+
// Get the physical memory size
352+
#if HAVE_SYSCONF && HAVE__SC_PHYS_PAGES
353+
long pages = sysconf(_SC_PHYS_PAGES);
354+
if (pages == -1)
355+
{
356+
return false;
357+
}
358+
359+
g_totalPhysicalMemSize = (uint64_t)pages * (uint64_t)g_pageSizeUnixInl;
360+
#elif HAVE_SYSCTL
361+
int mib[2];
362+
mib[0] = CTL_HW;
363+
mib[1] = HW_MEMSIZE;
364+
size_t length = sizeof(INT64);
365+
int rc = sysctl(mib, 2, &g_totalPhysicalMemSize, &length, NULL, 0);
366+
if (rc == 0)
367+
{
368+
return false;
369+
}
370+
#else // HAVE_SYSCTL
371+
#error "Don't know how to get total physical memory on this platform"
372+
#endif // HAVE_SYSCTL
373+
374+
assert(g_totalPhysicalMemSize != 0);
375+
376+
return true;
344377
}
345378

346379
// Shutdown the interface implementation
@@ -1234,34 +1267,7 @@ uint64_t GCToOSInterface::GetPhysicalMemoryLimit(bool* is_restricted)
12341267
return restricted_limit;
12351268
}
12361269

1237-
// Get the physical memory size
1238-
#if HAVE_SYSCONF && HAVE__SC_PHYS_PAGES
1239-
long pages = sysconf(_SC_PHYS_PAGES);
1240-
if (pages == -1)
1241-
{
1242-
return 0;
1243-
}
1244-
1245-
long pageSize = sysconf(_SC_PAGE_SIZE);
1246-
if (pageSize == -1)
1247-
{
1248-
return 0;
1249-
}
1250-
1251-
return (uint64_t)pages * (uint64_t)pageSize;
1252-
#elif HAVE_SYSCTL
1253-
int mib[3];
1254-
mib[0] = CTL_HW;
1255-
mib[1] = HW_MEMSIZE;
1256-
int64_t physical_memory = 0;
1257-
size_t length = sizeof(INT64);
1258-
int rc = sysctl(mib, 2, &physical_memory, &length, NULL, 0);
1259-
assert(rc == 0);
1260-
1261-
return physical_memory;
1262-
#else // HAVE_SYSCTL
1263-
#error "Don't know how to get total physical memory on this platform"
1264-
#endif // HAVE_SYSCTL
1270+
return g_totalPhysicalMemSize;
12651271
}
12661272

12671273
// Get amount of physical memory available for use in the system
@@ -1273,20 +1279,12 @@ uint64_t GetAvailablePhysicalMemory()
12731279
#if defined(__APPLE__)
12741280
uint32_t mem_free = 0;
12751281
size_t mem_free_length = sizeof(uint32_t);
1276-
if (g_kern_memorystatus_level_mib != NULL)
1282+
assert(g_kern_memorystatus_level_mib != NULL);
1283+
int rc = sysctl(g_kern_memorystatus_level_mib, g_kern_memorystatus_level_mib_length, &mem_free, &mem_free_length, NULL, 0);
1284+
assert(rc == 0);
1285+
if (rc == 0)
12771286
{
1278-
int rc = sysctl(g_kern_memorystatus_level_mib, g_kern_memorystatus_level_mib_length, &mem_free, &mem_free_length, NULL, 0);
1279-
if (rc == 0)
1280-
{
1281-
int64_t mem_size = 0;
1282-
size_t mem_size_length = sizeof(int64_t);
1283-
int mib[] = { CTL_HW, HW_MEMSIZE };
1284-
rc = sysctl(mib, 2, &mem_size, &mem_size_length, NULL, 0);
1285-
if (rc == 0)
1286-
{
1287-
available = (int64_t)mem_free * mem_size / 100;
1288-
}
1289-
}
1287+
available = (int64_t)mem_free * g_totalPhysicalMemSize / 100;
12901288
}
12911289
#elif defined(__FreeBSD__)
12921290
size_t inactive_count = 0, laundry_count = 0, free_count = 0;

0 commit comments

Comments
 (0)