Skip to content

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

C++

host: sampling profiler for the native runner

Samir Nuri831751d06945
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a6eb85a..a86a38b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -53,6 +53,10 @@ if(PS2_GENERATED)
add_executable(ps2run host/main.cpp)
target_link_libraries(ps2run PRIVATE ps2game ps2runtime)
+ if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")
+ target_sources(ps2run PRIVATE host/profile.cpp)
+ target_compile_definitions(ps2run PRIVATE PS2_PROFILE)
+ endif()
find_package(SDL2 QUIET)
if(SDL2_FOUND AND NOT EMSCRIPTEN)
target_sources(ps2run PRIVATE host/window.cpp)
diff --git a/host/main.cpp b/host/main.cpp
index dd884dd..c7afcf2 100644
--- a/host/main.cpp
+++ b/host/main.cpp
@@ -3,6 +3,9 @@
#ifdef PS2_WINDOW
#include "window.hpp"
#endif
+#ifdef PS2_PROFILE
+#include "profile.hpp"
+#endif
#include <cstdio>
#include <cstdlib>
@@ -93,5 +96,14 @@ int main(int argc, char **argv) {
return 1;
}
ps2::boot(entry, args);
+#ifdef PS2_PROFILE
+ const char *profile = std::getenv("PS2_PROFILE");
+ const char *profile_from = std::getenv("PS2_PROFILE_FROM");
+ if (profile) start_profile(profile_from ? std::strtoull(profile_from, nullptr, 10) : 0);
+ const int result = ps2::run();
+ if (profile) write_profile(profile);
+ return result;
+#else
return ps2::run();
+#endif
}
diff --git a/host/profile.cpp b/host/profile.cpp
new file mode 100644
index 0000000..4b72f34
--- /dev/null
+++ b/host/profile.cpp
@@ -0,0 +1,101 @@
+#include "profile.hpp"
+
+#include "ps2/system.hpp"
+
+#include <csignal>
+#include <cstdint>
+#include <cstdio>
+#include <cstdlib>
+#include <dlfcn.h>
+#include <functional>
+#include <link.h>
+#include <map>
+#include <string>
+#include <sys/time.h>
+#include <ucontext.h>
+
+namespace {
+
+constexpr std::size_t kSlots = 1u << 23;
+constexpr std::size_t kOutside = 1u << 16;
+std::uint32_t g_hits[kSlots];
+std::uintptr_t g_outside_pc[kOutside];
+std::uint32_t g_outside_hits[kOutside];
+std::uintptr_t g_base = 0;
+std::uint64_t g_samples = 0, g_lost = 0;
+std::uint64_t g_frame = 0, g_from = 0;
+
+void sample(int, siginfo_t *, void *context) {
+ const auto *uc = static_cast<const ucontext_t *>(context);
+ const std::uintptr_t pc = static_cast<std::uintptr_t>(uc->uc_mcontext.gregs[REG_RIP]);
+ ++g_samples;
+ const std::uintptr_t offset = (pc - g_base) >> 4;
+ if (pc >= g_base && offset < kSlots) {
+ ++g_hits[offset];
+ return;
+ }
+ const std::uintptr_t key = pc >> 4;
+ for (std::size_t i = 0, h = (key * 0x9E3779B97F4A7C15ull) >> 48; i < 64; ++i, h = (h + 1) & (kOutside - 1)) {
+ if (g_outside_pc[h] == key) { ++g_outside_hits[h]; return; }
+ if (!g_outside_pc[h]) { g_outside_pc[h] = key; g_outside_hits[h] = 1; return; }
+ }
+ ++g_lost;
+}
+
+int find_base(dl_phdr_info *info, std::size_t, void *) {
+ if (!info->dlpi_name || !info->dlpi_name[0]) {
+ g_base = info->dlpi_addr;
+ return 1;
+ }
+ return 0;
+}
+
+void arm() {
+ struct sigaction sa{};
+ sa.sa_sigaction = sample;
+ sa.sa_flags = SA_SIGINFO | SA_RESTART;
+ sigaction(SIGPROF, &sa, nullptr);
+ itimerval timer{};
+ timer.it_interval.tv_usec = 1000;
+ timer.it_value.tv_usec = 1000;
+ setitimer(ITIMER_PROF, &timer, nullptr);
+}
+
+}
+
+void start_profile(std::uint64_t from_frame) {
+ dl_iterate_phdr(find_base, nullptr);
+ g_from = from_frame;
+ if (!g_from) {
+ arm();
+ return;
+ }
+ auto previous = ps2::g_options.on_vblank;
+ ps2::g_options.on_vblank = [previous]() {
+ if (++g_frame == g_from) arm();
+ if (previous) previous();
+ };
+}
+
+void write_profile(const char *path) {
+ itimerval timer{};
+ setitimer(ITIMER_PROF, &timer, nullptr);
+ std::FILE *f = std::fopen(path, "w");
+ if (!f) return;
+ std::fprintf(f, "# samples %llu lost %llu\n", static_cast<unsigned long long>(g_samples), static_cast<unsigned long long>(g_lost));
+ for (std::size_t i = 0; i < kSlots; ++i)
+ if (g_hits[i]) std::fprintf(f, "%zx %u\n", i << 4, g_hits[i]);
+ std::map<std::string, std::uint64_t> outside;
+ for (std::size_t i = 0; i < kOutside; ++i) {
+ if (!g_outside_pc[i]) continue;
+ Dl_info info{};
+ std::string name = "?";
+ if (dladdr(reinterpret_cast<void *>(g_outside_pc[i] << 4), &info)) {
+ if (info.dli_sname) name = info.dli_sname;
+ else if (info.dli_fname) name = info.dli_fname;
+ }
+ outside[name] += g_outside_hits[i];
+ }
+ for (const auto &[name, hits] : outside) std::fprintf(f, "@ %llu %s\n", static_cast<unsigned long long>(hits), name.c_str());
+ std::fclose(f);
+}
diff --git a/host/profile.hpp b/host/profile.hpp
new file mode 100644
index 0000000..0e3f36b
--- /dev/null
+++ b/host/profile.hpp
@@ -0,0 +1,6 @@
+#pragma once
+
+#include <cstdint>
+
+void start_profile(std::uint64_t from_frame);
+void write_profile(const char *path);