Hello Jenkins Builder, I'd like you to reexamine a change. Please visit
https://gerrit.osmocom.org/5103 to look at the new patch set (#4). 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, 70 insertions(+), 0 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/03/5103/4 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..7df01ce 100644 --- a/include/osmocom/core/timer.h +++ b/include/osmocom/core/timer.h @@ -50,6 +50,18 @@ #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, +}; + /*! A structure representing a single instance of a timer */ struct osmo_timer_list { struct rb_node node; /*!< rb-tree node header */ @@ -61,6 +73,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..2b4e100 100644 --- a/src/timer.c +++ b/src/timer.c @@ -25,6 +25,7 @@ * */ +#include "config.h" /*! \addtogroup timer * @{ @@ -35,6 +36,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 +173,56 @@ return 0; } +#if !defined(EMBEDDED) + +/* 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; +} + +/*! Determine the time difference between now and the given time point + * \returns time difference + * + * \param[in] from timespec of a reference time point + * \param[in] kind expected return value type: milliseconds, centiseconds etc. + */ +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); + default: + LOGP(DLGLOBAL, LOGL_ERROR, "Unhandled kind in elapsed time computation: %u\n", kind); + } + + return 0; +} + +#endif /* !EMBEDDED */ + /*! 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: newpatchset Gerrit-Change-Id: I83d865ff633a7ebda2c943477205fd31aceda277 Gerrit-PatchSet: 4 Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Owner: Max <msur...@sysmocom.de> Gerrit-Reviewer: Harald Welte <lafo...@gnumonks.org> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: Vadim Yanitskiy <axilira...@gmail.com>