[PATCH] libosmocore[master]: Add function to estimate elapsed time

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/5103

Add function to estimate elapsed time

It uses monotonic clock for proper time estimation.

Change-Id: I83d865ff633a7ebda2c943477205fd31aceda277
Related: OS#2586
---
M TODO-RELEASE
M include/osmocom/core/timer.h
M src/timer.c
3 files changed, 62 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/03/5103/1

diff --git a/TODO-RELEASE b/TODO-RELEASE
index 99865c6..5861956 100644
--- a/TODO-RELEASE
+++ b/TODO-RELEASE
@@ -8,3 +8,4 @@
 # If any interfaces have been removed or changed since the last public release: c:r:0.
 #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
diff --git a/include/osmocom/core/timer.h b/include/osmocom/core/timer.h
index 4958efb..612cdd3 100644
--- a/include/osmocom/core/timer.h
+++ b/include/osmocom/core/timer.h
@@ -50,6 +50,19 @@
 #define OSMO_SEC2HRS(sec) ((sec % (60 * 60 * 24)) / (60 * 60))
 #define OSMO_SEC2DAY(sec) ((sec % (60 * 60 * 24 * 365)) / (60 * 60 * 24)) /* we ignore leap year for simplicity */
 
+#define OSMO_SEC2MS(sec) (sec * 1000)
+#define OSMO_SEC2CS(sec) (sec * 100)
+
+#define OSMO_NSEC2MS(ns) (ns / 1000000)
+#define OSMO_NSEC2CS(ns) (ns / 10000000)
+
+enum osmo_elapsed {
+	T_SECS,
+	T_MILLIS,
+	T_CENTIS,
+	T_TEST,
+};
+
 /*! A structure representing a single instance of a timer */
 struct osmo_timer_list {
 	struct rb_node node;	  /*!< rb-tree node header */
@@ -61,6 +74,8 @@
 	void *data;		  /*!< user data for callback */
 };
 
+time_t osmo_time_elapsed(const struct timespec *from, enum osmo_elapsed kind);
+
 /*
  * timer management
  */
diff --git a/src/timer.c b/src/timer.c
index 9ec7a00..46e80ce 100644
--- a/src/timer.c
+++ b/src/timer.c
@@ -35,6 +35,10 @@
 #include <assert.h>
 #include <string.h>
 #include <limits.h>
+#include <errno.h>
+#include <time.h>
+
+#include <osmocom/core/logging.h>
 #include <osmocom/core/timer.h>
 #include <osmocom/core/timer_compat.h>
 #include <osmocom/core/linuxlist.h>
@@ -168,6 +172,48 @@
 	return 0;
 }
 
+/* isolated nanoseconds clock difference */
+static inline long t_diff_nsec(const struct timespec *from, const struct timespec *to)
+{
+	return from->tv_nsec - to->tv_nsec;
+}
+
+/* isolated seconds clock difference */
+static inline time_t t_diff_sec(const struct timespec *from, const struct timespec *to)
+{
+	return from->tv_sec - to->tv_sec;
+}
+
+time_t osmo_time_elapsed(const struct timespec *from, enum osmo_elapsed kind)
+{
+	struct timespec t_now;
+	long ns;
+	time_t sec;
+
+	if (clock_gettime(CLOCK_MONOTONIC, &t_now) != 0) {
+		LOGP(DLGLOBAL, LOGL_ERROR, "Failed to get time for elapsed computation: %s\n", strerror(errno));
+		return 0;
+	}
+
+	ns = t_diff_nsec(from, &t_now);
+	sec = t_diff_sec(from, &t_now);
+
+	switch (kind) {
+	case T_SECS:
+		return sec;
+	case T_MILLIS:
+		return OSMO_SEC2MS(sec) + OSMO_NSEC2MS(ns);
+	case T_CENTIS:
+		return OSMO_SEC2CS(sec) + OSMO_NSEC2CS(ns);
+	case T_TEST:
+		return 1486385000 + 423423;
+	default:
+		LOGP(DLGLOBAL, LOGL_ERROR, "Unhandled kind in elapsed time computation: %u\n", kind);	
+	}
+
+	return 0;
+}
+
 /*! Determine time between now and the nearest timer
  *  \returns pointer to timeval of nearest timer, NULL if there is none
  *

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

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



More information about the gerrit-log mailing list