]> xenbits.xensource.com Git - mini-os.git/commitdiff
mini-os: simplify monotonic_clock()
authorJuergen Gross <jgross@suse.com>
Wed, 24 Jul 2024 10:02:25 +0000 (12:02 +0200)
committerJan Beulich <jbeulich@suse.com>
Wed, 24 Jul 2024 10:02:25 +0000 (12:02 +0200)
monotonic_clock() in arch/x86/time.c is more complex than needed: it
has basically two nested loops making sure the time data obtained from
Xen are valid.

Simplify that by merging some of the used sub-functions into the main
function and using only a single loop. Further simplify the code by
using struct vcpu_time_info for the local instance instead of defining
a similar structure in the code.

Signed-off-by: Juergen Gross <jgross@suse.com>
Reviewed-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
arch/x86/time.c

index 7fd7abeff22f3ec82da63da52164d746bfc93793..52916e1589764d84bf6599e263ba4eda475fa49a 100644 (file)
  *************************************************************************/
 
 /* These are peridically updated in shared_info, and then copied here. */
-struct shadow_time_info {
-    uint64_t tsc_timestamp;     /* TSC at last update of time vals.  */
-    uint64_t system_timestamp;  /* Time, in nanosecs, since boot.    */
-    uint32_t tsc_to_nsec_mul;
-    int tsc_shift;
-    uint32_t version;
-};
 static struct timespec shadow_ts;
 static uint32_t shadow_ts_version;
 
-static struct shadow_time_info shadow;
-
-static inline int time_values_up_to_date(void)
-{
-    struct vcpu_time_info *src = &HYPERVISOR_shared_info->vcpu_info[0].time;
-
-    return shadow.version == src->version;
-}
+static struct vcpu_time_info shadow;
 
 static inline int wc_values_up_to_date(void)
 {
@@ -113,22 +99,7 @@ static unsigned long get_nsec_offset(void)
     rdtscll(now);
     delta = now - shadow.tsc_timestamp;
 
-    return scale_delta(delta, shadow.tsc_to_nsec_mul, shadow.tsc_shift);
-}
-
-static void get_time_values_from_xen(void)
-{
-    struct vcpu_time_info *src = &HYPERVISOR_shared_info->vcpu_info[0].time;
-
-    do {
-        shadow.version = src->version;
-        rmb();
-        shadow.tsc_timestamp     = src->tsc_timestamp;
-        shadow.system_timestamp  = src->system_time;
-        shadow.tsc_to_nsec_mul   = src->tsc_to_system_mul;
-        shadow.tsc_shift         = src->tsc_shift;
-        rmb();
-    } while ( (src->version & 1) | (shadow.version ^ src->version) );
+    return scale_delta(delta, shadow.tsc_to_system_mul, shadow.tsc_shift);
 }
 
 /*
@@ -138,19 +109,22 @@ static void get_time_values_from_xen(void)
  */
 uint64_t monotonic_clock(void)
 {
-    uint64_t time;
-    uint32_t local_time_version;
+    struct vcpu_time_info *src = &HYPERVISOR_shared_info->vcpu_info[0].time;
 
-    do {
-        local_time_version = shadow.version;
-        rmb();
-        time = shadow.system_timestamp + get_nsec_offset();
-        if ( !time_values_up_to_date() )
-            get_time_values_from_xen();
-        rmb();
-    } while ( local_time_version != shadow.version );
+    if ( shadow.version != src->version )
+    {
+        do {
+            shadow.version = src->version;
+            rmb();
+            shadow.tsc_timestamp     = src->tsc_timestamp;
+            shadow.system_time       = src->system_time;
+            shadow.tsc_to_system_mul = src->tsc_to_system_mul;
+            shadow.tsc_shift         = src->tsc_shift;
+            rmb();
+        } while ( (src->version & 1) || (shadow.version != src->version) );
+    }
 
-    return time;
+    return shadow.system_time + get_nsec_offset();
 }
 
 static void update_wallclock(void)