summaryrefslogtreecommitdiff
path: root/src/core/core_timing.cpp
diff options
context:
space:
mode:
authorGravatar Fernando Sahmkow2019-09-09 21:37:29 -0400
committerGravatar FernandoS272019-10-09 12:30:31 -0400
commit555866f8dcb98897688d5d7b0e6c6cca55ac069f (patch)
tree287ffee685bd8fbd6a8a16aa02a198a6cab3ede3 /src/core/core_timing.cpp
parentMerge pull request #2921 from FreddyFunk/compiler-warnings-core (diff)
downloadyuzu-555866f8dcb98897688d5d7b0e6c6cca55ac069f.tar.gz
yuzu-555866f8dcb98897688d5d7b0e6c6cca55ac069f.tar.xz
yuzu-555866f8dcb98897688d5d7b0e6c6cca55ac069f.zip
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.cpp66
1 files changed, 50 insertions, 16 deletions
diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp
index a58f7b131..6da2dcfb4 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,14 @@ CoreTiming::CoreTiming() = default;
38CoreTiming::~CoreTiming() = default; 38CoreTiming::~CoreTiming() = default;
39 39
40void CoreTiming::Initialize() { 40void CoreTiming::Initialize() {
41 downcount = MAX_SLICE_LENGTH; 41 for (std::size_t core = 0; core < num_cpu_cores; core++) {
42 downcounts[core] = MAX_SLICE_LENGTH;
43 time_slice[core] = MAX_SLICE_LENGTH;
44 }
42 slice_length = MAX_SLICE_LENGTH; 45 slice_length = MAX_SLICE_LENGTH;
43 global_timer = 0; 46 global_timer = 0;
44 idled_cycles = 0; 47 idled_cycles = 0;
48 current_context = 0;
45 49
46 // The time between CoreTiming being initialized and the first call to Advance() is considered 50 // 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 51 // the slice boundary between slice -1 and slice 0. Dispatcher loops must call Advance() before
@@ -110,7 +114,7 @@ void CoreTiming::UnscheduleEvent(const EventType* event_type, u64 userdata) {
110u64 CoreTiming::GetTicks() const { 114u64 CoreTiming::GetTicks() const {
111 u64 ticks = static_cast<u64>(global_timer); 115 u64 ticks = static_cast<u64>(global_timer);
112 if (!is_global_timer_sane) { 116 if (!is_global_timer_sane) {
113 ticks += slice_length - downcount; 117 ticks += time_slice[current_context] - downcounts[current_context];
114 } 118 }
115 return ticks; 119 return ticks;
116} 120}
@@ -120,7 +124,7 @@ u64 CoreTiming::GetIdleTicks() const {
120} 124}
121 125
122void CoreTiming::AddTicks(u64 ticks) { 126void CoreTiming::AddTicks(u64 ticks) {
123 downcount -= static_cast<int>(ticks); 127 downcounts[current_context] -= static_cast<s64>(ticks);
124} 128}
125 129
126void CoreTiming::ClearPendingEvents() { 130void CoreTiming::ClearPendingEvents() {
@@ -141,22 +145,36 @@ void CoreTiming::RemoveEvent(const EventType* event_type) {
141 145
142void CoreTiming::ForceExceptionCheck(s64 cycles) { 146void CoreTiming::ForceExceptionCheck(s64 cycles) {
143 cycles = std::max<s64>(0, cycles); 147 cycles = std::max<s64>(0, cycles);
144 if (downcount <= cycles) { 148 if (downcounts[current_context] <= cycles) {
145 return; 149 return;
146 } 150 }
147 151
148 // downcount is always (much) smaller than MAX_INT so we can safely cast cycles to an int 152 // 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 153 // here. Account for cycles already executed by adjusting the g.slice_length
150 slice_length -= downcount - static_cast<int>(cycles); 154 slice_length -= downcounts[current_context] - static_cast<int>(cycles);
151 downcount = static_cast<int>(cycles); 155 downcounts[current_context] = static_cast<int>(cycles);
156}
157
158std::optional<u64> CoreTiming::NextAvailableCore(const s64 needed_ticks) const {
159 const u64 original_context = current_context;
160 u64 next_context = (original_context + 1) % num_cpu_cores;
161 while (next_context != original_context) {
162 if (time_slice[next_context] >= needed_ticks) {
163 return {next_context};
164 } else if (time_slice[next_context] >= 0) {
165 return {};
166 }
167 next_context = (next_context + 1) % num_cpu_cores;
168 }
169 return {};
152} 170}
153 171
154void CoreTiming::Advance() { 172void CoreTiming::Advance() {
155 std::unique_lock<std::mutex> guard(inner_mutex); 173 std::unique_lock<std::mutex> guard(inner_mutex);
156 174
157 const int cycles_executed = slice_length - downcount; 175 const int cycles_executed = time_slice[current_context] - downcounts[current_context];
176 time_slice[current_context] = std::max<s64>(0, downcounts[current_context]);
158 global_timer += cycles_executed; 177 global_timer += cycles_executed;
159 slice_length = MAX_SLICE_LENGTH;
160 178
161 is_global_timer_sane = true; 179 is_global_timer_sane = true;
162 180
@@ -173,24 +191,40 @@ void CoreTiming::Advance() {
173 191
174 // Still events left (scheduled in the future) 192 // Still events left (scheduled in the future)
175 if (!event_queue.empty()) { 193 if (!event_queue.empty()) {
176 slice_length = static_cast<int>( 194 s64 needed_ticks = std::min<s64>(event_queue.front().time - global_timer, MAX_SLICE_LENGTH);
177 std::min<s64>(event_queue.front().time - global_timer, MAX_SLICE_LENGTH)); 195 const auto next_core = NextAvailableCore(needed_ticks);
196 if (next_core) {
197 downcounts[*next_core] = needed_ticks;
198 }
178 } 199 }
179 200
180 downcount = slice_length; 201 downcounts[current_context] = time_slice[current_context];
202}
203
204void CoreTiming::ResetRun() {
205 for (std::size_t core = 0; core < num_cpu_cores; core++) {
206 downcounts[core] = MAX_SLICE_LENGTH;
207 time_slice[core] = MAX_SLICE_LENGTH;
208 }
209 current_context = 0;
210 // Still events left (scheduled in the future)
211 if (!event_queue.empty()) {
212 s64 needed_ticks = std::min<s64>(event_queue.front().time - global_timer, MAX_SLICE_LENGTH);
213 downcounts[current_context] = needed_ticks;
214 }
181} 215}
182 216
183void CoreTiming::Idle() { 217void CoreTiming::Idle() {
184 idled_cycles += downcount; 218 idled_cycles += downcounts[current_context];
185 downcount = 0; 219 downcounts[current_context] = 0;
186} 220}
187 221
188std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const { 222std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const {
189 return std::chrono::microseconds{GetTicks() * 1000000 / BASE_CLOCK_RATE}; 223 return std::chrono::microseconds{GetTicks() * 1000000 / BASE_CLOCK_RATE};
190} 224}
191 225
192int CoreTiming::GetDowncount() const { 226s64 CoreTiming::GetDowncount() const {
193 return downcount; 227 return downcounts[current_context];
194} 228}
195 229
196} // namespace Core::Timing 230} // namespace Core::Timing