summaryrefslogtreecommitdiff
path: root/src/core
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
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')
-rw-r--r--src/core/arm/dynarmic/arm_dynarmic.cpp2
-rw-r--r--src/core/arm/unicorn/arm_unicorn.cpp2
-rw-r--r--src/core/core_cpu.cpp14
-rw-r--r--src/core/core_timing.cpp70
-rw-r--r--src/core/core_timing.h25
-rw-r--r--src/core/cpu_core_manager.cpp19
6 files changed, 94 insertions, 38 deletions
diff --git a/src/core/arm/dynarmic/arm_dynarmic.cpp b/src/core/arm/dynarmic/arm_dynarmic.cpp
index f1506b372..700c4afff 100644
--- a/src/core/arm/dynarmic/arm_dynarmic.cpp
+++ b/src/core/arm/dynarmic/arm_dynarmic.cpp
@@ -116,7 +116,7 @@ public:
116 num_interpreted_instructions = 0; 116 num_interpreted_instructions = 0;
117 } 117 }
118 u64 GetTicksRemaining() override { 118 u64 GetTicksRemaining() override {
119 return std::max(parent.system.CoreTiming().GetDowncount(), 0); 119 return std::max(parent.system.CoreTiming().GetDowncount(), s64{0});
120 } 120 }
121 u64 GetCNTPCT() override { 121 u64 GetCNTPCT() override {
122 return Timing::CpuCyclesToClockCycles(parent.system.CoreTiming().GetTicks()); 122 return Timing::CpuCyclesToClockCycles(parent.system.CoreTiming().GetTicks());
diff --git a/src/core/arm/unicorn/arm_unicorn.cpp b/src/core/arm/unicorn/arm_unicorn.cpp
index 97d5c2a8a..d4f41bfc1 100644
--- a/src/core/arm/unicorn/arm_unicorn.cpp
+++ b/src/core/arm/unicorn/arm_unicorn.cpp
@@ -156,7 +156,7 @@ void ARM_Unicorn::Run() {
156 if (GDBStub::IsServerEnabled()) { 156 if (GDBStub::IsServerEnabled()) {
157 ExecuteInstructions(std::max(4000000, 0)); 157 ExecuteInstructions(std::max(4000000, 0));
158 } else { 158 } else {
159 ExecuteInstructions(std::max(system.CoreTiming().GetDowncount(), 0)); 159 ExecuteInstructions(std::max(system.CoreTiming().GetDowncount(), s64{0}));
160 } 160 }
161} 161}
162 162
diff --git a/src/core/core_cpu.cpp b/src/core/core_cpu.cpp
index 21c410e34..6bd9639c6 100644
--- a/src/core/core_cpu.cpp
+++ b/src/core/core_cpu.cpp
@@ -85,24 +85,16 @@ void Cpu::RunLoop(bool tight_loop) {
85 // instead advance to the next event and try to yield to the next thread 85 // instead advance to the next event and try to yield to the next thread
86 if (Kernel::GetCurrentThread() == nullptr) { 86 if (Kernel::GetCurrentThread() == nullptr) {
87 LOG_TRACE(Core, "Core-{} idling", core_index); 87 LOG_TRACE(Core, "Core-{} idling", core_index);
88 88 core_timing.Idle();
89 if (IsMainCore()) { 89 core_timing.Advance();
90 // TODO(Subv): Only let CoreTiming idle if all 4 cores are idling.
91 core_timing.Idle();
92 core_timing.Advance();
93 }
94
95 PrepareReschedule(); 90 PrepareReschedule();
96 } else { 91 } else {
97 if (IsMainCore()) {
98 core_timing.Advance();
99 }
100
101 if (tight_loop) { 92 if (tight_loop) {
102 arm_interface->Run(); 93 arm_interface->Run();
103 } else { 94 } else {
104 arm_interface->Step(); 95 arm_interface->Step();
105 } 96 }
97 core_timing.Advance();
106 } 98 }
107 99
108 Reschedule(); 100 Reschedule();
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
diff --git a/src/core/core_timing.h b/src/core/core_timing.h
index 161c7007d..3bb88c810 100644
--- a/src/core/core_timing.h
+++ b/src/core/core_timing.h
@@ -7,6 +7,7 @@
7#include <chrono> 7#include <chrono>
8#include <functional> 8#include <functional>
9#include <mutex> 9#include <mutex>
10#include <optional>
10#include <string> 11#include <string>
11#include <unordered_map> 12#include <unordered_map>
12#include <vector> 13#include <vector>
@@ -104,7 +105,19 @@ public:
104 105
105 std::chrono::microseconds GetGlobalTimeUs() const; 106 std::chrono::microseconds GetGlobalTimeUs() const;
106 107
107 int GetDowncount() const; 108 void ResetRun();
109
110 s64 GetDowncount() const;
111
112 void SwitchContext(u64 new_context) {
113 current_context = new_context;
114 }
115
116 bool CanCurrentContextRun() const {
117 return time_slice[current_context] > 0;
118 }
119
120 std::optional<u64> NextAvailableCore(const s64 needed_ticks) const;
108 121
109private: 122private:
110 struct Event; 123 struct Event;
@@ -112,10 +125,16 @@ private:
112 /// Clear all pending events. This should ONLY be done on exit. 125 /// Clear all pending events. This should ONLY be done on exit.
113 void ClearPendingEvents(); 126 void ClearPendingEvents();
114 127
128 static constexpr u64 num_cpu_cores = 4;
129
115 s64 global_timer = 0; 130 s64 global_timer = 0;
116 s64 idled_cycles = 0; 131 s64 idled_cycles = 0;
117 int slice_length = 0; 132 s64 slice_length = 0;
118 int downcount = 0; 133 u64 accumulated_ticks = 0;
134 std::array<s64, num_cpu_cores> downcounts{};
135 // Slice of time assigned to each core per run.
136 std::array<s64, num_cpu_cores> time_slice{};
137 u64 current_context = 0;
119 138
120 // Are we in a function that has been called from Advance() 139 // Are we in a function that has been called from Advance()
121 // If events are scheduled from a function that gets called from Advance(), 140 // If events are scheduled from a function that gets called from Advance(),
diff --git a/src/core/cpu_core_manager.cpp b/src/core/cpu_core_manager.cpp
index 8fcb4eeb1..16b384076 100644
--- a/src/core/cpu_core_manager.cpp
+++ b/src/core/cpu_core_manager.cpp
@@ -6,6 +6,7 @@
6#include "core/arm/exclusive_monitor.h" 6#include "core/arm/exclusive_monitor.h"
7#include "core/core.h" 7#include "core/core.h"
8#include "core/core_cpu.h" 8#include "core/core_cpu.h"
9#include "core/core_timing.h"
9#include "core/cpu_core_manager.h" 10#include "core/cpu_core_manager.h"
10#include "core/gdbstub/gdbstub.h" 11#include "core/gdbstub/gdbstub.h"
11#include "core/settings.h" 12#include "core/settings.h"
@@ -122,13 +123,19 @@ void CpuCoreManager::RunLoop(bool tight_loop) {
122 } 123 }
123 } 124 }
124 125
125 for (active_core = 0; active_core < NUM_CPU_CORES; ++active_core) { 126 auto& core_timing = system.CoreTiming();
126 cores[active_core]->RunLoop(tight_loop); 127 core_timing.ResetRun();
127 if (Settings::values.use_multi_core) { 128 bool keep_running{};
128 // Cores 1-3 are run on other threads in this mode 129 do {
129 break; 130 keep_running = false;
131 for (active_core = 0; active_core < NUM_CPU_CORES; ++active_core) {
132 core_timing.SwitchContext(active_core);
133 if (core_timing.CanCurrentContextRun()) {
134 cores[active_core]->RunLoop(tight_loop);
135 }
136 keep_running |= core_timing.CanCurrentContextRun();
130 } 137 }
131 } 138 } while (keep_running);
132 139
133 if (GDBStub::IsServerEnabled()) { 140 if (GDBStub::IsServerEnabled()) {
134 GDBStub::SetCpuStepFlag(false); 141 GDBStub::SetCpuStepFlag(false);