This factor is applied to time spent since we read clock for the first
time. It impacts value returned by get_clock() and get_clock_realtime().

Signed-off-by: Pierrick Bouvier <pierrick.bouv...@linaro.org>
---
 include/qemu/timer.h     | 22 ++++++++++++++++------
 util/qemu-timer-common.c |  1 +
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/include/qemu/timer.h b/include/qemu/timer.h
index abd2204f3be..9c3b8e5506d 100644
--- a/include/qemu/timer.h
+++ b/include/qemu/timer.h
@@ -801,6 +801,18 @@ static inline int64_t get_max_clock_jump(void)
     return 60 * NANOSECONDS_PER_SECOND;
 }
 
+extern int64_t clock_start;
+extern double clock_time_dilation;
+
+static inline int64_t dilate_time(int64_t now)
+{
+    g_assert(now >= clock_start);
+    if (!clock_time_dilation) {
+        return now;
+    }
+    return clock_start + (now - clock_start) * clock_time_dilation;
+}
+
 /*
  * Low level clock functions
  */
@@ -811,11 +823,9 @@ static inline int64_t get_clock_realtime(void)
     struct timeval tv;
 
     gettimeofday(&tv, NULL);
-    return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
+    return dilate_time(tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000));
 }
 
-extern int64_t clock_start;
-
 /* Warning: don't insert tracepoints into these functions, they are
    also used by simpletrace backend and tracepoints would cause
    an infinite recursion! */
@@ -826,7 +836,7 @@ static inline int64_t get_clock(void)
 {
     LARGE_INTEGER ti;
     QueryPerformanceCounter(&ti);
-    return muldiv64(ti.QuadPart, NANOSECONDS_PER_SECOND, clock_freq);
+    dilate_time(muldiv64(ti.QuadPart, NANOSECONDS_PER_SECOND, clock_freq));
 }
 
 #else
@@ -838,10 +848,10 @@ static inline int64_t get_clock(void)
     if (use_rt_clock) {
         struct timespec ts;
         clock_gettime(CLOCK_MONOTONIC, &ts);
-        return ts.tv_sec * 1000000000LL + ts.tv_nsec;
+        return dilate_time(ts.tv_sec * 1000000000LL + ts.tv_nsec);
     } else {
         /* XXX: using gettimeofday leads to problems if the date
-           changes, so it should be avoided. */
+           changes, so it should be avoided. Time is already dilated. */
         return get_clock_realtime();
     }
 }
diff --git a/util/qemu-timer-common.c b/util/qemu-timer-common.c
index cc1326f7264..d8895aaccad 100644
--- a/util/qemu-timer-common.c
+++ b/util/qemu-timer-common.c
@@ -28,6 +28,7 @@
 /* real time host monotonic timer */
 
 int64_t clock_start;
+double clock_time_dilation;
 
 #ifdef _WIN32
 
-- 
2.47.2


Reply via email to