Merge bitcoin/bitcoin#25456: rpc: Use steady_clock for getrpcinfo durations

fabae3541a rpc: Use steady_clock for getrpcinfo durations (MacroFake)

Pull request description:

  Currently it uses `GetTimeMicros`, which is the system time. Using steady time instead, makes the code type safe and avoids spurious offsets when the system time adjusts.

ACKs for top commit:
  laanwj:
    Code review ACK fabae3541a
  w0xlt:
    Code Review ACK fabae3541a
  shaavan:
    Code Review ACK fabae3541a

Tree-SHA512: eb25fe3e69bf42ec8a2d4aaa69b435de7654b0d07218ce3e0c03ebaef6eb7f713128779057d012621773a34675a81f5757e7b2502c13b82adaf6e2df970d8c66
This commit is contained in:
MacroFake 2022-06-24 17:27:15 +02:00
commit 1da1c0dd66
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
2 changed files with 14 additions and 5 deletions

View file

@ -11,14 +11,18 @@
#include <util/strencodings.h>
#include <util/string.h>
#include <util/system.h>
#include <util/time.h>
#include <boost/signals2/signal.hpp>
#include <cassert>
#include <memory> // for unique_ptr
#include <chrono>
#include <memory>
#include <mutex>
#include <unordered_map>
using SteadyClock = std::chrono::steady_clock;
static GlobalMutex g_rpc_warmup_mutex;
static std::atomic<bool> g_rpc_running{false};
static bool fRPCInWarmup GUARDED_BY(g_rpc_warmup_mutex) = true;
@ -33,7 +37,7 @@ static bool ExecuteCommand(const CRPCCommand& command, const JSONRPCRequest& req
struct RPCCommandExecutionInfo
{
std::string method;
int64_t start;
SteadyClock::time_point start;
};
struct RPCServerInfo
@ -50,7 +54,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()
{
@ -231,7 +235,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<std::chrono::microseconds>(SteadyClock::now() - info.start)});
active_commands.push_back(entry);
}

View file

@ -40,10 +40,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 <typename Dur1, typename Dur2>
constexpr auto Ticks(Dur2 d)
{
return std::chrono::duration_cast<Dur1>(d).count();
}
template <typename Duration, typename Timepoint>
constexpr auto TicksSinceEpoch(Timepoint t)
{
return std::chrono::time_point_cast<Duration>(t).time_since_epoch().count();
return Ticks<Duration>(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(); }