diff --git a/Common/TimeUtil.cpp b/Common/TimeUtil.cpp index 1c4be68bde..f0d1d4c70f 100644 --- a/Common/TimeUtil.cpp +++ b/Common/TimeUtil.cpp @@ -102,6 +102,10 @@ void TimeShutdown() { #endif } +double fast_ticks_to_nanos() { + return 1.0; +} + double time_now_d() { LARGE_INTEGER time; QueryPerformanceCounter(&time); @@ -164,10 +168,6 @@ int64_t Instant::ElapsedNanos() const { return (int64_t)(ElapsedSeconds() * 1000000000.0); } -int64_t Instant::NanosSince(const Instant &earlier) const { - return (int64_t)((double)(int64_t)(nativeStart_ - earlier.nativeStart_) * frequencyMult * 1000000000.0); -} - #else // Monotonic, in nanoseconds, and the clock VK_GOOGLE_display_timing reports in. @@ -206,6 +206,7 @@ void TimeInit() { if (g_startTime == 0) { g_startTime = time_now_raw(); g_startUnixTime = time_now_unix_utc(); + fast_ticks_to_nanos(); } } @@ -213,6 +214,37 @@ void TimeShutdown() { // Nothing to do. } +#if PPSSPP_ARCH(ARM64) && (PPSSPP_PLATFORM(ANDROID) || PPSSPP_PLATFORM(LINUX)) && !defined(_MSC_VER) +// Firmware doesn't always fill in cntfrq_el0 (a Galaxy S8 reads 0), so the counter is measured against +// time_now_raw(), from the first call (TimeInit()) to one at least 50 ms later. +static double MeasureFastTicks() { + static uint64_t startTicks = 0; + static uint64_t startRaw = 0; + static double nanosPerTick = 0.0; + if (nanosPerTick != 0.0) + return nanosPerTick; + const uint64_t ticks = time_now_fast_ticks(); + const uint64_t raw = time_now_raw(); + if (startRaw == 0) { + startTicks = ticks; + startRaw = raw; + } else if (raw - startRaw >= 50000000 && ticks > startTicks) { + nanosPerTick = (double)(raw - startRaw) / (double)(ticks - startTicks); + return nanosPerTick; + } + // Too early to tell. Most counters run at 19.2-26 MHz. + return 1e9 / 19200000.0; +} +#endif + +double fast_ticks_to_nanos() { +#if PPSSPP_ARCH(ARM64) && (PPSSPP_PLATFORM(ANDROID) || PPSSPP_PLATFORM(LINUX)) && !defined(_MSC_VER) + return MeasureFastTicks(); +#else + return 1.0; +#endif +} + double from_time_raw(uint64_t raw_time) { // Signed, so a timestamp from before TimeInit() comes out negative rather than enormous. return (double)(int64_t)(raw_time - g_startTime) * (1.0 / nanos); @@ -246,10 +278,6 @@ int64_t Instant::ElapsedNanos() const { return (int64_t)(time_now_raw() - nativeStart_); } -int64_t Instant::NanosSince(const Instant &earlier) const { - return (int64_t)(nativeStart_ - earlier.nativeStart_); -} - double Instant::ElapsedSeconds() const { return (double)ElapsedNanos() * (1.0 / nanos); } diff --git a/Common/TimeUtil.h b/Common/TimeUtil.h index 9c90788fc0..b238dcb4a3 100644 --- a/Common/TimeUtil.h +++ b/Common/TimeUtil.h @@ -1,6 +1,8 @@ #pragma once #include +#include "ppsspp_config.h" + void TimeInit(); void TimeShutdown(); // Not really necessary to call, just for completeness. @@ -11,6 +13,20 @@ double time_now_d(); // The only intended use is to match the timings from VK_GOOGLE_display_timing. uint64_t time_now_raw(); +// A timestamp for timing short spans, in units of fast_ticks_to_nanos(). Where the CPU has a counter +// user code can read (ARM64 Linux and Android), it's read directly: clock_gettime() costs around +// 0.3us on a Galaxy S8. Elsewhere it's time_now_raw(). +inline uint64_t time_now_fast_ticks() { +#if PPSSPP_ARCH(ARM64) && (PPSSPP_PLATFORM(ANDROID) || PPSSPP_PLATFORM(LINUX)) && !defined(_MSC_VER) + uint64_t ticks; + __asm__ volatile("mrs %0, cntvct_el0" : "=r"(ticks)); + return ticks; +#else + return time_now_raw(); +#endif +} +double fast_ticks_to_nanos(); + // This is only interesting for Linux, in relation to VK_GOOGLE_display_timing. double from_time_raw(uint64_t raw_time); double from_time_raw_relative(uint64_t raw_time); @@ -49,8 +65,6 @@ public: double ElapsedSeconds() const; double ElapsedMs() const { return ElapsedSeconds() * 1000.0; } int64_t ElapsedNanos() const; - // From an earlier Instant to this one, without reading the clock again. - int64_t NanosSince(const Instant &earlier) const; private: // Windows: raw QPC ticks. Everything else: nanoseconds, from time_now_raw(). uint64_t nativeStart_; diff --git a/Core/CmdLine.cpp b/Core/CmdLine.cpp index 8f069772b2..7f9812b1d5 100644 --- a/Core/CmdLine.cpp +++ b/Core/CmdLine.cpp @@ -231,6 +231,7 @@ static const CommandLineParam g_autoParams[] = { {POFF(bench), CmdParamType::Bool, "bench", 'b', "Enable benchmark mode", CmdLineMode::Headless}, {POFF(oldAtrac), CmdParamType::Bool, "old-atrac", '\0', "Use old ATRAC decoder"}, {POFF(jitProfile), CmdParamType::String, "jit-profile", '\0', "Profile the CPU core and write its hottest blocks to FILE on exit"}, + {POFF(jitBench), CmdParamType::Int, "jit-bench", '\0', "After N flips, recompile every block in the JIT cache a few times and log how long it took"}, {POFF(memStickInserted), CmdParamType::BoolInverse, "no-memstick", '\0', "Run with the memory stick removed"}, {POFF(log), CmdParamType::String, "log", '\0', "Output log to FILE", CmdLineMode::Application}, {POFF(enableLogging), CmdParamType::Bool, "log", '\0', "Full log output, not just emulated printfs", CmdLineMode::Headless}, @@ -725,6 +726,9 @@ void CommandLineOptions::ApplyToConfig() const { if (jitProfile.has_value()) { g_Config.sJitProfileFile = jitProfile.value(); } + if (jitBench.has_value()) { + g_Config.iJitBenchFlip = jitBench.value(); + } if (root.has_value()) { // No DoNotSaveSetting() here - mountRoot isn't an ordinary setting and never gets diff --git a/Core/CmdLine.h b/Core/CmdLine.h index d6c7c170cf..5e6c88cf0f 100644 --- a/Core/CmdLine.h +++ b/Core/CmdLine.h @@ -175,6 +175,7 @@ struct CommandLineOptions { std::optional oldAtrac; std::optional jitProfile; + std::optional jitBench; // Headless options that may also be mildly useful in application mode std::optional resolutionScale; diff --git a/Core/Config.h b/Core/Config.h index d4e35b6ba1..e5592274af 100644 --- a/Core/Config.h +++ b/Core/Config.h @@ -191,6 +191,8 @@ public: bool bBreakOnFrameTimeout; // not saved // Sample the JIT and write a report of the hottest blocks to this file. Not saved. std::string sJitProfileFile; + // After this many flips, time recompiling the whole JIT cache (--jit-bench). Not saved. + int iJitBenchFlip = 0; // General bool bScreenshotsAsPNG; diff --git a/Core/HW/Display.cpp b/Core/HW/Display.cpp index 39cc4c4e67..ee4ae9aa7d 100644 --- a/Core/HW/Display.cpp +++ b/Core/HW/Display.cpp @@ -106,6 +106,9 @@ static double CheckJitStall(double now) { static void CalculateFPS() { double now = time_now_d(); const double jitSeconds = CheckJitStall(now); + if (g_Config.iJitBenchFlip > 0 && actualFlips == g_Config.iJitBenchFlip) { + mipsr4k.BenchmarkJitDeferred(); + } if (now >= lastFpsTime + 1.0) { double frames = (numVBlanks - lastFpsFrame); diff --git a/Core/MIPS/IR/IRFrontend.cpp b/Core/MIPS/IR/IRFrontend.cpp index e6d2116ed5..4b8d7c1862 100644 --- a/Core/MIPS/IR/IRFrontend.cpp +++ b/Core/MIPS/IR/IRFrontend.cpp @@ -318,7 +318,7 @@ MIPSOpcode IRFrontend::GetOffsetInstruction(int offset) { } void IRFrontend::DoJit(u32 em_address, std::vector &instructions, u32 &mipsBytes) { - const Instant start = Instant::Now(); + const uint64_t start = time_now_fast_ticks(); js.cancel = false; js.blockStart = em_address; js.compilerPC = em_address; @@ -355,9 +355,9 @@ void IRFrontend::DoJit(u32 em_address, std::vector &instructions, u32 &m IRWriter simplified; IRWriter *code = &ir; - Instant passStart = Instant::Now(); + const uint64_t passStart = time_now_fast_ticks(); if (!js.hadBreakpoints) { - std::vector passes{ + IRPassFunc passes[16]{ &ApplyMemoryValidation, &RemoveLoadStoreLeftRight, &OptimizeFPMoves, @@ -370,12 +370,15 @@ void IRFrontend::DoJit(u32 em_address, std::vector &instructions, u32 &m // &MergeLoadStore, // &ThreeOpToTwoOp, }; + size_t numPasses = 0; + while (passes[numPasses]) + numPasses++; if (opts.optimizeForInterpreter) { // Add special passes here. - passes.push_back(&OptimizeForInterpreter); + passes[numPasses++] = &OptimizeForInterpreter; } - if (IRApplyPasses(passes.data(), passes.size(), ir, simplified, opts)) + if (IRApplyPasses(passes, numPasses, ir, simplified, opts)) logBlocks = 1; code = &simplified; //if (ir.GetInstructions().size() >= 24) @@ -394,9 +397,10 @@ void IRFrontend::DoJit(u32 em_address, std::vector &instructions, u32 &m instructions.push_back({ IROp::LogIRBlock, {0}, 0, 0, INVALID_TRACE_INDEX }); std::copy(block_instructions.begin() + 1, block_instructions.end(), std::back_inserter(instructions)); } - lastEnd_ = Instant::Now(); - lastFrontendNanos_ = passStart.NanosSince(start); - lastPassNanos_ = lastEnd_.NanosSince(passStart); + lastEnd_ = time_now_fast_ticks(); + const double nanosPerTick = fast_ticks_to_nanos(); + lastFrontendNanos_ = (int64_t)((passStart - start) * nanosPerTick); + lastPassNanos_ = (int64_t)((lastEnd_ - passStart) * nanosPerTick); if (logBlocks > 0 && dontLogBlocks == 0) { char temp2[256]; diff --git a/Core/MIPS/IR/IRFrontend.h b/Core/MIPS/IR/IRFrontend.h index 21b7adb001..2830e963b9 100644 --- a/Core/MIPS/IR/IRFrontend.h +++ b/Core/MIPS/IR/IRFrontend.h @@ -106,7 +106,8 @@ public: // How long the last DoJit spent making the IR, and running the passes over it, and when it ended. int64_t LastFrontendNanos() const { return lastFrontendNanos_; } int64_t LastPassNanos() const { return lastPassNanos_; } - const Instant &LastEnd() const { return lastEnd_; } + // In time_now_fast_ticks() units. + uint64_t LastEnd() const { return lastEnd_; } void EatPrefix() override { js.EatPrefix(); @@ -188,7 +189,7 @@ private: int64_t lastFrontendNanos_ = 0; int64_t lastPassNanos_ = 0; - Instant lastEnd_; + uint64_t lastEnd_ = 0; private: bool IsMediaEngine() const; diff --git a/Core/MIPS/IR/IRJit.cpp b/Core/MIPS/IR/IRJit.cpp index 8eb9b6313d..bfeb0debc8 100644 --- a/Core/MIPS/IR/IRJit.cpp +++ b/Core/MIPS/IR/IRJit.cpp @@ -172,10 +172,10 @@ void IRInterpreterJit::WriteProfileReport() { } void IRJit::InvalidateCacheAt(u32 em_address, int length) { - const Instant start = Instant::Now(); + const uint64_t start = time_now_fast_ticks(); std::vector numbers = blocks_.FindInvalidatedBlockNumbers(em_address, length); if (numbers.empty()) { - g_jitCompileTimes.invalidateNanos += start.ElapsedNanos(); + g_jitCompileTimes.invalidateNanos += (int64_t)((time_now_fast_ticks() - start) * fast_ticks_to_nanos()); return; } @@ -187,7 +187,7 @@ void IRJit::InvalidateCacheAt(u32 em_address, int length) { // TODO: We are invalidating a lot of blocks that are already invalid (yu gi oh). InvalidateBlock(block_num); } - g_jitCompileTimes.invalidateNanos += start.ElapsedNanos(); + g_jitCompileTimes.invalidateNanos += (int64_t)((time_now_fast_ticks() - start) * fast_ticks_to_nanos()); } void IRJit::InvalidateBlock(int block_num) { @@ -259,6 +259,57 @@ void IRJit::Compile(u32 em_address) { } } +void IRJit::BenchmarkCompile() { + std::vector starts; + for (int i = 0; i < blocks_.GetNumBlocks(); i++) { + const JitBlock *block = blocks_.GetBlock(i); + if (block->IsValid()) + starts.push_back(block->GetOriginalStart()); + } + std::sort(starts.begin(), starts.end()); + starts.erase(std::unique(starts.begin(), starts.end()), starts.end()); + if (starts.empty()) { + NOTICE_LOG(Log::JIT, "JIT bench: no blocks to compile"); + return; + } + + // The best of a few runs, since a phone's clocks and core assignment vary a lot. + constexpr int RUNS = 5; + JitCompileTimes best{}; + int64_t bestTotal = INT64_MAX; + std::vector instructions; + for (int run = 0; run < RUNS; run++) { + ClearCache(); + const JitCompileTimes before = g_jitCompileTimes; + for (u32 start : starts) { + u32 mipsBytes; + if (CompileBlock(start, instructions, mipsBytes) != CompileResult::OK) { + ERROR_LOG(Log::JIT, "JIT bench: the cache filled up, stopping"); + ClearCache(); + return; + } + } + const JitCompileTimes &after = g_jitCompileTimes; + JitCompileTimes t{}; + t.frontendNanos = after.frontendNanos - before.frontendNanos; + t.passNanos = after.passNanos - before.passNanos; + t.backendNanos = after.backendNanos - before.backendNanos; + t.finalizeNanos = after.finalizeNanos - before.finalizeNanos; + t.mipsInstructions = after.mipsInstructions - before.mipsInstructions; + t.irInstructions = after.irInstructions - before.irInstructions; + NOTICE_LOG(Log::JIT, "JIT bench run %d: %d blocks in %0.2f ms (frontend %0.2f, passes %0.2f, backend %0.2f, finalize %0.2f)", + run, (int)starts.size(), t.TotalNanos() * 1e-6, t.frontendNanos * 1e-6, t.passNanos * 1e-6, t.backendNanos * 1e-6, t.finalizeNanos * 1e-6); + if (t.TotalNanos() < bestTotal) { + bestTotal = t.TotalNanos(); + best = t; + } + } + NOTICE_LOG(Log::JIT, "JIT bench: %0.3f ns per timer tick", fast_ticks_to_nanos()); + NOTICE_LOG(Log::JIT, "JIT bench best: %d blocks, %d MIPS -> %d IR instructions, %0.2f ms, %0.2f us/block (frontend %0.2f, passes %0.2f, backend %0.2f, finalize %0.2f)", + (int)starts.size(), (int)best.mipsInstructions, (int)best.irInstructions, bestTotal * 1e-6, bestTotal * 1e-3 / starts.size(), + best.frontendNanos * 1e-3 / starts.size(), best.passNanos * 1e-3 / starts.size(), best.backendNanos * 1e-3 / starts.size(), best.finalizeNanos * 1e-3 / starts.size()); +} + void IRJit::TimedClearCache() { const Instant start = Instant::Now(); ClearCache(); @@ -277,8 +328,9 @@ IRJit::CompileResult IRJit::CompileBlock(u32 em_address, std::vector &in times.passNanos += frontend_.LastPassNanos(); times.mipsInstructions += mipsBytes / 4; times.irInstructions += instructions.size(); - // Reading the clock isn't free (around 0.3us on a Galaxy S8), so each phase ends where the next starts. - const Instant &backendStart = frontend_.LastEnd(); + // Each phase ends where the next starts, to read the clock as little as possible. + const uint64_t backendStart = frontend_.LastEnd(); + const double nanosPerTick = fast_ticks_to_nanos(); if (mipsTracer.tracing_enabled && instructions.size() > 1 && instructions[1].op == IROp::LogIRBlock) { // Before the IR interpreter's block cache takes its copy. @@ -295,8 +347,8 @@ IRJit::CompileResult IRJit::CompileBlock(u32 em_address, std::vector &in // Caller handles a full arena or code space. const CompileResult result = CompileTargetBlock(block_num, instructions); - const Instant finalizeStart = Instant::Now(); - const int64_t backendNanos = finalizeStart.NanosSince(backendStart); + const uint64_t finalizeStart = time_now_fast_ticks(); + const int64_t backendNanos = (int64_t)((finalizeStart - backendStart) * nanosPerTick); times.backendNanos += backendNanos; if (result != CompileResult::OK) return result; @@ -304,7 +356,7 @@ IRJit::CompileResult IRJit::CompileBlock(u32 em_address, std::vector &in // Updates stats, also enters the block in the block shadow. blocks_.FinalizeBlock(block_num); FinalizeTargetBlock(block_num); - const int64_t finalizeNanos = finalizeStart.ElapsedNanos(); + const int64_t finalizeNanos = (int64_t)((time_now_fast_ticks() - finalizeStart) * nanosPerTick); times.finalizeNanos += finalizeNanos; times.blocks++; const int64_t blockNanos = frontend_.LastFrontendNanos() + frontend_.LastPassNanos() + backendNanos + finalizeNanos; diff --git a/Core/MIPS/IR/IRJit.h b/Core/MIPS/IR/IRJit.h index 73be3a0749..800a90122b 100644 --- a/Core/MIPS/IR/IRJit.h +++ b/Core/MIPS/IR/IRJit.h @@ -194,6 +194,7 @@ public: void InvalidateCacheExcept(const std::vector> &keep) override; void UpdateFCR31() override; void GetStats(JitStats *stats) const override; + void BenchmarkCompile() override; void FlushProfile() override; diff --git a/Core/MIPS/JitCommon/JitCommon.h b/Core/MIPS/JitCommon/JitCommon.h index 756290e22a..ead1ae9d4c 100644 --- a/Core/MIPS/JitCommon/JitCommon.h +++ b/Core/MIPS/JitCommon/JitCommon.h @@ -170,6 +170,8 @@ namespace MIPSComp { virtual const u8 *GetCrashHandler() const = 0; virtual JitBlockCacheDebugInterface *GetBlockCacheDebugInterface() = 0; virtual void InvalidateCacheAt(u32 em_address, int length = 4) = 0; + // Recompiles every block a few times and logs how long it took. Only the IR JITs do it. + virtual void BenchmarkCompile() {} // Invalidates every block not entirely inside one of the (address, size) ranges. virtual void InvalidateCacheExcept(const std::vector> &keep) = 0; virtual void DoState(PointerWrap &p) = 0; diff --git a/Core/MIPS/MIPS.cpp b/Core/MIPS/MIPS.cpp index 781bc377da..caf5c43059 100644 --- a/Core/MIPS/MIPS.cpp +++ b/Core/MIPS/MIPS.cpp @@ -582,6 +582,18 @@ void MIPSState::ProcessPendingInvalidates() { jit->ClearCache(); } } + if (benchmarkJit_) { + benchmarkJit_ = false; + if (jit) + jit->BenchmarkCompile(); + } +} + +void MIPSState::BenchmarkJitDeferred() { + benchmarkJit_ = true; + if (jit && !IsMediaEngine()) { + Core_ReenterDispatcher(); + } } void MIPSState::InvalidateICacheRangeDeferred(u32 address, u32 length) { diff --git a/Core/MIPS/MIPS.h b/Core/MIPS/MIPS.h index 3301e77461..3097fdd939 100644 --- a/Core/MIPS/MIPS.h +++ b/Core/MIPS/MIPS.h @@ -317,6 +317,8 @@ public: // Actual clearing is deferred until execution begins again. void InvalidateICacheRangeDeferred(u32 address, u32 length); void ClearJitCacheDeferred(); + // Times recompiling the whole JIT cache, when execution begins again (--jit-bench). + void BenchmarkJitDeferred(); void ProcessPendingInvalidates(); @@ -324,6 +326,7 @@ public: // Doesn't need save stating. std::vector pendingInvalidates_; bool invalidateAll_ = false; + bool benchmarkJit_ = false; std::vector> keepJitBlocks_; // Owned. Null on the interpreter.