summaryrefslogtreecommitdiff
path: root/src/core/host_timing.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/host_timing.h')
-rw-r--r--src/core/host_timing.h160
1 files changed, 160 insertions, 0 deletions
diff --git a/src/core/host_timing.h b/src/core/host_timing.h
new file mode 100644
index 000000000..be6b68d7c
--- /dev/null
+++ b/src/core/host_timing.h
@@ -0,0 +1,160 @@
1// Copyright 2020 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <atomic>
8#include <chrono>
9#include <functional>
10#include <memory>
11#include <mutex>
12#include <optional>
13#include <string>
14#include <thread>
15#include <vector>
16
17#include "common/common_types.h"
18#include "common/spin_lock.h"
19#include "common/thread.h"
20#include "common/threadsafe_queue.h"
21#include "common/wall_clock.h"
22#include "core/hardware_properties.h"
23
24namespace Core::HostTiming {
25
26/// A callback that may be scheduled for a particular core timing event.
27using TimedCallback = std::function<void(u64 userdata, s64 cycles_late)>;
28
29/// Contains the characteristics of a particular event.
30struct EventType {
31 EventType(TimedCallback&& callback, std::string&& name)
32 : callback{std::move(callback)}, name{std::move(name)} {}
33
34 /// The event's callback function.
35 TimedCallback callback;
36 /// A pointer to the name of the event.
37 const std::string name;
38};
39
40/**
41 * This is a system to schedule events into the emulated machine's future. Time is measured
42 * in main CPU clock cycles.
43 *
44 * To schedule an event, you first have to register its type. This is where you pass in the
45 * callback. You then schedule events using the type id you get back.
46 *
47 * The int cyclesLate that the callbacks get is how many cycles late it was.
48 * So to schedule a new event on a regular basis:
49 * inside callback:
50 * ScheduleEvent(periodInCycles - cyclesLate, callback, "whatever")
51 */
52class CoreTiming {
53public:
54 CoreTiming();
55 ~CoreTiming();
56
57 CoreTiming(const CoreTiming&) = delete;
58 CoreTiming(CoreTiming&&) = delete;
59
60 CoreTiming& operator=(const CoreTiming&) = delete;
61 CoreTiming& operator=(CoreTiming&&) = delete;
62
63 /// CoreTiming begins at the boundary of timing slice -1. An initial call to Advance() is
64 /// required to end slice - 1 and start slice 0 before the first cycle of code is executed.
65 void Initialize();
66
67 /// Tears down all timing related functionality.
68 void Shutdown();
69
70 /// Pauses/Unpauses the execution of the timer thread.
71 void Pause(bool is_paused);
72
73 /// Pauses/Unpauses the execution of the timer thread and waits until paused.
74 void SyncPause(bool is_paused);
75
76 /// Checks if core timing is running.
77 bool IsRunning() const;
78
79 /// Checks if the timer thread has started.
80 bool HasStarted() const {
81 return has_started;
82 }
83
84 /// Checks if there are any pending time events.
85 bool HasPendingEvents() const;
86
87 /// Schedules an event in core timing
88 void ScheduleEvent(s64 ns_into_future, const std::shared_ptr<EventType>& event_type,
89 u64 userdata = 0);
90
91 void UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u64 userdata);
92
93 /// We only permit one event of each type in the queue at a time.
94 void RemoveEvent(const std::shared_ptr<EventType>& event_type);
95
96 void AddTicks(std::size_t core_index, u64 ticks);
97
98 void ResetTicks(std::size_t core_index);
99
100 /// Returns current time in emulated CPU cycles
101 u64 GetCPUTicks() const;
102
103 /// Returns current time in emulated in Clock cycles
104 u64 GetClockTicks() const;
105
106 /// Returns current time in microseconds.
107 std::chrono::microseconds GetGlobalTimeUs() const;
108
109 /// Returns current time in nanoseconds.
110 std::chrono::nanoseconds GetGlobalTimeNs() const;
111
112 /// Checks for events manually and returns time in nanoseconds for next event, threadsafe.
113 std::optional<u64> Advance();
114
115private:
116 struct Event;
117
118 /// Clear all pending events. This should ONLY be done on exit.
119 void ClearPendingEvents();
120
121 static void ThreadEntry(CoreTiming& instance);
122 void ThreadLoop();
123
124 std::unique_ptr<Common::WallClock> clock;
125
126 u64 global_timer = 0;
127
128 std::chrono::nanoseconds start_point;
129
130 // The queue is a min-heap using std::make_heap/push_heap/pop_heap.
131 // We don't use std::priority_queue because we need to be able to serialize, unserialize and
132 // erase arbitrary events (RemoveEvent()) regardless of the queue order. These aren't
133 // accomodated by the standard adaptor class.
134 std::vector<Event> event_queue;
135 u64 event_fifo_id = 0;
136
137 std::shared_ptr<EventType> ev_lost;
138 Common::Event event{};
139 Common::SpinLock basic_lock{};
140 Common::SpinLock advance_lock{};
141 std::unique_ptr<std::thread> timer_thread;
142 std::atomic<bool> paused{};
143 std::atomic<bool> paused_set{};
144 std::atomic<bool> wait_set{};
145 std::atomic<bool> shutting_down{};
146 std::atomic<bool> has_started{};
147
148 std::array<std::atomic<u64>, Core::Hardware::NUM_CPU_CORES> ticks_count{};
149};
150
151/// Creates a core timing event with the given name and callback.
152///
153/// @param name The name of the core timing event to create.
154/// @param callback The callback to execute for the event.
155///
156/// @returns An EventType instance representing the created event.
157///
158std::shared_ptr<EventType> CreateEvent(std::string name, TimedCallback&& callback);
159
160} // namespace Core::HostTiming