Skip to content

Commit 3e0d97d

Browse files
committed
Platform: detect system page size
1 parent 864e707 commit 3e0d97d

File tree

7 files changed

+27
-20
lines changed

7 files changed

+27
-20
lines changed

src/detection/memory/memory_apple.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,12 @@ const char* ffDetectMemory(FFMemoryResult* ram)
1111
if (sysctl((int[]){ CTL_HW, HW_MEMSIZE }, 2, &ram->bytesTotal, &length, NULL, 0))
1212
return "Failed to read hw.memsize";
1313

14-
uint32_t pagesize;
15-
length = sizeof(pagesize);
16-
if (sysctl((int[]){ CTL_HW, HW_PAGESIZE }, 2, &pagesize, &length, NULL, 0))
17-
return "Failed to read hw.pagesize";
18-
1914
mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
2015
vm_statistics64_data_t vmstat;
2116
if(host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t) (&vmstat), &count) != KERN_SUCCESS)
2217
return "Failed to read host_statistics64";
2318

24-
ram->bytesUsed = ((uint64_t) vmstat.active_count + vmstat.wire_count) * pagesize;
19+
ram->bytesUsed = ((uint64_t) vmstat.active_count + vmstat.wire_count) * instance.state.platform.pageSize;
2520

2621
return NULL;
2722
}

src/detection/memory/memory_bsd.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@ const char* ffDetectMemory(FFMemoryResult* ram)
77
if (sysctl((int[]){ CTL_HW, HW_PHYSMEM }, 2, &ram->bytesTotal, &length, NULL, 0))
88
return "Failed to read hw.physmem";
99

10-
uint32_t pageSize;
11-
length = sizeof(pageSize);
12-
if (sysctl((int[]){ CTL_HW, HW_PAGESIZE }, 2, &pageSize, &length, NULL, 0))
13-
return "Failed to read hw.pagesize";
14-
1510
// vm.stats.vm.* are int values
1611
int32_t pagesFree = ffSysctlGetInt("vm.stats.vm.v_free_count", 0)
1712
+ ffSysctlGetInt("vm.stats.vm.v_inactive_count", 0)
1813
+ ffSysctlGetInt("vm.stats.vm.v_cache_count", 0);
1914

20-
ram->bytesUsed = ram->bytesTotal - (uint64_t) pagesFree * pageSize;
15+
ram->bytesUsed = ram->bytesTotal - (uint64_t) pagesFree * instance.state.platform.pageSize;
2116

2217
return NULL;
2318
}

src/detection/swap/swap_windows.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
const char* ffDetectSwap(FFSwapResult* swap)
99
{
10-
SYSTEM_INFO sysInfo;
11-
GetNativeSystemInfo(&sysInfo);
12-
1310
ULONG size = sizeof(SYSTEM_PAGEFILE_INFORMATION);
1411
SYSTEM_PAGEFILE_INFORMATION* FF_AUTO_FREE pstart = (SYSTEM_PAGEFILE_INFORMATION*)malloc(size);
1512
while(true)
@@ -24,8 +21,10 @@ const char* ffDetectSwap(FFSwapResult* swap)
2421
return "NtQuerySystemInformation(SystemPagefileInformation, size) failed";
2522
break;
2623
}
27-
swap->bytesUsed = (uint64_t)pstart->TotalUsed * sysInfo.dwPageSize;
28-
swap->bytesTotal = (uint64_t)pstart->CurrentSize * sysInfo.dwPageSize;
24+
25+
uint32_t pageSize = instance.state.platform.pageSize;
26+
swap->bytesUsed = (uint64_t)pstart->TotalUsed * pageSize;
27+
swap->bytesTotal = (uint64_t)pstart->CurrentSize * pageSize;
2928

3029
return NULL;
3130
}

src/modules/title/title.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ void ffGenerateTitleJsonResult(FF_MAYBE_UNUSED FFTitleOptions* options, yyjson_m
176176
yyjson_mut_obj_add_strbuf(doc, obj, "homeDir", &instance.state.platform.homeDir);
177177
yyjson_mut_obj_add_strbuf(doc, obj, "exePath", &instance.state.platform.exePath);
178178
yyjson_mut_obj_add_strbuf(doc, obj, "userShell", &instance.state.platform.userShell);
179+
yyjson_mut_obj_add_uint(doc, obj, "pageSize", instance.state.platform.pageSize);
179180
}
180181

181182
void ffPrintTitleHelpFormat(void)

src/util/platform/FFPlatform.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ typedef struct FFPlatform {
2222
FFstrbuf systemVersion;
2323
FFstrbuf systemArchitecture;
2424
FFstrbuf systemDisplayVersion;
25+
26+
uint32_t pageSize;
2527
} FFPlatform;
2628

2729
void ffPlatformInit(FFPlatform* platform);

src/util/platform/FFPlatform_unix.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#ifdef __APPLE__
1212
#include <libproc.h>
13+
#include <sys/sysctl.h>
1314
#elif defined(__FreeBSD__)
1415
#include <sys/sysctl.h>
1516
#endif
@@ -153,6 +154,16 @@ static void getUserShell(FFPlatform* platform, const struct passwd* pwd)
153154
ffStrbufAppendS(&platform->userShell, shell);
154155
}
155156

157+
static void getPageSize(FFPlatform* platform)
158+
{
159+
#if defined(__FreeBSD__) || defined(__APPLE__)
160+
size_t length = sizeof(platform->pageSize);
161+
sysctl((int[]){ CTL_HW, HW_PAGESIZE }, 2, &platform->pageSize, &length, NULL, 0);
162+
#else
163+
platform->pageSize = (uint32_t) sysconf(_SC_PAGESIZE);
164+
#endif
165+
}
166+
156167
void ffPlatformInitImpl(FFPlatform* platform)
157168
{
158169
struct passwd* pwd = getpwuid(getuid());
@@ -176,4 +187,6 @@ void ffPlatformInitImpl(FFPlatform* platform)
176187
ffStrbufAppendS(&platform->systemVersion, uts.version);
177188
ffStrbufAppendS(&platform->systemArchitecture, uts.machine);
178189
ffStrbufInit(&platform->systemDisplayVersion);
190+
191+
getPageSize(platform);
179192
}

src/util/platform/FFPlatform_windows.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ static void getSystemReleaseAndVersion(FFPlatform* platform)
183183
}
184184
}
185185

186-
static void getSystemArchitecture(FFPlatform* platform)
186+
static void getSystemArchitectureAndPageSize(FFPlatform* platform)
187187
{
188-
SYSTEM_INFO sysInfo = {0};
188+
SYSTEM_INFO sysInfo;
189189
GetNativeSystemInfo(&sysInfo);
190190

191191
switch(sysInfo.wProcessorArchitecture)
@@ -215,6 +215,8 @@ static void getSystemArchitecture(FFPlatform* platform)
215215
default:
216216
break;
217217
}
218+
219+
platform->pageSize = sysInfo.dwPageSize;
218220
}
219221

220222
void ffPlatformInitImpl(FFPlatform* platform)
@@ -230,5 +232,5 @@ void ffPlatformInitImpl(FFPlatform* platform)
230232
getUserShell(platform);
231233

232234
getSystemReleaseAndVersion(platform);
233-
getSystemArchitecture(platform);
235+
getSystemArchitectureAndPageSize(platform);
234236
}

0 commit comments

Comments
 (0)