2017-11-25 13:56:57 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project / 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
2014-04-08 23:11:21 +00:00
|
|
|
// Refer to the license.txt file included.
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
#include "core/core_timing.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2015-09-16 12:38:12 +00:00
|
|
|
#include <cinttypes>
|
2018-05-03 11:53:05 +00:00
|
|
|
#include <limits>
|
2014-09-03 05:05:45 +00:00
|
|
|
#include <mutex>
|
2017-11-25 13:56:57 +00:00
|
|
|
#include <string>
|
|
|
|
#include <tuple>
|
|
|
|
#include <unordered_map>
|
2015-01-06 01:17:49 +00:00
|
|
|
#include <vector>
|
2017-11-25 13:56:57 +00:00
|
|
|
#include "common/assert.h"
|
2015-06-21 14:11:32 +00:00
|
|
|
#include "common/logging/log.h"
|
2017-11-25 13:56:57 +00:00
|
|
|
#include "common/thread.h"
|
|
|
|
#include "common/threadsafe_queue.h"
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
namespace CoreTiming {
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
static s64 global_timer;
|
|
|
|
static int slice_length;
|
|
|
|
static int downcount;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
struct EventType {
|
2014-04-08 23:11:21 +00:00
|
|
|
TimedCallback callback;
|
2017-11-25 13:56:57 +00:00
|
|
|
const std::string* name;
|
2013-10-01 23:10:47 +00:00
|
|
|
};
|
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
struct Event {
|
2014-04-08 23:11:21 +00:00
|
|
|
s64 time;
|
2017-11-25 13:56:57 +00:00
|
|
|
u64 fifo_order;
|
2014-04-08 23:11:21 +00:00
|
|
|
u64 userdata;
|
2017-11-25 13:56:57 +00:00
|
|
|
const EventType* type;
|
2013-10-01 23:10:47 +00:00
|
|
|
};
|
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
// Sort by time, unless the times are the same, in which case sort by the order added to the queue
|
|
|
|
static bool operator>(const Event& left, const Event& right) {
|
|
|
|
return std::tie(left.time, left.fifo_order) > std::tie(right.time, right.fifo_order);
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
static bool operator<(const Event& left, const Event& right) {
|
|
|
|
return std::tie(left.time, left.fifo_order) < std::tie(right.time, right.fifo_order);
|
2015-01-06 01:17:49 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
// unordered_map stores each element separately as a linked list node so pointers to elements
|
|
|
|
// remain stable regardless of rehashes/resizing.
|
|
|
|
static std::unordered_map<std::string, EventType> event_types;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
// The queue is a min-heap using std::make_heap/push_heap/pop_heap.
|
|
|
|
// We don't use std::priority_queue because we need to be able to serialize, unserialize and
|
|
|
|
// erase arbitrary events (RemoveEvent()) regardless of the queue order. These aren't accomodated
|
|
|
|
// by the standard adaptor class.
|
|
|
|
static std::vector<Event> event_queue;
|
|
|
|
static u64 event_fifo_id;
|
|
|
|
// the queue for storing the events from other threads threadsafe until they will be added
|
|
|
|
// to the event_queue by the emu thread
|
|
|
|
static Common::MPSCQueue<Event, false> ts_queue;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2018-05-03 11:53:05 +00:00
|
|
|
constexpr int MAX_SLICE_LENGTH = 20000;
|
|
|
|
constexpr u64 MAX_VALUE_TO_MULTIPLY = std::numeric_limits<s64>::max() / BASE_CLOCK_RATE;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
static s64 idled_cycles;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
// Are we in a function that has been called from Advance()
|
|
|
|
// If events are sheduled from a function that gets called from Advance(),
|
|
|
|
// don't change slice_length and downcount.
|
|
|
|
static bool is_global_timer_sane;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
static EventType* ev_lost = nullptr;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
static void EmptyTimedCallback(u64 userdata, s64 cyclesLate) {}
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2018-05-03 11:53:05 +00:00
|
|
|
s64 usToCycles(s64 us) {
|
|
|
|
if (us / 1000000 > MAX_VALUE_TO_MULTIPLY) {
|
|
|
|
NGLOG_ERROR(Core_Timing, "Integer overflow, use max value");
|
|
|
|
return std::numeric_limits<s64>::max();
|
|
|
|
}
|
|
|
|
if (us > MAX_VALUE_TO_MULTIPLY) {
|
|
|
|
NGLOG_DEBUG(Core_Timing, "Time very big, do rounding");
|
|
|
|
return BASE_CLOCK_RATE * (us / 1000000);
|
|
|
|
}
|
|
|
|
return (BASE_CLOCK_RATE * us) / 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
s64 usToCycles(u64 us) {
|
|
|
|
if (us / 1000000 > MAX_VALUE_TO_MULTIPLY) {
|
|
|
|
NGLOG_ERROR(Core_Timing, "Integer overflow, use max value");
|
|
|
|
return std::numeric_limits<s64>::max();
|
|
|
|
}
|
|
|
|
if (us > MAX_VALUE_TO_MULTIPLY) {
|
|
|
|
NGLOG_DEBUG(Core_Timing, "Time very big, do rounding");
|
|
|
|
return BASE_CLOCK_RATE * static_cast<s64>(us / 1000000);
|
|
|
|
}
|
|
|
|
return (BASE_CLOCK_RATE * static_cast<s64>(us)) / 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
s64 nsToCycles(s64 ns) {
|
|
|
|
if (ns / 1000000000 > MAX_VALUE_TO_MULTIPLY) {
|
|
|
|
NGLOG_ERROR(Core_Timing, "Integer overflow, use max value");
|
|
|
|
return std::numeric_limits<s64>::max();
|
|
|
|
}
|
|
|
|
if (ns > MAX_VALUE_TO_MULTIPLY) {
|
|
|
|
NGLOG_DEBUG(Core_Timing, "Time very big, do rounding");
|
|
|
|
return BASE_CLOCK_RATE * (ns / 1000000000);
|
|
|
|
}
|
|
|
|
return (BASE_CLOCK_RATE * ns) / 1000000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
s64 nsToCycles(u64 ns) {
|
|
|
|
if (ns / 1000000000 > MAX_VALUE_TO_MULTIPLY) {
|
|
|
|
NGLOG_ERROR(Core_Timing, "Integer overflow, use max value");
|
|
|
|
return std::numeric_limits<s64>::max();
|
|
|
|
}
|
|
|
|
if (ns > MAX_VALUE_TO_MULTIPLY) {
|
|
|
|
NGLOG_DEBUG(Core_Timing, "Time very big, do rounding");
|
|
|
|
return BASE_CLOCK_RATE * (static_cast<s64>(ns) / 1000000000);
|
|
|
|
}
|
|
|
|
return (BASE_CLOCK_RATE * static_cast<s64>(ns)) / 1000000000;
|
|
|
|
}
|
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
EventType* RegisterEvent(const std::string& name, TimedCallback callback) {
|
|
|
|
// check for existing type with same name.
|
|
|
|
// we want event type names to remain unique so that we can use them for serialization.
|
|
|
|
ASSERT_MSG(event_types.find(name) == event_types.end(),
|
2018-04-27 11:54:05 +00:00
|
|
|
"CoreTiming Event \"{}\" is already registered. Events should only be registered "
|
2017-11-25 13:56:57 +00:00
|
|
|
"during Init to avoid breaking save states.",
|
|
|
|
name.c_str());
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
auto info = event_types.emplace(name, EventType{callback, nullptr});
|
|
|
|
EventType* event_type = &info.first->second;
|
|
|
|
event_type->name = &info.first->first;
|
|
|
|
return event_type;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void UnregisterAllEvents() {
|
2017-11-25 13:56:57 +00:00
|
|
|
ASSERT_MSG(event_queue.empty(), "Cannot unregister events with events pending");
|
2014-04-08 23:11:21 +00:00
|
|
|
event_types.clear();
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void Init() {
|
2017-11-25 13:56:57 +00:00
|
|
|
downcount = MAX_SLICE_LENGTH;
|
|
|
|
slice_length = MAX_SLICE_LENGTH;
|
2015-01-06 01:17:49 +00:00
|
|
|
global_timer = 0;
|
|
|
|
idled_cycles = 0;
|
2015-04-28 02:46:19 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
// The time between CoreTiming being intialized and the first call to Advance() is considered
|
|
|
|
// the slice boundary between slice -1 and slice 0. Dispatcher loops must call Advance() before
|
|
|
|
// executing the first cycle of each slice to prepare the slice length and downcount for
|
|
|
|
// that slice.
|
|
|
|
is_global_timer_sane = true;
|
2015-04-28 02:46:19 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
event_fifo_id = 0;
|
|
|
|
ev_lost = RegisterEvent("_lost_event", &EmptyTimedCallback);
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void Shutdown() {
|
2014-04-08 23:11:21 +00:00
|
|
|
MoveEvents();
|
|
|
|
ClearPendingEvents();
|
|
|
|
UnregisterAllEvents();
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
// This should only be called from the CPU thread. If you are calling
|
|
|
|
// it from any other thread, you are doing something evil
|
|
|
|
u64 GetTicks() {
|
|
|
|
u64 ticks = static_cast<u64>(global_timer);
|
|
|
|
if (!is_global_timer_sane) {
|
|
|
|
ticks += slice_length - downcount;
|
2017-09-30 16:25:49 +00:00
|
|
|
}
|
2017-11-25 13:56:57 +00:00
|
|
|
return ticks;
|
2017-09-30 16:25:49 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
void AddTicks(u64 ticks) {
|
2018-01-19 23:01:41 +00:00
|
|
|
downcount -= static_cast<int>(ticks);
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
u64 GetIdleTicks() {
|
2017-11-25 13:56:57 +00:00
|
|
|
return static_cast<u64>(idled_cycles);
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void ClearPendingEvents() {
|
2017-11-25 13:56:57 +00:00
|
|
|
event_queue.clear();
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
void ScheduleEvent(s64 cycles_into_future, const EventType* event_type, u64 userdata) {
|
|
|
|
ASSERT(event_type != nullptr);
|
|
|
|
s64 timeout = GetTicks() + cycles_into_future;
|
2015-01-06 01:17:49 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
// If this event needs to be scheduled before the next advance(), force one early
|
|
|
|
if (!is_global_timer_sane)
|
|
|
|
ForceExceptionCheck(cycles_into_future);
|
2014-04-08 23:11:21 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type});
|
|
|
|
std::push_heap(event_queue.begin(), event_queue.end(), std::greater<Event>());
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
void ScheduleEventThreadsafe(s64 cycles_into_future, const EventType* event_type, u64 userdata) {
|
|
|
|
ts_queue.Push(Event{global_timer + cycles_into_future, 0, userdata, event_type});
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
void UnscheduleEvent(const EventType* event_type, u64 userdata) {
|
|
|
|
auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
|
|
|
|
return e.type == event_type && e.userdata == userdata;
|
|
|
|
});
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
// Removing random items breaks the invariant so we have to re-establish it.
|
|
|
|
if (itr != event_queue.end()) {
|
|
|
|
event_queue.erase(itr, event_queue.end());
|
|
|
|
std::make_heap(event_queue.begin(), event_queue.end(), std::greater<Event>());
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
void RemoveEvent(const EventType* event_type) {
|
|
|
|
auto itr = std::remove_if(event_queue.begin(), event_queue.end(),
|
|
|
|
[&](const Event& e) { return e.type == event_type; });
|
2015-01-06 01:17:49 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
// Removing random items breaks the invariant so we have to re-establish it.
|
|
|
|
if (itr != event_queue.end()) {
|
|
|
|
event_queue.erase(itr, event_queue.end());
|
|
|
|
std::make_heap(event_queue.begin(), event_queue.end(), std::greater<Event>());
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
void RemoveNormalAndThreadsafeEvent(const EventType* event_type) {
|
|
|
|
MoveEvents();
|
2014-04-08 23:11:21 +00:00
|
|
|
RemoveEvent(event_type);
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
void ForceExceptionCheck(s64 cycles) {
|
|
|
|
cycles = std::max<s64>(0, cycles);
|
|
|
|
if (downcount > cycles) {
|
|
|
|
// downcount is always (much) smaller than MAX_INT so we can safely cast cycles to an int
|
|
|
|
// here. Account for cycles already executed by adjusting the g.slice_length
|
|
|
|
slice_length -= downcount - static_cast<int>(cycles);
|
|
|
|
downcount = static_cast<int>(cycles);
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void MoveEvents() {
|
2017-11-25 13:56:57 +00:00
|
|
|
for (Event ev; ts_queue.Pop(ev);) {
|
|
|
|
ev.fifo_order = event_fifo_id++;
|
|
|
|
event_queue.emplace_back(std::move(ev));
|
|
|
|
std::push_heap(event_queue.begin(), event_queue.end(), std::greater<Event>());
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void Advance() {
|
2017-11-25 13:56:57 +00:00
|
|
|
MoveEvents();
|
|
|
|
|
|
|
|
int cycles_executed = slice_length - downcount;
|
2015-01-06 01:17:49 +00:00
|
|
|
global_timer += cycles_executed;
|
2017-11-25 13:56:57 +00:00
|
|
|
slice_length = MAX_SLICE_LENGTH;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
is_global_timer_sane = true;
|
|
|
|
|
|
|
|
while (!event_queue.empty() && event_queue.front().time <= global_timer) {
|
|
|
|
Event evt = std::move(event_queue.front());
|
|
|
|
std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<Event>());
|
|
|
|
event_queue.pop_back();
|
2018-01-19 23:01:41 +00:00
|
|
|
evt.type->callback(evt.userdata, static_cast<int>(global_timer - evt.time));
|
2015-01-06 01:17:49 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
is_global_timer_sane = false;
|
|
|
|
|
|
|
|
// Still events left (scheduled in the future)
|
|
|
|
if (!event_queue.empty()) {
|
|
|
|
slice_length = static_cast<int>(
|
|
|
|
std::min<s64>(event_queue.front().time - global_timer, MAX_SLICE_LENGTH));
|
2015-01-06 01:17:49 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
downcount = slice_length;
|
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
void Idle() {
|
|
|
|
idled_cycles += downcount;
|
|
|
|
downcount = 0;
|
2015-01-06 01:17:49 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
u64 GetGlobalTimeUs() {
|
|
|
|
return GetTicks() * 1000000 / BASE_CLOCK_RATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetDowncount() {
|
|
|
|
return downcount;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
} // namespace CoreTiming
|