diff options
| author | 2017-02-26 17:51:15 -0800 | |
|---|---|---|
| committer | 2017-02-26 17:51:15 -0800 | |
| commit | b250ce21b9a62cb573540fdb14f30c28fa66e6ad (patch) | |
| tree | ef55a0cd4a531a097de2152f563d712551972c4b /src/core | |
| parent | Merge pull request #2595 from jroweboy/patch (diff) | |
| parent | PerfStats: Re-order and document members better (diff) | |
| download | yuzu-b250ce21b9a62cb573540fdb14f30c28fa66e6ad.tar.gz yuzu-b250ce21b9a62cb573540fdb14f30c28fa66e6ad.tar.xz yuzu-b250ce21b9a62cb573540fdb14f30c28fa66e6ad.zip | |
Merge pull request #2587 from yuriks/status-bar
Replace built-in Profiler with indicators in status bar
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/core/core.cpp | 8 | ||||
| -rw-r--r-- | src/core/core.h | 7 | ||||
| -rw-r--r-- | src/core/frontend/emu_window.cpp | 5 | ||||
| -rw-r--r-- | src/core/hle/kernel/server_session.h | 1 | ||||
| -rw-r--r-- | src/core/hle/kernel/thread.h | 1 | ||||
| -rw-r--r-- | src/core/hle/service/gsp_gpu.cpp | 2 | ||||
| -rw-r--r-- | src/core/hle/service/ldr_ro/ldr_ro.cpp | 1 | ||||
| -rw-r--r-- | src/core/hw/gpu.cpp | 41 | ||||
| -rw-r--r-- | src/core/hw/gpu.h | 2 | ||||
| -rw-r--r-- | src/core/perf_stats.cpp | 105 | ||||
| -rw-r--r-- | src/core/perf_stats.h | 83 |
12 files changed, 213 insertions, 45 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 8334fece9..ffd67f074 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt | |||
| @@ -173,6 +173,7 @@ set(SRCS | |||
| 173 | loader/smdh.cpp | 173 | loader/smdh.cpp |
| 174 | tracer/recorder.cpp | 174 | tracer/recorder.cpp |
| 175 | memory.cpp | 175 | memory.cpp |
| 176 | perf_stats.cpp | ||
| 176 | settings.cpp | 177 | settings.cpp |
| 177 | ) | 178 | ) |
| 178 | 179 | ||
| @@ -363,6 +364,7 @@ set(HEADERS | |||
| 363 | memory.h | 364 | memory.h |
| 364 | memory_setup.h | 365 | memory_setup.h |
| 365 | mmio.h | 366 | mmio.h |
| 367 | perf_stats.h | ||
| 366 | settings.h | 368 | settings.h |
| 367 | ) | 369 | ) |
| 368 | 370 | ||
diff --git a/src/core/core.cpp b/src/core/core.cpp index c9c9b7615..140ff6451 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp | |||
| @@ -109,6 +109,10 @@ void System::PrepareReschedule() { | |||
| 109 | reschedule_pending = true; | 109 | reschedule_pending = true; |
| 110 | } | 110 | } |
| 111 | 111 | ||
| 112 | PerfStats::Results System::GetAndResetPerfStats() { | ||
| 113 | return perf_stats.GetAndResetStats(CoreTiming::GetGlobalTimeUs()); | ||
| 114 | } | ||
| 115 | |||
| 112 | void System::Reschedule() { | 116 | void System::Reschedule() { |
| 113 | if (!reschedule_pending) { | 117 | if (!reschedule_pending) { |
| 114 | return; | 118 | return; |
| @@ -140,6 +144,10 @@ System::ResultStatus System::Init(EmuWindow* emu_window, u32 system_mode) { | |||
| 140 | 144 | ||
| 141 | LOG_DEBUG(Core, "Initialized OK"); | 145 | LOG_DEBUG(Core, "Initialized OK"); |
| 142 | 146 | ||
| 147 | // Reset counters and set time origin to current frame | ||
| 148 | GetAndResetPerfStats(); | ||
| 149 | perf_stats.BeginSystemFrame(); | ||
| 150 | |||
| 143 | return ResultStatus::Success; | 151 | return ResultStatus::Success; |
| 144 | } | 152 | } |
| 145 | 153 | ||
diff --git a/src/core/core.h b/src/core/core.h index 17572a74f..6c9c936b5 100644 --- a/src/core/core.h +++ b/src/core/core.h | |||
| @@ -6,9 +6,9 @@ | |||
| 6 | 6 | ||
| 7 | #include <memory> | 7 | #include <memory> |
| 8 | #include <string> | 8 | #include <string> |
| 9 | |||
| 10 | #include "common/common_types.h" | 9 | #include "common/common_types.h" |
| 11 | #include "core/memory.h" | 10 | #include "core/memory.h" |
| 11 | #include "core/perf_stats.h" | ||
| 12 | 12 | ||
| 13 | class EmuWindow; | 13 | class EmuWindow; |
| 14 | class ARM_Interface; | 14 | class ARM_Interface; |
| @@ -83,6 +83,8 @@ public: | |||
| 83 | /// Prepare the core emulation for a reschedule | 83 | /// Prepare the core emulation for a reschedule |
| 84 | void PrepareReschedule(); | 84 | void PrepareReschedule(); |
| 85 | 85 | ||
| 86 | PerfStats::Results GetAndResetPerfStats(); | ||
| 87 | |||
| 86 | /** | 88 | /** |
| 87 | * Gets a reference to the emulated CPU. | 89 | * Gets a reference to the emulated CPU. |
| 88 | * @returns A reference to the emulated CPU. | 90 | * @returns A reference to the emulated CPU. |
| @@ -91,6 +93,9 @@ public: | |||
| 91 | return *cpu_core; | 93 | return *cpu_core; |
| 92 | } | 94 | } |
| 93 | 95 | ||
| 96 | PerfStats perf_stats; | ||
| 97 | FrameLimiter frame_limiter; | ||
| 98 | |||
| 94 | private: | 99 | private: |
| 95 | /** | 100 | /** |
| 96 | * Initialize the emulated system. | 101 | * Initialize the emulated system. |
diff --git a/src/core/frontend/emu_window.cpp b/src/core/frontend/emu_window.cpp index 6b4637741..a155b657d 100644 --- a/src/core/frontend/emu_window.cpp +++ b/src/core/frontend/emu_window.cpp | |||
| @@ -5,7 +5,7 @@ | |||
| 5 | #include <algorithm> | 5 | #include <algorithm> |
| 6 | #include <cmath> | 6 | #include <cmath> |
| 7 | #include "common/assert.h" | 7 | #include "common/assert.h" |
| 8 | #include "common/profiler_reporting.h" | 8 | #include "core/core.h" |
| 9 | #include "core/frontend/emu_window.h" | 9 | #include "core/frontend/emu_window.h" |
| 10 | #include "core/frontend/key_map.h" | 10 | #include "core/frontend/key_map.h" |
| 11 | #include "video_core/video_core.h" | 11 | #include "video_core/video_core.h" |
| @@ -104,8 +104,7 @@ void EmuWindow::AccelerometerChanged(float x, float y, float z) { | |||
| 104 | void EmuWindow::GyroscopeChanged(float x, float y, float z) { | 104 | void EmuWindow::GyroscopeChanged(float x, float y, float z) { |
| 105 | constexpr float FULL_FPS = 60; | 105 | constexpr float FULL_FPS = 60; |
| 106 | float coef = GetGyroscopeRawToDpsCoefficient(); | 106 | float coef = GetGyroscopeRawToDpsCoefficient(); |
| 107 | float stretch = | 107 | float stretch = Core::System::GetInstance().perf_stats.GetLastFrameTimeScale(); |
| 108 | FULL_FPS / Common::Profiling::GetTimingResultsAggregator()->GetAggregatedResults().fps; | ||
| 109 | std::lock_guard<std::mutex> lock(gyro_mutex); | 108 | std::lock_guard<std::mutex> lock(gyro_mutex); |
| 110 | gyro_x = static_cast<s16>(x * coef * stretch); | 109 | gyro_x = static_cast<s16>(x * coef * stretch); |
| 111 | gyro_y = static_cast<s16>(y * coef * stretch); | 110 | gyro_y = static_cast<s16>(y * coef * stretch); |
diff --git a/src/core/hle/kernel/server_session.h b/src/core/hle/kernel/server_session.h index c088b9a19..4ffe97b78 100644 --- a/src/core/hle/kernel/server_session.h +++ b/src/core/hle/kernel/server_session.h | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <memory> | ||
| 7 | #include <string> | 8 | #include <string> |
| 8 | #include "common/assert.h" | 9 | #include "common/assert.h" |
| 9 | #include "common/common_types.h" | 10 | #include "common/common_types.h" |
diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index c557a2279..6ab31c70b 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h | |||
| @@ -11,7 +11,6 @@ | |||
| 11 | #include <boost/container/flat_set.hpp> | 11 | #include <boost/container/flat_set.hpp> |
| 12 | #include "common/common_types.h" | 12 | #include "common/common_types.h" |
| 13 | #include "core/arm/arm_interface.h" | 13 | #include "core/arm/arm_interface.h" |
| 14 | #include "core/core.h" | ||
| 15 | #include "core/hle/kernel/kernel.h" | 14 | #include "core/hle/kernel/kernel.h" |
| 16 | #include "core/hle/result.h" | 15 | #include "core/hle/result.h" |
| 17 | 16 | ||
diff --git a/src/core/hle/service/gsp_gpu.cpp b/src/core/hle/service/gsp_gpu.cpp index 1457518d4..097ed87e4 100644 --- a/src/core/hle/service/gsp_gpu.cpp +++ b/src/core/hle/service/gsp_gpu.cpp | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | #include "common/bit_field.h" | 5 | #include "common/bit_field.h" |
| 6 | #include "common/microprofile.h" | 6 | #include "common/microprofile.h" |
| 7 | #include "core/core.h" | ||
| 7 | #include "core/hle/kernel/event.h" | 8 | #include "core/hle/kernel/event.h" |
| 8 | #include "core/hle/kernel/shared_memory.h" | 9 | #include "core/hle/kernel/shared_memory.h" |
| 9 | #include "core/hle/result.h" | 10 | #include "core/hle/result.h" |
| @@ -280,6 +281,7 @@ ResultCode SetBufferSwap(u32 screen_id, const FrameBufferInfo& info) { | |||
| 280 | 281 | ||
| 281 | if (screen_id == 0) { | 282 | if (screen_id == 0) { |
| 282 | MicroProfileFlip(); | 283 | MicroProfileFlip(); |
| 284 | Core::System::GetInstance().perf_stats.EndGameFrame(); | ||
| 283 | } | 285 | } |
| 284 | 286 | ||
| 285 | return RESULT_SUCCESS; | 287 | return RESULT_SUCCESS; |
diff --git a/src/core/hle/service/ldr_ro/ldr_ro.cpp b/src/core/hle/service/ldr_ro/ldr_ro.cpp index 8d00a7577..7af76676b 100644 --- a/src/core/hle/service/ldr_ro/ldr_ro.cpp +++ b/src/core/hle/service/ldr_ro/ldr_ro.cpp | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | #include "common/common_types.h" | 6 | #include "common/common_types.h" |
| 7 | #include "common/logging/log.h" | 7 | #include "common/logging/log.h" |
| 8 | #include "core/arm/arm_interface.h" | 8 | #include "core/arm/arm_interface.h" |
| 9 | #include "core/core.h" | ||
| 9 | #include "core/hle/kernel/process.h" | 10 | #include "core/hle/kernel/process.h" |
| 10 | #include "core/hle/kernel/vm_manager.h" | 11 | #include "core/hle/kernel/vm_manager.h" |
| 11 | #include "core/hle/service/ldr_ro/cro_helper.h" | 12 | #include "core/hle/service/ldr_ro/cro_helper.h" |
diff --git a/src/core/hw/gpu.cpp b/src/core/hw/gpu.cpp index fa8c13d36..42809c731 100644 --- a/src/core/hw/gpu.cpp +++ b/src/core/hw/gpu.cpp | |||
| @@ -8,17 +8,13 @@ | |||
| 8 | #include "common/color.h" | 8 | #include "common/color.h" |
| 9 | #include "common/common_types.h" | 9 | #include "common/common_types.h" |
| 10 | #include "common/logging/log.h" | 10 | #include "common/logging/log.h" |
| 11 | #include "common/math_util.h" | ||
| 12 | #include "common/microprofile.h" | 11 | #include "common/microprofile.h" |
| 13 | #include "common/thread.h" | ||
| 14 | #include "common/timer.h" | ||
| 15 | #include "common/vector_math.h" | 12 | #include "common/vector_math.h" |
| 16 | #include "core/core_timing.h" | 13 | #include "core/core_timing.h" |
| 17 | #include "core/hle/service/gsp_gpu.h" | 14 | #include "core/hle/service/gsp_gpu.h" |
| 18 | #include "core/hw/gpu.h" | 15 | #include "core/hw/gpu.h" |
| 19 | #include "core/hw/hw.h" | 16 | #include "core/hw/hw.h" |
| 20 | #include "core/memory.h" | 17 | #include "core/memory.h" |
| 21 | #include "core/settings.h" | ||
| 22 | #include "core/tracer/recorder.h" | 18 | #include "core/tracer/recorder.h" |
| 23 | #include "video_core/command_processor.h" | 19 | #include "video_core/command_processor.h" |
| 24 | #include "video_core/debug_utils/debug_utils.h" | 20 | #include "video_core/debug_utils/debug_utils.h" |
| @@ -32,19 +28,9 @@ namespace GPU { | |||
| 32 | Regs g_regs; | 28 | Regs g_regs; |
| 33 | 29 | ||
| 34 | /// 268MHz CPU clocks / 60Hz frames per second | 30 | /// 268MHz CPU clocks / 60Hz frames per second |
| 35 | const u64 frame_ticks = BASE_CLOCK_RATE_ARM11 / 60; | 31 | const u64 frame_ticks = BASE_CLOCK_RATE_ARM11 / SCREEN_REFRESH_RATE; |
| 36 | /// Event id for CoreTiming | 32 | /// Event id for CoreTiming |
| 37 | static int vblank_event; | 33 | static int vblank_event; |
| 38 | /// Total number of frames drawn | ||
| 39 | static u64 frame_count; | ||
| 40 | /// Start clock for frame limiter | ||
| 41 | static u32 time_point; | ||
| 42 | /// Total delay caused by slow frames | ||
| 43 | static float time_delay; | ||
| 44 | constexpr float FIXED_FRAME_TIME = 1000.0f / 60; | ||
| 45 | // Max lag caused by slow frames. Can be adjusted to compensate for too many slow frames. Higher | ||
| 46 | // values increases time needed to limit frame rate after spikes | ||
| 47 | constexpr float MAX_LAG_TIME = 18; | ||
| 48 | 34 | ||
| 49 | template <typename T> | 35 | template <typename T> |
| 50 | inline void Read(T& var, const u32 raw_addr) { | 36 | inline void Read(T& var, const u32 raw_addr) { |
| @@ -522,24 +508,8 @@ template void Write<u32>(u32 addr, const u32 data); | |||
| 522 | template void Write<u16>(u32 addr, const u16 data); | 508 | template void Write<u16>(u32 addr, const u16 data); |
| 523 | template void Write<u8>(u32 addr, const u8 data); | 509 | template void Write<u8>(u32 addr, const u8 data); |
| 524 | 510 | ||
| 525 | static void FrameLimiter() { | ||
| 526 | time_delay += FIXED_FRAME_TIME; | ||
| 527 | time_delay = MathUtil::Clamp(time_delay, -MAX_LAG_TIME, MAX_LAG_TIME); | ||
| 528 | s32 desired_time = static_cast<s32>(time_delay); | ||
| 529 | s32 elapsed_time = static_cast<s32>(Common::Timer::GetTimeMs() - time_point); | ||
| 530 | |||
| 531 | if (elapsed_time < desired_time) { | ||
| 532 | Common::SleepCurrentThread(desired_time - elapsed_time); | ||
| 533 | } | ||
| 534 | |||
| 535 | u32 frame_time = Common::Timer::GetTimeMs() - time_point; | ||
| 536 | |||
| 537 | time_delay -= frame_time; | ||
| 538 | } | ||
| 539 | |||
| 540 | /// Update hardware | 511 | /// Update hardware |
| 541 | static void VBlankCallback(u64 userdata, int cycles_late) { | 512 | static void VBlankCallback(u64 userdata, int cycles_late) { |
| 542 | frame_count++; | ||
| 543 | VideoCore::g_renderer->SwapBuffers(); | 513 | VideoCore::g_renderer->SwapBuffers(); |
| 544 | 514 | ||
| 545 | // Signal to GSP that GPU interrupt has occurred | 515 | // Signal to GSP that GPU interrupt has occurred |
| @@ -550,12 +520,6 @@ static void VBlankCallback(u64 userdata, int cycles_late) { | |||
| 550 | Service::GSP::SignalInterrupt(Service::GSP::InterruptId::PDC0); | 520 | Service::GSP::SignalInterrupt(Service::GSP::InterruptId::PDC0); |
| 551 | Service::GSP::SignalInterrupt(Service::GSP::InterruptId::PDC1); | 521 | Service::GSP::SignalInterrupt(Service::GSP::InterruptId::PDC1); |
| 552 | 522 | ||
| 553 | if (!Settings::values.use_vsync && Settings::values.toggle_framelimit) { | ||
| 554 | FrameLimiter(); | ||
| 555 | } | ||
| 556 | |||
| 557 | time_point = Common::Timer::GetTimeMs(); | ||
| 558 | |||
| 559 | // Reschedule recurrent event | 523 | // Reschedule recurrent event |
| 560 | CoreTiming::ScheduleEvent(frame_ticks - cycles_late, vblank_event); | 524 | CoreTiming::ScheduleEvent(frame_ticks - cycles_late, vblank_event); |
| 561 | } | 525 | } |
| @@ -590,9 +554,6 @@ void Init() { | |||
| 590 | framebuffer_sub.color_format.Assign(Regs::PixelFormat::RGB8); | 554 | framebuffer_sub.color_format.Assign(Regs::PixelFormat::RGB8); |
| 591 | framebuffer_sub.active_fb = 0; | 555 | framebuffer_sub.active_fb = 0; |
| 592 | 556 | ||
| 593 | frame_count = 0; | ||
| 594 | time_point = Common::Timer::GetTimeMs(); | ||
| 595 | |||
| 596 | vblank_event = CoreTiming::RegisterEvent("GPU::VBlankCallback", VBlankCallback); | 557 | vblank_event = CoreTiming::RegisterEvent("GPU::VBlankCallback", VBlankCallback); |
| 597 | CoreTiming::ScheduleEvent(frame_ticks, vblank_event); | 558 | CoreTiming::ScheduleEvent(frame_ticks, vblank_event); |
| 598 | 559 | ||
diff --git a/src/core/hw/gpu.h b/src/core/hw/gpu.h index d53381216..bdd997b2a 100644 --- a/src/core/hw/gpu.h +++ b/src/core/hw/gpu.h | |||
| @@ -13,6 +13,8 @@ | |||
| 13 | 13 | ||
| 14 | namespace GPU { | 14 | namespace GPU { |
| 15 | 15 | ||
| 16 | constexpr float SCREEN_REFRESH_RATE = 60; | ||
| 17 | |||
| 16 | // Returns index corresponding to the Regs member labeled by field_name | 18 | // Returns index corresponding to the Regs member labeled by field_name |
| 17 | // TODO: Due to Visual studio bug 209229, offsetof does not return constant expressions | 19 | // TODO: Due to Visual studio bug 209229, offsetof does not return constant expressions |
| 18 | // when used with array elements (e.g. GPU_REG_INDEX(memory_fill_config[0])). | 20 | // when used with array elements (e.g. GPU_REG_INDEX(memory_fill_config[0])). |
diff --git a/src/core/perf_stats.cpp b/src/core/perf_stats.cpp new file mode 100644 index 000000000..2cdfb9ded --- /dev/null +++ b/src/core/perf_stats.cpp | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <chrono> | ||
| 6 | #include <mutex> | ||
| 7 | #include <thread> | ||
| 8 | #include "common/math_util.h" | ||
| 9 | #include "core/hw/gpu.h" | ||
| 10 | #include "core/perf_stats.h" | ||
| 11 | #include "core/settings.h" | ||
| 12 | |||
| 13 | using namespace std::chrono_literals; | ||
| 14 | using DoubleSecs = std::chrono::duration<double, std::chrono::seconds::period>; | ||
| 15 | using std::chrono::duration_cast; | ||
| 16 | using std::chrono::microseconds; | ||
| 17 | |||
| 18 | namespace Core { | ||
| 19 | |||
| 20 | void PerfStats::BeginSystemFrame() { | ||
| 21 | std::lock_guard<std::mutex> lock(object_mutex); | ||
| 22 | |||
| 23 | frame_begin = Clock::now(); | ||
| 24 | } | ||
| 25 | |||
| 26 | void PerfStats::EndSystemFrame() { | ||
| 27 | std::lock_guard<std::mutex> lock(object_mutex); | ||
| 28 | |||
| 29 | auto frame_end = Clock::now(); | ||
| 30 | accumulated_frametime += frame_end - frame_begin; | ||
| 31 | system_frames += 1; | ||
| 32 | |||
| 33 | previous_frame_length = frame_end - previous_frame_end; | ||
| 34 | previous_frame_end = frame_end; | ||
| 35 | } | ||
| 36 | |||
| 37 | void PerfStats::EndGameFrame() { | ||
| 38 | std::lock_guard<std::mutex> lock(object_mutex); | ||
| 39 | |||
| 40 | game_frames += 1; | ||
| 41 | } | ||
| 42 | |||
| 43 | PerfStats::Results PerfStats::GetAndResetStats(u64 current_system_time_us) { | ||
| 44 | std::lock_guard<std::mutex> lock(object_mutex); | ||
| 45 | |||
| 46 | auto now = Clock::now(); | ||
| 47 | // Walltime elapsed since stats were reset | ||
| 48 | auto interval = duration_cast<DoubleSecs>(now - reset_point).count(); | ||
| 49 | |||
| 50 | auto system_us_per_second = | ||
| 51 | static_cast<double>(current_system_time_us - reset_point_system_us) / interval; | ||
| 52 | |||
| 53 | Results results{}; | ||
| 54 | results.system_fps = static_cast<double>(system_frames) / interval; | ||
| 55 | results.game_fps = static_cast<double>(game_frames) / interval; | ||
| 56 | results.frametime = duration_cast<DoubleSecs>(accumulated_frametime).count() / | ||
| 57 | static_cast<double>(system_frames); | ||
| 58 | results.emulation_speed = system_us_per_second / 1'000'000.0; | ||
| 59 | |||
| 60 | // Reset counters | ||
| 61 | reset_point = now; | ||
| 62 | reset_point_system_us = current_system_time_us; | ||
| 63 | accumulated_frametime = Clock::duration::zero(); | ||
| 64 | system_frames = 0; | ||
| 65 | game_frames = 0; | ||
| 66 | |||
| 67 | return results; | ||
| 68 | } | ||
| 69 | |||
| 70 | double PerfStats::GetLastFrameTimeScale() { | ||
| 71 | std::lock_guard<std::mutex> lock(object_mutex); | ||
| 72 | |||
| 73 | constexpr double FRAME_LENGTH = 1.0 / GPU::SCREEN_REFRESH_RATE; | ||
| 74 | return duration_cast<DoubleSecs>(previous_frame_length).count() / FRAME_LENGTH; | ||
| 75 | } | ||
| 76 | |||
| 77 | void FrameLimiter::DoFrameLimiting(u64 current_system_time_us) { | ||
| 78 | // Max lag caused by slow frames. Can be adjusted to compensate for too many slow frames. Higher | ||
| 79 | // values increase the time needed to recover and limit framerate again after spikes. | ||
| 80 | constexpr microseconds MAX_LAG_TIME_US = 25ms; | ||
| 81 | |||
| 82 | if (!Settings::values.toggle_framelimit) { | ||
| 83 | return; | ||
| 84 | } | ||
| 85 | |||
| 86 | auto now = Clock::now(); | ||
| 87 | |||
| 88 | frame_limiting_delta_err += microseconds(current_system_time_us - previous_system_time_us); | ||
| 89 | frame_limiting_delta_err -= duration_cast<microseconds>(now - previous_walltime); | ||
| 90 | frame_limiting_delta_err = | ||
| 91 | MathUtil::Clamp(frame_limiting_delta_err, -MAX_LAG_TIME_US, MAX_LAG_TIME_US); | ||
| 92 | |||
| 93 | if (frame_limiting_delta_err > microseconds::zero()) { | ||
| 94 | std::this_thread::sleep_for(frame_limiting_delta_err); | ||
| 95 | |||
| 96 | auto now_after_sleep = Clock::now(); | ||
| 97 | frame_limiting_delta_err -= duration_cast<microseconds>(now_after_sleep - now); | ||
| 98 | now = now_after_sleep; | ||
| 99 | } | ||
| 100 | |||
| 101 | previous_system_time_us = current_system_time_us; | ||
| 102 | previous_walltime = now; | ||
| 103 | } | ||
| 104 | |||
| 105 | } // namespace Core | ||
diff --git a/src/core/perf_stats.h b/src/core/perf_stats.h new file mode 100644 index 000000000..362b205c8 --- /dev/null +++ b/src/core/perf_stats.h | |||
| @@ -0,0 +1,83 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <chrono> | ||
| 8 | #include <mutex> | ||
| 9 | #include "common/common_types.h" | ||
| 10 | |||
| 11 | namespace Core { | ||
| 12 | |||
| 13 | /** | ||
| 14 | * Class to manage and query performance/timing statistics. All public functions of this class are | ||
| 15 | * thread-safe unless stated otherwise. | ||
| 16 | */ | ||
| 17 | class PerfStats { | ||
| 18 | public: | ||
| 19 | using Clock = std::chrono::high_resolution_clock; | ||
| 20 | |||
| 21 | struct Results { | ||
| 22 | /// System FPS (LCD VBlanks) in Hz | ||
| 23 | double system_fps; | ||
| 24 | /// Game FPS (GSP frame submissions) in Hz | ||
| 25 | double game_fps; | ||
| 26 | /// Walltime per system frame, in seconds, excluding any waits | ||
| 27 | double frametime; | ||
| 28 | /// Ratio of walltime / emulated time elapsed | ||
| 29 | double emulation_speed; | ||
| 30 | }; | ||
| 31 | |||
| 32 | void BeginSystemFrame(); | ||
| 33 | void EndSystemFrame(); | ||
| 34 | void EndGameFrame(); | ||
| 35 | |||
| 36 | Results GetAndResetStats(u64 current_system_time_us); | ||
| 37 | |||
| 38 | /** | ||
| 39 | * Gets the ratio between walltime and the emulated time of the previous system frame. This is | ||
| 40 | * useful for scaling inputs or outputs moving between the two time domains. | ||
| 41 | */ | ||
| 42 | double GetLastFrameTimeScale(); | ||
| 43 | |||
| 44 | private: | ||
| 45 | std::mutex object_mutex; | ||
| 46 | |||
| 47 | /// Point when the cumulative counters were reset | ||
| 48 | Clock::time_point reset_point = Clock::now(); | ||
| 49 | /// System time when the cumulative counters were reset | ||
| 50 | u64 reset_point_system_us = 0; | ||
| 51 | |||
| 52 | /// Cumulative duration (excluding v-sync/frame-limiting) of frames since last reset | ||
| 53 | Clock::duration accumulated_frametime = Clock::duration::zero(); | ||
| 54 | /// Cumulative number of system frames (LCD VBlanks) presented since last reset | ||
| 55 | u32 system_frames = 0; | ||
| 56 | /// Cumulative number of game frames (GSP frame submissions) since last reset | ||
| 57 | u32 game_frames = 0; | ||
| 58 | |||
| 59 | /// Point when the previous system frame ended | ||
| 60 | Clock::time_point previous_frame_end = reset_point; | ||
| 61 | /// Point when the current system frame began | ||
| 62 | Clock::time_point frame_begin = reset_point; | ||
| 63 | /// Total visible duration (including frame-limiting, etc.) of the previous system frame | ||
| 64 | Clock::duration previous_frame_length = Clock::duration::zero(); | ||
| 65 | }; | ||
| 66 | |||
| 67 | class FrameLimiter { | ||
| 68 | public: | ||
| 69 | using Clock = std::chrono::high_resolution_clock; | ||
| 70 | |||
| 71 | void DoFrameLimiting(u64 current_system_time_us); | ||
| 72 | |||
| 73 | private: | ||
| 74 | /// Emulated system time (in microseconds) at the last limiter invocation | ||
| 75 | u64 previous_system_time_us = 0; | ||
| 76 | /// Walltime at the last limiter invocation | ||
| 77 | Clock::time_point previous_walltime = Clock::now(); | ||
| 78 | |||
| 79 | /// Accumulated difference between walltime and emulated time | ||
| 80 | std::chrono::microseconds frame_limiting_delta_err{0}; | ||
| 81 | }; | ||
| 82 | |||
| 83 | } // namespace Core | ||