[Spice-devel] [PATCH spice-server 1/2] utils: Get monotonic time in a coherent way

Frediano Ziglio fziglio at redhat.com
Tue Oct 9 12:14:10 UTC 2018


Use a single function to get monotonic time.
As lot of code needs nanosecond precision, spice_get_monotonic_time_ns
is used.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 server/main-channel-client.c | 4 ++--
 server/reds.c                | 2 +-
 server/utils.h               | 3 ++-
 3 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/server/main-channel-client.c b/server/main-channel-client.c
index b1e5a3e8..54be9934 100644
--- a/server/main-channel-client.c
+++ b/server/main-channel-client.c
@@ -503,7 +503,7 @@ void main_channel_client_handle_pong(MainChannelClient *mcc, SpiceMsgPing *ping,
     uint64_t roundtrip;
     RedChannelClient* rcc = RED_CHANNEL_CLIENT(mcc);
 
-    roundtrip = g_get_monotonic_time() - ping->timestamp;
+    roundtrip = spice_get_monotonic_time_ns() / NSEC_PER_MICROSEC - ping->timestamp;
 
     if (ping->id != mcc->priv->net_test_id) {
         /*
@@ -749,7 +749,7 @@ static void main_channel_marshall_ping(RedChannelClient *rcc,
 
     red_channel_client_init_send_data(rcc, SPICE_MSG_PING);
     ping.id = main_channel_client_next_ping_id(mcc);
-    ping.timestamp = g_get_monotonic_time();
+    ping.timestamp = spice_get_monotonic_time_ns() / NSEC_PER_MICROSEC;
     spice_marshall_msg_ping(m, &ping);
 
     while (size_left > 0) {
diff --git a/server/reds.c b/server/reds.c
index bdd6a45c..bff5b68e 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -3004,7 +3004,7 @@ static void migrate_timeout(void *opaque)
 
 uint32_t reds_get_mm_time(void)
 {
-    return g_get_monotonic_time() / 1000;
+    return spice_get_monotonic_time_ns() / NSEC_PER_MILLISEC;
 }
 
 void reds_enable_mm_time(RedsState *reds)
diff --git a/server/utils.h b/server/utils.h
index 07878539..a2747cf7 100644
--- a/server/utils.h
+++ b/server/utils.h
@@ -54,8 +54,9 @@ typedef int64_t red_time_t;
 
 #define NSEC_PER_SEC      1000000000LL
 #define NSEC_PER_MILLISEC 1000000LL
+#define NSEC_PER_MICROSEC 1000
 
-/* FIXME: consider g_get_monotonic_time (), but in microseconds */
+/* g_get_monotonic_time() as not enough precision */
 static inline red_time_t spice_get_monotonic_time_ns(void)
 {
     struct timespec time;
-- 
2.17.1



More information about the Spice-devel mailing list