Windows: the first std::thread after libc.prx loads can cost its creator tens of ms of CPU; nkwebkit_wtfcputime fails on it #23267

Closed
opened 2026-11-18 21:33:29 +00:00 by videogamer10293 · 0 comments
videogamer10293 commented 2026-11-18 21:33:29 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by videogamer10293 on 2026-11-18T21:33:29Z.


What happens

On Windows, with libc.prx loaded, a process's first std::thread can cost the creating thread up to 34 ms of CPU time in a small probe and about 47 ms in the test process, which makes nkwebkit_wtfcputime fail under load with WTF::CPUTime check failed at line 111.

Title, test or build step

ctest nkwebkit_wtfcputime; also a standalone program, below.

AnyPS5 commit

main 158e669f66, no pull requests on top. The tree carries an uncommitted _putenv_s guard in UploadCacheReadOnlyFlush.cpp, without which main does not build on Windows (#19899, #22373 and #22634 fix it). main has moved since, to 866864a5bc; the newer commits change libc's MathExact.cpp, libSceAudioOut, libSceFios2, libSceRtc, libSceSysmodule, tests and tools. This was not run again on them.

System

Windows 10 Pro 10.0.19045, AMD Ryzen 5 7600X (12 logical CPUs), WinLibs GCC 15.2.0 r7, Release. GPU: not involved.

Host report

Not run; no title is involved.

Steps to reproduce

  1. Start burner.cpp (below) as burner 12 3600: 12 threads in busy loops.
  2. Run ctest --test-dir build -R "^nkwebkit_wtfcputime$" 20 times.

Failing runs abort within about 1 s with exit code 0xc0000409.

To see the cost outside the test, build create_probe.cpp (below) with g++ -O2 -std=c++20 -static. Run it with no argument or with a library name, with build/core/libs/libs/unpatched and the WinLibs bin folder on PATH. It loads the library, then creates two std::threads in turn and reads the creating thread's QueryThreadCycleTime, its GetThreadTimes kernel and user times and the wall time around each constructor.

Log

WTF::CPUTime check failed at line 111
FATAL: abort() (SIGABRT) on thread ...

What was checked

Line 111 is Require(after - before < 0.03); in TestGuestClockCountsOnlyThisThread, added by #20139. It checks that the main thread's own CPU time stays under 30 ms while it creates the test's first std::thread, joins it and sleeps 50 ms.

The test on 158e669f66, 20 runs each:

  • Alone: 0 failed. With the 12 busy threads: 10 failed, all at line 111.
  • With a local print of the main thread's cycle time around each step, and the 12 busy threads: 11 of 20 failed (9 of 20 in a first run), at line 118 because of the added lines. In all 11, the std::thread constructor took 42.7 to 46.9 ms (44.3 to 46.3 ms in the 9 of the first run); join and the 50 ms sleep took under 0.01 ms. In 8 of the 9 passing runs the constructor took about 0.05 ms. One passed at 43.2 ms; I did not read the test's clock there. On main that clock moves in 15.6 ms steps (#23238), which may be why.
  • With a local std::thread([] {}).join(); added before before, and the 12 busy threads: 0 of 20 failed, and the measured constructor took 0.037 to 0.046 ms.
  • It also failed on main in my ctest -j 8 runs for #23238 on older main commits, without the busy threads but with 8 tests running at once (Tested). CI runs ctest in parallel too (CONTRIBUTING).

create_probe.cpp, 30 runs per row, CPU time of the first std::thread constructor to the creating thread. The ms values divide the cycle count by the TSC rate the program measures over 20 ms, so they assume the count runs at that rate.

loaded load largest runs over 10 ms
nothing none 0.074 ms 0
libc.prx none 17.523 ms 9
libkernel.prx none 14.541 ms 5
nothing 12 busy threads 0.068 ms 0
libc.prx 12 busy threads 27.396 ms 2
libkernel.prx 12 busy threads 34.370 ms 8
  • The second constructor cost 0.066 ms or less in every row; later ones were not measured.
  • libc.prx does not import libkernel.prx; libkernel.prx loads libc.prx. Loading shell32.dll, iphlpapi.dll, advapi32.dll, libstdc++-6.dll, libwinpthread-1.dll or libgcc_s_seh-1.dll alone, the other DLLs libc.prx imports apart from KERNEL32.dll and the api-ms-win-crt set, does not reproduce it: at most 0.088 ms, with and without the busy threads.
  • In 23 of the 24 runs over 10 ms, GetThreadTimes, which moves in 15.6 ms steps, showed kernel time and none showed user time.
  • The test process loads more libraries than libc.prx; there the slow constructor took about 45 ms.

Not found: what libc.prx does at load that makes the first thread expensive. No kernel stack trace was taken. Not checked: other CPUs, Windows 11, Linux, macOS, and whether a title shows the cost.

Creating a thread before before, as above, made the test pass in 20 of 20 runs, but it leaves the start-up cost in libc.prx unexplained. I can open that test change as a pull request if maintainers want it.

create_probe.cpp
#include <windows.h>
#include <cstdint>
#include <cstdio>
#include <string_view>
#include <thread>
#include <intrin.h>

static std::uint64_t Cycles() {
    ULONG64 cycles = 0;
    QueryThreadCycleTime(GetCurrentThread(), &cycles);
    return cycles;
}

static void Times(std::uint64_t& kernel, std::uint64_t& user) {
    FILETIME creation, exitTime, k, u;
    GetThreadTimes(GetCurrentThread(), &creation, &exitTime, &k, &u);
    kernel = (static_cast<std::uint64_t>(k.dwHighDateTime) << 32) | k.dwLowDateTime;
    user = (static_cast<std::uint64_t>(u.dwHighDateTime) << 32) | u.dwLowDateTime;
}

int main(int argc, char** argv) {
    const char* library = argc > 1 ? argv[1] : "none";
    if (std::string_view(library) != "none" && LoadLibraryA(library) == nullptr) {
        std::printf("%s: LoadLibrary failed %lu\n", library, GetLastError());
        return 2;
    }
    LARGE_INTEGER f;
    QueryPerformanceFrequency(&f);
    double cyclesPerMs = 0;
    {
        LARGE_INTEGER s, e;
        QueryPerformanceCounter(&s);
        const auto c0 = __rdtsc();
        Sleep(20);
        QueryPerformanceCounter(&e);
        cyclesPerMs = static_cast<double>(__rdtsc() - c0) / (static_cast<double>(e.QuadPart - s.QuadPart) * 1000.0 / static_cast<double>(f.QuadPart));
    }
    for (int round = 1; round <= 2; ++round) {
        std::uint64_t k0, u0, k1, u1;
        Times(k0, u0);
        const auto c0 = Cycles();
        LARGE_INTEGER w0, w1;
        QueryPerformanceCounter(&w0);
        std::thread worker([] {});
        QueryPerformanceCounter(&w1);
        const auto c1 = Cycles();
        Times(k1, u1);
        worker.join();
        std::printf("%s: std::thread #%d constructor: creator cycles %.3f ms, GetThreadTimes kernel %.1f ms user %.1f ms, wall %.3f ms\n", library, round,
                    static_cast<double>(c1 - c0) / cyclesPerMs, static_cast<double>(k1 - k0) / 1e4, static_cast<double>(u1 - u0) / 1e4,
                    static_cast<double>(w1.QuadPart - w0.QuadPart) * 1000.0 / static_cast<double>(f.QuadPart));
    }
    return 0;
}
burner.cpp
#include <atomic>
#include <chrono>
#include <cstdlib>
#include <thread>
#include <vector>
int main(int argc, char** argv) {
    const int threads = argc > 1 ? std::atoi(argv[1]) : 12;
    const int seconds = argc > 2 ? std::atoi(argv[2]) : 60;
    std::atomic<bool> stop{false};
    std::vector<std::thread> pool;
    for (int i = 0; i < threads; ++i) pool.emplace_back([&] { volatile unsigned x = 0; while (!stop) x = x + 1; });
    std::this_thread::sleep_for(std::chrono::seconds(seconds));
    stop = true;
    for (auto& t : pool) t.join();
}

AI-assisted: yes. Claude Code found this while checking why nkwebkit_wtfcputime failed during the tests for #23238, wrote the probe programs and the local test prints, ran them, and drafted this issue.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6477171560 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/23267). Originally posted by [videogamer10293](https://github.com/videogamer10293) on 2026-11-18T21:33:29Z. --- ### What happens On Windows, with `libc.prx` loaded, a process's first `std::thread` can cost the creating thread up to 34 ms of CPU time in a small probe and about 47 ms in the test process, which makes `nkwebkit_wtfcputime` fail under load with `WTF::CPUTime check failed at line 111`. ### Title, test or build step ctest `nkwebkit_wtfcputime`; also a standalone program, below. ### AnyPS5 commit `main` `158e669f66`, no pull requests on top. The tree carries an uncommitted `_putenv_s` guard in `UploadCacheReadOnlyFlush.cpp`, without which `main` does not build on Windows (#19899, #22373 and #22634 fix it). `main` has moved since, to `866864a5bc`; the newer commits change `libc`'s `MathExact.cpp`, `libSceAudioOut`, `libSceFios2`, `libSceRtc`, `libSceSysmodule`, tests and tools. This was not run again on them. ### System Windows 10 Pro 10.0.19045, AMD Ryzen 5 7600X (12 logical CPUs), WinLibs GCC 15.2.0 r7, Release. GPU: not involved. ### Host report Not run; no title is involved. ### Steps to reproduce 1. Start `burner.cpp` (below) as `burner 12 3600`: 12 threads in busy loops. 2. Run `ctest --test-dir build -R "^nkwebkit_wtfcputime$"` 20 times. Failing runs abort within about 1 s with exit code `0xc0000409`. To see the cost outside the test, build `create_probe.cpp` (below) with `g++ -O2 -std=c++20 -static`. Run it with no argument or with a library name, with `build/core/libs/libs/unpatched` and the WinLibs `bin` folder on `PATH`. It loads the library, then creates two `std::thread`s in turn and reads the creating thread's `QueryThreadCycleTime`, its `GetThreadTimes` kernel and user times and the wall time around each constructor. ### Log ```text WTF::CPUTime check failed at line 111 FATAL: abort() (SIGABRT) on thread ... ``` ### What was checked Line 111 is `Require(after - before < 0.03);` in `TestGuestClockCountsOnlyThisThread`, added by #20139. It checks that the main thread's own CPU time stays under 30 ms while it creates the test's first `std::thread`, joins it and sleeps 50 ms. **The test** on `158e669f66`, 20 runs each: - Alone: 0 failed. With the 12 busy threads: 10 failed, all at line 111. - With a local print of the main thread's cycle time around each step, and the 12 busy threads: 11 of 20 failed (9 of 20 in a first run), at line 118 because of the added lines. In all 11, the `std::thread` constructor took 42.7 to 46.9 ms (44.3 to 46.3 ms in the 9 of the first run); `join` and the 50 ms sleep took under 0.01 ms. In 8 of the 9 passing runs the constructor took about 0.05 ms. One passed at 43.2 ms; I did not read the test's clock there. On `main` that clock moves in 15.6 ms steps (#23238), which may be why. - With a local `std::thread([] {}).join();` added before `before`, and the 12 busy threads: 0 of 20 failed, and the measured constructor took 0.037 to 0.046 ms. - It also failed on `main` in my `ctest -j 8` runs for #23238 on older `main` commits, without the busy threads but with 8 tests running at once ([Tested](https://github.com/boykopovar/AnyPS5/pull/23238)). CI runs ctest in parallel too ([CONTRIBUTING](https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md)). **`create_probe.cpp`**, 30 runs per row, CPU time of the first `std::thread` constructor to the creating thread. The ms values divide the cycle count by the TSC rate the program measures over 20 ms, so they assume the count runs at that rate. | loaded | load | largest | runs over 10 ms | |---|---|---|---| | nothing | none | 0.074 ms | 0 | | `libc.prx` | none | 17.523 ms | 9 | | `libkernel.prx` | none | 14.541 ms | 5 | | nothing | 12 busy threads | 0.068 ms | 0 | | `libc.prx` | 12 busy threads | 27.396 ms | 2 | | `libkernel.prx` | 12 busy threads | 34.370 ms | 8 | - The second constructor cost 0.066 ms or less in every row; later ones were not measured. - `libc.prx` does not import `libkernel.prx`; `libkernel.prx` loads `libc.prx`. Loading `shell32.dll`, `iphlpapi.dll`, `advapi32.dll`, `libstdc++-6.dll`, `libwinpthread-1.dll` or `libgcc_s_seh-1.dll` alone, the other DLLs `libc.prx` imports apart from `KERNEL32.dll` and the `api-ms-win-crt` set, does not reproduce it: at most 0.088 ms, with and without the busy threads. - In 23 of the 24 runs over 10 ms, `GetThreadTimes`, which moves in 15.6 ms steps, showed kernel time and none showed user time. - The test process loads more libraries than `libc.prx`; there the slow constructor took about 45 ms. Not found: what `libc.prx` does at load that makes the first thread expensive. No kernel stack trace was taken. Not checked: other CPUs, Windows 11, Linux, macOS, and whether a title shows the cost. Creating a thread before `before`, as above, made the test pass in 20 of 20 runs, but it leaves the start-up cost in `libc.prx` unexplained. I can open that test change as a pull request if maintainers want it. <details><summary>create_probe.cpp</summary> ```cpp #include <windows.h> #include <cstdint> #include <cstdio> #include <string_view> #include <thread> #include <intrin.h> static std::uint64_t Cycles() { ULONG64 cycles = 0; QueryThreadCycleTime(GetCurrentThread(), &cycles); return cycles; } static void Times(std::uint64_t& kernel, std::uint64_t& user) { FILETIME creation, exitTime, k, u; GetThreadTimes(GetCurrentThread(), &creation, &exitTime, &k, &u); kernel = (static_cast<std::uint64_t>(k.dwHighDateTime) << 32) | k.dwLowDateTime; user = (static_cast<std::uint64_t>(u.dwHighDateTime) << 32) | u.dwLowDateTime; } int main(int argc, char** argv) { const char* library = argc > 1 ? argv[1] : "none"; if (std::string_view(library) != "none" && LoadLibraryA(library) == nullptr) { std::printf("%s: LoadLibrary failed %lu\n", library, GetLastError()); return 2; } LARGE_INTEGER f; QueryPerformanceFrequency(&f); double cyclesPerMs = 0; { LARGE_INTEGER s, e; QueryPerformanceCounter(&s); const auto c0 = __rdtsc(); Sleep(20); QueryPerformanceCounter(&e); cyclesPerMs = static_cast<double>(__rdtsc() - c0) / (static_cast<double>(e.QuadPart - s.QuadPart) * 1000.0 / static_cast<double>(f.QuadPart)); } for (int round = 1; round <= 2; ++round) { std::uint64_t k0, u0, k1, u1; Times(k0, u0); const auto c0 = Cycles(); LARGE_INTEGER w0, w1; QueryPerformanceCounter(&w0); std::thread worker([] {}); QueryPerformanceCounter(&w1); const auto c1 = Cycles(); Times(k1, u1); worker.join(); std::printf("%s: std::thread #%d constructor: creator cycles %.3f ms, GetThreadTimes kernel %.1f ms user %.1f ms, wall %.3f ms\n", library, round, static_cast<double>(c1 - c0) / cyclesPerMs, static_cast<double>(k1 - k0) / 1e4, static_cast<double>(u1 - u0) / 1e4, static_cast<double>(w1.QuadPart - w0.QuadPart) * 1000.0 / static_cast<double>(f.QuadPart)); } return 0; } ``` </details> <details><summary>burner.cpp</summary> ```cpp #include <atomic> #include <chrono> #include <cstdlib> #include <thread> #include <vector> int main(int argc, char** argv) { const int threads = argc > 1 ? std::atoi(argv[1]) : 12; const int seconds = argc > 2 ? std::atoi(argv[2]) : 60; std::atomic<bool> stop{false}; std::vector<std::thread> pool; for (int i = 0; i < threads; ++i) pool.emplace_back([&] { volatile unsigned x = 0; while (!stop) x = x + 1; }); std::this_thread::sleep_for(std::chrono::seconds(seconds)); stop = true; for (auto& t : pool) t.join(); } ``` </details> AI-assisted: yes. Claude Code found this while checking why `nkwebkit_wtfcputime` failed during the tests for #23238, wrote the probe programs and the local test prints, ran them, and drafted this issue.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#23267
No description provided.