From 494c1808db91d8da37e694dfec50ccb786a39a7f Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Mon, 4 Sep 2023 08:58:22 -0700 Subject: [PATCH 1/2] Delete unused methods from CoreCLR PAL --- src/coreclr/pal/inc/pal.h | 24 - src/coreclr/pal/src/config.h.in | 5 - src/coreclr/pal/src/configure.cmake | 27 -- src/coreclr/pal/src/misc/sysinfo.cpp | 435 ------------------ src/coreclr/pal/tests/palsuite/CMakeLists.txt | 1 - .../pal/tests/palsuite/compilableTests.txt | 1 - .../GlobalMemoryStatusEx/test1/test.cpp | 55 --- .../pal/tests/palsuite/paltestlist.txt | 1 - 8 files changed, 549 deletions(-) delete mode 100644 src/coreclr/pal/tests/palsuite/miscellaneous/GlobalMemoryStatusEx/test1/test.cpp diff --git a/src/coreclr/pal/inc/pal.h b/src/coreclr/pal/inc/pal.h index 84744d64d2f68..3e1d460f9e70d 100644 --- a/src/coreclr/pal/inc/pal.h +++ b/src/coreclr/pal/inc/pal.h @@ -2678,12 +2678,6 @@ BOOL PALAPI PAL_GetCpuLimit(UINT* val); -PALIMPORT -size_t -PALAPI -PAL_GetLogicalProcessorCacheSizeFromOS(); -#define GetLogicalProcessorCacheSizeFromOS PAL_GetLogicalProcessorCacheSizeFromOS - typedef BOOL(*UnwindReadMemoryCallback)(PVOID address, PVOID buffer, SIZE_T size); PALIMPORT BOOL PALAPI PAL_VirtualUnwind(CONTEXT *context, KNONVOLATILE_CONTEXT_POINTERS *contextPointers); @@ -3035,24 +3029,6 @@ VirtualProtect( IN DWORD flNewProtect, OUT PDWORD lpflOldProtect); -typedef struct _MEMORYSTATUSEX { - DWORD dwLength; - DWORD dwMemoryLoad; - DWORDLONG ullTotalPhys; - DWORDLONG ullAvailPhys; - DWORDLONG ullTotalPageFile; - DWORDLONG ullAvailPageFile; - DWORDLONG ullTotalVirtual; - DWORDLONG ullAvailVirtual; - DWORDLONG ullAvailExtendedVirtual; -} MEMORYSTATUSEX, *LPMEMORYSTATUSEX; - -PALIMPORT -BOOL -PALAPI -GlobalMemoryStatusEx( - IN OUT LPMEMORYSTATUSEX lpBuffer); - typedef struct _MEMORY_BASIC_INFORMATION { PVOID BaseAddress; PVOID AllocationBase_PAL_Undefined; diff --git a/src/coreclr/pal/src/config.h.in b/src/coreclr/pal/src/config.h.in index 89cbc3ecafacf..e52db481c01c8 100644 --- a/src/coreclr/pal/src/config.h.in +++ b/src/coreclr/pal/src/config.h.in @@ -45,7 +45,6 @@ #cmakedefine01 HAVE_SYSCTL #cmakedefine01 HAVE_SYSCTLBYNAME #cmakedefine01 HAVE_SYSCONF -#cmakedefine01 HAVE_SYSINFO #cmakedefine01 HAVE_GMTIME_R #cmakedefine01 HAVE_TIMEGM #cmakedefine01 HAVE_POLL @@ -68,12 +67,9 @@ #cmakedefine HAVE_UNW_GET_SAVE_LOC #cmakedefine HAVE_UNW_GET_ACCESSORS #cmakedefine HAVE_UNW_AARCH64_X19 -#cmakedefine01 HAVE_XSWDEV -#cmakedefine01 HAVE_XSW_USAGE #cmakedefine01 HAVE_PUBLIC_XSTATE_STRUCT #cmakedefine01 HAVE__FPX_SW_BYTES_WITH_XSTATE_BV #cmakedefine01 HAVE_PR_SET_PTRACER -#cmakedefine01 HAVE_SWAPCTL #cmakedefine01 HAVE_STAT_TIMESPEC #cmakedefine01 HAVE_STAT_TIM @@ -89,7 +85,6 @@ #cmakedefine01 HAVE_PTHREAD_RWLOCK_T #cmakedefine01 HAVE_PRWATCH_T #cmakedefine SIZEOF_OFF_T @SIZEOF_OFF_T@ -#cmakedefine01 HAVE_DIRENT_D_TYPE #cmakedefine01 HAVE_FPREGS_WITH_CW #cmakedefine01 HAVE_YIELD_SYSCALL diff --git a/src/coreclr/pal/src/configure.cmake b/src/coreclr/pal/src/configure.cmake index 63e67f2997539..304bc3461e9f3 100644 --- a/src/coreclr/pal/src/configure.cmake +++ b/src/coreclr/pal/src/configure.cmake @@ -117,7 +117,6 @@ if(CLR_CMAKE_TARGET_LINUX) else() check_function_exists(sysctl HAVE_SYSCTL) endif() -check_function_exists(sysinfo HAVE_SYSINFO) check_function_exists(sysconf HAVE_SYSCONF) check_function_exists(gmtime_r HAVE_GMTIME_R) check_function_exists(timegm HAVE_TIMEGM) @@ -150,8 +149,6 @@ check_struct_has_member ("struct tm" tm_gmtoff time.h HAVE_TM_GMTOFF) check_struct_has_member ("ucontext_t" uc_mcontext.gregs[0] ucontext.h HAVE_GREGSET_T) check_struct_has_member ("ucontext_t" uc_mcontext.__gregs[0] ucontext.h HAVE___GREGSET_T) check_struct_has_member ("ucontext_t" uc_mcontext.fpregs->__glibc_reserved1[0] ucontext.h HAVE_FPSTATE_GLIBC_RESERVED1) -check_struct_has_member ("struct sysinfo" mem_unit "sys/sysinfo.h" HAVE_SYSINFO_WITH_MEM_UNIT) -check_struct_has_member ("struct dirent" d_type dirent.h HAVE_DIRENT_D_TYPE) check_struct_has_member ("struct _fpchip_state" cw sys/ucontext.h HAVE_FPREGS_WITH_CW) set(CMAKE_EXTRA_INCLUDE_FILES machine/reg.h) @@ -179,7 +176,6 @@ check_cxx_symbol_exists(CHAR_BIT limits.h HAVE_CHAR_BIT) check_cxx_symbol_exists(_DEBUG sys/user.h USER_H_DEFINES_DEBUG) check_cxx_symbol_exists(_SC_PHYS_PAGES unistd.h HAVE__SC_PHYS_PAGES) check_cxx_symbol_exists(_SC_AVPHYS_PAGES unistd.h HAVE__SC_AVPHYS_PAGES) -check_cxx_symbol_exists(swapctl sys/swap.h HAVE_SWAPCTL) check_cxx_source_runs(" #include @@ -955,29 +951,6 @@ if(NOT CLR_CMAKE_USE_SYSTEM_LIBUNWIND) list(REMOVE_AT CMAKE_REQUIRED_INCLUDES 0 1) endif() -check_cxx_source_compiles(" -#include -#include -#include - -int main(int argc, char **argv) -{ - struct xswdev xsw; - - return 0; -}" HAVE_XSWDEV) - -check_cxx_source_compiles(" -#include -#include - -int main(int argc, char **argv) -{ - struct xsw_usage xsu; - - return 0; -}" HAVE_XSW_USAGE) - check_cxx_source_compiles(" #include diff --git a/src/coreclr/pal/src/misc/sysinfo.cpp b/src/coreclr/pal/src/misc/sysinfo.cpp index cd7f0304b955a..d20fa7b2aabec 100644 --- a/src/coreclr/pal/src/misc/sysinfo.cpp +++ b/src/coreclr/pal/src/misc/sysinfo.cpp @@ -40,20 +40,12 @@ Revision History: #include #endif -#if HAVE_SYSINFO -#include -#endif - #include #if HAVE_SYS_VMPARAM_H #include #endif // HAVE_SYS_VMPARAM_H -#if HAVE_XSWDEV -#include -#endif // HAVE_XSWDEV - #if HAVE_MACH_VM_TYPES_H #include #endif // HAVE_MACH_VM_TYPES_H @@ -92,10 +84,6 @@ Revision History: #include -#if HAVE_SWAPCTL -#include -#endif - SET_DEFAULT_DEBUG_CHANNEL(MISC); #ifndef __APPLE__ @@ -249,426 +237,3 @@ GetSystemInfo( LOGEXIT("GetSystemInfo returns VOID\n"); PERF_EXIT(GetSystemInfo); } - -// Get memory size multiplier based on the passed in units (k = kilo, m = mega, g = giga) -static uint64_t GetMemorySizeMultiplier(char units) -{ - switch(units) - { - case 'g': - case 'G': return 1024 * 1024 * 1024; - case 'm': - case 'M': return 1024 * 1024; - case 'k': - case 'K': return 1024; - } - - // No units multiplier - return 1; -} - -#ifndef __APPLE__ -// Try to read the MemAvailable entry from /proc/meminfo. -// Return true if the /proc/meminfo existed, the entry was present and we were able to parse it. -static bool ReadMemAvailable(uint64_t* memAvailable) -{ - bool foundMemAvailable = false; - FILE* memInfoFile = fopen("/proc/meminfo", "r"); - if (memInfoFile != NULL) - { - char *line = nullptr; - size_t lineLen = 0; - - while (getline(&line, &lineLen, memInfoFile) != -1) - { - char units = '\0'; - uint64_t available; - int fieldsParsed = sscanf(line, "MemAvailable: %" SCNu64 " %cB", &available, &units); - - if (fieldsParsed >= 1) - { - uint64_t multiplier = GetMemorySizeMultiplier(units); - *memAvailable = available * multiplier; - foundMemAvailable = true; - break; - } - } - - free(line); - fclose(memInfoFile); - } - - return foundMemAvailable; -} -#endif // __APPLE__ - -/*++ -Function: - GlobalMemoryStatusEx - -GlobalMemoryStatusEx - -Retrieves information about the system's current usage of both physical and virtual memory. - -Return Values - -This function returns a BOOL to indicate its success status. - ---*/ -BOOL -PALAPI -GlobalMemoryStatusEx( - IN OUT LPMEMORYSTATUSEX lpBuffer) -{ - - PERF_ENTRY(GlobalMemoryStatusEx); - ENTRY("GlobalMemoryStatusEx (lpBuffer=%p)\n", lpBuffer); - - lpBuffer->dwMemoryLoad = 0; - lpBuffer->ullTotalPhys = 0; - lpBuffer->ullAvailPhys = 0; - lpBuffer->ullTotalPageFile = 0; - lpBuffer->ullAvailPageFile = 0; - lpBuffer->ullTotalVirtual = 0; - lpBuffer->ullAvailVirtual = 0; - lpBuffer->ullAvailExtendedVirtual = 0; - - BOOL fRetVal = FALSE; - int rc; - - // Get the physical memory size -#if HAVE_SYSCONF && HAVE__SC_PHYS_PAGES - uint64_t physical_memory; - - // Get the Physical memory size - physical_memory = ((uint64_t)sysconf( _SC_PHYS_PAGES )) * ((uint64_t) sysconf( _SC_PAGE_SIZE )); - lpBuffer->ullTotalPhys = (DWORDLONG)physical_memory; - fRetVal = TRUE; -#elif HAVE_SYSCTL - int64_t physical_memory; - size_t length; - // Get the Physical memory size - int mib[] = { CTL_HW, HW_MEMSIZE }; - length = sizeof(INT64); - rc = sysctl(mib, 2, &physical_memory, &length, NULL, 0); - if (rc != 0) - { - ASSERT("sysctl failed for HW_MEMSIZE (%d)\n", errno); - } - else - { - lpBuffer->ullTotalPhys = (DWORDLONG)physical_memory; - fRetVal = TRUE; - } - -#endif // HAVE_SYSCTL - - // Get swap file size, consider the ability to get the values optional - // (don't return FALSE from the GlobalMemoryStatusEx) -#if HAVE_XSW_USAGE - // This is available on OSX - struct xsw_usage xsu; - int mib[] = { CTL_HW, VM_SWAPUSAGE }; - size_t length = sizeof(xsu); - rc = sysctl(mib, 2, &xsu, &length, NULL, 0); - if (rc == 0) - { - lpBuffer->ullTotalPageFile = xsu.xsu_total; - lpBuffer->ullAvailPageFile = xsu.xsu_avail; - } -#elif HAVE_XSWDEV - // E.g. FreeBSD - struct xswdev xsw; - int mib[3]; - size_t length = 2; - rc = sysctlnametomib("vm.swap_info", mib, &length); - if (rc == 0) - { - int pagesize = getpagesize(); - // Aggregate the information for all swap files on the system - for (mib[2] = 0; ; mib[2]++) - { - length = sizeof(xsw); - rc = sysctl(mib, 3, &xsw, &length, NULL, 0); - if ((rc < 0) || (xsw.xsw_version != XSWDEV_VERSION)) - { - // All the swap files were processed or coreclr was built against - // a version of headers not compatible with the current XSWDEV_VERSION. - break; - } - - DWORDLONG avail = xsw.xsw_nblks - xsw.xsw_used; - lpBuffer->ullTotalPageFile += (DWORDLONG)xsw.xsw_nblks * pagesize; - lpBuffer->ullAvailPageFile += (DWORDLONG)avail * pagesize; - } - } -#elif HAVE_SWAPCTL - struct anoninfo ai; - if (swapctl(SC_AINFO, &ai) != -1) - { - int pagesize = getpagesize(); - lpBuffer->ullTotalPageFile = ai.ani_max * pagesize; - lpBuffer->ullAvailPageFile = ai.ani_free * pagesize; - } -#elif HAVE_SYSINFO - // Linux - struct sysinfo info; - rc = sysinfo(&info); - if (rc == 0) - { - lpBuffer->ullTotalPageFile = info.totalswap; - lpBuffer->ullAvailPageFile = info.freeswap; -#if HAVE_SYSINFO_WITH_MEM_UNIT - // A newer version of the sysinfo structure represents all the sizes - // in mem_unit instead of bytes - lpBuffer->ullTotalPageFile *= info.mem_unit; - lpBuffer->ullAvailPageFile *= info.mem_unit; -#endif // HAVE_SYSINFO_WITH_MEM_UNIT - } -#endif // HAVE_SYSINFO - - // Get the physical memory in use - from it, we can get the physical memory available. - // We do this only when we have the total physical memory available. - if (lpBuffer->ullTotalPhys > 0) - { -#ifndef __APPLE__ - static volatile bool tryReadMemInfo = true; - - if (tryReadMemInfo) - { - // Ensure that we don't try to read the /proc/meminfo in successive calls to the GlobalMemoryStatusEx - // if we have failed to access the file or the file didn't contain the MemAvailable value. - tryReadMemInfo = ReadMemAvailable((uint64_t*)&lpBuffer->ullAvailPhys); - } - - if (!tryReadMemInfo) - { - // The /proc/meminfo doesn't exist or it doesn't contain the MemAvailable row or the format of the row is invalid - // Fall back to getting the available pages using sysconf. - lpBuffer->ullAvailPhys = sysconf(SYSCONF_PAGES) * sysconf(_SC_PAGE_SIZE); - } - - 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__ - } - -#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 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); - PERF_EXIT(GlobalMemoryStatusEx); - - return fRetVal; -} - -bool -PAL_ReadMemoryValueFromFile(const char* filename, uint64_t* val) -{ - bool result = false; - char *line = nullptr; - size_t lineLen = 0; - char* endptr = nullptr; - uint64_t num = 0, multiplier; - - if (val == nullptr) - return false; - - FILE* file = fopen(filename, "r"); - if (file == nullptr) - goto done; - - if (getline(&line, &lineLen, file) == -1) - goto done; - - errno = 0; - num = strtoull(line, &endptr, 0); - if (errno != 0) - goto done; - - multiplier = GetMemorySizeMultiplier(*endptr); - *val = num * multiplier; - result = true; - if (*val/multiplier != num) - result = false; -done: - if (file) - fclose(file); - free(line); - return result; -} - -#define UPDATE_CACHE_SIZE_AND_LEVEL(NEW_CACHE_SIZE, NEW_CACHE_LEVEL) if (NEW_CACHE_SIZE > cacheSize) { cacheSize = NEW_CACHE_SIZE; cacheLevel = NEW_CACHE_LEVEL; } - -size_t -PALAPI -PAL_GetLogicalProcessorCacheSizeFromOS() -{ - size_t cacheLevel = 0; - size_t cacheSize = 0; - size_t size; - -#ifdef _SC_LEVEL1_DCACHE_SIZE - size = ( size_t) sysconf(_SC_LEVEL1_DCACHE_SIZE); - UPDATE_CACHE_SIZE_AND_LEVEL(size, 1) -#endif -#ifdef _SC_LEVEL2_CACHE_SIZE - size = ( size_t) sysconf(_SC_LEVEL2_CACHE_SIZE); - UPDATE_CACHE_SIZE_AND_LEVEL(size, 2) -#endif -#ifdef _SC_LEVEL3_CACHE_SIZE - size = ( size_t) sysconf(_SC_LEVEL3_CACHE_SIZE); - UPDATE_CACHE_SIZE_AND_LEVEL(size, 3) -#endif -#ifdef _SC_LEVEL4_CACHE_SIZE - size = ( size_t) sysconf(_SC_LEVEL4_CACHE_SIZE); - UPDATE_CACHE_SIZE_AND_LEVEL(size, 4) -#endif - -#if defined(TARGET_LINUX) && !defined(HOST_ARM) && !defined(HOST_X86) - if (cacheSize == 0) - { - // - // Fallback to retrieve cachesize via /sys/.. if sysconf was not available - // for the platform. Currently musl and arm64 should be only cases to use - // this method to determine cache size. - // - size_t level; - char path_to_size_file[] = "/sys/devices/system/cpu/cpu0/cache/index-/size"; - char path_to_level_file[] = "/sys/devices/system/cpu/cpu0/cache/index-/level"; - int index = 40; - _ASSERTE(path_to_size_file[index] == '-'); - _ASSERTE(path_to_level_file[index] == '-'); - - for (int i = 0; i < 5; i++) - { - path_to_size_file[index] = (char)(48 + i); - - if (PAL_ReadMemoryValueFromFile(path_to_size_file, &size)) - { - path_to_level_file[index] = (char)(48 + i); - - if (PAL_ReadMemoryValueFromFile(path_to_level_file, &level)) - { - UPDATE_CACHE_SIZE_AND_LEVEL(size, level) - } - else - { - cacheSize = std::max(cacheSize, size); - } - } - } - } -#endif - -#if (defined(HOST_ARM64) || defined(HOST_LOONGARCH64)) && !defined(TARGET_OSX) - if (cacheSize == 0) - { - // We expect to get the L3 cache size for Arm64 but currently expected to be missing that info - // from most of the machines with an exceptions on some machines. - // - // _SC_LEVEL*_*CACHE_SIZE is not yet present. Work is in progress to enable this for arm64 - // - // /sys/devices/system/cpu/cpu*/cache/index*/ is also not yet present in most systems. - // Arm64 patch is in Linux kernel tip. - // - // midr_el1 is available in "/sys/devices/system/cpu/cpu0/regs/identification/midr_el1", - // but without an exhaustive list of ARM64 processors any decode of midr_el1 - // Would likely be incomplete - - // Published information on ARM64 architectures is limited. - // If we use recent high core count chips as a guide for state of the art, we find - // total L3 cache to be 1-2MB/core. As always, there are exceptions. - - // Estimate cache size based on CPU count - // Assume lower core count are lighter weight parts which are likely to have smaller caches - // Assume L3$/CPU grows linearly from 256K to 1.5M/CPU as logicalCPUs grows from 2 to 12 CPUs - DWORD logicalCPUs = PAL_GetLogicalCpuCountFromOS(); - - cacheSize = logicalCPUs*std::min(1536, std::max(256, (int)logicalCPUs*128))*1024; - } -#endif - -#if HAVE_SYSCTLBYNAME - if (cacheSize == 0) - { - int64_t cacheSizeFromSysctl = 0; - size_t sz = sizeof(cacheSizeFromSysctl); - const bool success = false - // macOS: Since macOS 12.0, Apple added ".perflevelX." to determinate cache sizes for efficiency - // and performance cores separately. "perflevel0" stands for "performance" - || sysctlbyname("hw.perflevel0.l3cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0 - || sysctlbyname("hw.perflevel0.l2cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0 - // macOS: these report cache sizes for efficiency cores only: - || sysctlbyname("hw.l3cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0 - || sysctlbyname("hw.l2cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0 - || sysctlbyname("hw.l1dcachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0; - if (success) - { - _ASSERTE(cacheSizeFromSysctl > 0); - cacheSize = ( size_t) cacheSizeFromSysctl; - } - } -#endif - -#if (defined(HOST_ARM64) || defined(HOST_LOONGARCH64)) && !defined(TARGET_OSX) - if (cacheLevel != 3) - { - // We expect to get the L3 cache size for Arm64 but currently expected to be missing that info - // from most of the machines. - // Hence, just use the following heuristics at best depending on the CPU count - // 1 ~ 4 : 4 MB - // 5 ~ 16 : 8 MB - // 17 ~ 64 : 16 MB - // 65+ : 32 MB - DWORD logicalCPUs = PAL_GetLogicalCpuCountFromOS(); - if (logicalCPUs < 5) - { - cacheSize = 4; - } - else if (logicalCPUs < 17) - { - cacheSize = 8; - } - else if (logicalCPUs < 65) - { - cacheSize = 16; - } - else - { - cacheSize = 32; - } - - cacheSize *= (1024 * 1024); - } -#endif - - return cacheSize; -} diff --git a/src/coreclr/pal/tests/palsuite/CMakeLists.txt b/src/coreclr/pal/tests/palsuite/CMakeLists.txt index e3698bfc25775..f9166670e1240 100644 --- a/src/coreclr/pal/tests/palsuite/CMakeLists.txt +++ b/src/coreclr/pal/tests/palsuite/CMakeLists.txt @@ -487,7 +487,6 @@ add_executable_clr(paltests miscellaneous/GetLastError/test1/test.cpp miscellaneous/GetSystemInfo/test1/test.cpp miscellaneous/GetTickCount/test1/test.cpp - miscellaneous/GlobalMemoryStatusEx/test1/test.cpp miscellaneous/InterlockedCompareExchange/test1/test.cpp miscellaneous/InterlockedCompareExchange/test2/test.cpp miscellaneous/InterlockedCompareExchange64/test1/test.cpp diff --git a/src/coreclr/pal/tests/palsuite/compilableTests.txt b/src/coreclr/pal/tests/palsuite/compilableTests.txt index 37191618934cf..8b388c44004b9 100644 --- a/src/coreclr/pal/tests/palsuite/compilableTests.txt +++ b/src/coreclr/pal/tests/palsuite/compilableTests.txt @@ -387,7 +387,6 @@ miscellaneous/GetEnvironmentVariableW/test6/paltest_getenvironmentvariablew_test miscellaneous/GetLastError/test1/paltest_getlasterror_test1 miscellaneous/GetSystemInfo/test1/paltest_getsysteminfo_test1 miscellaneous/GetTickCount/test1/paltest_gettickcount_test1 -miscellaneous/GlobalMemoryStatusEx/test1/paltest_globalmemorystatusex_test1 miscellaneous/InterlockedCompareExchange/test1/paltest_interlockedcompareexchange_test1 miscellaneous/InterlockedCompareExchange/test2/paltest_interlockedcompareexchange_test2 miscellaneous/InterlockedCompareExchange64/test1/paltest_interlockedcompareexchange64_test1 diff --git a/src/coreclr/pal/tests/palsuite/miscellaneous/GlobalMemoryStatusEx/test1/test.cpp b/src/coreclr/pal/tests/palsuite/miscellaneous/GlobalMemoryStatusEx/test1/test.cpp deleted file mode 100644 index 49e01ffdb4075..0000000000000 --- a/src/coreclr/pal/tests/palsuite/miscellaneous/GlobalMemoryStatusEx/test1/test.cpp +++ /dev/null @@ -1,55 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -/*============================================================ -** -** Source : test.c -** -** Purpose: Test for GlobalMemoryStatusEx() function -** -** -**=========================================================*/ - -#include - -PALTEST(miscellaneous_GlobalMemoryStatusEx_test1_paltest_globalmemorystatusex_test1, "miscellaneous/GlobalMemoryStatusEx/test1/paltest_globalmemorystatusex_test1") -{ - - MEMORYSTATUSEX memoryStatus; - - /* - * Initialize the PAL and return FAILURE if this fails - */ - - if(0 != (PAL_Initialize(argc, argv))) - { - return FAIL; - } - - if (!GlobalMemoryStatusEx(&memoryStatus)) - { - Fail("ERROR: GlobalMemoryStatusEx failed."); - } - - printf("GlobalMemoryStatusEx:\n"); - printf(" ullTotalPhys: %" PRIu64 "\n", memoryStatus.ullTotalPhys); - printf(" ullAvailPhys: %" PRIu64 "\n", memoryStatus.ullAvailPhys); - printf(" ullTotalVirtual: %" PRIu64 "\n", memoryStatus.ullTotalVirtual); - printf(" ullAvailVirtual: %" PRIu64 "\n", memoryStatus.ullAvailVirtual); - printf(" ullTotalPageFile: %" PRIu64 "\n", memoryStatus.ullTotalPageFile); - printf(" ullAvailPageFile: %" PRIu64 "\n", memoryStatus.ullAvailPageFile); - printf(" ullAvailExtendedVirtual: %" PRIu64 "\n", memoryStatus.ullAvailExtendedVirtual); - printf(" dwMemoryLoad: %u\n", memoryStatus.dwMemoryLoad); - - if (memoryStatus.ullTotalPhys == 0 || - memoryStatus.ullAvailPhys == 0 || - memoryStatus.ullTotalVirtual == 0 || - memoryStatus.ullAvailVirtual == 0 - ) - { - Fail("ERROR: GlobalMemoryStatusEx succeeded, but returned zero physical of virtual memory sizes."); - } - - PAL_Terminate(); - return PASS; -} diff --git a/src/coreclr/pal/tests/palsuite/paltestlist.txt b/src/coreclr/pal/tests/palsuite/paltestlist.txt index e2fdbdd79f411..7491006cc7e60 100644 --- a/src/coreclr/pal/tests/palsuite/paltestlist.txt +++ b/src/coreclr/pal/tests/palsuite/paltestlist.txt @@ -340,7 +340,6 @@ miscellaneous/GetEnvironmentVariableW/test5/paltest_getenvironmentvariablew_test miscellaneous/GetEnvironmentVariableW/test6/paltest_getenvironmentvariablew_test6 miscellaneous/GetLastError/test1/paltest_getlasterror_test1 miscellaneous/GetSystemInfo/test1/paltest_getsysteminfo_test1 -miscellaneous/GlobalMemoryStatusEx/test1/paltest_globalmemorystatusex_test1 miscellaneous/GetTickCount/test1/paltest_gettickcount_test1 miscellaneous/InterlockedCompareExchange/test1/paltest_interlockedcompareexchange_test1 miscellaneous/InterlockedCompareExchange/test2/paltest_interlockedcompareexchange_test2 From 2dfe05181385e2317311e6be97f48b7e831c1fca Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Mon, 4 Sep 2023 09:29:17 -0700 Subject: [PATCH 2/2] FB --- src/coreclr/pal/src/misc/sysinfo.cpp | 53 ++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/coreclr/pal/src/misc/sysinfo.cpp b/src/coreclr/pal/src/misc/sysinfo.cpp index d20fa7b2aabec..499b905180950 100644 --- a/src/coreclr/pal/src/misc/sysinfo.cpp +++ b/src/coreclr/pal/src/misc/sysinfo.cpp @@ -237,3 +237,56 @@ GetSystemInfo( LOGEXIT("GetSystemInfo returns VOID\n"); PERF_EXIT(GetSystemInfo); } + +// Get memory size multiplier based on the passed in units (k = kilo, m = mega, g = giga) +static uint64_t GetMemorySizeMultiplier(char units) +{ + switch(units) + { + case 'g': + case 'G': return 1024 * 1024 * 1024; + case 'm': + case 'M': return 1024 * 1024; + case 'k': + case 'K': return 1024; + } + + // No units multiplier + return 1; +} + +bool +PAL_ReadMemoryValueFromFile(const char* filename, uint64_t* val) +{ + bool result = false; + char *line = nullptr; + size_t lineLen = 0; + char* endptr = nullptr; + uint64_t num = 0, multiplier; + + if (val == nullptr) + return false; + + FILE* file = fopen(filename, "r"); + if (file == nullptr) + goto done; + + if (getline(&line, &lineLen, file) == -1) + goto done; + + errno = 0; + num = strtoull(line, &endptr, 0); + if (errno != 0) + goto done; + + multiplier = GetMemorySizeMultiplier(*endptr); + *val = num * multiplier; + result = true; + if (*val/multiplier != num) + result = false; +done: + if (file) + fclose(file); + free(line); + return result; +}