From 3e0d97db2ea0e878c285b9b61adccb39fec02605 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 8 Nov 2023 00:02:00 +0800 Subject: [PATCH] Platform: detect system page size --- src/detection/memory/memory_apple.c | 7 +------ src/detection/memory/memory_bsd.c | 7 +------ src/detection/swap/swap_windows.c | 9 ++++----- src/modules/title/title.c | 1 + src/util/platform/FFPlatform.h | 2 ++ src/util/platform/FFPlatform_unix.c | 13 +++++++++++++ src/util/platform/FFPlatform_windows.c | 8 +++++--- 7 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/detection/memory/memory_apple.c b/src/detection/memory/memory_apple.c index 752c588c4..b3a50a1eb 100644 --- a/src/detection/memory/memory_apple.c +++ b/src/detection/memory/memory_apple.c @@ -11,17 +11,12 @@ const char* ffDetectMemory(FFMemoryResult* ram) if (sysctl((int[]){ CTL_HW, HW_MEMSIZE }, 2, &ram->bytesTotal, &length, NULL, 0)) return "Failed to read hw.memsize"; - uint32_t pagesize; - length = sizeof(pagesize); - if (sysctl((int[]){ CTL_HW, HW_PAGESIZE }, 2, &pagesize, &length, NULL, 0)) - return "Failed to read hw.pagesize"; - mach_msg_type_number_t count = HOST_VM_INFO64_COUNT; vm_statistics64_data_t vmstat; if(host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t) (&vmstat), &count) != KERN_SUCCESS) return "Failed to read host_statistics64"; - ram->bytesUsed = ((uint64_t) vmstat.active_count + vmstat.wire_count) * pagesize; + ram->bytesUsed = ((uint64_t) vmstat.active_count + vmstat.wire_count) * instance.state.platform.pageSize; return NULL; } diff --git a/src/detection/memory/memory_bsd.c b/src/detection/memory/memory_bsd.c index 95e587a25..9af77cb70 100644 --- a/src/detection/memory/memory_bsd.c +++ b/src/detection/memory/memory_bsd.c @@ -7,17 +7,12 @@ const char* ffDetectMemory(FFMemoryResult* ram) if (sysctl((int[]){ CTL_HW, HW_PHYSMEM }, 2, &ram->bytesTotal, &length, NULL, 0)) return "Failed to read hw.physmem"; - uint32_t pageSize; - length = sizeof(pageSize); - if (sysctl((int[]){ CTL_HW, HW_PAGESIZE }, 2, &pageSize, &length, NULL, 0)) - return "Failed to read hw.pagesize"; - // vm.stats.vm.* are int values int32_t pagesFree = ffSysctlGetInt("vm.stats.vm.v_free_count", 0) + ffSysctlGetInt("vm.stats.vm.v_inactive_count", 0) + ffSysctlGetInt("vm.stats.vm.v_cache_count", 0); - ram->bytesUsed = ram->bytesTotal - (uint64_t) pagesFree * pageSize; + ram->bytesUsed = ram->bytesTotal - (uint64_t) pagesFree * instance.state.platform.pageSize; return NULL; } diff --git a/src/detection/swap/swap_windows.c b/src/detection/swap/swap_windows.c index f541f1964..a5788a358 100644 --- a/src/detection/swap/swap_windows.c +++ b/src/detection/swap/swap_windows.c @@ -7,9 +7,6 @@ const char* ffDetectSwap(FFSwapResult* swap) { - SYSTEM_INFO sysInfo; - GetNativeSystemInfo(&sysInfo); - ULONG size = sizeof(SYSTEM_PAGEFILE_INFORMATION); SYSTEM_PAGEFILE_INFORMATION* FF_AUTO_FREE pstart = (SYSTEM_PAGEFILE_INFORMATION*)malloc(size); while(true) @@ -24,8 +21,10 @@ const char* ffDetectSwap(FFSwapResult* swap) return "NtQuerySystemInformation(SystemPagefileInformation, size) failed"; break; } - swap->bytesUsed = (uint64_t)pstart->TotalUsed * sysInfo.dwPageSize; - swap->bytesTotal = (uint64_t)pstart->CurrentSize * sysInfo.dwPageSize; + + uint32_t pageSize = instance.state.platform.pageSize; + swap->bytesUsed = (uint64_t)pstart->TotalUsed * pageSize; + swap->bytesTotal = (uint64_t)pstart->CurrentSize * pageSize; return NULL; } diff --git a/src/modules/title/title.c b/src/modules/title/title.c index daad8413f..3e9f7cece 100644 --- a/src/modules/title/title.c +++ b/src/modules/title/title.c @@ -176,6 +176,7 @@ void ffGenerateTitleJsonResult(FF_MAYBE_UNUSED FFTitleOptions* options, yyjson_m yyjson_mut_obj_add_strbuf(doc, obj, "homeDir", &instance.state.platform.homeDir); yyjson_mut_obj_add_strbuf(doc, obj, "exePath", &instance.state.platform.exePath); yyjson_mut_obj_add_strbuf(doc, obj, "userShell", &instance.state.platform.userShell); + yyjson_mut_obj_add_uint(doc, obj, "pageSize", instance.state.platform.pageSize); } void ffPrintTitleHelpFormat(void) diff --git a/src/util/platform/FFPlatform.h b/src/util/platform/FFPlatform.h index 48c72513e..6386cf479 100644 --- a/src/util/platform/FFPlatform.h +++ b/src/util/platform/FFPlatform.h @@ -22,6 +22,8 @@ typedef struct FFPlatform { FFstrbuf systemVersion; FFstrbuf systemArchitecture; FFstrbuf systemDisplayVersion; + + uint32_t pageSize; } FFPlatform; void ffPlatformInit(FFPlatform* platform); diff --git a/src/util/platform/FFPlatform_unix.c b/src/util/platform/FFPlatform_unix.c index 33c76bc69..04687f0be 100644 --- a/src/util/platform/FFPlatform_unix.c +++ b/src/util/platform/FFPlatform_unix.c @@ -10,6 +10,7 @@ #ifdef __APPLE__ #include + #include #elif defined(__FreeBSD__) #include #endif @@ -153,6 +154,16 @@ static void getUserShell(FFPlatform* platform, const struct passwd* pwd) ffStrbufAppendS(&platform->userShell, shell); } +static void getPageSize(FFPlatform* platform) +{ + #if defined(__FreeBSD__) || defined(__APPLE__) + size_t length = sizeof(platform->pageSize); + sysctl((int[]){ CTL_HW, HW_PAGESIZE }, 2, &platform->pageSize, &length, NULL, 0); + #else + platform->pageSize = (uint32_t) sysconf(_SC_PAGESIZE); + #endif +} + void ffPlatformInitImpl(FFPlatform* platform) { struct passwd* pwd = getpwuid(getuid()); @@ -176,4 +187,6 @@ void ffPlatformInitImpl(FFPlatform* platform) ffStrbufAppendS(&platform->systemVersion, uts.version); ffStrbufAppendS(&platform->systemArchitecture, uts.machine); ffStrbufInit(&platform->systemDisplayVersion); + + getPageSize(platform); } diff --git a/src/util/platform/FFPlatform_windows.c b/src/util/platform/FFPlatform_windows.c index 0774c81c3..59696df43 100644 --- a/src/util/platform/FFPlatform_windows.c +++ b/src/util/platform/FFPlatform_windows.c @@ -183,9 +183,9 @@ static void getSystemReleaseAndVersion(FFPlatform* platform) } } -static void getSystemArchitecture(FFPlatform* platform) +static void getSystemArchitectureAndPageSize(FFPlatform* platform) { - SYSTEM_INFO sysInfo = {0}; + SYSTEM_INFO sysInfo; GetNativeSystemInfo(&sysInfo); switch(sysInfo.wProcessorArchitecture) @@ -215,6 +215,8 @@ static void getSystemArchitecture(FFPlatform* platform) default: break; } + + platform->pageSize = sysInfo.dwPageSize; } void ffPlatformInitImpl(FFPlatform* platform) @@ -230,5 +232,5 @@ void ffPlatformInitImpl(FFPlatform* platform) getUserShell(platform); getSystemReleaseAndVersion(platform); - getSystemArchitecture(platform); + getSystemArchitectureAndPageSize(platform); }