Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FreeBSD case to gcenv. #91524

Merged
merged 4 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions src/coreclr/gc/unix/gcenv.unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,34 @@ uint64_t GetAvailablePhysicalMemory()
uint64_t available = 0;

// Get the physical memory available.
#ifndef __APPLE__
#if defined(__APPLE__)
vm_size_t page_size;
mach_port_t mach_port;
mach_msg_type_number_t count;
vm_statistics_data_t vm_stats;
mach_port = mach_host_self();
count = sizeof(vm_stats) / sizeof(natural_t);
if (KERN_SUCCESS == host_page_size(mach_port, &page_size))
{
if (KERN_SUCCESS == host_statistics(mach_port, HOST_VM_INFO, (host_info_t)&vm_stats, &count))
{
available = (int64_t)vm_stats.free_count * (int64_t)page_size;
}
}
mach_port_deallocate(mach_task_self(), mach_port);
#elif defined(__FreeBSD__)
size_t inactive_count = 0, laundry_count = 0, free_count = 0;
size_t sz = sizeof(inactive_count);
sysctlbyname("vm.stats.vm.v_inactive_count", &inactive_count, &sz, NULL, 0);

sz = sizeof(laundry_count);
sysctlbyname("vm.stats.vm.v_laundry_count", &laundry_count, &sz, NULL, 0);

sz = sizeof(free_count);
sysctlbyname("vm.stats.vm.v_free_count", &free_count, &sz, NULL, 0);

available = (inactive_count + laundry_count + free_count) * sysconf(_SC_PAGESIZE);
#else // Linux
static volatile bool tryReadMemInfo = true;

if (tryReadMemInfo)
Expand All @@ -1176,22 +1203,7 @@ uint64_t GetAvailablePhysicalMemory()
// Fall back to getting the available pages using sysconf.
available = sysconf(SYSCONF_PAGES) * sysconf(_SC_PAGE_SIZE);
}
#else // __APPLE__
vm_size_t page_size;
mach_port_t mach_port;
mach_msg_type_number_t count;
vm_statistics_data_t vm_stats;
mach_port = mach_host_self();
count = sizeof(vm_stats) / sizeof(natural_t);
if (KERN_SUCCESS == host_page_size(mach_port, &page_size))
{
if (KERN_SUCCESS == host_statistics(mach_port, HOST_VM_INFO, (host_info_t)&vm_stats, &count))
{
available = (int64_t)vm_stats.free_count * (int64_t)page_size;
}
}
mach_port_deallocate(mach_task_self(), mach_port);
#endif // __APPLE__
#endif

return available;
}
Expand Down
53 changes: 34 additions & 19 deletions src/coreclr/pal/src/misc/sysinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,39 @@ GlobalMemoryStatusEx(
// We do this only when we have the total physical memory available.
if (lpBuffer->ullTotalPhys > 0)
{
#ifndef __APPLE__
#if defined (__APPLE__)
Copy link
Member

@am11 am11 Sep 4, 2023

Choose a reason for hiding this comment

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

GlobalMemoryStatusEx in PAL is unused (outside of PAL tests); good candidate of deletion?
i.e. everything this query reports: git grep GlobalMemoryStatusEx ':/src/coreclr/pal'

Copy link
Member

Choose a reason for hiding this comment

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

I have submitted the cleanup in separate PR: #91554

@Thefrank Could you please revert changes in this file? It is dead code.

vm_size_t page_size;
mach_port_t mach_port;
mach_msg_type_number_t count;
vm_statistics_data_t vm_stats;
mach_port = mach_host_self();
count = sizeof(vm_stats) / sizeof(natural_t);
if (KERN_SUCCESS == host_page_size(mach_port, &page_size))
{
if (KERN_SUCCESS == host_statistics(mach_port, HOST_VM_INFO, (host_info_t)&vm_stats, &count))
{
lpBuffer->ullAvailPhys = (int64_t)vm_stats.free_count * (int64_t)page_size;
INT64 used_memory = ((INT64)vm_stats.active_count + (INT64)vm_stats.inactive_count + (INT64)vm_stats.wire_count) * (INT64)page_size;
lpBuffer->dwMemoryLoad = (DWORD)((used_memory * 100) / lpBuffer->ullTotalPhys);
}
}
mach_port_deallocate(mach_task_self(), mach_port);
#elif defined (__FreeBSD__)
size_t inactive_count = 0, laundry_count = 0, free_count = 0;
size_t sz = sizeof(inactive_count);
sysctlbyname("vm.stats.vm.v_inactive_count", &inactive_count, &sz, NULL, 0);

sz = sizeof(laundry_count);
sysctlbyname("vm.stats.vm.v_laundry_count", &laundry_count, &sz, NULL, 0);

sz = sizeof(free_count);
sysctlbyname("vm.stats.vm.v_free_count", &free_count, &sz, NULL, 0);

lpBuffer->ullAvailPhys = (inactive_count + laundry_count + free_count) * sysconf(_SC_PAGE_SIZE);

INT64 used_memory = lpBuffer->ullTotalPhys - lpBuffer->ullAvailPhys;
lpBuffer->dwMemoryLoad = (DWORD)((used_memory * 100) / lpBuffer->ullTotalPhys);
#else //Linux
static volatile bool tryReadMemInfo = true;

if (tryReadMemInfo)
Expand All @@ -450,24 +482,7 @@ GlobalMemoryStatusEx(

INT64 used_memory = lpBuffer->ullTotalPhys - lpBuffer->ullAvailPhys;
lpBuffer->dwMemoryLoad = (DWORD)((used_memory * 100) / lpBuffer->ullTotalPhys);
#else
vm_size_t page_size;
mach_port_t mach_port;
mach_msg_type_number_t count;
vm_statistics_data_t vm_stats;
mach_port = mach_host_self();
count = sizeof(vm_stats) / sizeof(natural_t);
if (KERN_SUCCESS == host_page_size(mach_port, &page_size))
{
if (KERN_SUCCESS == host_statistics(mach_port, HOST_VM_INFO, (host_info_t)&vm_stats, &count))
{
lpBuffer->ullAvailPhys = (int64_t)vm_stats.free_count * (int64_t)page_size;
INT64 used_memory = ((INT64)vm_stats.active_count + (INT64)vm_stats.inactive_count + (INT64)vm_stats.wire_count) * (INT64)page_size;
lpBuffer->dwMemoryLoad = (DWORD)((used_memory * 100) / lpBuffer->ullTotalPhys);
}
}
mach_port_deallocate(mach_task_self(), mach_port);
#endif // __APPLE__
#endif
}

#ifndef TARGET_RISCV64
Expand Down
1 change: 0 additions & 1 deletion src/libraries/System.Runtime/tests/System/GCTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,6 @@ public static void GetAllocatedBytesForCurrentThread(int size)

private static bool IsNotArmProcessAndRemoteExecutorSupported => PlatformDetection.IsNotArmProcess && RemoteExecutor.IsSupported;

[ActiveIssue("https://github.com/dotnet/runtime/issues/64935", TestPlatforms.FreeBSD)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/73167", TestRuntimes.Mono)]
[ConditionalFact(nameof(IsNotArmProcessAndRemoteExecutorSupported))] // [ActiveIssue("https://github.com/dotnet/runtime/issues/29434")]
public static void GetGCMemoryInfo()
Expand Down
Loading