You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "michallenc (via GitHub)" <gi...@apache.org> on 2026/04/28 09:42:59 UTC
[PR] clock_settime: don't block when up_rtc_settime is call [nuttx]
michallenc opened a new pull request, #18816:
URL: https://github.com/apache/nuttx/pull/18816
## Summary
Setting the current time in RTC may be a blocking operation (driver needs to wait for oscillator stabilization after reset and so on). This may cause the unwanted effect of `clock_settime` blocking the code execution for a considerable amount of time.
The solution is to plan a low priority work that takes care of setting the time in RTC and let `clock_settime` continue. We don't have to check if the work is available, just cancel it if there is a new time set request.
This is used only if `CONFIG_SCHED_LPWORK` is enabled. I was thinking about putting this under a special separate config option, but it seems like something you always want if you have LPWORK - but feel free to suggest otherwise, I am not against creating an option solely for this.
## Impact
`clock_settime` now shouldn't block the code execution.
## Testing
We encountered this with `mcp794xx` RTC - the controller waits until the oscillator is started, which may take tens of milliseconds (we measured up to ~40 ms on oscilloscope delay). This caused issue in a customer's custom protocol implementation - we receive the current time, set it and response with status message - but there were timeouts in the communication if time was set.
The issue no longer occurs if RTC set time is done in a separate work thread as `clock_settime` doesn't block.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] clock_settime: don't block when up_rtc_settime is called [nuttx]
Posted by "acassis (via GitHub)" <gi...@apache.org>.
acassis commented on code in PR #18816:
URL: https://github.com/apache/nuttx/pull/18816#discussion_r3154467959
##########
sched/clock/clock_settime.c:
##########
@@ -44,10 +44,22 @@
# include "clock/clock_timekeeping.h"
#endif
+#if defined(CONFIG_RTC) && defined(CONFIG_SCHED_LPWORK)
+static struct work_s rtc_work;
+static struct timespec rtc_to_set;
Review Comment:
Please add "g_" on these variables to indicate they are global variables.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] clock_settime: don't block when up_rtc_settime is called [nuttx]
Posted by "michallenc (via GitHub)" <gi...@apache.org>.
michallenc commented on code in PR #18816:
URL: https://github.com/apache/nuttx/pull/18816#discussion_r3154974546
##########
sched/clock/clock_settime.c:
##########
@@ -44,10 +44,22 @@
# include "clock/clock_timekeeping.h"
#endif
+#if defined(CONFIG_RTC) && defined(CONFIG_SCHED_LPWORK)
+static struct work_s rtc_work;
+static struct timespec rtc_to_set;
Review Comment:
You are right, changed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] clock_settime: don't block when up_rtc_settime is called [nuttx]
Posted by "linguini1 (via GitHub)" <gi...@apache.org>.
linguini1 merged PR #18816:
URL: https://github.com/apache/nuttx/pull/18816
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] clock_settime: don't block when up_rtc_settime is called [nuttx]
Posted by "linguini1 (via GitHub)" <gi...@apache.org>.
linguini1 commented on PR #18816:
URL: https://github.com/apache/nuttx/pull/18816#issuecomment-4337000760
> You still get the correct time after the set, because the read with `clock_gettime` is performed from system clock, not from RTC. The user won't have the confirmation the write to RTC was done, but he can't rely on it now neither as we don't handle possible error from `up_rtc_settime` - we can't, it would violate POSIX interface of `clock_settime`. So even now successful `clock_settime` doesn't mean RTC was written.
>
> The entire RTC clock setting is unfortunately not standardized and platform specific. Linux for example has a specific IOCTL or you can configure periodic writes to RTC if I remember it correctly, but it doesn't change it on `clock_settime`.
>
> It can be fixed at the application level, but that is fixing the kernel problem from the app instead of fixing it in the kernel. And I think this is kernel issue because `clock_settime` shouldn't block. I am ok with adding new config option that would not make the proposed behavior default but only if user selects it if you prefer.
This explanation makes a lot of sense, I think that fixing in the kernel and having it as default behavior is fine!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] clock_settime: don't block when up_rtc_settime is called [nuttx]
Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #18816:
URL: https://github.com/apache/nuttx/pull/18816#discussion_r3161953866
##########
sched/clock/clock_settime.c:
##########
@@ -82,7 +94,24 @@ static void nxclock_set_realtime(FAR const struct timespec *tp)
# ifdef CONFIG_RTC
if (g_rtc_enabled)
{
+# ifdef CONFIG_SCHED_LPWORK
+ /* Setting the current time in RTC may be a blocking operation (driver
+ * needs to wait for oscillator stabilization after reset and so on).
+ * This may cause the unwanted effect of clock_settime blocking the
+ * code execution for a considerable amount of time.
+ *
+ * The solution is to plan a low priority work that takes care
+ * of setting the time in RTC and let clock_settime continue. We don't
+ * have to check if the work is available, just cancel it if there
+ * is a new time set request.
+ */
+
+ g_rtc_to_set = *tp;
+ work_queue(LPWORK, &g_rtc_work, rtc_worker,
Review Comment:
> True, but blocking for 30 ms is also not correct. The user can choose higher priority for lp work if the system is really busy and preempt other not so important tasks. Most RTCs store time in seconds, so the write would have to be significantly delayed to show any time drift.
it's depends on hardware, nuttx even has the code to support the high res RTC(CONFIG_RTC_HIRES).
>
> I don't really see other good solution here, except from removing RTC set from `clock_settime` and having a separate API like Linux.
you can update RTC through /dev/rtcx.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] clock_settime: don't block when up_rtc_settime is called [nuttx]
Posted by "michallenc (via GitHub)" <gi...@apache.org>.
michallenc commented on PR #18816:
URL: https://github.com/apache/nuttx/pull/18816#issuecomment-4336766825
> Seems fine! Would this cause issues with getting the clock time after the set though? Where the caller might expect the time to be set correctly once the caller returns?
>
> Can this issue be fixed by performing the call in a worker thread at the application level, instead of in the kernel?
You still get the correct time after the set, because the read with `clock_gettime` is performed from system clock, not from RTC. The user won't have the confirmation the write to RTC was done, but he can't rely on it now neither as we don't handle possible error from `up_rtc_settime` - we can't, it would violate POSIX interface of `clock_settime`. So even now successful `clock_settime` doesn't mean RTC was written.
The entire RTC clock setting is unfortunately not standardized and platform specific. Linux for example has a specific IOCTL or you can configure periodic writes to RTC if I remember it correctly, but it doesn't change it on `clock_settime`.
It can be fixed at the application level, but that is fixing the kernel problem from the app instead of fixing it in the kernel. And I think this is kernel issue because `clock_settime` shouldn't block. I am ok with adding new config option that would not make the proposed behavior default but only if user selects it if you prefer.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] clock_settime: don't block when up_rtc_settime is called [nuttx]
Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #18816:
URL: https://github.com/apache/nuttx/pull/18816#discussion_r3159098785
##########
sched/clock/clock_settime.c:
##########
@@ -82,7 +94,24 @@ static void nxclock_set_realtime(FAR const struct timespec *tp)
# ifdef CONFIG_RTC
if (g_rtc_enabled)
{
+# ifdef CONFIG_SCHED_LPWORK
+ /* Setting the current time in RTC may be a blocking operation (driver
+ * needs to wait for oscillator stabilization after reset and so on).
+ * This may cause the unwanted effect of clock_settime blocking the
+ * code execution for a considerable amount of time.
+ *
+ * The solution is to plan a low priority work that takes care
+ * of setting the time in RTC and let clock_settime continue. We don't
+ * have to check if the work is available, just cancel it if there
+ * is a new time set request.
+ */
+
+ g_rtc_to_set = *tp;
+ work_queue(LPWORK, &g_rtc_work, rtc_worker,
Review Comment:
but in a busy system, lp work mayn't schedule for while, how to handle the time drift. @michallenc
if caller really care the blocking, it's better let caller call clock_settime in the background thread.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] clock_settime: don't block when up_rtc_settime is called [nuttx]
Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #18816:
URL: https://github.com/apache/nuttx/pull/18816#discussion_r3167563971
##########
sched/clock/clock_settime.c:
##########
@@ -82,7 +94,24 @@ static void nxclock_set_realtime(FAR const struct timespec *tp)
# ifdef CONFIG_RTC
if (g_rtc_enabled)
{
+# ifdef CONFIG_SCHED_LPWORK
+ /* Setting the current time in RTC may be a blocking operation (driver
+ * needs to wait for oscillator stabilization after reset and so on).
+ * This may cause the unwanted effect of clock_settime blocking the
+ * code execution for a considerable amount of time.
+ *
+ * The solution is to plan a low priority work that takes care
+ * of setting the time in RTC and let clock_settime continue. We don't
+ * have to check if the work is available, just cancel it if there
+ * is a new time set request.
+ */
+
+ g_rtc_to_set = *tp;
+ work_queue(LPWORK, &g_rtc_work, rtc_worker,
Review Comment:
> > you can update RTC through /dev/rtcx.
>
> That's what I would prefer in a long term, but that would mean removing `up_rtc_settime` from `clock_settime`, which would break many applications that rely on this behavior.
> And we would have to rewrite some RTC drivers to actually register a device driver.
you can utilize arch_rtc which could simplify the code a lot:
https://github.com/apache/nuttx/blob/master/drivers/timers/arch_rtc.c
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] clock_settime: don't block when up_rtc_settime is called [nuttx]
Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #18816:
URL: https://github.com/apache/nuttx/pull/18816#discussion_r3161953866
##########
sched/clock/clock_settime.c:
##########
@@ -82,7 +94,24 @@ static void nxclock_set_realtime(FAR const struct timespec *tp)
# ifdef CONFIG_RTC
if (g_rtc_enabled)
{
+# ifdef CONFIG_SCHED_LPWORK
+ /* Setting the current time in RTC may be a blocking operation (driver
+ * needs to wait for oscillator stabilization after reset and so on).
+ * This may cause the unwanted effect of clock_settime blocking the
+ * code execution for a considerable amount of time.
+ *
+ * The solution is to plan a low priority work that takes care
+ * of setting the time in RTC and let clock_settime continue. We don't
+ * have to check if the work is available, just cancel it if there
+ * is a new time set request.
+ */
+
+ g_rtc_to_set = *tp;
+ work_queue(LPWORK, &g_rtc_work, rtc_worker,
Review Comment:
> True, but blocking for 30 ms is also not correct. The user can choose higher priority for lp work if the system is really busy and preempt other not so important tasks. Most RTCs store time in seconds, so the write would have to be significantly delayed to show any time drift.
it's depends on hardware, nuttx even has the code to support the high res RTC(CONFIG_RTC_HIRES).
>
> I don't really see other good solution here, except from removing RTC set from `clock_settime` and having a separate API like Linux.
you can update RTC through /dev/rtcx.
BT, timespec has two fields, the second clock_settime may corrupt the setting in the work thread.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] clock_settime: don't block when up_rtc_settime is called [nuttx]
Posted by "michallenc (via GitHub)" <gi...@apache.org>.
michallenc commented on code in PR #18816:
URL: https://github.com/apache/nuttx/pull/18816#discussion_r3161122097
##########
sched/clock/clock_settime.c:
##########
@@ -82,7 +94,24 @@ static void nxclock_set_realtime(FAR const struct timespec *tp)
# ifdef CONFIG_RTC
if (g_rtc_enabled)
{
+# ifdef CONFIG_SCHED_LPWORK
+ /* Setting the current time in RTC may be a blocking operation (driver
+ * needs to wait for oscillator stabilization after reset and so on).
+ * This may cause the unwanted effect of clock_settime blocking the
+ * code execution for a considerable amount of time.
+ *
+ * The solution is to plan a low priority work that takes care
+ * of setting the time in RTC and let clock_settime continue. We don't
+ * have to check if the work is available, just cancel it if there
+ * is a new time set request.
+ */
+
+ g_rtc_to_set = *tp;
+ work_queue(LPWORK, &g_rtc_work, rtc_worker,
Review Comment:
True, but blocking for 30 ms is also not correct. The user can choose higher priority for lp work if the system is really busy and preempt other not so important tasks. Most RTCs store time in seconds, so the write would have to be significantly delayed to show any time drift.
I don't really see other good solution here, except from removing RTC set from `clock_settime` and having a separate API like Linux.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] clock_settime: don't block when up_rtc_settime is called [nuttx]
Posted by "linguini1 (via GitHub)" <gi...@apache.org>.
linguini1 commented on PR #18816:
URL: https://github.com/apache/nuttx/pull/18816#issuecomment-4336610539
Seems fine! Would this cause issues with getting the clock time after the set though? Where the caller might expect the time to be set correctly once the caller returns?
Can this issue be fixed by performing the call in a worker thread at the application level, instead of in the kernel?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] clock_settime: don't block when up_rtc_settime is called [nuttx]
Posted by "michallenc (via GitHub)" <gi...@apache.org>.
michallenc commented on code in PR #18816:
URL: https://github.com/apache/nuttx/pull/18816#discussion_r3166051540
##########
sched/clock/clock_settime.c:
##########
@@ -82,7 +94,24 @@ static void nxclock_set_realtime(FAR const struct timespec *tp)
# ifdef CONFIG_RTC
if (g_rtc_enabled)
{
+# ifdef CONFIG_SCHED_LPWORK
+ /* Setting the current time in RTC may be a blocking operation (driver
+ * needs to wait for oscillator stabilization after reset and so on).
+ * This may cause the unwanted effect of clock_settime blocking the
+ * code execution for a considerable amount of time.
+ *
+ * The solution is to plan a low priority work that takes care
+ * of setting the time in RTC and let clock_settime continue. We don't
+ * have to check if the work is available, just cancel it if there
+ * is a new time set request.
+ */
+
+ g_rtc_to_set = *tp;
+ work_queue(LPWORK, &g_rtc_work, rtc_worker,
Review Comment:
> you can update RTC through /dev/rtcx.
That's what I would prefer in a long term, but that would mean removing `up_rtc_settime` from `clock_settime`, which would break many applications that rely on this behavior. And we would have to rewrite some RTC drivers to actually register a device driver.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org