1 // Copyright 2008 Dolphin Emulator Project / 2017 Citra Emulator Project
2 // Licensed under GPLv2+
3 // Refer to the license.txt file included.
4 
5 #pragma once
6 
7 /**
8  * This is a system to schedule events into the emulated machine's future. Time is measured
9  * in main CPU clock cycles.
10  *
11  * To schedule an event, you first have to register its type. This is where you pass in the
12  * callback. You then schedule events using the type id you get back.
13  *
14  * The int cyclesLate that the callbacks get is how many cycles late it was.
15  * So to schedule a new event on a regular basis:
16  * inside callback:
17  *   ScheduleEvent(periodInCycles - cyclesLate, callback, "whatever")
18  */
19 
20 #include <chrono>
21 #include <functional>
22 #include <limits>
23 #include <string>
24 #include <unordered_map>
25 #include <vector>
26 #include <boost/serialization/split_member.hpp>
27 #include <boost/serialization/vector.hpp>
28 #include "common/common_types.h"
29 #include "common/logging/log.h"
30 #include "common/threadsafe_queue.h"
31 #include "core/global.h"
32 
33 // The timing we get from the assembly is 268,111,855.956 Hz
34 // It is possible that this number isn't just an integer because the compiler could have
35 // optimized the multiplication by a multiply-by-constant division.
36 // Rounding to the nearest integer should be fine
37 constexpr u64 BASE_CLOCK_RATE_ARM11 = 268111856;
38 constexpr u64 MAX_VALUE_TO_MULTIPLY = std::numeric_limits<s64>::max() / BASE_CLOCK_RATE_ARM11;
39 
msToCycles(int ms)40 constexpr s64 msToCycles(int ms) {
41     // since ms is int there is no way to overflow
42     return BASE_CLOCK_RATE_ARM11 * static_cast<s64>(ms) / 1000;
43 }
44 
msToCycles(float ms)45 constexpr s64 msToCycles(float ms) {
46     return static_cast<s64>(BASE_CLOCK_RATE_ARM11 * (0.001f) * ms);
47 }
48 
msToCycles(double ms)49 constexpr s64 msToCycles(double ms) {
50     return static_cast<s64>(BASE_CLOCK_RATE_ARM11 * (0.001) * ms);
51 }
52 
usToCycles(float us)53 constexpr s64 usToCycles(float us) {
54     return static_cast<s64>(BASE_CLOCK_RATE_ARM11 * (0.000001f) * us);
55 }
56 
usToCycles(int us)57 constexpr s64 usToCycles(int us) {
58     return (BASE_CLOCK_RATE_ARM11 * static_cast<s64>(us) / 1000000);
59 }
60 
usToCycles(s64 us)61 inline s64 usToCycles(s64 us) {
62     if (us / 1000000 > static_cast<s64>(MAX_VALUE_TO_MULTIPLY)) {
63         LOG_ERROR(Core_Timing, "Integer overflow, use max value");
64         return std::numeric_limits<s64>::max();
65     }
66     if (us > static_cast<s64>(MAX_VALUE_TO_MULTIPLY)) {
67         LOG_DEBUG(Core_Timing, "Time very big, do rounding");
68         return BASE_CLOCK_RATE_ARM11 * (us / 1000000);
69     }
70     return (BASE_CLOCK_RATE_ARM11 * us) / 1000000;
71 }
72 
usToCycles(u64 us)73 inline s64 usToCycles(u64 us) {
74     if (us / 1000000 > MAX_VALUE_TO_MULTIPLY) {
75         LOG_ERROR(Core_Timing, "Integer overflow, use max value");
76         return std::numeric_limits<s64>::max();
77     }
78     if (us > MAX_VALUE_TO_MULTIPLY) {
79         LOG_DEBUG(Core_Timing, "Time very big, do rounding");
80         return BASE_CLOCK_RATE_ARM11 * static_cast<s64>(us / 1000000);
81     }
82     return (BASE_CLOCK_RATE_ARM11 * static_cast<s64>(us)) / 1000000;
83 }
84 
nsToCycles(float ns)85 constexpr s64 nsToCycles(float ns) {
86     return static_cast<s64>(BASE_CLOCK_RATE_ARM11 * (0.000000001f) * ns);
87 }
88 
nsToCycles(int ns)89 constexpr s64 nsToCycles(int ns) {
90     return BASE_CLOCK_RATE_ARM11 * static_cast<s64>(ns) / 1000000000;
91 }
92 
nsToCycles(s64 ns)93 inline s64 nsToCycles(s64 ns) {
94     if (ns / 1000000000 > static_cast<s64>(MAX_VALUE_TO_MULTIPLY)) {
95         LOG_ERROR(Core_Timing, "Integer overflow, use max value");
96         return std::numeric_limits<s64>::max();
97     }
98     if (ns > static_cast<s64>(MAX_VALUE_TO_MULTIPLY)) {
99         LOG_DEBUG(Core_Timing, "Time very big, do rounding");
100         return BASE_CLOCK_RATE_ARM11 * (ns / 1000000000);
101     }
102     return (BASE_CLOCK_RATE_ARM11 * ns) / 1000000000;
103 }
104 
nsToCycles(u64 ns)105 inline s64 nsToCycles(u64 ns) {
106     if (ns / 1000000000 > MAX_VALUE_TO_MULTIPLY) {
107         LOG_ERROR(Core_Timing, "Integer overflow, use max value");
108         return std::numeric_limits<s64>::max();
109     }
110     if (ns > MAX_VALUE_TO_MULTIPLY) {
111         LOG_DEBUG(Core_Timing, "Time very big, do rounding");
112         return BASE_CLOCK_RATE_ARM11 * (static_cast<s64>(ns) / 1000000000);
113     }
114     return (BASE_CLOCK_RATE_ARM11 * static_cast<s64>(ns)) / 1000000000;
115 }
116 
cyclesToNs(s64 cycles)117 constexpr u64 cyclesToNs(s64 cycles) {
118     return cycles * 1000000000 / BASE_CLOCK_RATE_ARM11;
119 }
120 
cyclesToUs(s64 cycles)121 constexpr s64 cyclesToUs(s64 cycles) {
122     return cycles * 1000000 / BASE_CLOCK_RATE_ARM11;
123 }
124 
cyclesToMs(s64 cycles)125 constexpr u64 cyclesToMs(s64 cycles) {
126     return cycles * 1000 / BASE_CLOCK_RATE_ARM11;
127 }
128 
129 namespace Core {
130 
131 using TimedCallback = std::function<void(u64 userdata, int cycles_late)>;
132 
133 struct TimingEventType {
134     TimedCallback callback;
135     const std::string* name;
136 };
137 
138 class Timing {
139 
140 public:
141     struct Event {
142         s64 time;
143         u64 fifo_order;
144         u64 userdata;
145         const TimingEventType* type;
146 
147         bool operator>(const Event& right) const;
148         bool operator<(const Event& right) const;
149 
150     private:
151         template <class Archive>
saveEvent152         void save(Archive& ar, const unsigned int) const {
153             ar& time;
154             ar& fifo_order;
155             ar& userdata;
156             std::string name = *(type->name);
157             ar << name;
158         }
159 
160         template <class Archive>
loadEvent161         void load(Archive& ar, const unsigned int) {
162             ar& time;
163             ar& fifo_order;
164             ar& userdata;
165             std::string name;
166             ar >> name;
167             type = Global<Timing>().RegisterEvent(name, nullptr);
168         }
169         friend class boost::serialization::access;
170 
171         BOOST_SERIALIZATION_SPLIT_MEMBER()
172     };
173 
174     // currently Service::HID::pad_update_ticks is the smallest interval for an event that gets
175     // always scheduled. Therfore we use this as orientation for the MAX_SLICE_LENGTH
176     // For performance bigger slice length are desired, though this will lead to cores desync
177     // But we never want to schedule events into the current slice, because then cores might to
178     // run small slices to sync up again. This is especially important for events that are always
179     // scheduled and repated.
180     static constexpr int MAX_SLICE_LENGTH = BASE_CLOCK_RATE_ARM11 / 234;
181 
182     class Timer {
183     public:
184         Timer();
185         ~Timer();
186 
187         s64 GetMaxSliceLength() const;
188 
189         void Advance();
190 
191         void SetNextSlice(s64 max_slice_length = MAX_SLICE_LENGTH);
192 
193         void Idle();
194 
195         u64 GetTicks() const;
196         u64 GetIdleTicks() const;
197 
198         void AddTicks(u64 ticks);
199 
200         s64 GetDowncount() const;
201 
202         void ForceExceptionCheck(s64 cycles);
203 
204         void MoveEvents();
205 
206     private:
207         friend class Timing;
208         // The queue is a min-heap using std::make_heap/push_heap/pop_heap.
209         // We don't use std::priority_queue because we need to be able to serialize, unserialize and
210         // erase arbitrary events (RemoveEvent()) regardless of the queue order. These aren't
211         // accommodated by the standard adaptor class.
212         std::vector<Event> event_queue;
213         u64 event_fifo_id = 0;
214         // the queue for storing the events from other threads threadsafe until they will be added
215         // to the event_queue by the emu thread
216         Common::MPSCQueue<Event> ts_queue;
217         // Are we in a function that has been called from Advance()
218         // If events are sheduled from a function that gets called from Advance(),
219         // don't change slice_length and downcount.
220         // The time between CoreTiming being intialized and the first call to Advance() is
221         // considered the slice boundary between slice -1 and slice 0. Dispatcher loops must call
222         // Advance() before executing the first cycle of each slice to prepare the slice length and
223         // downcount for that slice.
224         bool is_timer_sane = true;
225 
226         s64 slice_length = MAX_SLICE_LENGTH;
227         s64 downcount = MAX_SLICE_LENGTH;
228         s64 executed_ticks = 0;
229         u64 idled_cycles = 0;
230         // Stores a scaling for the internal clockspeed. Changing this number results in
231         // under/overclocking the guest cpu
232         double cpu_clock_scale = 1.0;
233 
234         template <class Archive>
serialize(Archive & ar,const unsigned int)235         void serialize(Archive& ar, const unsigned int) {
236             MoveEvents();
237             // NOTE: ts_queue should be empty now
238             // TODO(SaveState): Remove the next two lines when we break compatibility
239             s64 x;
240             ar& x; // to keep compatibility with old save states that stored global_timer
241             ar& event_queue;
242             ar& event_fifo_id;
243             ar& slice_length;
244             ar& downcount;
245             ar& executed_ticks;
246             ar& idled_cycles;
247         }
248         friend class boost::serialization::access;
249     };
250 
251     explicit Timing(std::size_t num_cores, u32 cpu_clock_percentage);
252 
~Timing()253     ~Timing(){};
254 
255     /**
256      * Returns the event_type identifier. if name is not unique, it will assert.
257      */
258     TimingEventType* RegisterEvent(const std::string& name, TimedCallback callback);
259 
260     void ScheduleEvent(s64 cycles_into_future, const TimingEventType* event_type, u64 userdata = 0,
261                        std::size_t core_id = std::numeric_limits<std::size_t>::max());
262 
263     void UnscheduleEvent(const TimingEventType* event_type, u64 userdata);
264 
265     /// We only permit one event of each type in the queue at a time.
266     void RemoveEvent(const TimingEventType* event_type);
267 
268     void SetCurrentTimer(std::size_t core_id);
269 
270     s64 GetTicks() const;
271 
272     s64 GetGlobalTicks() const;
273 
274     /**
275      * Updates the value of the cpu clock scaling to the new percentage.
276      */
277     void UpdateClockSpeed(u32 cpu_clock_percentage);
278 
279     std::chrono::microseconds GetGlobalTimeUs() const;
280 
281     std::shared_ptr<Timer> GetTimer(std::size_t cpu_id);
282 
283     // Used after deserializing to unprotect the event queue.
UnlockEventQueue()284     void UnlockEventQueue() {
285         event_queue_locked = false;
286     }
287 
288 private:
289     // unordered_map stores each element separately as a linked list node so pointers to
290     // elements remain stable regardless of rehashes/resizing.
291     std::unordered_map<std::string, TimingEventType> event_types = {};
292 
293     std::vector<std::shared_ptr<Timer>> timers;
294     Timer* current_timer = nullptr;
295 
296     // Stores a scaling for the internal clockspeed. Changing this number results in
297     // under/overclocking the guest cpu
298     double cpu_clock_scale = 1.0;
299 
300     // When true, the event queue can't be modified. Used while deserializing to workaround
301     // destructor side effects.
302     bool event_queue_locked = false;
303 
304     template <class Archive>
serialize(Archive & ar,const unsigned int file_version)305     void serialize(Archive& ar, const unsigned int file_version) {
306         // event_types set during initialization of other things
307         ar& timers;
308         if (file_version == 0) {
309             std::shared_ptr<Timer> x;
310             ar& x;
311             current_timer = x.get();
312         } else {
313             ar& current_timer;
314         }
315         if (Archive::is_loading::value) {
316             event_queue_locked = true;
317         }
318     }
319     friend class boost::serialization::access;
320 };
321 
322 } // namespace Core
323 
324 BOOST_CLASS_VERSION(Core::Timing, 1)
325