Tests: Add base tests to host timing
This commit is contained in:
parent
62e35ffc0e
commit
0f8e5a1465
|
@ -9,6 +9,7 @@
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include "common/common_types.h"
|
||||||
|
|
||||||
namespace Common {
|
namespace Common {
|
||||||
|
|
||||||
|
@ -28,8 +29,7 @@ public:
|
||||||
is_set = false;
|
is_set = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Duration>
|
bool WaitFor(const std::chrono::nanoseconds& time) {
|
||||||
bool WaitFor(const std::chrono::duration<Duration>& time) {
|
|
||||||
std::unique_lock lk{mutex};
|
std::unique_lock lk{mutex};
|
||||||
if (!condvar.wait_for(lk, time, [this] { return is_set; }))
|
if (!condvar.wait_for(lk, time, [this] { return is_set; }))
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
|
||||||
#include "common/assert.h"
|
#include "common/assert.h"
|
||||||
#include "common/thread.h"
|
|
||||||
#include "core/core_timing_util.h"
|
#include "core/core_timing_util.h"
|
||||||
|
|
||||||
namespace Core::HostTiming {
|
namespace Core::HostTiming {
|
||||||
|
@ -47,39 +46,55 @@ void CoreTiming::Initialize() {
|
||||||
event_fifo_id = 0;
|
event_fifo_id = 0;
|
||||||
const auto empty_timed_callback = [](u64, s64) {};
|
const auto empty_timed_callback = [](u64, s64) {};
|
||||||
ev_lost = CreateEvent("_lost_event", empty_timed_callback);
|
ev_lost = CreateEvent("_lost_event", empty_timed_callback);
|
||||||
start_time = std::chrono::system_clock::now();
|
start_time = std::chrono::steady_clock::now();
|
||||||
timer_thread = std::make_unique<std::thread>(ThreadEntry, std::ref(*this));
|
timer_thread = std::make_unique<std::thread>(ThreadEntry, std::ref(*this));
|
||||||
}
|
}
|
||||||
|
|
||||||
void CoreTiming::Shutdown() {
|
void CoreTiming::Shutdown() {
|
||||||
std::unique_lock<std::mutex> guard(inner_mutex);
|
paused = true;
|
||||||
shutting_down = true;
|
shutting_down = true;
|
||||||
if (!is_set) {
|
event.Set();
|
||||||
is_set = true;
|
|
||||||
condvar.notify_one();
|
|
||||||
}
|
|
||||||
inner_mutex.unlock();
|
|
||||||
timer_thread->join();
|
timer_thread->join();
|
||||||
ClearPendingEvents();
|
ClearPendingEvents();
|
||||||
|
timer_thread.reset();
|
||||||
|
has_started = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CoreTiming::Pause(bool is_paused) {
|
||||||
|
paused = is_paused;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CoreTiming::SyncPause(bool is_paused) {
|
||||||
|
if (is_paused == paused && paused_set == paused) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Pause(is_paused);
|
||||||
|
event.Set();
|
||||||
|
while (paused_set != is_paused);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CoreTiming::IsRunning() {
|
||||||
|
return !paused_set;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CoreTiming::HasPendingEvents() {
|
||||||
|
return !(wait_set && event_queue.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
void CoreTiming::ScheduleEvent(s64 ns_into_future, const std::shared_ptr<EventType>& event_type,
|
void CoreTiming::ScheduleEvent(s64 ns_into_future, const std::shared_ptr<EventType>& event_type,
|
||||||
u64 userdata) {
|
u64 userdata) {
|
||||||
std::lock_guard guard{inner_mutex};
|
basic_lock.lock();
|
||||||
const u64 timeout = static_cast<u64>(GetGlobalTimeNs().count() + ns_into_future);
|
const u64 timeout = static_cast<u64>(GetGlobalTimeNs().count() + ns_into_future);
|
||||||
|
|
||||||
event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type});
|
event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type});
|
||||||
|
|
||||||
std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
||||||
if (!is_set) {
|
basic_lock.unlock();
|
||||||
is_set = true;
|
event.Set();
|
||||||
condvar.notify_one();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u64 userdata) {
|
void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u64 userdata) {
|
||||||
std::lock_guard guard{inner_mutex};
|
basic_lock.lock();
|
||||||
|
|
||||||
const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
|
const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
|
||||||
return e.type.lock().get() == event_type.get() && e.userdata == userdata;
|
return e.type.lock().get() == event_type.get() && e.userdata == userdata;
|
||||||
});
|
});
|
||||||
|
@ -89,6 +104,7 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u
|
||||||
event_queue.erase(itr, event_queue.end());
|
event_queue.erase(itr, event_queue.end());
|
||||||
std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
||||||
}
|
}
|
||||||
|
basic_lock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 CoreTiming::GetCPUTicks() const {
|
u64 CoreTiming::GetCPUTicks() const {
|
||||||
|
@ -106,7 +122,7 @@ void CoreTiming::ClearPendingEvents() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) {
|
void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) {
|
||||||
std::lock_guard guard{inner_mutex};
|
basic_lock.lock();
|
||||||
|
|
||||||
const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
|
const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
|
||||||
return e.type.lock().get() == event_type.get();
|
return e.type.lock().get() == event_type.get();
|
||||||
|
@ -117,43 +133,54 @@ void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) {
|
||||||
event_queue.erase(itr, event_queue.end());
|
event_queue.erase(itr, event_queue.end());
|
||||||
std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
||||||
}
|
}
|
||||||
|
basic_lock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CoreTiming::Advance() {
|
void CoreTiming::Advance() {
|
||||||
while (true) {
|
has_started = true;
|
||||||
std::unique_lock<std::mutex> guard(inner_mutex);
|
while (!shutting_down) {
|
||||||
|
while (!paused) {
|
||||||
|
paused_set = false;
|
||||||
|
basic_lock.lock();
|
||||||
|
global_timer = GetGlobalTimeNs().count();
|
||||||
|
|
||||||
global_timer = GetGlobalTimeNs().count();
|
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_queue.pop_back();
|
||||||
|
basic_lock.unlock();
|
||||||
|
|
||||||
while (!event_queue.empty() && event_queue.front().time <= global_timer) {
|
if (auto event_type{evt.type.lock()}) {
|
||||||
Event evt = std::move(event_queue.front());
|
event_type->callback(evt.userdata, global_timer - evt.time);
|
||||||
std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
}
|
||||||
event_queue.pop_back();
|
|
||||||
inner_mutex.unlock();
|
|
||||||
|
|
||||||
if (auto event_type{evt.type.lock()}) {
|
basic_lock.lock();
|
||||||
event_type->callback(evt.userdata, global_timer - evt.time);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inner_mutex.lock();
|
if (!event_queue.empty()) {
|
||||||
}
|
std::chrono::nanoseconds next_time = std::chrono::nanoseconds(event_queue.front().time - global_timer);
|
||||||
auto next_time = std::chrono::nanoseconds(event_queue.front().time - global_timer);
|
basic_lock.unlock();
|
||||||
condvar.wait_for(guard, next_time, [this] { return is_set; });
|
event.WaitFor(next_time);
|
||||||
is_set = false;
|
} else {
|
||||||
if (shutting_down) {
|
basic_lock.unlock();
|
||||||
break;
|
wait_set = true;
|
||||||
|
event.Wait();
|
||||||
|
}
|
||||||
|
|
||||||
|
wait_set = false;
|
||||||
}
|
}
|
||||||
|
paused_set = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::chrono::nanoseconds CoreTiming::GetGlobalTimeNs() const {
|
std::chrono::nanoseconds CoreTiming::GetGlobalTimeNs() const {
|
||||||
sys_time_point current = std::chrono::system_clock::now();
|
sys_time_point current = std::chrono::steady_clock::now();
|
||||||
auto elapsed = current - start_time;
|
auto elapsed = current - start_time;
|
||||||
return std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed);
|
return std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const {
|
std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const {
|
||||||
sys_time_point current = std::chrono::system_clock::now();
|
sys_time_point current = std::chrono::steady_clock::now();
|
||||||
auto elapsed = current - start_time;
|
auto elapsed = current - start_time;
|
||||||
return std::chrono::duration_cast<std::chrono::microseconds>(elapsed);
|
return std::chrono::duration_cast<std::chrono::microseconds>(elapsed);
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,13 +14,15 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "common/common_types.h"
|
#include "common/common_types.h"
|
||||||
|
#include "common/spin_lock.h"
|
||||||
|
#include "common/thread.h"
|
||||||
#include "common/threadsafe_queue.h"
|
#include "common/threadsafe_queue.h"
|
||||||
|
|
||||||
namespace Core::HostTiming {
|
namespace Core::HostTiming {
|
||||||
|
|
||||||
/// A callback that may be scheduled for a particular core timing event.
|
/// A callback that may be scheduled for a particular core timing event.
|
||||||
using TimedCallback = std::function<void(u64 userdata, s64 cycles_late)>;
|
using TimedCallback = std::function<void(u64 userdata, s64 cycles_late)>;
|
||||||
using sys_time_point = std::chrono::time_point<std::chrono::system_clock>;
|
using sys_time_point = std::chrono::time_point<std::chrono::steady_clock>;
|
||||||
|
|
||||||
/// Contains the characteristics of a particular event.
|
/// Contains the characteristics of a particular event.
|
||||||
struct EventType {
|
struct EventType {
|
||||||
|
@ -63,6 +65,23 @@ public:
|
||||||
/// Tears down all timing related functionality.
|
/// Tears down all timing related functionality.
|
||||||
void Shutdown();
|
void Shutdown();
|
||||||
|
|
||||||
|
/// Pauses/Unpauses the execution of the timer thread.
|
||||||
|
void Pause(bool is_paused);
|
||||||
|
|
||||||
|
/// Pauses/Unpauses the execution of the timer thread and waits until paused.
|
||||||
|
void SyncPause(bool is_paused);
|
||||||
|
|
||||||
|
/// Checks if core timing is running.
|
||||||
|
bool IsRunning();
|
||||||
|
|
||||||
|
/// Checks if the timer thread has started.
|
||||||
|
bool HasStarted() {
|
||||||
|
return has_started;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if there are any pending time events.
|
||||||
|
bool HasPendingEvents();
|
||||||
|
|
||||||
/// Schedules an event in core timing
|
/// Schedules an event in core timing
|
||||||
void ScheduleEvent(s64 ns_into_future, const std::shared_ptr<EventType>& event_type,
|
void ScheduleEvent(s64 ns_into_future, const std::shared_ptr<EventType>& event_type,
|
||||||
u64 userdata = 0);
|
u64 userdata = 0);
|
||||||
|
@ -107,11 +126,14 @@ private:
|
||||||
u64 event_fifo_id = 0;
|
u64 event_fifo_id = 0;
|
||||||
|
|
||||||
std::shared_ptr<EventType> ev_lost;
|
std::shared_ptr<EventType> ev_lost;
|
||||||
bool is_set = false;
|
Common::Event event{};
|
||||||
std::condition_variable condvar;
|
Common::SpinLock basic_lock{};
|
||||||
std::mutex inner_mutex;
|
|
||||||
std::unique_ptr<std::thread> timer_thread;
|
std::unique_ptr<std::thread> timer_thread;
|
||||||
|
std::atomic<bool> paused{};
|
||||||
|
std::atomic<bool> paused_set{};
|
||||||
|
std::atomic<bool> wait_set{};
|
||||||
std::atomic<bool> shutting_down{};
|
std::atomic<bool> shutting_down{};
|
||||||
|
std::atomic<bool> has_started{};
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Creates a core timing event with the given name and callback.
|
/// Creates a core timing event with the given name and callback.
|
||||||
|
|
|
@ -8,6 +8,7 @@ add_executable(tests
|
||||||
core/arm/arm_test_common.cpp
|
core/arm/arm_test_common.cpp
|
||||||
core/arm/arm_test_common.h
|
core/arm/arm_test_common.h
|
||||||
core/core_timing.cpp
|
core/core_timing.cpp
|
||||||
|
core/host_timing.cpp
|
||||||
tests.cpp
|
tests.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,150 @@
|
||||||
|
// Copyright 2016 Dolphin Emulator Project / 2017 Dolphin Emulator Project
|
||||||
|
// Licensed under GPLv2+
|
||||||
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
|
#include <catch2/catch.hpp>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <bitset>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "common/file_util.h"
|
||||||
|
#include "core/core.h"
|
||||||
|
#include "core/host_timing.h"
|
||||||
|
|
||||||
|
// Numbers are chosen randomly to make sure the correct one is given.
|
||||||
|
static constexpr std::array<u64, 5> CB_IDS{{42, 144, 93, 1026, UINT64_C(0xFFFF7FFFF7FFFF)}};
|
||||||
|
static constexpr int MAX_SLICE_LENGTH = 10000; // Copied from CoreTiming internals
|
||||||
|
static constexpr std::array<u64, 5> calls_order{{2,0,1,4,3}};
|
||||||
|
static std::array<s64, 5> delays{};
|
||||||
|
|
||||||
|
static std::bitset<CB_IDS.size()> callbacks_ran_flags;
|
||||||
|
static u64 expected_callback = 0;
|
||||||
|
static s64 lateness = 0;
|
||||||
|
|
||||||
|
template <unsigned int IDX>
|
||||||
|
void HostCallbackTemplate(u64 userdata, s64 nanoseconds_late) {
|
||||||
|
static_assert(IDX < CB_IDS.size(), "IDX out of range");
|
||||||
|
callbacks_ran_flags.set(IDX);
|
||||||
|
REQUIRE(CB_IDS[IDX] == userdata);
|
||||||
|
REQUIRE(CB_IDS[IDX] == CB_IDS[calls_order[expected_callback]]);
|
||||||
|
delays[IDX] = nanoseconds_late;
|
||||||
|
++expected_callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
static u64 callbacks_done = 0;
|
||||||
|
|
||||||
|
struct ScopeInit final {
|
||||||
|
ScopeInit() {
|
||||||
|
core_timing.Initialize();
|
||||||
|
}
|
||||||
|
~ScopeInit() {
|
||||||
|
core_timing.Shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
Core::HostTiming::CoreTiming core_timing;
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_CASE("HostTiming[BasicOrder]", "[core]") {
|
||||||
|
ScopeInit guard;
|
||||||
|
auto& core_timing = guard.core_timing;
|
||||||
|
std::vector<std::shared_ptr<Core::HostTiming::EventType>> events;
|
||||||
|
events.resize(5);
|
||||||
|
events[0] =
|
||||||
|
Core::HostTiming::CreateEvent("callbackA", HostCallbackTemplate<0>);
|
||||||
|
events[1] =
|
||||||
|
Core::HostTiming::CreateEvent("callbackB", HostCallbackTemplate<1>);
|
||||||
|
events[2] =
|
||||||
|
Core::HostTiming::CreateEvent("callbackC", HostCallbackTemplate<2>);
|
||||||
|
events[3] =
|
||||||
|
Core::HostTiming::CreateEvent("callbackD", HostCallbackTemplate<3>);
|
||||||
|
events[4] =
|
||||||
|
Core::HostTiming::CreateEvent("callbackE", HostCallbackTemplate<4>);
|
||||||
|
|
||||||
|
expected_callback = 0;
|
||||||
|
|
||||||
|
core_timing.SyncPause(true);
|
||||||
|
|
||||||
|
u64 one_micro = 1000U;
|
||||||
|
for (std::size_t i = 0; i < events.size(); i++) {
|
||||||
|
u64 order = calls_order[i];
|
||||||
|
core_timing.ScheduleEvent(i*one_micro + 100U, events[order], CB_IDS[order]);
|
||||||
|
}
|
||||||
|
/// test pause
|
||||||
|
REQUIRE(callbacks_ran_flags.none());
|
||||||
|
|
||||||
|
core_timing.Pause(false); // No need to sync
|
||||||
|
|
||||||
|
while (core_timing.HasPendingEvents());
|
||||||
|
|
||||||
|
REQUIRE(callbacks_ran_flags.all());
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < delays.size(); i++) {
|
||||||
|
const double delay = static_cast<double>(delays[i]);
|
||||||
|
const double micro = delay / 1000.0f;
|
||||||
|
const double mili = micro / 1000.0f;
|
||||||
|
printf("HostTimer Pausing Delay[%zu]: %.3f %.6f\n", i, micro, mili);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma optimize("", off)
|
||||||
|
u64 TestTimerSpeed(Core::HostTiming::CoreTiming& core_timing) {
|
||||||
|
u64 start = core_timing.GetGlobalTimeNs().count();
|
||||||
|
u64 placebo = 0;
|
||||||
|
for (std::size_t i = 0; i < 1000; i++) {
|
||||||
|
placebo += core_timing.GetGlobalTimeNs().count();
|
||||||
|
}
|
||||||
|
u64 end = core_timing.GetGlobalTimeNs().count();
|
||||||
|
return (end - start);
|
||||||
|
}
|
||||||
|
#pragma optimize("", on)
|
||||||
|
|
||||||
|
TEST_CASE("HostTiming[BasicOrderNoPausing]", "[core]") {
|
||||||
|
ScopeInit guard;
|
||||||
|
auto& core_timing = guard.core_timing;
|
||||||
|
std::vector<std::shared_ptr<Core::HostTiming::EventType>> events;
|
||||||
|
events.resize(5);
|
||||||
|
events[0] =
|
||||||
|
Core::HostTiming::CreateEvent("callbackA", HostCallbackTemplate<0>);
|
||||||
|
events[1] =
|
||||||
|
Core::HostTiming::CreateEvent("callbackB", HostCallbackTemplate<1>);
|
||||||
|
events[2] =
|
||||||
|
Core::HostTiming::CreateEvent("callbackC", HostCallbackTemplate<2>);
|
||||||
|
events[3] =
|
||||||
|
Core::HostTiming::CreateEvent("callbackD", HostCallbackTemplate<3>);
|
||||||
|
events[4] =
|
||||||
|
Core::HostTiming::CreateEvent("callbackE", HostCallbackTemplate<4>);
|
||||||
|
|
||||||
|
core_timing.SyncPause(true);
|
||||||
|
core_timing.SyncPause(false);
|
||||||
|
|
||||||
|
expected_callback = 0;
|
||||||
|
|
||||||
|
u64 start = core_timing.GetGlobalTimeNs().count();
|
||||||
|
u64 one_micro = 1000U;
|
||||||
|
for (std::size_t i = 0; i < events.size(); i++) {
|
||||||
|
u64 order = calls_order[i];
|
||||||
|
core_timing.ScheduleEvent(i*one_micro + 100U, events[order], CB_IDS[order]);
|
||||||
|
}
|
||||||
|
u64 end = core_timing.GetGlobalTimeNs().count();
|
||||||
|
const double scheduling_time = static_cast<double>(end - start);
|
||||||
|
const double timer_time = static_cast<double>(TestTimerSpeed(core_timing));
|
||||||
|
|
||||||
|
while (core_timing.HasPendingEvents());
|
||||||
|
|
||||||
|
REQUIRE(callbacks_ran_flags.all());
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < delays.size(); i++) {
|
||||||
|
const double delay = static_cast<double>(delays[i]);
|
||||||
|
const double micro = delay / 1000.0f;
|
||||||
|
const double mili = micro / 1000.0f;
|
||||||
|
printf("HostTimer No Pausing Delay[%zu]: %.3f %.6f\n", i, micro, mili);
|
||||||
|
}
|
||||||
|
|
||||||
|
const double micro = scheduling_time / 1000.0f;
|
||||||
|
const double mili = micro / 1000.0f;
|
||||||
|
printf("HostTimer No Pausing Scheduling Time: %.3f %.6f\n", micro, mili);
|
||||||
|
printf("HostTimer No Pausing Timer Time: %.3f %.6f\n", timer_time / 1000.f, timer_time / 1000000.f);
|
||||||
|
}
|
Loading…
Reference in New Issue