diff --git a/Source/Core/Core/Core.cpp b/Source/Core/Core/Core.cpp index 49348fd078..46de565a01 100644 --- a/Source/Core/Core/Core.cpp +++ b/Source/Core/Core/Core.cpp @@ -98,6 +98,10 @@ static bool s_request_refresh_info = false; static bool s_is_throttler_temp_disabled = false; static bool s_frame_step = false; +#ifdef USE_MEMORYWATCHER +static std::unique_ptr s_memory_watcher; +#endif + struct HostJob { std::function job; @@ -126,6 +130,13 @@ void FrameUpdateOnCPUThread() NetPlay::NetPlayClient::SendTimeBase(); } +void OnFrameEnd() +{ +#ifdef USE_MEMORYWATCHER + s_memory_watcher->Step(); +#endif +} + // Display messages and return values // Formatted stop message @@ -272,7 +283,7 @@ void Stop() // - Hammertime! ResetRumble(); #ifdef USE_MEMORYWATCHER - MemoryWatcher::Shutdown(); + s_memory_watcher.reset(); #endif } @@ -317,7 +328,7 @@ static void CpuThread(const std::optional& savestate_path, bool del EMM::InstallExceptionHandler(); // Let's run under memory watch #ifdef USE_MEMORYWATCHER - MemoryWatcher::Init(); + s_memory_watcher = std::make_unique(); #endif if (savestate_path) diff --git a/Source/Core/Core/Core.h b/Source/Core/Core/Core.h index 056e5ed3a1..fdd30a539a 100644 --- a/Source/Core/Core/Core.h +++ b/Source/Core/Core/Core.h @@ -66,6 +66,7 @@ void Callback_WiimoteInterruptChannel(int number, u16 channel_id, const u8* data void DisplayMessage(const std::string& message, int time_in_ms); void FrameUpdateOnCPUThread(); +void OnFrameEnd(); void VideoThrottle(); void RequestRefreshInfo(); diff --git a/Source/Core/Core/HW/VideoInterface.cpp b/Source/Core/Core/HW/VideoInterface.cpp index a2750a5840..0fc1993801 100644 --- a/Source/Core/Core/HW/VideoInterface.cpp +++ b/Source/Core/Core/HW/VideoInterface.cpp @@ -726,6 +726,7 @@ static void BeginField(FieldType field, u64 ticks) static void EndField() { Core::VideoThrottle(); + Core::OnFrameEnd(); } // Purpose: Send VI interrupt when triggered diff --git a/Source/Core/Core/MemoryWatcher.cpp b/Source/Core/Core/MemoryWatcher.cpp index e4bd3d0318..3e6e61c227 100644 --- a/Source/Core/Core/MemoryWatcher.cpp +++ b/Source/Core/Core/MemoryWatcher.cpp @@ -4,39 +4,14 @@ #include #include -#include #include #include #include "Common/FileUtil.h" -#include "Core/CoreTiming.h" #include "Core/HW/Memmap.h" #include "Core/HW/SystemTimers.h" #include "Core/MemoryWatcher.h" -static std::unique_ptr s_memory_watcher; -static CoreTiming::EventType* s_event; -static const int MW_RATE = 600; // Steps per second - -static void MWCallback(u64 userdata, s64 cyclesLate) -{ - s_memory_watcher->Step(); - CoreTiming::ScheduleEvent(SystemTimers::GetTicksPerSecond() / MW_RATE - cyclesLate, s_event); -} - -void MemoryWatcher::Init() -{ - s_memory_watcher = std::make_unique(); - s_event = CoreTiming::RegisterEvent("MemoryWatcher", MWCallback); - CoreTiming::ScheduleEvent(0, s_event); -} - -void MemoryWatcher::Shutdown() -{ - CoreTiming::RemoveEvent(s_event); - s_memory_watcher.reset(); -} - MemoryWatcher::MemoryWatcher() { m_running = false; @@ -84,7 +59,6 @@ void MemoryWatcher::ParseLine(const std::string& line) bool MemoryWatcher::OpenSocket(const std::string& path) { - memset(&m_addr, 0, sizeof(m_addr)); m_addr.sun_family = AF_UNIX; strncpy(m_addr.sun_path, path.c_str(), sizeof(m_addr.sun_path) - 1); @@ -100,17 +74,10 @@ u32 MemoryWatcher::ChasePointer(const std::string& line) return value; } -std::string MemoryWatcher::ComposeMessage(const std::string& line, u32 value) +std::string MemoryWatcher::ComposeMessages() { std::stringstream message_stream; - message_stream << line << '\n' << std::hex << value; - return message_stream.str(); -} - -void MemoryWatcher::Step() -{ - if (!m_running) - return; + message_stream << std::hex; for (auto& entry : m_values) { @@ -122,9 +89,19 @@ void MemoryWatcher::Step() { // Update the value current_value = new_value; - std::string message = ComposeMessage(address, new_value); - sendto(m_fd, message.c_str(), message.size() + 1, 0, reinterpret_cast(&m_addr), - sizeof(m_addr)); + message_stream << address << '\n' << new_value << '\n'; } } + + return message_stream.str(); +} + +void MemoryWatcher::Step() +{ + if (!m_running) + return; + + std::string message = ComposeMessages(); + sendto(m_fd, message.c_str(), message.size() + 1, 0, reinterpret_cast(&m_addr), + sizeof(m_addr)); } diff --git a/Source/Core/Core/MemoryWatcher.h b/Source/Core/Core/MemoryWatcher.h index e2dc4790bc..c180e4ebb5 100644 --- a/Source/Core/Core/MemoryWatcher.h +++ b/Source/Core/Core/MemoryWatcher.h @@ -24,21 +24,18 @@ public: ~MemoryWatcher(); void Step(); - static void Init(); - static void Shutdown(); - private: bool LoadAddresses(const std::string& path); bool OpenSocket(const std::string& path); void ParseLine(const std::string& line); u32 ChasePointer(const std::string& line); - std::string ComposeMessage(const std::string& line, u32 value); + std::string ComposeMessages(); - bool m_running; + bool m_running = false; int m_fd; - sockaddr_un m_addr; + sockaddr_un m_addr{}; // Address as stored in the file -> list of offsets to follow std::map> m_addresses;