[PATCH] libosmocore[master]: NS: use monotonic clock

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Max gerrit-no-reply at lists.osmocom.org
Wed Nov 29 16:52:00 UTC 2017


Review at  https://gerrit.osmocom.org/5104

NS: use monotonic clock

Use monotonic clock to compute elapsed time to make sure it's not affected
by system clock changes.

Change-Id: Ib6949601a80747f0de1a05e0790a1bace209efbf
Related: OS#2586
---
M TODO-RELEASE
M include/osmocom/gprs/gprs_ns.h
M src/gb/gprs_ns.c
3 files changed, 10 insertions(+), 13 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/04/5104/1

diff --git a/TODO-RELEASE b/TODO-RELEASE
index 5861956..804c2dd 100644
--- a/TODO-RELEASE
+++ b/TODO-RELEASE
@@ -9,3 +9,4 @@
 #library	what			description / commit summary line
 core		msgb_queue_free()	add inline func to msgb.h
 core		osmo_time_elapsed()	add function to estimate elapsed time
+gb		struct gprs_nsvc	add t_start parameter
diff --git a/include/osmocom/gprs/gprs_ns.h b/include/osmocom/gprs/gprs_ns.h
index b368ad9..a303732 100644
--- a/include/osmocom/gprs/gprs_ns.h
+++ b/include/osmocom/gprs/gprs_ns.h
@@ -127,7 +127,8 @@
 
 	struct osmo_timer_list timer;
 	enum nsvc_timer_mode timer_mode;
-	struct timeval timer_started;
+	struct timeval timer_started; /* deprecated! will be replaced by t_start eventually */
+	struct timespec t_start;
 	int alive_retries;
 
 	unsigned int remote_end_is_sgsn:1;
diff --git a/src/gb/gprs_ns.c b/src/gb/gprs_ns.c
index 47d170d..5a0aae3 100644
--- a/src/gb/gprs_ns.c
+++ b/src/gb/gprs_ns.c
@@ -66,7 +66,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <stdint.h>
-
+#include <time.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <arpa/inet.h>
@@ -612,18 +612,13 @@
 	if (osmo_timer_pending(&nsvc->timer))
 		osmo_timer_del(&nsvc->timer);
 
-	osmo_gettimeofday(&nsvc->timer_started, NULL);
+	if (clock_gettime(CLOCK_MONOTONIC, &nsvc->t_start) != 0) {
+		LOGP(DNS, LOGL_ERROR, "NSEI=%u failed to start timer in mode %s (%u seconds): %s\n",
+		     nsvc->nsei, get_value_string(timer_mode_strs, mode), seconds, strerror(errno));
+	}
+
 	nsvc->timer_mode = mode;
 	osmo_timer_schedule(&nsvc->timer, seconds, 0);
-}
-
-static int nsvc_timer_elapsed_ms(struct gprs_nsvc *nsvc)
-{
-	struct timeval now, elapsed;
-	osmo_gettimeofday(&now, NULL);
-	timersub(&now, &nsvc->timer_started, &elapsed);
-
-	return 1000 * elapsed.tv_sec + elapsed.tv_usec / 1000;
 }
 
 static void gprs_ns_timer_cb(void *data)
@@ -1387,7 +1382,7 @@
 	case NS_PDUT_ALIVE_ACK:
 		if ((*nsvc)->timer_mode == NSVC_TIMER_TNS_ALIVE)
 			osmo_stat_item_set((*nsvc)->statg->items[NS_STAT_ALIVE_DELAY],
-				nsvc_timer_elapsed_ms(*nsvc));
+					   osmo_time_elapsed(&(*nsvc)->t_start, T_MILLIS));
 		/* stop Tns-alive and start Tns-test */
 		nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST);
 		if ((*nsvc)->remote_end_is_sgsn) {

-- 
To view, visit https://gerrit.osmocom.org/5104
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib6949601a80747f0de1a05e0790a1bace209efbf
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Max <msuraev at sysmocom.de>



More information about the gerrit-log mailing list