Merge pull request #339 from DotDebian/feat/cpu-time-clocks

feat(libkernel): support the CPU-time clocks in clock_gettime and clock_getres on Windows
This commit is contained in:
Dean
2026-10-04 14:21:40 +13:00
committed by GitHub
3 changed files with 171 additions and 0 deletions
+10
View File
@@ -166,6 +166,16 @@ if(BUILD_TESTING)
set_tests_properties(guest_cond_timedwait PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
endif()
add_test_executable(guest_cpu_time_clocks_tests ${LIBS_INCLUDE_DIR}/tests/GuestCpuTimeClocks.cpp)
target_include_directories(guest_cpu_time_clocks_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_cpu_time_clocks_tests PRIVATE libkernel libc)
configure_windows_unwind(guest_cpu_time_clocks_tests)
add_test(NAME guest_cpu_time_clocks COMMAND guest_cpu_time_clocks_tests)
set_tests_properties(guest_cpu_time_clocks PROPERTIES TIMEOUT 20)
if(WIN32)
set_tests_properties(guest_cpu_time_clocks PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
endif()
add_test_executable(guest_pthread_sem_tests ${LIBS_INCLUDE_DIR}/tests/GuestPthreadSem.cpp)
target_include_directories(guest_pthread_sem_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_pthread_sem_tests PRIVATE libkernel libc)
+44
View File
@@ -14,6 +14,7 @@
#include <vector>
#include <stdexcept>
#include <string>
#include <system_error>
#include <x86intrin.h>
#ifdef _WIN32
@@ -250,6 +251,37 @@ int APS5_VABI usleep_nid_postfix(KernelUseconds microseconds) {
return 0;
}
#ifdef _WIN32
static std::uint64_t CpuTimeNanos(bool callingThread) {
FILETIME creation{};
FILETIME exitTime{};
FILETIME kernel{};
FILETIME user{};
if (callingThread) {
if (!GetThreadTimes(GetCurrentThread(), &creation, &exitTime, &kernel, &user))
throw std::system_error(GetLastError(), std::system_category(), "clock_gettime: GetThreadTimes failed");
} else {
if (!GetProcessTimes(GetCurrentProcess(), &creation, &exitTime, &kernel, &user))
throw std::system_error(GetLastError(), std::system_category(), "clock_gettime: GetProcessTimes failed");
}
const auto toTicks = [](const FILETIME& time) {
return (static_cast<std::uint64_t>(time.dwHighDateTime) << 32) | time.dwLowDateTime;
};
return (toTicks(kernel) + toTicks(user)) * 100ULL;
}
static std::uint64_t CpuTimeResolutionNanos() {
DWORD adjustment = 0;
DWORD increment = 0;
BOOL adjustmentDisabled = FALSE;
if (!GetSystemTimeAdjustment(&adjustment, &increment, &adjustmentDisabled))
throw std::system_error(GetLastError(), std::system_category(), "clock_getres: GetSystemTimeAdjustment failed");
if (increment == 0)
throw std::runtime_error("clock_getres: GetSystemTimeAdjustment reported no clock interrupt period");
return static_cast<std::uint64_t>(increment) * 100ULL;
}
#endif
int APS5_VABI clock_gettime_nid_postfix(int clockId, KernelTimespec* tp) {
if (tp == nullptr) {
APS5_INVALID_ARG_EX;
@@ -281,6 +313,12 @@ int APS5_VABI clock_gettime_nid_postfix(int clockId, KernelTimespec* tp) {
tp->tv_nsec = static_cast<std::int64_t>(nanos % 1000000000ULL);
return 0;
}
if (clockId == 14 || clockId == 15) {
const std::uint64_t nanos = CpuTimeNanos(clockId == 14);
tp->tv_sec = static_cast<std::int64_t>(nanos / 1000000000ULL);
tp->tv_nsec = static_cast<std::int64_t>(nanos % 1000000000ULL);
return 0;
}
throw std::runtime_error(std::string(__func__) + ": unsupported clock_id " + std::to_string(clockId));
#else
clockid_t nativeId;
@@ -373,6 +411,12 @@ int APS5_VABI clock_getres_nid_postfix(int clockId, KernelTimespec* res) {
res->tv_nsec = static_cast<std::int64_t>(nsPerTick);
return 0;
}
if (clockId == 14 || clockId == 15) {
const std::uint64_t nanos = CpuTimeResolutionNanos();
res->tv_sec = static_cast<std::int64_t>(nanos / 1000000000ULL);
res->tv_nsec = static_cast<std::int64_t>(nanos % 1000000000ULL);
return 0;
}
throw std::runtime_error(std::string(__func__) + ": unsupported clock_id " + std::to_string(clockId));
#else
clockid_t nativeId;
+117
View File
@@ -0,0 +1,117 @@
#include "SceTypes.hpp"
#include <atomic>
#include <cstdint>
#include <cstdlib>
extern "C" {
int APS5_VABI clock_gettime_nid_postfix(int clockId, KernelTimespec* tp);
int APS5_VABI clock_getres_nid_postfix(int clockId, KernelTimespec* res);
int APS5_VABI sceKernelClockGettime(KernelClockid clockId, KernelTimespec* tp);
int APS5_VABI sceKernelClockGetres(KernelClockid clockId, KernelTimespec* tp);
int APS5_VABI sceKernelUsleep_nid_postfix(KernelUseconds microseconds);
int APS5_VABI scePthreadCreate(Pthread* thread, const PthreadAttr* attr, PthreadEntry entry, void* arg, const char* name);
int APS5_VABI scePthreadJoin(Pthread thread, void** retval);
}
static constexpr int SCE_OK = 0;
static constexpr int SCE_KERNEL_ERROR_EFAULT = static_cast<int>(0x8002000E);
static constexpr int GUEST_CLOCK_MONOTONIC = 4;
static constexpr int GUEST_CLOCK_THREAD_CPUTIME_ID = 14;
static constexpr int GUEST_CLOCK_PROCESS_CPUTIME_ID = 15;
static constexpr std::int64_t NANOS_PER_SECOND = 1000000000LL;
static constexpr std::int64_t NANOS_PER_MILLISECOND = 1000000LL;
static constexpr std::int64_t BURN_NANOS = 200 * NANOS_PER_MILLISECOND;
static constexpr std::int64_t TICK_MARGIN_NANOS = 50 * NANOS_PER_MILLISECOND;
static constexpr std::int64_t GIVE_UP_NANOS = 10 * NANOS_PER_SECOND;
static void Require(bool value) { if (!value) std::abort(); }
static std::int64_t Nanos(int clockId) {
KernelTimespec time{-1, -1};
Require(clock_gettime_nid_postfix(clockId, &time) == SCE_OK);
Require(time.tv_sec >= 0);
Require(time.tv_nsec >= 0 && time.tv_nsec < NANOS_PER_SECOND);
return time.tv_sec * NANOS_PER_SECOND + time.tv_nsec;
}
static void RequireResolution(int clockId) {
KernelTimespec resolution{-1, -1};
Require(clock_getres_nid_postfix(clockId, &resolution) == SCE_OK);
Require(resolution.tv_sec == 0);
Require(resolution.tv_nsec > 0 && resolution.tv_nsec < NANOS_PER_SECOND);
KernelTimespec sceResolution{-1, -1};
Require(sceKernelClockGetres(clockId, &sceResolution) == SCE_OK);
Require(sceResolution.tv_sec == resolution.tv_sec && sceResolution.tv_nsec == resolution.tv_nsec);
}
static void RequireNeverDecreases(int clockId) {
std::int64_t previous = Nanos(clockId);
for (int i = 0; i < 10000; ++i) {
const std::int64_t current = Nanos(clockId);
Require(current >= previous);
previous = current;
}
}
static std::atomic<bool> stopBurning{false};
static void* APS5_VABI Burn(void*) {
while (!stopBurning.load()) {}
return nullptr;
}
static void BusyThreadAccumulatesCpuTime() {
const std::int64_t wallStart = Nanos(GUEST_CLOCK_MONOTONIC);
const std::int64_t cpuStart = Nanos(GUEST_CLOCK_THREAD_CPUTIME_ID);
std::int64_t cpu = cpuStart;
while (cpu - cpuStart < BURN_NANOS) {
Require(Nanos(GUEST_CLOCK_MONOTONIC) - wallStart < GIVE_UP_NANOS);
const std::int64_t current = Nanos(GUEST_CLOCK_THREAD_CPUTIME_ID);
Require(current >= cpu);
cpu = current;
}
const std::int64_t wall = Nanos(GUEST_CLOCK_MONOTONIC);
Require(cpu - cpuStart <= wall - wallStart + TICK_MARGIN_NANOS);
}
static void SleepingThreadAccumulatesNone() {
const std::int64_t cpuStart = Nanos(GUEST_CLOCK_THREAD_CPUTIME_ID);
Require(sceKernelUsleep_nid_postfix(200000) == SCE_OK);
Require(Nanos(GUEST_CLOCK_THREAD_CPUTIME_ID) - cpuStart <= TICK_MARGIN_NANOS);
}
static void ProcessClockCountsOtherThreads() {
const std::int64_t wallStart = Nanos(GUEST_CLOCK_MONOTONIC);
const std::int64_t threadStart = Nanos(GUEST_CLOCK_THREAD_CPUTIME_ID);
const std::int64_t processStart = Nanos(GUEST_CLOCK_PROCESS_CPUTIME_ID);
Pthread burner = nullptr;
Require(scePthreadCreate(&burner, nullptr, Burn, nullptr, nullptr) == SCE_OK);
while (Nanos(GUEST_CLOCK_PROCESS_CPUTIME_ID) - processStart < BURN_NANOS) {
Require(Nanos(GUEST_CLOCK_MONOTONIC) - wallStart < GIVE_UP_NANOS);
Require(sceKernelUsleep_nid_postfix(50000) == SCE_OK);
}
stopBurning.store(true);
Require(scePthreadJoin(burner, nullptr) == SCE_OK);
Require(Nanos(GUEST_CLOCK_THREAD_CPUTIME_ID) - threadStart <= BURN_NANOS / 2);
}
int main() {
KernelTimespec time{-1, -1};
Require(sceKernelClockGettime(GUEST_CLOCK_THREAD_CPUTIME_ID, &time) == SCE_OK);
Require(time.tv_sec >= 0 && time.tv_nsec >= 0 && time.tv_nsec < NANOS_PER_SECOND);
Require(sceKernelClockGettime(GUEST_CLOCK_PROCESS_CPUTIME_ID, &time) == SCE_OK);
Require(time.tv_sec >= 0 && time.tv_nsec >= 0 && time.tv_nsec < NANOS_PER_SECOND);
Require(sceKernelClockGettime(GUEST_CLOCK_THREAD_CPUTIME_ID, nullptr) == SCE_KERNEL_ERROR_EFAULT);
Require(sceKernelClockGetres(GUEST_CLOCK_PROCESS_CPUTIME_ID, nullptr) == SCE_KERNEL_ERROR_EFAULT);
RequireResolution(GUEST_CLOCK_THREAD_CPUTIME_ID);
RequireResolution(GUEST_CLOCK_PROCESS_CPUTIME_ID);
RequireNeverDecreases(GUEST_CLOCK_THREAD_CPUTIME_ID);
RequireNeverDecreases(GUEST_CLOCK_PROCESS_CPUTIME_ID);
BusyThreadAccumulatesCpuTime();
SleepingThreadAccumulatesNone();
ProcessClockCountsOtherThreads();
}