From c7cb26ba0538a7effdb85d54a4289c57cd169e2d Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Thu, 23 Jun 2022 15:51:16 +0200 Subject: [PATCH] merge bitcoin#25456: Use steady_clock for getrpcinfo durations --- src/rpc/server.cpp | 12 ++++++++---- src/util/time.h | 7 ++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index 17a83402a8..f5514958e6 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -16,16 +16,20 @@ #include #include #include +#include #include #include #include #include -#include // for unique_ptr +#include +#include #include #include +using SteadyClock = std::chrono::steady_clock; + static Mutex g_rpc_warmup_mutex; static std::atomic g_rpc_running{false}; static bool fRPCInWarmup GUARDED_BY(g_rpc_warmup_mutex) = true; @@ -43,7 +47,7 @@ static const std::string defaultPlatformUser = "platform-user"; struct RPCCommandExecutionInfo { std::string method; - int64_t start; + SteadyClock::time_point start; }; struct RPCServerInfo @@ -60,7 +64,7 @@ struct RPCCommandExecution explicit RPCCommandExecution(const std::string& method) { LOCK(g_rpc_server_info.mutex); - it = g_rpc_server_info.active_commands.insert(g_rpc_server_info.active_commands.end(), {method, GetTimeMicros()}); + it = g_rpc_server_info.active_commands.insert(g_rpc_server_info.active_commands.end(), {method, SteadyClock::now()}); } ~RPCCommandExecution() { @@ -272,7 +276,7 @@ static RPCHelpMan getrpcinfo() for (const RPCCommandExecutionInfo& info : g_rpc_server_info.active_commands) { UniValue entry(UniValue::VOBJ); entry.pushKV("method", info.method); - entry.pushKV("duration", GetTimeMicros() - info.start); + entry.pushKV("duration", int64_t{Ticks(SteadyClock::now() - info.start)}); active_commands.push_back(entry); } diff --git a/src/util/time.h b/src/util/time.h index 54e5524250..19d3cbc347 100644 --- a/src/util/time.h +++ b/src/util/time.h @@ -41,10 +41,15 @@ void UninterruptibleSleep(const std::chrono::microseconds& n); * This helper is used to convert durations/time_points before passing them over an * interface that doesn't support std::chrono (e.g. RPC, debug log, or the GUI) */ +template +constexpr auto Ticks(Dur2 d) +{ + return std::chrono::duration_cast(d).count(); +} template constexpr auto TicksSinceEpoch(Timepoint t) { - return std::chrono::time_point_cast(t).time_since_epoch().count(); + return Ticks(t.time_since_epoch()); } constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); } constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); }