#include <System/machine/cpu_capabilities.h>
#include <darwintest.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/sysctl.h>
T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
#ifndef _COMM_PAGE_CPU_QUIESCENT_COUNTER
T_DECL(test_quiescent_counter, "Validate that _COMM_PAGE_CPU_QUIESCENT_COUNTER increments",
T_META_CHECK_LEAKS(false))
{
T_SKIP("_COMM_PAGE_CPU_QUIESCENT_COUNTER doesn't exist on this system");
}
#else
T_DECL(test_quiescent_counter, "Validate that _COMM_PAGE_CPU_QUIESCENT_COUNTER increments",
T_META_CHECK_LEAKS(false))
{
int rv;
uint32_t cpu_checkin_min_interval = 0;
size_t value_size = sizeof(cpu_checkin_min_interval);
rv = sysctlbyname("kern.cpu_checkin_interval", &cpu_checkin_min_interval, &value_size, NULL, 0);
T_ASSERT_POSIX_SUCCESS(rv, "sysctlbyname(kern.cpu_checkin_interval)");
T_LOG("kern.cpu_checkin_interval is %d", cpu_checkin_min_interval);
T_ASSERT_GT(cpu_checkin_min_interval, 0, "kern.cpu_checkin_interval should be > 0");
uint64_t* commpage_addr = (uint64_t *)(uintptr_t)_COMM_PAGE_CPU_QUIESCENT_COUNTER;
T_LOG("address of _COMM_PAGE_CPU_QUIESCENT_COUNTER is %p", (void*) commpage_addr);
uint64_t counter = *commpage_addr;
uint64_t last_counter = counter;
T_LOG("first value of _COMM_PAGE_CPU_QUIESCENT_COUNTER is %llu", counter);
for (int i = 0; i < 10; i++) {
sleep(1);
last_counter = counter;
counter = *commpage_addr;
T_LOG("value of _COMM_PAGE_CPU_QUIESCENT_COUNTER is %llu", counter);
T_ASSERT_GT(counter, last_counter, "_COMM_PAGE_CPU_QUIESCENT_COUNTER must monotonically increase at least once per second");
}
}
#endif