Skip to content

PS2 games in the browser: static recompilation to WebAssembly with a native runtime

C++

Native window shows per-thread idle time, and the render thread wakes on work outside Linux

Samir Nuri059aac812363
diff --git a/host/window.cpp b/host/window.cpp
index 44d3252..bbeae7d 100644
--- a/host/window.cpp
+++ b/host/window.cpp
@@ -258,7 +258,18 @@ void present() {
constexpr Uint32 kAudioBytesPerMs = 48 * 4;
+std::atomic<double> g_paced_ms{0.0};
+
+struct PaceTimer {
+ std::uint64_t start = SDL_GetPerformanceCounter();
+ ~PaceTimer() {
+ const double ms = static_cast<double>(SDL_GetPerformanceCounter() - start) * 1000.0 / static_cast<double>(SDL_GetPerformanceFrequency());
+ g_paced_ms.store(g_paced_ms.load() + ms);
+ }
+};
+
void pace() {
+ const PaceTimer timer;
if (g_audio) {
// The audio device clock paces emulation: wait while more than ~80 ms of sound is queued.
while (SDL_GetQueuedAudioSize(g_audio) > 80 * kAudioBytesPerMs) SDL_Delay(1);
@@ -298,24 +309,32 @@ double resident_mb() {
void show_speed() {
static std::uint64_t start = 0, frames = 0;
- static double cpu_start = 0.0, gpu_start = 0.0;
+ static double cpu_start = 0.0, gpu_start = 0.0, emu_start = 0.0, vu_start = 0.0, gs_start = 0.0;
+ const auto emu_idle = [] { return ps2::thread_waited_ms(0) + g_paced_ms.load(); };
const std::uint64_t now = SDL_GetPerformanceCounter();
if (!start) {
start = now;
cpu_start = process_cpu_seconds();
gpu_start = ps2::gpu_busy_ms();
+ emu_start = emu_idle();
+ vu_start = ps2::vu_thread_idle_ms();
+ gs_start = ps2::thread_waited_ms(2);
}
++frames;
const double seconds = static_cast<double>(now - start) / static_cast<double>(SDL_GetPerformanceFrequency());
if (seconds < 1.0) return;
- const double cpu = process_cpu_seconds(), gpu = ps2::gpu_busy_ms();
- char title[192];
+ const double cpu = process_cpu_seconds(), gpu = ps2::gpu_busy_ms(), emu = emu_idle(), vu = ps2::vu_thread_idle_ms(), gs = ps2::thread_waited_ms(2);
+ char title[256];
const double queued = g_audio ? SDL_GetQueuedAudioSize(g_audio) / static_cast<double>(kAudioBytesPerMs) : 0.0;
- std::snprintf(title, sizeof title, "ps2-web-recomp %.1f fps CPU %.0f%% GPU %.0f%% RAM %.0f MB audio %.0f ms", static_cast<double>(frames) / seconds,
- (cpu - cpu_start) / seconds * 100.0, (gpu - gpu_start) / (seconds * 10.0), resident_mb(), queued);
+ std::snprintf(title, sizeof title, "ps2-web-recomp %.1f fps CPU %.0f%% GPU %.0f%% idle emu %.0f%% vu %.0f%% gs %.0f%% RAM %.0f MB audio %.0f ms",
+ static_cast<double>(frames) / seconds, (cpu - cpu_start) / seconds * 100.0, (gpu - gpu_start) / (seconds * 10.0),
+ (emu - emu_start) / (seconds * 10.0), (vu - vu_start) / (seconds * 10.0), (gs - gs_start) / (seconds * 10.0), resident_mb(), queued);
SDL_SetWindowTitle(g_window, title);
start = now;
frames = 0;
+ emu_start = emu;
+ vu_start = vu;
+ gs_start = gs;
cpu_start = cpu;
gpu_start = gpu;
}
diff --git a/runtime/include/ps2/system.hpp b/runtime/include/ps2/system.hpp
index ccda2d0..f447223 100644
--- a/runtime/include/ps2/system.hpp
+++ b/runtime/include/ps2/system.hpp
@@ -118,6 +118,7 @@ void start_render_thread();
void start_vu_thread();
void run_vu_thread();
double vu_thread_idle_ms();
+double thread_waited_ms(int role);
std::uint64_t vu1_interpreted_calls();
void post_present();
enum class Rendered { idle, presented, busy };
diff --git a/runtime/src/gs/queue.cpp b/runtime/src/gs/queue.cpp
index 316ff9c..66cdf48 100644
--- a/runtime/src/gs/queue.cpp
+++ b/runtime/src/gs/queue.cpp
@@ -1,6 +1,7 @@
#include "queue.hpp"
#include <algorithm>
+#include <chrono>
#include <climits>
#include <cmath>
#include <cstring>
@@ -26,6 +27,15 @@ constexpr std::uint64_t align16(std::uint64_t n) { return (n + 15) & ~15ull; }
Ring g_render, g_vu;
thread_local Role g_role = Role::Emulation;
+std::atomic<std::uint64_t> g_waited_us[3] = {};
+
+struct WaitTimer {
+ std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
+ ~WaitTimer() {
+ const auto us = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count();
+ g_waited_us[static_cast<int>(g_role)].fetch_add(static_cast<std::uint64_t>(us), std::memory_order_relaxed);
+ }
+};
#if defined(__linux__) && !defined(__EMSCRIPTEN__)
void futex_wait(std::atomic<std::uint32_t> &word, std::uint32_t seen, int timeout_ms) {
@@ -45,7 +55,10 @@ EM_ASYNC_JS(void, ps2_wait_async, (std::atomic<std::uint32_t> *word, std::uint32
});
#endif
+double waited_ms(Role role) { return static_cast<double>(g_waited_us[static_cast<int>(role)].load(std::memory_order_relaxed)) / 1000.0; }
+
void wait(std::atomic<std::uint32_t> &value, std::uint32_t seen) {
+ const WaitTimer timer;
#ifdef __EMSCRIPTEN__
emscripten_futex_wait(&value, seen, INFINITY);
#elif defined(__linux__)
@@ -135,7 +148,14 @@ void Ring::pop(const Header &header) {
void Ring::wake() {
posted_.fetch_add(1);
+#if !defined(__EMSCRIPTEN__) && !defined(__linux__)
+ if (consumer_waiting_.exchange(0)) {
+ std::lock_guard<std::mutex> lock(wake_mutex_);
+ wake_cv_.notify_all();
+ }
+#else
if (consumer_waiting_.exchange(0)) notify(posted_);
+#endif
}
void Ring::wait_for_work(int timeout_ms) {
@@ -145,14 +165,18 @@ void Ring::wait_for_work(int timeout_ms) {
consumer_waiting_.store(0);
return;
}
+ const WaitTimer timer;
#ifdef __EMSCRIPTEN__
if (emscripten_is_main_browser_thread()) ps2_wait_async(&posted_, seen, timeout_ms);
else emscripten_futex_wait(&posted_, seen, timeout_ms);
#elif defined(__linux__)
futex_wait(posted_, seen, timeout_ms);
#else
- (void)seen;
- std::this_thread::sleep_for(std::chrono::milliseconds(std::min(timeout_ms, 1)));
+ std::unique_lock<std::mutex> lock(wake_mutex_);
+ const auto woken = [&] { return posted_.load() != seen || head_.load() != tail_.load(); };
+ if (timeout_ms < 0) wake_cv_.wait(lock, woken);
+ else wake_cv_.wait_for(lock, std::chrono::milliseconds(timeout_ms), woken);
+ consumer_waiting_.store(0);
#endif
}
diff --git a/runtime/src/gs/queue.hpp b/runtime/src/gs/queue.hpp
index 127c151..bc3cbd3 100644
--- a/runtime/src/gs/queue.hpp
+++ b/runtime/src/gs/queue.hpp
@@ -1,6 +1,10 @@
#pragma once
#include <atomic>
+#if !defined(__EMSCRIPTEN__) && !defined(__linux__)
+#include <condition_variable>
+#include <mutex>
+#endif
#include <cstddef>
#include <cstdint>
#include <memory>
@@ -33,6 +37,10 @@ private:
alignas(64) std::atomic<std::uint32_t> posted_{0};
alignas(64) std::atomic<std::uint32_t> freed_{0};
std::atomic<std::uint32_t> consumer_waiting_{0}, producer_waiting_{0};
+#if !defined(__EMSCRIPTEN__) && !defined(__linux__)
+ std::mutex wake_mutex_;
+ std::condition_variable wake_cv_;
+#endif
};
Ring &render();
@@ -45,6 +53,7 @@ bool vu_active();
void post(Type type, std::uint32_t arg0, std::uint32_t arg1, const void *data, std::uint32_t bytes);
void wait(std::atomic<std::uint32_t> &value, std::uint32_t seen);
+double waited_ms(Role role);
void notify(std::atomic<std::uint32_t> &value);
}
diff --git a/runtime/src/system.cpp b/runtime/src/system.cpp
index 3a89572..3be55cc 100644
--- a/runtime/src/system.cpp
+++ b/runtime/src/system.cpp
@@ -508,6 +508,7 @@ void start_vu_thread() { gs::queue::vu().start(32u << 20); }
std::atomic<double> g_vu_idle_ms{0.0};
double vu_thread_idle_ms() { return g_vu_idle_ms.load(); }
+double thread_waited_ms(int role) { return gs::queue::waited_ms(static_cast<gs::queue::Role>(role)); }
std::uint64_t vu1_interpreted_calls() { return vu::interpreted_calls(); }
void run_vu_thread() {