read monotonic clock with clock_gettime() instead of gettimeofday()

There have been test failures on the osmo-pcu Jenkins builders due
to apparent clock drift. Switch relevant code from gettimeofday()
to clock_gettime() with CLOCK_MONOTONIC to prevent time from going
backwards and causing negative time deltas in calculations.

Change-Id: I775d85d0d3ac740330879e588bdab6fce7f0b46c
Related: OS#3225
diff --git a/src/gprs_rlcmac_meas.cpp b/src/gprs_rlcmac_meas.cpp
index 41a7531..ac01f2a 100644
--- a/src/gprs_rlcmac_meas.cpp
+++ b/src/gprs_rlcmac_meas.cpp
@@ -71,22 +71,22 @@
 /* RSSI values received from MS */
 int gprs_rlcmac_rssi(struct gprs_rlcmac_tbf *tbf, int8_t rssi)
 {
-	struct timeval now_tv, *rssi_tv = &tbf->meas.rssi_tv;
+	struct timespec now_tv, *rssi_tv = &tbf->meas.rssi_tv;
 	uint32_t elapsed;
 
 	tbf->meas.rssi_sum += rssi;
 	tbf->meas.rssi_num++;
 
-	gettimeofday(&now_tv, NULL);
+	osmo_clock_gettime(CLOCK_MONOTONIC, &now_tv);
 	elapsed = ((now_tv.tv_sec - rssi_tv->tv_sec) << 7)
-		+ ((now_tv.tv_usec - rssi_tv->tv_usec) << 7) / 1000000;
+		+ (((now_tv.tv_nsec - rssi_tv->tv_nsec)/1000) << 7) / 1000000;
 	if (elapsed < 128)
 		return 0;
 
 	gprs_rlcmac_rssi_rep(tbf);
 
 	/* reset rssi values and timestamp */
-	memcpy(rssi_tv, &now_tv, sizeof(struct timeval));
+	memcpy(rssi_tv, &now_tv, sizeof(*rssi_tv));
 	tbf->meas.rssi_sum = 0;
 	tbf->meas.rssi_num = 0;
 
@@ -115,7 +115,7 @@
 int gprs_rlcmac_received_lost(struct gprs_rlcmac_dl_tbf *tbf, uint16_t received,
 	uint16_t lost)
 {
-	struct timeval now_tv, *loss_tv = &tbf->m_bw.dl_loss_tv;
+	struct timespec now_tv, *loss_tv = &tbf->m_bw.dl_loss_tv;
 	uint32_t elapsed;
 	uint16_t sum = received + lost;
 
@@ -129,16 +129,16 @@
 	tbf->m_bw.dl_loss_received += received;
 	tbf->m_bw.dl_loss_lost += lost;
 
-	gettimeofday(&now_tv, NULL);
+	osmo_clock_gettime(CLOCK_MONOTONIC, &now_tv);
 	elapsed = ((now_tv.tv_sec - loss_tv->tv_sec) << 7)
-		+ ((now_tv.tv_usec - loss_tv->tv_usec) << 7) / 1000000;
+		+ (((now_tv.tv_nsec - loss_tv->tv_nsec)/1000) << 7) / 1000000;
 	if (elapsed < 128)
 		return 0;
 
 	gprs_rlcmac_lost_rep(tbf);
 
 	/* reset lost values and timestamp */
-	memcpy(loss_tv, &now_tv, sizeof(struct timeval));
+	memcpy(loss_tv, &now_tv, sizeof(*loss_tv));
 	tbf->m_bw.dl_loss_received = 0;
 	tbf->m_bw.dl_loss_lost = 0;
 
@@ -168,14 +168,14 @@
 
 int gprs_rlcmac_dl_bw(struct gprs_rlcmac_dl_tbf *tbf, uint16_t octets)
 {
-	struct timeval now_tv, *bw_tv = &tbf->m_bw.dl_bw_tv;
+	struct timespec now_tv, *bw_tv = &tbf->m_bw.dl_bw_tv;
 	uint32_t elapsed;
 
 	tbf->m_bw.dl_bw_octets += octets;
 
-	gettimeofday(&now_tv, NULL);
+	osmo_clock_gettime(CLOCK_MONOTONIC, &now_tv);
 	elapsed = ((now_tv.tv_sec - bw_tv->tv_sec) << 7)
-		+ ((now_tv.tv_usec - bw_tv->tv_usec) << 7) / 1000000;
+		+ (((now_tv.tv_nsec - bw_tv->tv_nsec)/1000) << 7) / 1000000;
 	if (elapsed < 128)
 		return 0;
 
@@ -186,7 +186,7 @@
 		tbf->m_bw.dl_bw_octets / elapsed);
 
 	/* reset bandwidth values timestamp */
-	memcpy(bw_tv, &now_tv, sizeof(struct timeval));
+	memcpy(bw_tv, &now_tv, sizeof(*bw_tv));
 	tbf->m_bw.dl_bw_octets = 0;
 
 	return 0;
diff --git a/src/tbf.cpp b/src/tbf.cpp
index 14c1ee2..76137df 100644
--- a/src/tbf.cpp
+++ b/src/tbf.cpp
@@ -40,6 +40,7 @@
 #include <osmocom/core/talloc.h>
 #include <osmocom/core/stats.h>
 #include <osmocom/core/logging.h>
+#include <osmocom/core/timer_compat.h>
 	#include <osmocom/core/bitvec.h>
 	#include <osmocom/core/rate_ctr.h>
 	#include <osmocom/gsm/protocol/gsm_04_08.h>
@@ -182,7 +183,7 @@
 	rssi_sum(0),
 	rssi_num(0)
 {
-	timerclear(&rssi_tv);
+	timespecclear(&rssi_tv);
 }
 
 gprs_rlcmac_tbf::gprs_rlcmac_tbf(BTS *bts_, gprs_rlcmac_tbf_direction dir) :
@@ -913,7 +914,7 @@
 	}
 
 	/* set timestamp */
-	gettimeofday(&tbf->meas.rssi_tv, NULL);
+	osmo_clock_gettime(CLOCK_MONOTONIC, &tbf->meas.rssi_tv);
 
 	tbf->set_ms(ms);
 
@@ -1024,8 +1025,8 @@
 	dl_loss_lost(0),
 	dl_loss_received(0)
 {
-	timerclear(&dl_bw_tv);
-	timerclear(&dl_loss_tv);
+	timespecclear(&dl_bw_tv);
+	timespecclear(&dl_loss_tv);
 }
 
 gprs_rlcmac_dl_tbf::gprs_rlcmac_dl_tbf(BTS *bts_) :
@@ -1114,8 +1115,8 @@
 	tbf->m_last_dl_poll_fn = -1;
 	tbf->m_last_dl_drained_fn = -1;
 
-	gettimeofday(&tbf->m_bw.dl_bw_tv, NULL);
-	gettimeofday(&tbf->m_bw.dl_loss_tv, NULL);
+	osmo_clock_gettime(CLOCK_MONOTONIC, &tbf->m_bw.dl_bw_tv);
+	osmo_clock_gettime(CLOCK_MONOTONIC, &tbf->m_bw.dl_loss_tv);
 
 	return tbf;
 }
diff --git a/src/tbf.h b/src/tbf.h
index 803b294..dc0b050 100644
--- a/src/tbf.h
+++ b/src/tbf.h
@@ -318,7 +318,7 @@
 	unsigned int num_fT_exp; /* number of consecutive fT expirations */
 
 	struct Meas {
-		struct timeval rssi_tv; /* timestamp for rssi calculation */
+		struct timespec rssi_tv; /* timestamp for rssi calculation */
 		int32_t rssi_sum; /* sum of rssi values */
 		int rssi_num; /* number of rssi values added since rssi_tv */
 
@@ -665,11 +665,11 @@
 	int32_t m_last_dl_drained_fn;
 
 	struct BandWidth {
-		struct timeval dl_bw_tv; /* timestamp for dl bw calculation */
+		struct timespec dl_bw_tv; /* timestamp for dl bw calculation */
 		uint32_t dl_bw_octets; /* number of octets since bw_tv */
 		uint32_t dl_throughput; /* throughput to be displayed in stats */
 
-		struct timeval dl_loss_tv; /* timestamp for loss calculation */
+		struct timespec dl_loss_tv; /* timestamp for loss calculation */
 		uint16_t dl_loss_lost; /* sum of lost packets */
 		uint16_t dl_loss_received; /* sum of received packets */