summaryrefslogtreecommitdiff
path: root/src/core/core_timing.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2019-10-15 11:48:30 -0400
committerGravatar GitHub2019-10-15 11:48:30 -0400
commitcab2619aeb111bd6c5dbcc5adc0d2e8154a1e8fc (patch)
tree1664df6e9abff74f37adee0c90ae3c9eaff6babf /src/core/core_timing.cpp
parentMerge pull request #2897 from DarkLordZach/oss-ext-fonts-1 (diff)
parentCore_Timing: Address Remaining feedback. (diff)
downloadyuzu-cab2619aeb111bd6c5dbcc5adc0d2e8154a1e8fc.tar.gz
yuzu-cab2619aeb111bd6c5dbcc5adc0d2e8154a1e8fc.tar.xz
yuzu-cab2619aeb111bd6c5dbcc5adc0d2e8154a1e8fc.zip
Merge pull request #2965 from FernandoS27/fair-core-timing
Core Timing: Rework Core Timing to run all cores evenly.
Diffstat (limited to 'src/core/core_timing.cpp')
-rw-r--r--src/core/core_timing.cpp70
1 files changed, 54 insertions, 16 deletions
diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp
index a58f7b131..0e9570685 100644
--- a/src/core/core_timing.cpp
+++ b/src/core/core_timing.cpp
@@ -15,7 +15,7 @@
15 15
16namespace Core::Timing { 16namespace Core::Timing {
17 17
18constexpr int MAX_SLICE_LENGTH = 20000; 18constexpr int MAX_SLICE_LENGTH = 10000;
19 19
20struct CoreTiming::Event { 20struct CoreTiming::Event {
21 s64 time; 21 s64 time;
@@ -38,10 +38,12 @@ CoreTiming::CoreTiming() = default;
38CoreTiming::~CoreTiming() = default; 38CoreTiming::~CoreTiming() = default;
39 39
40void CoreTiming::Initialize() { 40void CoreTiming::Initialize() {
41 downcount = MAX_SLICE_LENGTH; 41 downcounts.fill(MAX_SLICE_LENGTH);
42 time_slice.fill(MAX_SLICE_LENGTH);
42 slice_length = MAX_SLICE_LENGTH; 43 slice_length = MAX_SLICE_LENGTH;
43 global_timer = 0; 44 global_timer = 0;
44 idled_cycles = 0; 45 idled_cycles = 0;
46 current_context = 0;
45 47
46 // The time between CoreTiming being initialized and the first call to Advance() is considered 48 // The time between CoreTiming being initialized and the first call to Advance() is considered
47 // the slice boundary between slice -1 and slice 0. Dispatcher loops must call Advance() before 49 // the slice boundary between slice -1 and slice 0. Dispatcher loops must call Advance() before
@@ -110,7 +112,7 @@ void CoreTiming::UnscheduleEvent(const EventType* event_type, u64 userdata) {
110u64 CoreTiming::GetTicks() const { 112u64 CoreTiming::GetTicks() const {
111 u64 ticks = static_cast<u64>(global_timer); 113 u64 ticks = static_cast<u64>(global_timer);
112 if (!is_global_timer_sane) { 114 if (!is_global_timer_sane) {
113 ticks += slice_length - downcount; 115 ticks += accumulated_ticks;
114 } 116 }
115 return ticks; 117 return ticks;
116} 118}
@@ -120,7 +122,8 @@ u64 CoreTiming::GetIdleTicks() const {
120} 122}
121 123
122void CoreTiming::AddTicks(u64 ticks) { 124void CoreTiming::AddTicks(u64 ticks) {
123 downcount -= static_cast<int>(ticks); 125 accumulated_ticks += ticks;
126 downcounts[current_context] -= static_cast<s64>(ticks);
124} 127}
125 128
126void CoreTiming::ClearPendingEvents() { 129void CoreTiming::ClearPendingEvents() {
@@ -141,22 +144,35 @@ void CoreTiming::RemoveEvent(const EventType* event_type) {
141 144
142void CoreTiming::ForceExceptionCheck(s64 cycles) { 145void CoreTiming::ForceExceptionCheck(s64 cycles) {
143 cycles = std::max<s64>(0, cycles); 146 cycles = std::max<s64>(0, cycles);
144 if (downcount <= cycles) { 147 if (downcounts[current_context] <= cycles) {
145 return; 148 return;
146 } 149 }
147 150
148 // downcount is always (much) smaller than MAX_INT so we can safely cast cycles to an int 151 // downcount is always (much) smaller than MAX_INT so we can safely cast cycles to an int
149 // here. Account for cycles already executed by adjusting the g.slice_length 152 // here. Account for cycles already executed by adjusting the g.slice_length
150 slice_length -= downcount - static_cast<int>(cycles); 153 downcounts[current_context] = static_cast<int>(cycles);
151 downcount = static_cast<int>(cycles); 154}
155
156std::optional<u64> CoreTiming::NextAvailableCore(const s64 needed_ticks) const {
157 const u64 original_context = current_context;
158 u64 next_context = (original_context + 1) % num_cpu_cores;
159 while (next_context != original_context) {
160 if (time_slice[next_context] >= needed_ticks) {
161 return {next_context};
162 } else if (time_slice[next_context] >= 0) {
163 return std::nullopt;
164 }
165 next_context = (next_context + 1) % num_cpu_cores;
166 }
167 return std::nullopt;
152} 168}
153 169
154void CoreTiming::Advance() { 170void CoreTiming::Advance() {
155 std::unique_lock<std::mutex> guard(inner_mutex); 171 std::unique_lock<std::mutex> guard(inner_mutex);
156 172
157 const int cycles_executed = slice_length - downcount; 173 const u64 cycles_executed = accumulated_ticks;
174 time_slice[current_context] = std::max<s64>(0, time_slice[current_context] - accumulated_ticks);
158 global_timer += cycles_executed; 175 global_timer += cycles_executed;
159 slice_length = MAX_SLICE_LENGTH;
160 176
161 is_global_timer_sane = true; 177 is_global_timer_sane = true;
162 178
@@ -173,24 +189,46 @@ void CoreTiming::Advance() {
173 189
174 // Still events left (scheduled in the future) 190 // Still events left (scheduled in the future)
175 if (!event_queue.empty()) { 191 if (!event_queue.empty()) {
176 slice_length = static_cast<int>( 192 const s64 needed_ticks =
177 std::min<s64>(event_queue.front().time - global_timer, MAX_SLICE_LENGTH)); 193 std::min<s64>(event_queue.front().time - global_timer, MAX_SLICE_LENGTH);
194 const auto next_core = NextAvailableCore(needed_ticks);
195 if (next_core) {
196 downcounts[*next_core] = needed_ticks;
197 }
198 }
199
200 accumulated_ticks = 0;
201
202 downcounts[current_context] = time_slice[current_context];
203}
204
205void CoreTiming::ResetRun() {
206 downcounts.fill(MAX_SLICE_LENGTH);
207 time_slice.fill(MAX_SLICE_LENGTH);
208 current_context = 0;
209 // Still events left (scheduled in the future)
210 if (!event_queue.empty()) {
211 const s64 needed_ticks =
212 std::min<s64>(event_queue.front().time - global_timer, MAX_SLICE_LENGTH);
213 downcounts[current_context] = needed_ticks;
178 } 214 }
179 215
180 downcount = slice_length; 216 is_global_timer_sane = false;
217 accumulated_ticks = 0;
181} 218}
182 219
183void CoreTiming::Idle() { 220void CoreTiming::Idle() {
184 idled_cycles += downcount; 221 accumulated_ticks += downcounts[current_context];
185 downcount = 0; 222 idled_cycles += downcounts[current_context];
223 downcounts[current_context] = 0;
186} 224}
187 225
188std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const { 226std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const {
189 return std::chrono::microseconds{GetTicks() * 1000000 / BASE_CLOCK_RATE}; 227 return std::chrono::microseconds{GetTicks() * 1000000 / BASE_CLOCK_RATE};
190} 228}
191 229
192int CoreTiming::GetDowncount() const { 230s64 CoreTiming::GetDowncount() const {
193 return downcount; 231 return downcounts[current_context];
194} 232}
195 233
196} // namespace Core::Timing 234} // namespace Core::Timing