summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/common/thread.h4
-rw-r--r--src/core/host_timing.cpp101
-rw-r--r--src/core/host_timing.h30
-rw-r--r--src/tests/CMakeLists.txt1
-rw-r--r--src/tests/core/host_timing.cpp150
5 files changed, 243 insertions, 43 deletions
diff --git a/src/common/thread.h b/src/common/thread.h
index 2fc071685..127cc7e23 100644
--- a/src/common/thread.h
+++ b/src/common/thread.h
@@ -9,6 +9,7 @@
9#include <cstddef> 9#include <cstddef>
10#include <mutex> 10#include <mutex>
11#include <thread> 11#include <thread>
12#include "common/common_types.h"
12 13
13namespace Common { 14namespace Common {
14 15
@@ -28,8 +29,7 @@ public:
28 is_set = false; 29 is_set = false;
29 } 30 }
30 31
31 template <class Duration> 32 bool WaitFor(const std::chrono::nanoseconds& time) {
32 bool WaitFor(const std::chrono::duration<Duration>& time) {
33 std::unique_lock lk{mutex}; 33 std::unique_lock lk{mutex};
34 if (!condvar.wait_for(lk, time, [this] { return is_set; })) 34 if (!condvar.wait_for(lk, time, [this] { return is_set; }))
35 return false; 35 return false;
diff --git a/src/core/host_timing.cpp b/src/core/host_timing.cpp
index c02f571c6..d9514b2c5 100644
--- a/src/core/host_timing.cpp
+++ b/src/core/host_timing.cpp
@@ -10,7 +10,6 @@
10#include <tuple> 10#include <tuple>
11 11
12#include "common/assert.h" 12#include "common/assert.h"
13#include "common/thread.h"
14#include "core/core_timing_util.h" 13#include "core/core_timing_util.h"
15 14
16namespace Core::HostTiming { 15namespace Core::HostTiming {
@@ -47,39 +46,55 @@ void CoreTiming::Initialize() {
47 event_fifo_id = 0; 46 event_fifo_id = 0;
48 const auto empty_timed_callback = [](u64, s64) {}; 47 const auto empty_timed_callback = [](u64, s64) {};
49 ev_lost = CreateEvent("_lost_event", empty_timed_callback); 48 ev_lost = CreateEvent("_lost_event", empty_timed_callback);
50 start_time = std::chrono::system_clock::now(); 49 start_time = std::chrono::steady_clock::now();
51 timer_thread = std::make_unique<std::thread>(ThreadEntry, std::ref(*this)); 50 timer_thread = std::make_unique<std::thread>(ThreadEntry, std::ref(*this));
52} 51}
53 52
54void CoreTiming::Shutdown() { 53void CoreTiming::Shutdown() {
55 std::unique_lock<std::mutex> guard(inner_mutex); 54 paused = true;
56 shutting_down = true; 55 shutting_down = true;
57 if (!is_set) { 56 event.Set();
58 is_set = true;
59 condvar.notify_one();
60 }
61 inner_mutex.unlock();
62 timer_thread->join(); 57 timer_thread->join();
63 ClearPendingEvents(); 58 ClearPendingEvents();
59 timer_thread.reset();
60 has_started = false;
61}
62
63void CoreTiming::Pause(bool is_paused) {
64 paused = is_paused;
65}
66
67void CoreTiming::SyncPause(bool is_paused) {
68 if (is_paused == paused && paused_set == paused) {
69 return;
70 }
71 Pause(is_paused);
72 event.Set();
73 while (paused_set != is_paused);
74}
75
76bool CoreTiming::IsRunning() {
77 return !paused_set;
78}
79
80bool CoreTiming::HasPendingEvents() {
81 return !(wait_set && event_queue.empty());
64} 82}
65 83
66void CoreTiming::ScheduleEvent(s64 ns_into_future, const std::shared_ptr<EventType>& event_type, 84void CoreTiming::ScheduleEvent(s64 ns_into_future, const std::shared_ptr<EventType>& event_type,
67 u64 userdata) { 85 u64 userdata) {
68 std::lock_guard guard{inner_mutex}; 86 basic_lock.lock();
69 const u64 timeout = static_cast<u64>(GetGlobalTimeNs().count() + ns_into_future); 87 const u64 timeout = static_cast<u64>(GetGlobalTimeNs().count() + ns_into_future);
70 88
71 event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type}); 89 event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type});
72 90
73 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 91 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
74 if (!is_set) { 92 basic_lock.unlock();
75 is_set = true; 93 event.Set();
76 condvar.notify_one();
77 }
78} 94}
79 95
80void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u64 userdata) { 96void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u64 userdata) {
81 std::lock_guard guard{inner_mutex}; 97 basic_lock.lock();
82
83 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { 98 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
84 return e.type.lock().get() == event_type.get() && e.userdata == userdata; 99 return e.type.lock().get() == event_type.get() && e.userdata == userdata;
85 }); 100 });
@@ -89,6 +104,7 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u
89 event_queue.erase(itr, event_queue.end()); 104 event_queue.erase(itr, event_queue.end());
90 std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 105 std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
91 } 106 }
107 basic_lock.unlock();
92} 108}
93 109
94u64 CoreTiming::GetCPUTicks() const { 110u64 CoreTiming::GetCPUTicks() const {
@@ -106,7 +122,7 @@ void CoreTiming::ClearPendingEvents() {
106} 122}
107 123
108void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) { 124void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) {
109 std::lock_guard guard{inner_mutex}; 125 basic_lock.lock();
110 126
111 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { 127 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
112 return e.type.lock().get() == event_type.get(); 128 return e.type.lock().get() == event_type.get();
@@ -117,43 +133,54 @@ void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) {
117 event_queue.erase(itr, event_queue.end()); 133 event_queue.erase(itr, event_queue.end());
118 std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 134 std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
119 } 135 }
136 basic_lock.unlock();
120} 137}
121 138
122void CoreTiming::Advance() { 139void CoreTiming::Advance() {
123 while (true) { 140 has_started = true;
124 std::unique_lock<std::mutex> guard(inner_mutex); 141 while (!shutting_down) {
125 142 while (!paused) {
126 global_timer = GetGlobalTimeNs().count(); 143 paused_set = false;
127 144 basic_lock.lock();
128 while (!event_queue.empty() && event_queue.front().time <= global_timer) { 145 global_timer = GetGlobalTimeNs().count();
129 Event evt = std::move(event_queue.front()); 146
130 std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 147 while (!event_queue.empty() && event_queue.front().time <= global_timer) {
131 event_queue.pop_back(); 148 Event evt = std::move(event_queue.front());
132 inner_mutex.unlock(); 149 std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
150 event_queue.pop_back();
151 basic_lock.unlock();
152
153 if (auto event_type{evt.type.lock()}) {
154 event_type->callback(evt.userdata, global_timer - evt.time);
155 }
156
157 basic_lock.lock();
158 }
133 159
134 if (auto event_type{evt.type.lock()}) { 160 if (!event_queue.empty()) {
135 event_type->callback(evt.userdata, global_timer - evt.time); 161 std::chrono::nanoseconds next_time = std::chrono::nanoseconds(event_queue.front().time - global_timer);
162 basic_lock.unlock();
163 event.WaitFor(next_time);
164 } else {
165 basic_lock.unlock();
166 wait_set = true;
167 event.Wait();
136 } 168 }
137 169
138 inner_mutex.lock(); 170 wait_set = false;
139 }
140 auto next_time = std::chrono::nanoseconds(event_queue.front().time - global_timer);
141 condvar.wait_for(guard, next_time, [this] { return is_set; });
142 is_set = false;
143 if (shutting_down) {
144 break;
145 } 171 }
172 paused_set = true;
146 } 173 }
147} 174}
148 175
149std::chrono::nanoseconds CoreTiming::GetGlobalTimeNs() const { 176std::chrono::nanoseconds CoreTiming::GetGlobalTimeNs() const {
150 sys_time_point current = std::chrono::system_clock::now(); 177 sys_time_point current = std::chrono::steady_clock::now();
151 auto elapsed = current - start_time; 178 auto elapsed = current - start_time;
152 return std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed); 179 return std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed);
153} 180}
154 181
155std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const { 182std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const {
156 sys_time_point current = std::chrono::system_clock::now(); 183 sys_time_point current = std::chrono::steady_clock::now();
157 auto elapsed = current - start_time; 184 auto elapsed = current - start_time;
158 return std::chrono::duration_cast<std::chrono::microseconds>(elapsed); 185 return std::chrono::duration_cast<std::chrono::microseconds>(elapsed);
159} 186}
diff --git a/src/core/host_timing.h b/src/core/host_timing.h
index a3a32e087..1d053a7fa 100644
--- a/src/core/host_timing.h
+++ b/src/core/host_timing.h
@@ -14,13 +14,15 @@
14#include <vector> 14#include <vector>
15 15
16#include "common/common_types.h" 16#include "common/common_types.h"
17#include "common/spin_lock.h"
18#include "common/thread.h"
17#include "common/threadsafe_queue.h" 19#include "common/threadsafe_queue.h"
18 20
19namespace Core::HostTiming { 21namespace Core::HostTiming {
20 22
21/// A callback that may be scheduled for a particular core timing event. 23/// A callback that may be scheduled for a particular core timing event.
22using TimedCallback = std::function<void(u64 userdata, s64 cycles_late)>; 24using TimedCallback = std::function<void(u64 userdata, s64 cycles_late)>;
23using sys_time_point = std::chrono::time_point<std::chrono::system_clock>; 25using sys_time_point = std::chrono::time_point<std::chrono::steady_clock>;
24 26
25/// Contains the characteristics of a particular event. 27/// Contains the characteristics of a particular event.
26struct EventType { 28struct EventType {
@@ -63,6 +65,23 @@ public:
63 /// Tears down all timing related functionality. 65 /// Tears down all timing related functionality.
64 void Shutdown(); 66 void Shutdown();
65 67
68 /// Pauses/Unpauses the execution of the timer thread.
69 void Pause(bool is_paused);
70
71 /// Pauses/Unpauses the execution of the timer thread and waits until paused.
72 void SyncPause(bool is_paused);
73
74 /// Checks if core timing is running.
75 bool IsRunning();
76
77 /// Checks if the timer thread has started.
78 bool HasStarted() {
79 return has_started;
80 }
81
82 /// Checks if there are any pending time events.
83 bool HasPendingEvents();
84
66 /// Schedules an event in core timing 85 /// Schedules an event in core timing
67 void ScheduleEvent(s64 ns_into_future, const std::shared_ptr<EventType>& event_type, 86 void ScheduleEvent(s64 ns_into_future, const std::shared_ptr<EventType>& event_type,
68 u64 userdata = 0); 87 u64 userdata = 0);
@@ -107,11 +126,14 @@ private:
107 u64 event_fifo_id = 0; 126 u64 event_fifo_id = 0;
108 127
109 std::shared_ptr<EventType> ev_lost; 128 std::shared_ptr<EventType> ev_lost;
110 bool is_set = false; 129 Common::Event event{};
111 std::condition_variable condvar; 130 Common::SpinLock basic_lock{};
112 std::mutex inner_mutex;
113 std::unique_ptr<std::thread> timer_thread; 131 std::unique_ptr<std::thread> timer_thread;
132 std::atomic<bool> paused{};
133 std::atomic<bool> paused_set{};
134 std::atomic<bool> wait_set{};
114 std::atomic<bool> shutting_down{}; 135 std::atomic<bool> shutting_down{};
136 std::atomic<bool> has_started{};
115}; 137};
116 138
117/// Creates a core timing event with the given name and callback. 139/// Creates a core timing event with the given name and callback.
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
index 47ef30aa9..3f750b51c 100644
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -8,6 +8,7 @@ add_executable(tests
8 core/arm/arm_test_common.cpp 8 core/arm/arm_test_common.cpp
9 core/arm/arm_test_common.h 9 core/arm/arm_test_common.h
10 core/core_timing.cpp 10 core/core_timing.cpp
11 core/host_timing.cpp
11 tests.cpp 12 tests.cpp
12) 13)
13 14
diff --git a/src/tests/core/host_timing.cpp b/src/tests/core/host_timing.cpp
new file mode 100644
index 000000000..ca9c8e50a
--- /dev/null
+++ b/src/tests/core/host_timing.cpp
@@ -0,0 +1,150 @@
1// Copyright 2016 Dolphin Emulator Project / 2017 Dolphin Emulator Project
2// Licensed under GPLv2+
3// Refer to the license.txt file included.
4
5#include <catch2/catch.hpp>
6
7#include <array>
8#include <bitset>
9#include <cstdlib>
10#include <memory>
11#include <string>
12
13#include "common/file_util.h"
14#include "core/core.h"
15#include "core/host_timing.h"
16
17// Numbers are chosen randomly to make sure the correct one is given.
18static constexpr std::array<u64, 5> CB_IDS{{42, 144, 93, 1026, UINT64_C(0xFFFF7FFFF7FFFF)}};
19static constexpr int MAX_SLICE_LENGTH = 10000; // Copied from CoreTiming internals
20static constexpr std::array<u64, 5> calls_order{{2,0,1,4,3}};
21static std::array<s64, 5> delays{};
22
23static std::bitset<CB_IDS.size()> callbacks_ran_flags;
24static u64 expected_callback = 0;
25static s64 lateness = 0;
26
27template <unsigned int IDX>
28void HostCallbackTemplate(u64 userdata, s64 nanoseconds_late) {
29 static_assert(IDX < CB_IDS.size(), "IDX out of range");
30 callbacks_ran_flags.set(IDX);
31 REQUIRE(CB_IDS[IDX] == userdata);
32 REQUIRE(CB_IDS[IDX] == CB_IDS[calls_order[expected_callback]]);
33 delays[IDX] = nanoseconds_late;
34 ++expected_callback;
35}
36
37static u64 callbacks_done = 0;
38
39struct ScopeInit final {
40 ScopeInit() {
41 core_timing.Initialize();
42 }
43 ~ScopeInit() {
44 core_timing.Shutdown();
45 }
46
47 Core::HostTiming::CoreTiming core_timing;
48};
49
50TEST_CASE("HostTiming[BasicOrder]", "[core]") {
51 ScopeInit guard;
52 auto& core_timing = guard.core_timing;
53 std::vector<std::shared_ptr<Core::HostTiming::EventType>> events;
54 events.resize(5);
55 events[0] =
56 Core::HostTiming::CreateEvent("callbackA", HostCallbackTemplate<0>);
57 events[1] =
58 Core::HostTiming::CreateEvent("callbackB", HostCallbackTemplate<1>);
59 events[2] =
60 Core::HostTiming::CreateEvent("callbackC", HostCallbackTemplate<2>);
61 events[3] =
62 Core::HostTiming::CreateEvent("callbackD", HostCallbackTemplate<3>);
63 events[4] =
64 Core::HostTiming::CreateEvent("callbackE", HostCallbackTemplate<4>);
65
66 expected_callback = 0;
67
68 core_timing.SyncPause(true);
69
70 u64 one_micro = 1000U;
71 for (std::size_t i = 0; i < events.size(); i++) {
72 u64 order = calls_order[i];
73 core_timing.ScheduleEvent(i*one_micro + 100U, events[order], CB_IDS[order]);
74 }
75 /// test pause
76 REQUIRE(callbacks_ran_flags.none());
77
78 core_timing.Pause(false); // No need to sync
79
80 while (core_timing.HasPendingEvents());
81
82 REQUIRE(callbacks_ran_flags.all());
83
84 for (std::size_t i = 0; i < delays.size(); i++) {
85 const double delay = static_cast<double>(delays[i]);
86 const double micro = delay / 1000.0f;
87 const double mili = micro / 1000.0f;
88 printf("HostTimer Pausing Delay[%zu]: %.3f %.6f\n", i, micro, mili);
89 }
90}
91
92#pragma optimize("", off)
93u64 TestTimerSpeed(Core::HostTiming::CoreTiming& core_timing) {
94 u64 start = core_timing.GetGlobalTimeNs().count();
95 u64 placebo = 0;
96 for (std::size_t i = 0; i < 1000; i++) {
97 placebo += core_timing.GetGlobalTimeNs().count();
98 }
99 u64 end = core_timing.GetGlobalTimeNs().count();
100 return (end - start);
101}
102#pragma optimize("", on)
103
104TEST_CASE("HostTiming[BasicOrderNoPausing]", "[core]") {
105 ScopeInit guard;
106 auto& core_timing = guard.core_timing;
107 std::vector<std::shared_ptr<Core::HostTiming::EventType>> events;
108 events.resize(5);
109 events[0] =
110 Core::HostTiming::CreateEvent("callbackA", HostCallbackTemplate<0>);
111 events[1] =
112 Core::HostTiming::CreateEvent("callbackB", HostCallbackTemplate<1>);
113 events[2] =
114 Core::HostTiming::CreateEvent("callbackC", HostCallbackTemplate<2>);
115 events[3] =
116 Core::HostTiming::CreateEvent("callbackD", HostCallbackTemplate<3>);
117 events[4] =
118 Core::HostTiming::CreateEvent("callbackE", HostCallbackTemplate<4>);
119
120 core_timing.SyncPause(true);
121 core_timing.SyncPause(false);
122
123 expected_callback = 0;
124
125 u64 start = core_timing.GetGlobalTimeNs().count();
126 u64 one_micro = 1000U;
127 for (std::size_t i = 0; i < events.size(); i++) {
128 u64 order = calls_order[i];
129 core_timing.ScheduleEvent(i*one_micro + 100U, events[order], CB_IDS[order]);
130 }
131 u64 end = core_timing.GetGlobalTimeNs().count();
132 const double scheduling_time = static_cast<double>(end - start);
133 const double timer_time = static_cast<double>(TestTimerSpeed(core_timing));
134
135 while (core_timing.HasPendingEvents());
136
137 REQUIRE(callbacks_ran_flags.all());
138
139 for (std::size_t i = 0; i < delays.size(); i++) {
140 const double delay = static_cast<double>(delays[i]);
141 const double micro = delay / 1000.0f;
142 const double mili = micro / 1000.0f;
143 printf("HostTimer No Pausing Delay[%zu]: %.3f %.6f\n", i, micro, mili);
144 }
145
146 const double micro = scheduling_time / 1000.0f;
147 const double mili = micro / 1000.0f;
148 printf("HostTimer No Pausing Scheduling Time: %.3f %.6f\n", micro, mili);
149 printf("HostTimer No Pausing Timer Time: %.3f %.6f\n", timer_time / 1000.f, timer_time / 1000000.f);
150}