Files
Thomas MartinandClaude Opus 5.5 68409d1283 Phase 4 M0: TMB's runtime on BozoEngine, headless (-DTMB_BOZO=ON -DBOZO_ENGINE_DIR=<BozoEngine>)
The runtime builds against BozoEngine (its phase4/m0) instead of the PS2Recomp fork; the fork build of this branch no
longer builds it. cmake/bozo.cmake: the generated code (bozo-recomp with the externals bozo-externals writes from the
ELF, recomp/tmb_stubs.txt and the override list tools/tmb_overrides_list.py makes from runtime/*.cpp), and two
executables:
- tmb-check: BozoEngine's call-log checker with TMB's overrides running natively, each call checked against the
  reference's log (boot --pinned: 3000 of 3000 frames, every record);
- tmb-headless: the game with no log and no window, every library native, BozoEngine's own timing; it reaches the title
  (uiTitle_Main__Fv from frame 1116) and, with the 1P pad script, a match (rtMain__Fi from frame 5591).

What changed in runtime/:
- runtime/tmb_port.h: the context helpers, the generated functions by name, bozo/port.h; PS2Runtime is BozoEngine's
  Runtime, the install functions take the registry as `runtime` (their registration lines unchanged).
- tmb_overrides.cpp: the scheduler calls through the port API (switchThread, native frames: accountCycles, frameEnd,
  ioRead32 / ioWrite32 for T0); the fork's experiments dropped (GS lag, VIF1 thread, vblank hold, A/B, rain lift, idle
  and thread traces, profiler gates, fps overlay notes, env defaults for the IOP and GS); the libmpeg HLE calls dropped
  (BozoEngine's libmpeg sees the stream's end itself); sceGsExecStoreImage and the timeline's sceGs calls through
  bozo::port::callLibrary; the Logitech wheel's TOML stubs as returnZero overrides; findDiscImage kept for the
  executable; the multitap flag in BozoEngine's libpad.
- tmb_native.cpp: the VIF1 stream adapter, VU1 translations, GS front end and their toggles dropped (3515 -> 270
  lines); what the hooks share kept (match / menu frames, screen, view clip, fog colour, flare depth, publish).
- tmb_vu0.cpp and vu1/tmb_vu1_rain.cpp deleted (BozoEngine's VU0 programs; no VU1).
- tmb_2d, tmb_meshes, tmb_particles, tmb_textures, tmb_cheats: includes and the renderer's namespace (bozo::render),
  the store patch (bozo:store marks the dialog kick), the particles' FPU / VU0 helpers on BozoEngine's PS2 floats,
  particleDraw resumed through its resume entry, sceGsSetDefLoadImage's original through callLibrary, the pack
  folders from tmb_host (executableDirectory, elfDirectory).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 11:02:15 +01:00

218 lines
7.6 KiB
C++

#include "tmb_host.h"
#include <cstdlib>
#include <filesystem>
#include <string>
#if defined(_WIN32)
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <tlhelp32.h>
#include <intrin.h>
#include <chrono>
#include <thread>
#else
#include <ctime>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <unistd.h>
#endif
namespace tmb::host
{
#if defined(_WIN32)
namespace
{
// GetThreadTimes only moves at each scheduler tick (15.6 ms); QueryThreadCycleTime counts the TSC cycles the
// thread ran, turned into nanoseconds with the TSC rate measured once against the performance counter.
double tscCyclesPerNs()
{
static const double rate = []
{
LARGE_INTEGER frequency, start, end;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&start);
const uint64_t tscStart = __rdtsc();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
QueryPerformanceCounter(&end);
const uint64_t tscEnd = __rdtsc();
const double ns = static_cast<double>(end.QuadPart - start.QuadPart) * 1e9 / static_cast<double>(frequency.QuadPart);
return ns > 0.0 ? static_cast<double>(tscEnd - tscStart) / ns : 1.0;
}();
return rate;
}
int64_t cpuNsOf(HANDLE thread)
{
ULONG64 cycles = 0;
if (!QueryThreadCycleTime(thread, &cycles))
return 0;
return static_cast<int64_t>(static_cast<double>(cycles) / tscCyclesPerNs());
}
// GetThreadDescription (Windows 10 1607+), looked up at run time.
std::string threadName(HANDLE thread)
{
using GetThreadDescriptionFn = HRESULT(WINAPI *)(HANDLE, PWSTR *);
static const auto getThreadDescription = reinterpret_cast<GetThreadDescriptionFn>(
reinterpret_cast<void *>(GetProcAddress(GetModuleHandleW(L"Kernel32.dll"), "GetThreadDescription")));
if (!getThreadDescription)
return {};
PWSTR description = nullptr;
if (FAILED(getThreadDescription(thread, &description)) || !description)
return {};
std::string name;
for (const wchar_t *c = description; *c; ++c)
name += static_cast<char>(*c < 0x80 ? *c : '?');
LocalFree(description);
return name;
}
// visit(id, handle) for each thread of this process until it returns false.
template <typename Visit>
void forEachThread(Visit visit)
{
const HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (snapshot == INVALID_HANDLE_VALUE)
return;
const DWORD process = GetCurrentProcessId();
THREADENTRY32 entry{};
entry.dwSize = sizeof(entry);
bool more = Thread32First(snapshot, &entry);
for (; more; more = Thread32Next(snapshot, &entry))
{
if (entry.th32OwnerProcessID != process)
continue;
const HANDLE thread = OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, entry.th32ThreadID);
if (!thread)
continue;
const bool next = visit(entry.th32ThreadID, thread);
CloseHandle(thread);
if (!next)
break;
}
CloseHandle(snapshot);
}
}
std::vector<ThreadCpu> threadCpuTimes()
{
std::vector<ThreadCpu> threads;
forEachThread([&](DWORD id, HANDLE thread)
{
threads.push_back({id, threadName(thread), cpuNsOf(thread)});
return true; });
return threads;
}
int64_t findThreadCpuClock(std::string_view name)
{
DWORD found = 0;
forEachThread([&](DWORD id, HANDLE thread)
{
if (threadName(thread) != name)
return true;
found = id;
return false; });
// A handle of its own, kept open: it is the clock.
const HANDLE thread = found ? OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, found) : nullptr;
return thread ? static_cast<int64_t>(reinterpret_cast<intptr_t>(thread)) : -1;
}
int64_t readThreadCpuNs(int64_t clock)
{
return clock == -1 ? 0 : cpuNsOf(reinterpret_cast<HANDLE>(static_cast<intptr_t>(clock)));
}
void setEnvDefault(const char *name, const char *value)
{
if (!std::getenv(name))
_putenv_s(name, value);
}
#else
// utime + stime from /proc/self/task/<tid>/stat.
std::vector<ThreadCpu> threadCpuTimes()
{
std::vector<ThreadCpu> threads;
const double nsPerTick = 1e9 / static_cast<double>(sysconf(_SC_CLK_TCK));
std::error_code ec;
for (const auto &task : std::filesystem::directory_iterator("/proc/self/task", ec))
{
std::ifstream stat(task.path() / "stat");
std::string line;
std::getline(stat, line);
const size_t open = line.find('('), close = line.rfind(')');
if (open == std::string::npos || close == std::string::npos)
continue;
std::istringstream rest(line.substr(close + 2));
std::string field;
uint64_t utime = 0, stime = 0;
for (int i = 3; i <= 15 && rest >> field; ++i)
{
if (i == 14) utime = std::stoull(field);
if (i == 15) stime = std::stoull(field);
}
threads.push_back({std::stoull(task.path().filename().string()), line.substr(open + 1, close - open - 1),
static_cast<int64_t>(static_cast<double>(utime + stime) * nsPerTick)});
}
return threads;
}
int64_t findThreadCpuClock(std::string_view name)
{
std::error_code ec;
for (const auto &task : std::filesystem::directory_iterator("/proc/self/task", ec))
{
std::ifstream comm(task.path() / "comm");
std::string threadName;
std::getline(comm, threadName);
if (threadName != name)
continue;
// A thread id makes a clock id (CPUCLOCK_SCHED of that thread).
const pid_t tid = static_cast<pid_t>(std::stol(task.path().filename().string()));
return static_cast<int64_t>((~static_cast<clockid_t>(tid) << 3) | 6);
}
return -1;
}
int64_t readThreadCpuNs(int64_t clock)
{
timespec ts{};
if (clock == -1 || clock_gettime(static_cast<clockid_t>(clock), &ts) != 0)
return 0;
return ts.tv_sec * 1000000000ll + ts.tv_nsec;
}
void setEnvDefault(const char *name, const char *value)
{
setenv(name, value, 0);
}
#endif
namespace
{
std::string g_elfDirectory;
}
std::string executableDirectory()
{
#if defined(_WIN32)
wchar_t path[MAX_PATH];
const DWORD n = GetModuleFileNameW(nullptr, path, MAX_PATH);
if (n == 0 || n >= MAX_PATH)
return {};
return std::filesystem::path(std::wstring(path, n)).parent_path().string();
#else
std::error_code ec;
return std::filesystem::read_symlink("/proc/self/exe", ec).parent_path().string();
#endif
}
std::string elfDirectory() { return g_elfDirectory; }
void setElfDirectory(const std::string &folder) { g_elfDirectory = folder; }
}