Commit graph

126 commits

Author SHA1 Message Date
Xiang Xiao
c47b1e2c5b !sys/types.h: change time_t and clock_t to int64_t to align with other OSes
POSIX leaves the signedness of time_t and clock_t unspecified, but
mainstream implementations (Linux glibc/musl, the BSDs, macOS, RTEMS,
Zephyr's POSIX layer, Windows _time64) expose time_t as signed 64-bit.
NuttX has historically used uint64_t only because it was tied to the
CONFIG_SYSTEM_TIME64 knob; with that gone, switch:

  time_t   : uint64_t  -> int64_t
  clock_t  : uint64_t  -> int64_t
  CLOCK_MAX: UINT64_MAX -> INT64_MAX

This lets (time_t)-1 sentinels, negative tick deltas, and host-side
headers behave as on every other POSIX system without source churn.

Headers updated:
  - include/sys/types.h, include/limits.h, include/nuttx/clock.h
  - include/nuttx/fs/hostfs.h (nuttx_time_t alias)
  - include/nuttx/{mqueue.h,wdog.h,wqueue.h,timers/clkcnt.h}

Because clock_t is now signed 64-bit, the NuttX-internal sclock_t
alias becomes redundant: every sclock_t/SCLOCK_MAX use is folded
back to clock_t/CLOCK_MAX (notably in sched/wdog, sched/mqueue,
sched/sched, sched/clock, sched/timer, libs/libc/time, fs/vfs and
the drivers/arch consumers below).

Tick/period constants (NSEC_PER_SEC, USEC_PER_SEC, MSEC_PER_SEC,
SEC_PER_MIN, ...) in include/nuttx/clock.h are retyped from "long"
literals to INT64_C(...) so that 64-bit arithmetic no longer
depends on the host's long width.

Strip now-redundant (time_t)/(clock_t)/(unsigned long) casts and
unsigned-only branches across the tree:
  - arch RTC / oneshot / tickless lowerhalfs:
      arm: cxd56xx, efm32, imxrt, lc823450, max326xx, sam34, sama5,
           samd5e5, samv7, stm32, stm32f7, stm32h7, stm32l4, stm32wb,
           xmc4
      mips: pic32mz       sparc: bm3803       x86_64: intel64
      risc-v/xtensa: espressif (esp_i2c[_slave], esp_rtc,
           esp32c3{_i2c,_rtc,_wifi_adapter}, esp32{,s2,s3}_*),
           mpfs_perf
  - drivers: audio/tone, input/aw86225, power/pm/{activity,
           stability}_governor, rpmsg/rpmsg_ping,
           timers/{ds3231,mcp794xx,pcf85263,rx8010},
           wireless/ieee80211/bcm43xxx, wireless/spirit/spirit_spi
  - core: fs/vfs/{fs_poll,fs_timerfd}, mm/iob/iob_alloc,
          libs/libc/{netdb/lib_dnscache,time/{lib_calendar2utc,
          lib_time}}, net/icmp/icmp_pmtu, net/icmpv6/icmpv6_pmtu,
          net/ipfrag, net/tcp/{tcp.h,tcp_timer},
          net/utils/net_snoop, net/mld/mld_query (drop the now-dead
          mld_mrc2mrd helper since signed math handles it directly),
          sched/clock/{clock,clock_initialize},
          sched/sched/{sched_profil,sched_setparam,sched_setscheduler},
          sched/pthread/pthread_create,
          sched/wdog/{wd_gettime,wd_start,wdog.h},
          sched/timer/timer_gettime, sched/mqueue/*

Flip the few in-tree printf format strings that assumed an
unsigned 64-bit tv_sec:
  * drivers/rpmsg/rpmsg_ping.c                       PRIu64 -> PRId64
  * arch/xtensa/src/esp32{,s2,s3}/esp32*_oneshot_lowerhalf.c
                                          PRIu32 (already wrong) -> PRId64

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2026-05-19 16:21:28 +08:00
Piyush Patle
0dccc8ba21 include/debug.h: Move to include/nuttx/debug.h
debug.h is a NuttX-specific, non-POSIX header. Placing it in the
top-level include/ directory creates naming conflicts with external
projects that define their own debug.h.
This commit moves the canonical header to include/nuttx/debug.h,
following the NuttX convention for non-POSIX/non-standard headers,
and updates all in-tree references.

A backward-compatibility shim is left at include/debug.h that
emits a deprecation #warning and re-includes <nuttx/debug.h>,
allowing out-of-tree code to continue building while migrating.

Signed-off-by: Piyush Patle <piyushpatle228@gmail.com>
2026-04-07 07:50:06 -03:00
ouyangxiangzhen
3eedf5f22b sched/wdog: Simplify timer expiration for hrtimer.
This commit simplified the timer expiration for hrtimer.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
wangchengdong
165f1cc063 sched/sched: enable OS tickless mode support using hrtimer
Add support for running the OS in tickless mode with hrtimer, allowing
the scheduler to operate without periodic ticks while maintaining
high-resolution timer functionality.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2026-01-27 19:35:31 +08:00
ouyangxiangzhen
702e1d95f3 sched/wdog: Simplify the wd_timer_start.
This commit simplified the wd_timer_start.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-22 19:31:28 +08:00
ouyangxiangzhen
4cbc56eec5 sched/wdog: Add wd_adjust_next_tick.
This commit added wd_adjust_next_tick to simplify the scheduler.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-22 19:31:28 +08:00
ouyangxiangzhen
c69cf36b7c sched/wdog: Simplify the wdog expiration handling.
This commit simplified the wdog expiration handling by introducing the
`g_sched_event` timer.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-10 17:30:25 +08:00
ouyangxiangzhen
7b47b9de17 sched/wdog: Simplify the wd_gettime.
This commit simplified the wd_gettime.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-10 17:30:25 +08:00
chao an
6300bc5522 sched/wdog: fix race condition in wd_gettime()
Move WDOG_ISACTIVE check inside critical section protection to avoid race condition

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-12-26 11:51:02 +08:00
ouyangxiangzhen
2dc2b30a6a sched/wdog: Simplify the code and correct the critical_section.
This commmit simplified the code and correct the critical_section.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
jiangtao16
c5814ac910 sched/wdog: compatible with MISRA-C
This commit fixed the MISRA C-2004 violation rule 11.1, 10.4 and more.

Signed-off-by: jiangtao16 <jiangtao16@xiaomi.com>
Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
ouyangxiangzhen
6b63a81f54 sched/wdog: Simplify wd_timer to reduce WCET.
This commit simplified wd_timer() to reduce WCET when `noswitches` is false.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
ouyangxiangzhen
43e0c50aa7 sched/wdog: Revert the spin-lock to big-kernel-lock (enter_critical_section).
If the wdog use the fine-grained spin-lock, and allow the callback execution without the lock held, there will be incorrect interleaving.
E.g. The first `nxsem_timeout` callback function caused the second semaphore wait to fail.

    Core 0 [nxsem_clockwait]       |  Core 1
    enter_critical_section()       |  ...
    wd_start(nxsem_timeout)        |  ...
    nxsem_wait(sem)                |  wd_expiration() --> nxsem_timeout
    wd_cancel(&rtcb->waitdog)      |  try enter_critical_section()
    leave_critical_section()       |  Failed retry...
    ....nxsem_clockwait            |  Failed retry...
    enter_critical_section()       |   Failed retry...
    wd_start(nxsem_timeout)        |  Failed retry...
    nxsem_wait(sem)                |  Core 1 enter the critical section
                                   |  nxsem_wait_irq(wtcb, ETIMEDOUT) -> incorrectly wake-up the rtcb.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
ouyangxiangzhen
164b075d3f sched/wdog: Remove workaround for wdog latency.
This commit removed the workaround for wdog latency.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
ouyangxiangzhen
476e02c7e0 sched/wdog: Faster wdog deleting.
This commit reduced 1 write operation for wdog deleting and decoupled
the WDOG_ISACTIVE with the list implementation.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
ouyangxiangzhen
694dfc7a0c sched/wdog: Remove wd_recover.
This commit Removed wd_recover.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-24 16:46:16 +08:00
ouyangxiangzhen
7d01d8aab5 sched/wdog: Move the g_wdtimernested to sched_timerexpiration.
This commit moved the g_wdtimernested to sched_timerexpiration, since
wdog and hrtimer can share it.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-22 21:17:13 +08:00
ouyangxiangzhen
5569755868 sched/sched: Change the return value semantics.
This commit changed the return value semantics.
In past designs, timers that were expected to fire immediately were delayed by at least one tick, resulting in a decrease in real-time performance.
This commit re-encoded the return value to allow the timers fire
immediately.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-12-22 21:17:13 +08:00
wangchengdong
71f53c6c74 sched/wdog: Inline wd_start() to improve performance
Move wd_start() to an inline function to reduce function call
    overhead and improve performance in time-critical watchdog operations.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-11-10 23:16:57 +08:00
wangchengdong
de0aead39f sched/wdog: Fix wd_start() boundary check for WDOG_MAX_DELAY
The current implementation allows the delay passed to wd_start() to be
   equal to WDOG_MAX_DELAY. However, since clock_delay2abstick() internally
   increments the delay by one tick, using a delay equal to WDOG_MAX_DELAY
   will cause an overflow and make the clock comparison invalid.

   This patch fixes the issue by disallowing the delay to be equal to
   WDOG_MAX_DELAY.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-11-10 10:45:15 +08:00
wangchengdong
190daef2bf sched/wdog: Use list_in_list() to detect active watchdogs
Use list_in_list() to determine whether a watchdog is active,
     eliminating the need to set the watchdog function pointer to NULL
     as an indicator of inactivity.

Signed-off-by: Chengdong Wang wangchengdong@lixiang.com
2025-11-07 10:30:53 +08:00
wangchengdong
1217fcf907 sched/wdog: Improve wdog Precision
Currently, wd_timer() obtains the current systick
    from the input parameter, and the wdog processes
    the list without updating the systick value.
    This is not incorrect, as the systick should continue
    to increase while the watchdog is processing callbacks.
    This patch addresses the issue by retrieving the updated
    systick value after each watchdog node loop.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-11-06 13:26:58 +08:00
wangchengdong
08050f506c sched/wdog: remove unnecessary list_node redefinition
The wdog implementation redefined `list_node`, which could cause
  conflicts or unexpected behavior when both `wdog/wdog.h` and
  `list.h` are included in the same source file. This patch removes
  the redundant definition to avoid such issues.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-10-09 11:41:32 +08:00
ouyangxiangzhen
9d1b958524 sched/wdog: Remove the wd_start_period.
For better simplicity, this commit replaced the periodical wdog APIs with the more expressive wd_start_next. The wd_start_next restarts watchdog timer based on the last expiration time.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-06-10 11:02:45 -03:00
ouyangxiangzhen
947856efe0 sched/wdog: Add max delay tick limitation.
This commit changed the type of the delay ticks to the unsigned, which can reduce the useless branch conditions
Besides, this commit added max delay tick limitation to fix incorrect timing behavior if we delay SCLOCK_MAX in the SMP build.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-27 12:10:39 +02:00
Jukka Laitinen
466525891d Revert "sched/wdog/wd_cancel: Return 0 (OK) when cancelling an inactive wdog"
This reverts commit c28c14ad26.
2025-05-16 10:30:41 +08:00
Jukka Laitinen
c28c14ad26 sched/wdog/wd_cancel: Return 0 (OK) when cancelling an inactive wdog
In case the wdog has already completed when calling cancel, the cancel is
supposed to just return OK.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
2025-05-14 21:13:48 +08:00
ouyangxiangzhen
64a7049dec clock: Add clock_delay2abstick.
This commit added a macro function clock_delay2abstick to calculate the
absolute tick after the delay.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-12 19:56:29 +08:00
ouyangxiangzhen
c67ac63da3 timer: pt_expected + 1 before set the timer.
This commit is to prevent the insufficient sleep time.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-12 19:56:29 +08:00
ouyangxiangzhen
8e65375365 sched: Remove unnecessary tick++ for absolute wdog timer.
To improve the absolute timer accuracy, this commit move the tick++ to relative wdog timer.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-12 19:56:29 +08:00
ouyangxiangzhen
fb592064cb sched: Improved the wdog and wq list insertion.
For the watchdog list and the workqueue list, we wonder whether the list head has changed after the insertion. In the original implementation, we have to access the list->next field and compare the result to the currently inserted node. In this commit, we mark the list head before the insertion and comparing the current traversed node with the list head we marked, which can avoid accessing the list->next and is more cache-friendly.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-05-07 02:02:10 +08:00
ouyangxiangzhen
fa5590d5b1 sched/wdog: Fix int-to-pointer-cast and pointer-to-int-cast warnings.
This commit fixed the wdparm to pointer and pointer to wdparm cast
warnings.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-03-08 13:52:37 -03:00
ouyangxiangzhen
f495cd4ffd sched/wdog: allow wd_cancel_period in the periodical wdog callback.
This commit addresses an issue where calling `wd_cancel_period` within the periodic watchdog callback would fail to cancel the watchdog timer.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-03-08 13:52:37 -03:00
ouyangxiangzhen
51399a76d9 sched/wdog: support for periodic wdog.
This commit added support for periodic wdog.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2025-03-08 13:52:37 -03:00
chao an
8877366c0e sched/wdog: remove wd_cancel_irq() implement
we do not need this implement after change the lock from csec to spin_lock

Signed-off-by: chao an <anchao.archer@bytedance.com>
2025-01-17 09:05:11 +08:00
hujun5
9dad781d07 sched/wdog: use small lock to protect g_wdactivelist
reason:
We would like to replace the critical section with a small lock.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2025-01-14 12:29:29 +08:00
ouyangxiangzhen
93890321c5 sched/wdog: Fix code style
This commit fixed code style issue left by the previous patch.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2024-10-10 13:57:46 +08:00
ouyangxiangzhen
110ab50f50 sched/wdog: Revert wd_cancel semantics
In the original implementation, if wdog is not ACTIVE, -EINVAL will be returned. The rp2040 i2c driver relies on this semantics to correctly release the semaphore.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2024-10-09 16:18:27 -03:00
xuxingliang
bc5a337a58 sched/note: add note for wdog module
Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-10-09 08:39:58 +08:00
ligd
9c423ee5fa wdog: rename wd_start_absolute() to wd_start_abstick()
Signed-off-by: ligd <liguiding1@xiaomi.com>
2024-09-17 12:15:14 +08:00
Alin Jerpelea
eb9030c891 sched: migrate to SPDX identifier
Most tools used for compliance and SBOM generation use SPDX identifiers
This change brings us a step closer to an easy SBOM generation.

Signed-off-by: Alin Jerpelea <alin.jerpelea@sony.com>
2024-09-12 01:10:14 +08:00
ligd
7ff7f6ec21 wdog: optimize a bit speed in wd_start
After these wdog refactor:
We conducted a latency measurement using the rt-tests/cyclictest (commit cadd661) on an x86_64 NUC12 equipped with an i7-1255U processor and 16GB of LPDDR5 memory. The specific command used for this microbenchmark was cyclictest -q -l 100000 -h 30000, which is designed to assess the responsiveness of the cyclic timer.

The findings from our benchmark are summarized below, highlighting the minimum, median, and maximum latency values for each operating system tested:

Operating System	Minimum Latency (us)	Median Latency (us)	Maximum Latency (us)
Linux	            48	                    53	                410
PreemptRT	        6	                    57	                148
Xenomai	            53	                    53	                64
NuttX	            64	                    626	                1212
NuttX (refactor)	1	                    1	                3
In this table, "Min" indicates the shortest latency observed, "Median" represents the middle value of the latency distribution, and "Max" denotes the longest latency encountered.

The systems tested were as follows:

Linux: ACRN version 6.1.80 (commit f528146)
PreemptRT: Linux kernel 5.4.251 with the 5.4.254-rt85 patch applied
Xenomai: Linux kernel 5.4.251 patched with ipipe-core-5.4.239-x86-13
These results clearly demonstrate the varying performance of different operating systems in terms of timer latency, the refactored NuttX showing particularly low latency values.

Signed-off-by: ligd <liguiding1@xiaomi.com>
2024-09-10 23:32:30 +08:00
ligd
cb5bab34f1 wdog: always ticks++ when wd_start
Now we have CONFIG_USEC_PER_TICK, and for our timer system, all the calculation used 'tick'.
And all the timespec should change to 'tick' before use wd_start(), so USEC2TICK() can NOT be avoided.

Then there must be an 'less then one tick' loss.

One resolution:
ticks++ anyway when wd_start(). But this will caused time expired more a tick.

Another resolution:
Change the testcase, and allow the following logic:

t1 = current_time();
sleep(3);
t2 = current_time();

allow: t2 - t1 >= 3;
(original test must be: t2- t1 > 3)

The original test think the time must be elapse-ing, and the (t2 - t1) must bigger then 3,
but in our system, we use 'tick' as the minimal wdog unit, then there must a precision loss.

Now we choose first resolution.

Signed-off-by: ligd <liguiding1@xiaomi.com>
2024-09-10 23:32:30 +08:00
ligd
e334df6d9a wdog: add API wd_cancel_irq() support
Signed-off-by: ligd <liguiding1@xiaomi.com>
2024-09-10 23:32:30 +08:00
ligd
b010356b26 wdog: fix timer IRQ busy when timer list empty
when timer list empty, should return 0 to indicate
we are no longer set time

Signed-off-by: ligd <liguiding1@xiaomi.com>
2024-09-10 23:32:30 +08:00
ligd
f23fb7ec09 sched: disable interrupt in non-SMP case
For the nested interrupt, one thing should decleared:
We are in ISR context, but no meaning we are disabled the interrupts.

Signed-off-by: ligd <liguiding1@xiaomi.com>
2024-09-10 23:32:30 +08:00
ouyangxiangzhen
66871ced4d sched/wdog: move g_wdtimernested to wd_start.c
This patch moved the g_wdtimernested to wd_start.c

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Signed-off-by: ligd <liguiding1@xiaomi.com>
2024-09-10 23:32:30 +08:00
ouyangxiangzhen
50f801dfaa sched/wdog: Fix list traversal problem in nested wdog process
If g_wdactivelist has been changed in the wdog callback, the list traversal with next pointer will cause problem.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Signed-off-by: ligd <liguiding1@xiaomi.com>
2024-09-10 23:32:30 +08:00
ouyangxiangzhen
ebf7f5f748 sched/sched: Fix recursive watchdog handling
This commit fixed recursive watchdog handling caused by calling wd_start within watchdog timeout callback function.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Signed-off-by: ligd <liguiding1@xiaomi.com>
2024-09-10 23:32:30 +08:00
ouyangxiangzhen
3b111c8b99 sched/wdog: Refactor wdog module
This commit refactors the wdog module to use absolute time representation internally. The main improvements include:
1. Fixed recursive watchdog handling caused by calling wd_start within watchdog timeout callback function.
2. Simplified timer processing to improve performance and enhance code readability.
3. Improved accuracy of timers.
4. Reduced critical section and interrupt disable time, improving real-time performance.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Signed-off-by: ligd <liguiding1@xiaomi.com>
2024-09-10 23:32:30 +08:00