diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/common/profiler.cpp | 101 | ||||
| -rw-r--r-- | src/common/profiler_reporting.h | 83 | ||||
| -rw-r--r-- | src/common/synchronized_wrapper.h | 43 |
4 files changed, 25 insertions, 204 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 26c83efda..8a6170257 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt | |||
| @@ -35,7 +35,6 @@ set(SRCS | |||
| 35 | memory_util.cpp | 35 | memory_util.cpp |
| 36 | microprofile.cpp | 36 | microprofile.cpp |
| 37 | misc.cpp | 37 | misc.cpp |
| 38 | profiler.cpp | ||
| 39 | scm_rev.cpp | 38 | scm_rev.cpp |
| 40 | string_util.cpp | 39 | string_util.cpp |
| 41 | symbols.cpp | 40 | symbols.cpp |
| @@ -68,7 +67,6 @@ set(HEADERS | |||
| 68 | microprofile.h | 67 | microprofile.h |
| 69 | microprofileui.h | 68 | microprofileui.h |
| 70 | platform.h | 69 | platform.h |
| 71 | profiler_reporting.h | ||
| 72 | quaternion.h | 70 | quaternion.h |
| 73 | scm_rev.h | 71 | scm_rev.h |
| 74 | scope_exit.h | 72 | scope_exit.h |
diff --git a/src/common/profiler.cpp b/src/common/profiler.cpp deleted file mode 100644 index b40e7205d..000000000 --- a/src/common/profiler.cpp +++ /dev/null | |||
| @@ -1,101 +0,0 @@ | |||
| 1 | // Copyright 2015 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <vector> | ||
| 8 | #include "common/assert.h" | ||
| 9 | #include "common/profiler_reporting.h" | ||
| 10 | #include "common/synchronized_wrapper.h" | ||
| 11 | |||
| 12 | namespace Common { | ||
| 13 | namespace Profiling { | ||
| 14 | |||
| 15 | ProfilingManager::ProfilingManager() | ||
| 16 | : last_frame_end(Clock::now()), this_frame_start(Clock::now()) {} | ||
| 17 | |||
| 18 | void ProfilingManager::BeginFrame() { | ||
| 19 | this_frame_start = Clock::now(); | ||
| 20 | } | ||
| 21 | |||
| 22 | void ProfilingManager::FinishFrame() { | ||
| 23 | Clock::time_point now = Clock::now(); | ||
| 24 | |||
| 25 | results.interframe_time = now - last_frame_end; | ||
| 26 | results.frame_time = now - this_frame_start; | ||
| 27 | |||
| 28 | last_frame_end = now; | ||
| 29 | } | ||
| 30 | |||
| 31 | TimingResultsAggregator::TimingResultsAggregator(size_t window_size) | ||
| 32 | : max_window_size(window_size), window_size(0) { | ||
| 33 | interframe_times.resize(window_size, Duration::zero()); | ||
| 34 | frame_times.resize(window_size, Duration::zero()); | ||
| 35 | } | ||
| 36 | |||
| 37 | void TimingResultsAggregator::Clear() { | ||
| 38 | window_size = cursor = 0; | ||
| 39 | } | ||
| 40 | |||
| 41 | void TimingResultsAggregator::AddFrame(const ProfilingFrameResult& frame_result) { | ||
| 42 | interframe_times[cursor] = frame_result.interframe_time; | ||
| 43 | frame_times[cursor] = frame_result.frame_time; | ||
| 44 | |||
| 45 | ++cursor; | ||
| 46 | if (cursor == max_window_size) | ||
| 47 | cursor = 0; | ||
| 48 | if (window_size < max_window_size) | ||
| 49 | ++window_size; | ||
| 50 | } | ||
| 51 | |||
| 52 | static AggregatedDuration AggregateField(const std::vector<Duration>& v, size_t len) { | ||
| 53 | AggregatedDuration result; | ||
| 54 | result.avg = Duration::zero(); | ||
| 55 | result.min = result.max = (len == 0 ? Duration::zero() : v[0]); | ||
| 56 | |||
| 57 | for (size_t i = 0; i < len; ++i) { | ||
| 58 | Duration value = v[i]; | ||
| 59 | result.avg += value; | ||
| 60 | result.min = std::min(result.min, value); | ||
| 61 | result.max = std::max(result.max, value); | ||
| 62 | } | ||
| 63 | if (len != 0) | ||
| 64 | result.avg /= len; | ||
| 65 | |||
| 66 | return result; | ||
| 67 | } | ||
| 68 | |||
| 69 | static float tof(Common::Profiling::Duration dur) { | ||
| 70 | using FloatMs = std::chrono::duration<float, std::chrono::milliseconds::period>; | ||
| 71 | return std::chrono::duration_cast<FloatMs>(dur).count(); | ||
| 72 | } | ||
| 73 | |||
| 74 | AggregatedFrameResult TimingResultsAggregator::GetAggregatedResults() const { | ||
| 75 | AggregatedFrameResult result; | ||
| 76 | |||
| 77 | result.interframe_time = AggregateField(interframe_times, window_size); | ||
| 78 | result.frame_time = AggregateField(frame_times, window_size); | ||
| 79 | |||
| 80 | if (result.interframe_time.avg != Duration::zero()) { | ||
| 81 | result.fps = 1000.0f / tof(result.interframe_time.avg); | ||
| 82 | } else { | ||
| 83 | result.fps = 0.0f; | ||
| 84 | } | ||
| 85 | |||
| 86 | return result; | ||
| 87 | } | ||
| 88 | |||
| 89 | ProfilingManager& GetProfilingManager() { | ||
| 90 | // Takes advantage of "magic" static initialization for race-free initialization. | ||
| 91 | static ProfilingManager manager; | ||
| 92 | return manager; | ||
| 93 | } | ||
| 94 | |||
| 95 | SynchronizedRef<TimingResultsAggregator> GetTimingResultsAggregator() { | ||
| 96 | static SynchronizedWrapper<TimingResultsAggregator> aggregator(30); | ||
| 97 | return SynchronizedRef<TimingResultsAggregator>(aggregator); | ||
| 98 | } | ||
| 99 | |||
| 100 | } // namespace Profiling | ||
| 101 | } // namespace Common | ||
diff --git a/src/common/profiler_reporting.h b/src/common/profiler_reporting.h deleted file mode 100644 index e9ce6d41c..000000000 --- a/src/common/profiler_reporting.h +++ /dev/null | |||
| @@ -1,83 +0,0 @@ | |||
| 1 | // Copyright 2015 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 <cstddef> | ||
| 9 | #include <vector> | ||
| 10 | #include "common/synchronized_wrapper.h" | ||
| 11 | |||
| 12 | namespace Common { | ||
| 13 | namespace Profiling { | ||
| 14 | |||
| 15 | using Clock = std::chrono::high_resolution_clock; | ||
| 16 | using Duration = Clock::duration; | ||
| 17 | |||
| 18 | struct ProfilingFrameResult { | ||
| 19 | /// Time since the last delivered frame | ||
| 20 | Duration interframe_time; | ||
| 21 | |||
| 22 | /// Time spent processing a frame, excluding VSync | ||
| 23 | Duration frame_time; | ||
| 24 | }; | ||
| 25 | |||
| 26 | class ProfilingManager final { | ||
| 27 | public: | ||
| 28 | ProfilingManager(); | ||
| 29 | |||
| 30 | /// This should be called after swapping screen buffers. | ||
| 31 | void BeginFrame(); | ||
| 32 | /// This should be called before swapping screen buffers. | ||
| 33 | void FinishFrame(); | ||
| 34 | |||
| 35 | /// Get the timing results from the previous frame. This is updated when you call FinishFrame(). | ||
| 36 | const ProfilingFrameResult& GetPreviousFrameResults() const { | ||
| 37 | return results; | ||
| 38 | } | ||
| 39 | |||
| 40 | private: | ||
| 41 | Clock::time_point last_frame_end; | ||
| 42 | Clock::time_point this_frame_start; | ||
| 43 | |||
| 44 | ProfilingFrameResult results; | ||
| 45 | }; | ||
| 46 | |||
| 47 | struct AggregatedDuration { | ||
| 48 | Duration avg, min, max; | ||
| 49 | }; | ||
| 50 | |||
| 51 | struct AggregatedFrameResult { | ||
| 52 | /// Time since the last delivered frame | ||
| 53 | AggregatedDuration interframe_time; | ||
| 54 | |||
| 55 | /// Time spent processing a frame, excluding VSync | ||
| 56 | AggregatedDuration frame_time; | ||
| 57 | |||
| 58 | float fps; | ||
| 59 | }; | ||
| 60 | |||
| 61 | class TimingResultsAggregator final { | ||
| 62 | public: | ||
| 63 | TimingResultsAggregator(size_t window_size); | ||
| 64 | |||
| 65 | void Clear(); | ||
| 66 | |||
| 67 | void AddFrame(const ProfilingFrameResult& frame_result); | ||
| 68 | |||
| 69 | AggregatedFrameResult GetAggregatedResults() const; | ||
| 70 | |||
| 71 | size_t max_window_size; | ||
| 72 | size_t window_size; | ||
| 73 | size_t cursor; | ||
| 74 | |||
| 75 | std::vector<Duration> interframe_times; | ||
| 76 | std::vector<Duration> frame_times; | ||
| 77 | }; | ||
| 78 | |||
| 79 | ProfilingManager& GetProfilingManager(); | ||
| 80 | SynchronizedRef<TimingResultsAggregator> GetTimingResultsAggregator(); | ||
| 81 | |||
| 82 | } // namespace Profiling | ||
| 83 | } // namespace Common | ||
diff --git a/src/common/synchronized_wrapper.h b/src/common/synchronized_wrapper.h index 04b4f2e51..4a1984c46 100644 --- a/src/common/synchronized_wrapper.h +++ b/src/common/synchronized_wrapper.h | |||
| @@ -9,25 +9,8 @@ | |||
| 9 | 9 | ||
| 10 | namespace Common { | 10 | namespace Common { |
| 11 | 11 | ||
| 12 | /** | ||
| 13 | * Wraps an object, only allowing access to it via a locking reference wrapper. Good to ensure no | ||
| 14 | * one forgets to lock a mutex before acessing an object. To access the wrapped object construct a | ||
| 15 | * SyncronizedRef on this wrapper. Inspired by Rust's Mutex type | ||
| 16 | * (http://doc.rust-lang.org/std/sync/struct.Mutex.html). | ||
| 17 | */ | ||
| 18 | template <typename T> | 12 | template <typename T> |
| 19 | class SynchronizedWrapper { | 13 | class SynchronizedWrapper; |
| 20 | public: | ||
| 21 | template <typename... Args> | ||
| 22 | SynchronizedWrapper(Args&&... args) : data(std::forward<Args>(args)...) {} | ||
| 23 | |||
| 24 | private: | ||
| 25 | template <typename U> | ||
| 26 | friend class SynchronizedRef; | ||
| 27 | |||
| 28 | std::mutex mutex; | ||
| 29 | T data; | ||
| 30 | }; | ||
| 31 | 14 | ||
| 32 | /** | 15 | /** |
| 33 | * Synchronized reference, that keeps a SynchronizedWrapper's mutex locked during its lifetime. This | 16 | * Synchronized reference, that keeps a SynchronizedWrapper's mutex locked during its lifetime. This |
| @@ -75,4 +58,28 @@ private: | |||
| 75 | SynchronizedWrapper<T>* wrapper; | 58 | SynchronizedWrapper<T>* wrapper; |
| 76 | }; | 59 | }; |
| 77 | 60 | ||
| 61 | /** | ||
| 62 | * Wraps an object, only allowing access to it via a locking reference wrapper. Good to ensure no | ||
| 63 | * one forgets to lock a mutex before acessing an object. To access the wrapped object construct a | ||
| 64 | * SyncronizedRef on this wrapper. Inspired by Rust's Mutex type | ||
| 65 | * (http://doc.rust-lang.org/std/sync/struct.Mutex.html). | ||
| 66 | */ | ||
| 67 | template <typename T> | ||
| 68 | class SynchronizedWrapper { | ||
| 69 | public: | ||
| 70 | template <typename... Args> | ||
| 71 | SynchronizedWrapper(Args&&... args) : data(std::forward<Args>(args)...) {} | ||
| 72 | |||
| 73 | SynchronizedRef<T> Lock() { | ||
| 74 | return {*this}; | ||
| 75 | } | ||
| 76 | |||
| 77 | private: | ||
| 78 | template <typename U> | ||
| 79 | friend class SynchronizedRef; | ||
| 80 | |||
| 81 | std::mutex mutex; | ||
| 82 | T data; | ||
| 83 | }; | ||
| 84 | |||
| 78 | } // namespace Common | 85 | } // namespace Common |