#include <darwintest.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <mach/mach.h>
#include <sys/utsname.h>
#include <TargetConditionals.h>
#define WIRED_MEM_THRESHOLD_PERCENTAGE 30
T_DECL(wired_mem_bench,
"report the amount of wired memory consumed by the booted OS; guard against egregious or unexpected regressions",
T_META_CHECK_LEAKS(false),
T_META_ASROOT(true),
T_META_REQUIRES_REBOOT(true)) {
vm_statistics64_data_t stat;
uint64_t memsize;
vm_size_t page_size = 0;
unsigned int count = HOST_VM_INFO64_COUNT;
kern_return_t ret;
int wired_mem_pct;
struct utsname uname_vers;
T_SETUPBEGIN;
ret = uname(&uname_vers);
T_QUIET;
T_ASSERT_POSIX_SUCCESS(ret, "uname()");
if (strnstr(uname_vers.version, "KASAN", sizeof(uname_vers.version)) != NULL) {
T_SKIP("wired memory metrics are not meaningful on KASAN kernels.");
}
ret = host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t)&stat, &count);
T_QUIET;
T_ASSERT_MACH_SUCCESS(ret, "wired memory query via host_statistics64()");
size_t s = sizeof(memsize);
ret = sysctlbyname("hw.memsize", &memsize, &s, NULL, 0);
T_QUIET;
T_ASSERT_POSIX_SUCCESS(ret, "sysctlbyname(\"hw.memsize\")");
T_QUIET;
T_EXPECT_NE(memsize, 0ULL, "hw.memsize sysctl failed to provide device DRAM size");
ret = host_page_size(mach_host_self(), &page_size);
T_QUIET;
T_ASSERT_MACH_SUCCESS(ret, "page size query via host_page_size()");
T_SETUPEND;
T_PERF("wired_memory", (double)(stat.wire_count * (mach_vm_size_t)vm_kernel_page_size >> 10), "kB",
"Wired memory at boot");
T_LOG("\nwired memory: %llu kB (%llu MB)\n", stat.wire_count * (mach_vm_size_t)vm_kernel_page_size >> 10,
stat.wire_count * (mach_vm_size_t)vm_kernel_page_size >> 20);
#if TARGET_OS_IOS || TARGET_OS_OSX
int r;
if ((r = system("zprint")) != 0) {
T_FAIL("couldn't run zprint: %d", r);
}
#endif
wired_mem_pct = (int)((stat.wire_count * page_size * 100ULL) / memsize);
T_PERF("wired_memory_percentage", wired_mem_pct, "%", "Wired memory as percentage of device DRAM size");
T_ASSERT_LT(wired_mem_pct, WIRED_MEM_THRESHOLD_PERCENTAGE,
"Wired memory percentage is below allowable threshold (%llu bytes / %u pages / %llu total device memory)",
(uint64_t)stat.wire_count * page_size, stat.wire_count, memsize);
}