Export epoch time via proc_global_info (XCC)
[akaros.git] / tools / compilers / gcc-glibc / glibc-2.19-akaros / sysdeps / akaros / time.c
index 3aa0248..8abf8ac 100644 (file)
@@ -1,6 +1,8 @@
 #include <time.h>
 #include <stdbool.h>
 #include <sys/time.h>
+#include <parlib/timing.h>
+#include <ros/procinfo.h>
 
 time_t
 time(time_t* p)
@@ -43,3 +45,25 @@ void subtract_timespecs(struct timespec *diff, const struct timespec *minuend,
        diff->tv_nsec = borrow_amt + minuend->tv_nsec - subtrahend->tv_nsec;
        diff->tv_sec = minuend->tv_sec - subtrahend->tv_sec - (borrow_amt ? 1 : 0);
 }
+
+/* Declared in parlib/timing.h */
+
+uint64_t epoch_nsec_to_tsc(uint64_t epoch_ns)
+{
+       return nsec2tsc(epoch_ns - __proc_global_info.walltime_ns_last) +
+              __proc_global_info.tsc_cycles_last;
+}
+
+uint64_t tsc_to_epoch_nsec(uint64_t tsc)
+{
+       return tsc2nsec(tsc - __proc_global_info.tsc_cycles_last) +
+              __proc_global_info.walltime_ns_last;
+}
+
+uint64_t epoch_nsec(void)
+{
+       /* in case we get called before the constructor.  it's a little racy, but
+        * this all happens when we're single threaded.  for those curious, this
+        * seems to happen a lot due to the proliferation of gettimeofday calls. */
+       return tsc_to_epoch_nsec(read_tsc());
+}