From 55eacf80df8fa1578e41512c8c761d5a13b76ae9 Mon Sep 17 00:00:00 2001 From: Zach Toogood Date: Wed, 20 Sep 2023 16:57:06 +0100 Subject: [PATCH] [core] Add options for logging slow SQL queries --- settings/default/logging.lua | 4 ++++ src/common/sql.cpp | 22 ++++++++++++++-------- src/map/map.cpp | 4 ++++ 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/settings/default/logging.lua b/settings/default/logging.lua index 3de08da1de0..5c721a87f39 100644 --- a/settings/default/logging.lua +++ b/settings/default/logging.lua @@ -54,4 +54,8 @@ xi.settings.logging = DEBUG_MODULES = false, -- Calls in C++: DebugModules(...) DEBUG_PACKET_BACKLOG = false, -- Special logic in map.cpp::send_parse DEBUG_DELIVERY_BOX = false, -- Special logic in packet_system.cpp::SmallPacket0x04D + + SQL_SLOW_QUERY_LOG_ENABLE = true, -- true/false. If true, slow SQL queries will generate warning or error logs if they exceed the durations listed below. + SQL_SLOW_QUERY_WARNING_TIME = 100, -- uint (milliseconds). + SQL_SLOW_QUERY_ERROR_TIME = 250, -- uint (milliseconds). } diff --git a/src/common/sql.cpp b/src/common/sql.cpp index 140a1942214..5164eb5ff81 100644 --- a/src/common/sql.cpp +++ b/src/common/sql.cpp @@ -34,6 +34,12 @@ #include #include +// TODO: Since kernel.cpp isn't used by the processes which now use Application, we can't +// : store this global flag there. So we're storing it here until all processes are +// : refactored to use Application. Once that's done this should be moved out of static +// : storage in this unit to a member of Application. +std::atomic gProcessLoaded = false; + SqlConnection::SqlConnection() : SqlConnection(settings::get("network.SQL_LOGIN").c_str(), settings::get("network.SQL_PASSWORD").c_str(), @@ -41,12 +47,11 @@ SqlConnection::SqlConnection() settings::get("network.SQL_PORT"), settings::get("network.SQL_DATABASE").c_str()) { - // Just forwarding the default credentials to the next contrictor + // Just forwarding the default credentials to the next constructor } SqlConnection::SqlConnection(const char* user, const char* passwd, const char* host, uint16 port, const char* db) -: m_LatencyWarning(false) -, m_ThreadId(std::this_thread::get_id()) +: m_ThreadId(std::this_thread::get_id()) { TracyZoneScoped; @@ -335,15 +340,16 @@ int32 SqlConnection::QueryStr(const char* query) auto endTime = hires_clock::now(); auto dTime = std::chrono::duration_cast(endTime - startTime); - if (m_LatencyWarning) + + if (gProcessLoaded && settings::get("logging.SQL_SLOW_QUERY_LOG_ENABLE")) { - if (dTime > 250ms) + if (dTime > std::chrono::milliseconds(settings::get("logging.SQL_SLOW_QUERY_ERROR_TIME"))) { - ShowError(fmt::format("Query took {}ms: {}", dTime.count(), self->buf)); + ShowError(fmt::format("SQL query took {}ms: {}", dTime.count(), self->buf)); } - else if (dTime > 100ms) + else if (dTime > std::chrono::milliseconds(settings::get("logging.SQL_SLOW_QUERY_WARNING_TIME"))) { - ShowWarning(fmt::format("Query took {}ms: {}", dTime.count(), self->buf)); + ShowWarning(fmt::format("SQL query took {}ms: {}", dTime.count(), self->buf)); } } diff --git a/src/map/map.cpp b/src/map/map.cpp index 4e4c2c00aef..0bbd9ee932d 100755 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -99,6 +99,8 @@ bool gLoadAllLua = false; std::unordered_map>>> PacketMods; +extern std::atomic gProcessLoaded; + namespace { uint32 MAX_BUFFER_SIZE = 2500U; @@ -369,6 +371,8 @@ int32 do_init(int32 argc, char** argv) }); // clang-format on + gProcessLoaded = true; + return 0; }