Skip to content

Commit 1da1c0d

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#25456: rpc: Use steady_clock for getrpcinfo durations
fabae35 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 fabae35 w0xlt: Code Review ACK bitcoin/bitcoin@fabae35 shaavan: Code Review ACK fabae35 Tree-SHA512: eb25fe3e69bf42ec8a2d4aaa69b435de7654b0d07218ce3e0c03ebaef6eb7f713128779057d012621773a34675a81f5757e7b2502c13b82adaf6e2df970d8c66
2 parents f697c06 + fabae35 commit 1da1c0d

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/rpc/server.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111
#include <util/strencodings.h>
1212
#include <util/string.h>
1313
#include <util/system.h>
14+
#include <util/time.h>
1415

1516
#include <boost/signals2/signal.hpp>
1617

1718
#include <cassert>
18-
#include <memory> // for unique_ptr
19+
#include <chrono>
20+
#include <memory>
1921
#include <mutex>
2022
#include <unordered_map>
2123

24+
using SteadyClock = std::chrono::steady_clock;
25+
2226
static GlobalMutex g_rpc_warmup_mutex;
2327
static std::atomic<bool> g_rpc_running{false};
2428
static bool fRPCInWarmup GUARDED_BY(g_rpc_warmup_mutex) = true;
@@ -33,7 +37,7 @@ static bool ExecuteCommand(const CRPCCommand& command, const JSONRPCRequest& req
3337
struct RPCCommandExecutionInfo
3438
{
3539
std::string method;
36-
int64_t start;
40+
SteadyClock::time_point start;
3741
};
3842

3943
struct RPCServerInfo
@@ -50,7 +54,7 @@ struct RPCCommandExecution
5054
explicit RPCCommandExecution(const std::string& method)
5155
{
5256
LOCK(g_rpc_server_info.mutex);
53-
it = g_rpc_server_info.active_commands.insert(g_rpc_server_info.active_commands.end(), {method, GetTimeMicros()});
57+
it = g_rpc_server_info.active_commands.insert(g_rpc_server_info.active_commands.end(), {method, SteadyClock::now()});
5458
}
5559
~RPCCommandExecution()
5660
{
@@ -231,7 +235,7 @@ static RPCHelpMan getrpcinfo()
231235
for (const RPCCommandExecutionInfo& info : g_rpc_server_info.active_commands) {
232236
UniValue entry(UniValue::VOBJ);
233237
entry.pushKV("method", info.method);
234-
entry.pushKV("duration", GetTimeMicros() - info.start);
238+
entry.pushKV("duration", int64_t{Ticks<std::chrono::microseconds>(SteadyClock::now() - info.start)});
235239
active_commands.push_back(entry);
236240
}
237241

src/util/time.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,15 @@ void UninterruptibleSleep(const std::chrono::microseconds& n);
4040
* This helper is used to convert durations/time_points before passing them over an
4141
* interface that doesn't support std::chrono (e.g. RPC, debug log, or the GUI)
4242
*/
43+
template <typename Dur1, typename Dur2>
44+
constexpr auto Ticks(Dur2 d)
45+
{
46+
return std::chrono::duration_cast<Dur1>(d).count();
47+
}
4348
template <typename Duration, typename Timepoint>
4449
constexpr auto TicksSinceEpoch(Timepoint t)
4550
{
46-
return std::chrono::time_point_cast<Duration>(t).time_since_epoch().count();
51+
return Ticks<Duration>(t.time_since_epoch());
4752
}
4853
constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); }
4954
constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); }

0 commit comments

Comments
 (0)