From 7695224af25c8e153f8912fea4763c2e76cb1920 Mon Sep 17 00:00:00 2001 From: Erling Rennemo Jellum Date: Sat, 7 Oct 2023 16:33:12 +0200 Subject: [PATCH 1/6] Cast timespec fields to 64bit before multiplying by a billion to avoid overflows on 32bit archs --- core/platform/lf_unix_clock_support.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/platform/lf_unix_clock_support.c b/core/platform/lf_unix_clock_support.c index 2ab20546a..d5c032862 100644 --- a/core/platform/lf_unix_clock_support.c +++ b/core/platform/lf_unix_clock_support.c @@ -17,13 +17,13 @@ interval_t _lf_time_epoch_offset = 0LL; instant_t convert_timespec_to_ns(struct timespec tp) { - return tp.tv_sec * 1000000000 + tp.tv_nsec; + return ((instant_t) tp.tv_sec) * BILLION + tp.tv_nsec; } struct timespec convert_ns_to_timespec(instant_t t) { struct timespec tp; - tp.tv_sec = t / 1000000000; - tp.tv_nsec = (t % 1000000000); + tp.tv_sec = t / BILLION; + tp.tv_nsec = (t % BILLION); return tp; } From fa094dba835b33507604e595ecc7211cd6a47c73 Mon Sep 17 00:00:00 2001 From: Erling Rennemo Jellum Date: Sat, 7 Oct 2023 16:38:55 +0200 Subject: [PATCH 2/6] Update lf-ref --- lingua-franca-ref.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lingua-franca-ref.txt b/lingua-franca-ref.txt index fe4328b6c..1f7391f92 100644 --- a/lingua-franca-ref.txt +++ b/lingua-franca-ref.txt @@ -1 +1 @@ -c-remove-deprecated-schedulers +master From b0a8e8269c4a4f191a4780239ef5cf7198482fb9 Mon Sep 17 00:00:00 2001 From: Erling Rennemo Jellum Date: Sat, 7 Oct 2023 16:42:28 +0200 Subject: [PATCH 3/6] Add some security checks regarding available clocks --- include/core/platform/lf_linux_support.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/include/core/platform/lf_linux_support.h b/include/core/platform/lf_linux_support.h index 828deb901..021b236db 100644 --- a/include/core/platform/lf_linux_support.h +++ b/include/core/platform/lf_linux_support.h @@ -34,6 +34,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include // For fixed-width integral types #include // For CLOCK_MONOTONIC +#include // _POSIX_TIMERS _POSIX_CLOCK_MONOTONIC // Use 64-bit times and 32-bit unsigned microsteps #include "lf_tag_64_32.h" @@ -47,7 +48,15 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endif #endif +#if !defined(_POSIX_TIMERS) || _POSIX_TIMERS <= 0 + #error Linux platform misses clock support +#endif + // The underlying physical clock for Linux -#define _LF_CLOCK CLOCK_MONOTONIC +#if defined(_POSIX_CLOCK_MONOTONIC) + #define _LF_CLOCK CLOCK_MONOTONIC +#else + #define _LF_CLOCK CLOCK_REALTIME +#endif #endif // LF_LINUX_SUPPORT_H From 0c777f9e1b528233cf2a5b6d334f17d1dbee9c8f Mon Sep 17 00:00:00 2001 From: Erling Rennemo Jellum Date: Sat, 7 Oct 2023 17:06:24 +0200 Subject: [PATCH 4/6] Print out system clock resolution at startup --- core/platform/lf_unix_clock_support.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/platform/lf_unix_clock_support.c b/core/platform/lf_unix_clock_support.c index d5c032862..92823d85b 100644 --- a/core/platform/lf_unix_clock_support.c +++ b/core/platform/lf_unix_clock_support.c @@ -50,6 +50,15 @@ void calculate_epoch_offset(void) { void _lf_initialize_clock() { calculate_epoch_offset(); + + struct timespec res; + // Adjust the clock by the epoch offset, so epoch time is always reported. + int return_value = clock_getres(_LF_CLOCK, (struct timespec*) &res); + if (return_value < 0) { + lf_print_error_and_exit("Could not obtain resolution for _LF_CLOCK"); + } + + lf_print("---- System clock resolution: %u nsec", res.tv_nsec); } /** From 6a105d182bfde8e202eab644f097899732db4ae5 Mon Sep 17 00:00:00 2001 From: Erling Rennemo Jellum Date: Sat, 7 Oct 2023 17:13:09 +0200 Subject: [PATCH 5/6] Missing include --- core/platform/lf_unix_clock_support.c | 1 + 1 file changed, 1 insertion(+) diff --git a/core/platform/lf_unix_clock_support.c b/core/platform/lf_unix_clock_support.c index 92823d85b..ebc6b1f7e 100644 --- a/core/platform/lf_unix_clock_support.c +++ b/core/platform/lf_unix_clock_support.c @@ -3,6 +3,7 @@ #include #include "platform.h" +#include "util.h" #include "lf_unix_clock_support.h" /** From 80eae2bf10f99c48ca942cf7e28a3c96d93014d0 Mon Sep 17 00:00:00 2001 From: erling Date: Sat, 7 Oct 2023 17:54:59 +0200 Subject: [PATCH 6/6] Update core/platform/lf_unix_clock_support.c --- core/platform/lf_unix_clock_support.c | 1 - 1 file changed, 1 deletion(-) diff --git a/core/platform/lf_unix_clock_support.c b/core/platform/lf_unix_clock_support.c index ebc6b1f7e..e20cb0b8f 100644 --- a/core/platform/lf_unix_clock_support.c +++ b/core/platform/lf_unix_clock_support.c @@ -53,7 +53,6 @@ void _lf_initialize_clock() { calculate_epoch_offset(); struct timespec res; - // Adjust the clock by the epoch offset, so epoch time is always reported. int return_value = clock_getres(_LF_CLOCK, (struct timespec*) &res); if (return_value < 0) { lf_print_error_and_exit("Could not obtain resolution for _LF_CLOCK");