[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH v3 2/7] add current_time function to time manager



From: Paul Semel <phentex@xxxxxxxxx>

this function returns the "epoch" time

Signed-off-by: Paul Semel <phentex@xxxxxxxxx>
---
 common/time.c      | 39 +++++++++++++++++++++++++++++++++++++++
 include/xtf/time.h |  4 ++++
 2 files changed, 43 insertions(+)

diff --git a/common/time.c b/common/time.c
index 7859c21..29b38ca 100644
--- a/common/time.c
+++ b/common/time.c
@@ -3,6 +3,7 @@
 #include <xtf/time.h>
 
 #include <arch/barrier.h>
+#include <arch/div.h>
 
 /* This function was taken from mini-os source code */
 /* It returns ((delta << shift) * mul_frac) >> 32 */
@@ -70,6 +71,44 @@ uint64_t since_boot_time(void)
     return system_time;
 }
 
+static void get_time_info(uint64_t *boot_time, uint64_t *sec, uint32_t *nsec)
+{
+    uint32_t ver1, ver2;
+    do {
+        ver1 = ACCESS_ONCE(shared_info.wc_version);
+        smp_rmb();
+        *boot_time = since_boot_time();
+#if defined(__i386__)
+        *sec = (uint64_t)ACCESS_ONCE(shared_info.wc_sec);
+#else
+        *sec = ((uint64_t)ACCESS_ONCE(shared_info.wc_sec_hi) << 32)
+            | ACCESS_ONCE(shared_info.wc_sec);
+#endif
+        *nsec = (uint64_t)ACCESS_ONCE(shared_info.wc_nsec);
+        smp_rmb();
+        ver2 = ACCESS_ONCE(shared_info.wc_version);
+        smp_rmb();
+    } while ( (ver1 & 1) != 0 && ver1 != ver2 );
+}
+
+/* This function return the epoch time (number of seconds elapsed
+ * since Juanary 1, 1970) */
+uint64_t current_time(void)
+{
+    uint32_t nsec;
+    uint64_t boot_time, sec;
+
+    get_time_info(&boot_time, &sec, &nsec);
+
+#if defined(__i386__)
+    divmod64(&boot_time, SEC_TO_NSEC(1));
+#else
+    boot_time /= SEC_TO_NSEC(1);
+#endif
+
+    return sec + boot_time;
+}
+
 /*
  * Local variables:
  * mode: C
diff --git a/include/xtf/time.h b/include/xtf/time.h
index b88da63..4d94958 100644
--- a/include/xtf/time.h
+++ b/include/xtf/time.h
@@ -14,10 +14,14 @@
     tsc = ((uint64_t)hi << 32) | lo;\
 }
 
+#define SEC_TO_NSEC(x) ((x) * 1000000000ul)
+
 
 /* Time from boot in nanoseconds */
 uint64_t since_boot_time(void);
 
+uint64_t current_time(void);
+
 #endif /* XTF_TIME_H */
 
 /*
-- 
2.16.1


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.