From ec81df3de82175159ba6c649ad3b29d45a4e3f18 Mon Sep 17 00:00:00 2001
From: Nadav Har'El <nyh@cloudius-systems.com>
Date: Sun, 26 Jan 2014 15:36:18 +0200
Subject: [PATCH] clock: Use new clock APIs in BSD time functions

Reimplement the BSD functions getmicrotime(9), getmicrouptime(9)
and variable "ticks", using the new clock APIs.

getmicrotime() returns the system time ("wall clock"), while getmicrouptime
and ticks return the time since boot.

I believe this is the correct implementation according to the FreeBSD
documentation, but our previous implementation didn't quite do this and
it also worked ;-) The previous implementation pretended, according to
getmicrouptime() and get_ticks(), that the system is up since 1970,
and yet the variable "time_uptime" (which FreeBSD has) is never updated,
and is fixed at 1 second :-)

Reviewed-by: Glauber Costa <glommer@cloudius-systems.com>
Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
---
 bsd/porting/netport1.cc | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/bsd/porting/netport1.cc b/bsd/porting/netport1.cc
index 38cefd695..4917d6f70 100644
--- a/bsd/porting/netport1.cc
+++ b/bsd/porting/netport1.cc
@@ -6,7 +6,6 @@
  */
 
 #include <osv/types.h>
-#include "drivers/clock.hh"
 #include <osv/sched.hh>
 #include <osv/mempool.hh>
 #include "bsd/sys/cddl/compat/opensolaris/sys/kcondvar.h"
@@ -25,7 +24,7 @@ int osv_curtid(void)
     return (sched::thread::current()->id());
 }
 
-void ntm2tv(u64 ntm, struct timeval *tvp)
+static void ntm2tv(u64 ntm, struct timeval *tvp)
 {
     u64 utm = ntm / 1000L;
 
@@ -35,20 +34,22 @@ void ntm2tv(u64 ntm, struct timeval *tvp)
 
 void getmicrotime(struct timeval *tvp)
 {
-    u64 ntm = clock::get()->time();
+    u64 ntm = std::chrono::duration_cast<std::chrono::nanoseconds>
+                (osv::clock::wall::now().time_since_epoch()).count();
     ntm2tv(ntm, tvp);
 }
 
 void getmicrouptime(struct timeval *tvp)
 {
-    /* FIXME: OSv - initialize time_uptime */
-    u64 ntm = clock::get()->time() - time_uptime;
+    u64 ntm = std::chrono::duration_cast<std::chrono::nanoseconds>
+                (osv::clock::uptime::now().time_since_epoch()).count();
     ntm2tv(ntm, tvp);
 }
 
 int get_ticks(void)
 {
-    u64 ntm = clock::get()->time();
+    u64 ntm = std::chrono::duration_cast<std::chrono::nanoseconds>
+                (osv::clock::uptime::now().time_since_epoch()).count();
     return (ns2ticks(ntm));
 }
 
-- 
GitLab