From 435775c53675531cfabec4ba2a75adb7a003eb94 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Thu, 11 Apr 2019 16:55:47 +0800 Subject: [PATCH] crimson: use lowres_system_clock for keepalive timestamps we should use chrono types for representing time_point and duration, utime_t could be used for the format on wire though. Signed-off-by: Kefu Chai --- src/crimson/mon/MonClient.cc | 6 +++--- src/crimson/net/Connection.h | 12 +++++++----- src/crimson/net/ProtocolV2.cc | 37 +++++++++++++++++++++++++++++++++-- src/include/utime.h | 5 +++++ 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/crimson/mon/MonClient.cc b/src/crimson/mon/MonClient.cc index 7bcfd16cc20..f042275440e 100644 --- a/src/crimson/mon/MonClient.cc +++ b/src/crimson/mon/MonClient.cc @@ -10,7 +10,6 @@ #include "auth/AuthMethodList.h" #include "auth/RotatingKeyRing.h" -#include "common/Clock.h" #include "common/hostname.h" #include "crimson/auth/KeyRing.h" @@ -99,7 +98,8 @@ private: // v1 seastar::promise> reply; // v2 - utime_t auth_start; + using clock_t = seastar::lowres_system_clock; + clock_t::time_point auth_start; ceph::auth::method_t auth_method = 0; seastar::promise<> auth_done; AuthRegistry* auth_registry; @@ -265,7 +265,7 @@ Connection::authenticate_v1(epoch_t epoch, seastar::future<> Connection::authenticate_v2() { - auth_start = ceph_clock_now(); + auth_start = seastar::lowres_system_clock::now(); return conn->send(make_message()).then([this] { return auth_done.get_future(); }); diff --git a/src/crimson/net/Connection.h b/src/crimson/net/Connection.h index b7481fc7db7..32b057e1080 100644 --- a/src/crimson/net/Connection.h +++ b/src/crimson/net/Connection.h @@ -29,7 +29,9 @@ class Connection : public seastar::enable_shared_from_this { entity_addr_t peer_addr; peer_type_t peer_type = -1; int64_t peer_id = -1; - utime_t last_keepalive, last_keepalive_ack; + using clock_t = seastar::lowres_system_clock; + clock_t::time_point last_keepalive; + clock_t::time_point last_keepalive_ack; public: uint64_t peer_global_id = 0; @@ -60,14 +62,14 @@ class Connection : public seastar::enable_shared_from_this { virtual void print(ostream& out) const = 0; - void set_last_keepalive(utime_t when) { + void set_last_keepalive(clock_t::time_point when) { last_keepalive = when; } - void set_last_keepalive_ack(utime_t when) { + void set_last_keepalive_ack(clock_t::time_point when) { last_keepalive_ack = when; } - utime_t get_last_keepalive() const { return last_keepalive; } - utime_t get_last_keepalive_ack() const { return last_keepalive_ack; } + auto get_last_keepalive() const { return last_keepalive; } + auto get_last_keepalive_ack() const { return last_keepalive_ack; } }; inline ostream& operator<<(ostream& out, const Connection& conn) { diff --git a/src/crimson/net/ProtocolV2.cc b/src/crimson/net/ProtocolV2.cc index c76fb0c8ded..086914da95d 100644 --- a/src/crimson/net/ProtocolV2.cc +++ b/src/crimson/net/ProtocolV2.cc @@ -4,6 +4,8 @@ #include "ProtocolV2.h" #include +#include +#include #include "include/msgr.h" #include "include/random.h" @@ -57,6 +59,36 @@ inline seastar::future<> unexpected_tag(const Tag& unexpected, } // namespace anonymous +namespace fmt { +template <> +struct formatter { + // ignore the format string + template + constexpr auto parse(ParseContext &ctx) { return ctx.begin(); } + + template + auto format(const seastar::lowres_system_clock::time_point& t, + FormatContext& ctx) { + struct tm bdt; + time_t tt = std::chrono::duration_cast( + t.time_since_epoch()).count(); + localtime_r(&tt, &bdt); + auto milliseconds = (t.time_since_epoch() % + std::chrono::seconds(1)).count(); + return format_to(ctx.out(), "{:%Y-%m-%d %H:%M:%S} {:03d}", + bdt, milliseconds); + } +}; +} + +namespace std { +inline ostream& operator<<( + ostream& out, const seastar::lowres_system_clock::time_point& t) +{ + return out << fmt::format("{}", t); +} +} + namespace ceph::net { ProtocolV2::ProtocolV2(Dispatcher& dispatcher, @@ -1496,14 +1528,15 @@ void ProtocolV2::execute_ready() last_keepalive_ack_to_send = keepalive_frame.timestamp(); logger().debug("{} got KEEPALIVE2 {}", conn, last_keepalive_ack_to_send); - conn.last_keepalive = utime_t{seastar::lowres_system_clock::now()}; + conn.set_last_keepalive(seastar::lowres_system_clock::now()); notify_keepalive_ack(); }); case Tag::KEEPALIVE2_ACK: return read_frame_payload().then([this] { // handle_keepalive2_ack() logic auto keepalive_ack_frame = KeepAliveFrameAck::Decode(rx_segments_data.back()); - conn.last_keepalive_ack = keepalive_ack_frame.timestamp(); + conn.set_last_keepalive_ack( + seastar::lowres_system_clock::time_point{keepalive_ack_frame.timestamp()}); logger().debug("{} got KEEPALIVE_ACK {}", conn, conn.last_keepalive_ack); }); diff --git a/src/include/utime.h b/src/include/utime.h index b7327e0067e..f2f88b55e0c 100644 --- a/src/include/utime.h +++ b/src/include/utime.h @@ -88,6 +88,11 @@ public: tv.tv_nsec = std::chrono::duration_cast( t.time_since_epoch() % std::chrono::seconds(1)).count(); } + explicit operator seastar::lowres_system_clock::time_point() const noexcept { + using clock_t = seastar::lowres_system_clock; + return clock_t::time_point{std::chrono::duration_cast( + std::chrono::seconds{tv.tv_sec} + std::chrono::nanoseconds{tv.tv_nsec})}; + } #endif utime_t(const struct timeval &v) { -- 2.39.5