This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 72bbca1c2bdae3d88e9c31e98a8dd791d7256d78 Author: dongjiuzhu1 <[email protected]> AuthorDate: Fri Nov 29 12:08:49 2024 +0800 sched/clock: support sync monotonic clock with remote server Let CLOCK_MONOTONIC carry an offset that a remote time server can step: - new g_monotonic_basetime, guarded by g_basetime_lock, is added to the tick time in the CLOCK_MONOTONIC path of nxclock_gettime() - new clock_synchronize_monotonic() steps that offset from the server side (not available under CONFIG_CLOCK_TIMEKEEPING) - build the CLOCK_REALTIME result in place instead of through a scratch timespec Signed-off-by: dongjiuzhu1 <[email protected]> --- include/nuttx/clock.h | 20 ++++++++++++++++++++ sched/clock/clock.h | 1 + sched/clock/clock_gettime.c | 15 ++++++++++++--- sched/clock/clock_initialize.c | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 3 deletions(-) diff --git a/include/nuttx/clock.h b/include/nuttx/clock.h index dcf574b6a92..e442c139405 100644 --- a/include/nuttx/clock.h +++ b/include/nuttx/clock.h @@ -655,6 +655,26 @@ time_t clock_calendar2utc(int year, int month, int day); void clock_synchronize(FAR const struct timespec *tp); #endif +/**************************************************************************** + * Name: clock_synchronize_monotonic + * + * Description: + * Synchronize the system boottime with remote hardware server. + * + * Input Parameters: + * tp: boot time should be synced. + * + * Returned Value: + * None + * + * Assumptions: + * + ****************************************************************************/ + +#ifndef CONFIG_CLOCK_TIMEKEEPING +void clock_synchronize_monotonic(FAR const struct timespec *tp); +#endif + /**************************************************************************** * Name: clock_resynchronize * diff --git a/sched/clock/clock.h b/sched/clock/clock.h index 003c9264546..a852366433f 100644 --- a/sched/clock/clock.h +++ b/sched/clock/clock.h @@ -41,6 +41,7 @@ #ifndef CONFIG_CLOCK_TIMEKEEPING extern struct timespec g_basetime; +extern struct timespec g_monotonic_basetime; extern spinlock_t g_basetime_lock; #endif diff --git a/sched/clock/clock_gettime.c b/sched/clock/clock_gettime.c index 67bd3d43dd9..662b0077826 100644 --- a/sched/clock/clock_gettime.c +++ b/sched/clock/clock_gettime.c @@ -108,14 +108,23 @@ int nxclock_gettime(clockid_t clock_id, FAR struct timespec *tp) */ clock_systime_timespec(tp); + +#ifndef CONFIG_CLOCK_TIMEKEEPING + { + irqstate_t flags; + + flags = spin_lock_irqsave(&g_basetime_lock); + clock_timespec_add(&g_monotonic_basetime, tp, tp); + spin_unlock_irqrestore(&g_basetime_lock, flags); + } +#endif } else if (clock_id == CLOCK_REALTIME) { #ifndef CONFIG_CLOCK_TIMEKEEPING - struct timespec ts; irqstate_t flags; - clock_systime_timespec(&ts); + clock_systime_timespec(tp); /* Add the base time to this. The base time is the time-of-day * setting. When added to the elapsed time since the time-of-day @@ -123,7 +132,7 @@ int nxclock_gettime(clockid_t clock_id, FAR struct timespec *tp) */ flags = spin_lock_irqsave(&g_basetime_lock); - clock_timespec_add(&g_basetime, &ts, tp); + clock_timespec_add(&g_basetime, tp, tp); spin_unlock_irqrestore(&g_basetime_lock, flags); #else clock_timekeeping_get_wall_time(tp); diff --git a/sched/clock/clock_initialize.c b/sched/clock/clock_initialize.c index 117929a4177..474f13d6a67 100644 --- a/sched/clock/clock_initialize.c +++ b/sched/clock/clock_initialize.c @@ -54,6 +54,7 @@ #ifndef CONFIG_CLOCK_TIMEKEEPING struct timespec g_basetime; +struct timespec g_monotonic_basetime; spinlock_t g_basetime_lock = SP_UNLOCKED; #endif @@ -273,6 +274,45 @@ void clock_synchronize(FAR const struct timespec *tp) } #endif +/**************************************************************************** + * Name: clock_synchronize_monotonic + * + * Description: + * Synchronize the system boottime with remote hardware server. + * + * Input Parameters: + * tp: boot time should be synced. + * + * Returned Value: + * None + * + * Assumptions: + * + ****************************************************************************/ + +#ifndef CONFIG_CLOCK_TIMEKEEPING +void clock_synchronize_monotonic(FAR const struct timespec *tp) +{ + irqstate_t flags; + struct timespec ts; + + /* Re-initialize the time value to match the RTC */ + + flags = spin_lock_irqsave(&g_basetime_lock); + + clock_systime_timespec(&ts); + g_monotonic_basetime.tv_sec = tp->tv_sec - ts.tv_sec; + g_monotonic_basetime.tv_nsec = tp->tv_nsec - ts.tv_nsec; + if (g_monotonic_basetime.tv_nsec < 0) + { + g_monotonic_basetime.tv_sec -= 1; + g_monotonic_basetime.tv_nsec += NSEC_PER_SEC; + } + + spin_unlock_irqrestore(&g_basetime_lock, flags); +} +#endif + /**************************************************************************** * Name: clock_resynchronize *
