Skip to content

Conversation

@Fix-Point
Copy link
Contributor

@Fix-Point Fix-Point commented Jan 28, 2026

Summary

This PR is part of the #17675, it introduces significant improvements to the high-resolution timer (hrtimer) subsystem and its integration with the watchdog timer system. The key changes include:

  1. Guard Timer Implementation: Added a guard timer (g_hrtimer_guard) that always remains in the timer queue with maximum expiration time. This simplifies empty queue checks, reduces branch conditions, and provides health monitoring capabilities for time acquisition errors.

  2. Watchdog-HRTimer Integration: Replaced the separate scheduler tick timer with direct hrtimer integration for watchdog processing. The watchdog now uses its own hrtimer (g_wdog_hrtimer) instead of a separate tick-based mechanism, simplifying the architecture.

  3. Red-Black Tree Optimization: Fixed left-most node detection in the hrtimer RB-tree by introducing g_cached_first caching, eliminating incorrect NULL checks on left child nodes.

  4. API Clarifications: Updated function comments and signatures for better documentation, particularly for hrtimer_cancel() and hrtimer_cancel_sync() to clarify ownership semantics and concurrency considerations.

  5. Code Simplification: Removed redundant timer processing logic, inlined hrtimer_start() for performance, and consolidated timer expiration handling between tick and tickless modes.

Impact

  • Performance: Reduced branch checking through guard timer improves hrtimer processing efficiency. Inlined hrtimer_start() eliminates at least 1 branch.
  • Memory Footprint: Guard timer uses minimal additional memory while providing monitoring and performance benefits.
  • Correctness: Fixed RB-tree left-most node detection and timer cancellation edge cases prevent missed expirations and hardware timer leaks.
  • Architecture: Simplified timer subsystem by eliminating separate scheduler tick timer code (sched_timer.c) and unifying watchdog processing under hrtimer.
  • Build System: Updated CMakeLists.txt and Make.defs to reflect source file changes (removed sched_timer.c, renamed sched_tickexpiration.c to sched_processtickless.c).
  • API Compatibility: Maintains backward compatibility while clarifying documentation for safer usage patterns.
  • Error Handling: Guard timer enables system recovery from time acquisition errors through configurable callback mechanisms.
  • Concurrency: Improved documentation for cancel operations helps prevent race conditions in multi-CPU environments.

Test

Tested on rv-virt:smp, ostest passed, parallel stressed hrtimer test passed.

@github-actions github-actions bot added Area: OS Components OS Components issues Size: L The size of the change in this PR is large labels Jan 28, 2026
@wangchdo
Copy link
Contributor

wangchdo commented Jan 28, 2026

I see lots of update that is not necessary, do you have performance or function comparison between the current implementation and your change?

@Fix-Point Fix-Point force-pushed the hrtimer_refactor_p2.6 branch from 088a0b9 to 9ce283a Compare January 28, 2026 09:10
@Fix-Point Fix-Point requested a review from anchao as a code owner January 28, 2026 09:10
@Fix-Point
Copy link
Contributor Author

Fix-Point commented Jan 28, 2026

I see lots of update that is not necessary, do you have performance or function comparison between the current implementation and your change?

I have spent a significant amount of time testing and fixing functional correctness issues in this implementation. These issues should have been fixed during the earlier testing phase or can be avoided by the implementation #17675. Please don't waste our time anymore.

As you suggested, I won't waste time "showing off" the performance improvements. If anyone else is interested, I'll provide the performance test data. If you don't believe me, you can test it yourself.

@Fix-Point Fix-Point force-pushed the hrtimer_refactor_p2.6 branch from 9ce283a to 1d17973 Compare January 28, 2026 09:30
@Fix-Point Fix-Point force-pushed the hrtimer_refactor_p2.6 branch from 1d17973 to 676c55f Compare January 28, 2026 12:00
This commit inlined the `hrtimer_start` to allow the compiler to optimize at least 1 branch in
hrtimer_start.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
@Fix-Point Fix-Point force-pushed the hrtimer_refactor_p2.6 branch 4 times, most recently from 74e9144 to ca69a31 Compare January 28, 2026 13:22
#endif

#ifdef CONFIG_SCHED_TICKLESS
bool g_wdtimernested;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we still need g_wdtimernested

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The in_expiration parameter was added because we can not restart the hrtimer within its callback function; instead, we should return the nanoseconds of the next delay. Its semantics are different from g_wdtimernested.

GUIDINGLI
GUIDINGLI previously approved these changes Jan 28, 2026
This commit renamed the hrtimer_is_armed to hrtimer_is_pending, which is
more accurate in the semamtic, and simplify it.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit simplified the rbtree in hrtimer and provided better
branchless compare function.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
When a hrtimer is removed and then re-enqueued, if the removed hrtimer is the head node and the re-enqueued node is not the head node, the hardware timer still needs to be reset. This patch fixes this issue and simplifies the enqueuing.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit simplified the hrtimer_process.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit fixed the overflow check and renamed the period to delay, since the callback return value is the next delay not the period.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Determining whether a red-black tree node is the left-mode node using `RB_LEFT(hrtimer, node) == NULL` is functionally incorrect. This is because the left node of any leaf node can also be NULL. For example, in the following rbtree:
 5
/ \
3 7
\
4
the left node of the right-most node 7 would also be NULL.
To avoid extra performance overhead to find the left-most node when the
rb-tree changed, this commit used `g_cached_first` to cache the
left-most node.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
If there are no timers in the hrtimer queue, we should cancel the timers to avoid unnecessary timer interruptions. Since we currently do not have a timer cancellation interface, we can achieve cancellation by setting the timer to its maximum value.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit added the guard timer for hrtimer.
The guard timer uses a small memory footprint, offering two main advantages:
- Reduced branches checking for an empty hrtimer queue, simplifying code implementation and improving the performance.
- Additional health monitoring allows the system to enter a safe state in case of time acquisition errors, and supports custom error handling callback functions.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
@Fix-Point Fix-Point force-pushed the hrtimer_refactor_p2.6 branch 2 times, most recently from 999ea24 to 1816155 Compare January 29, 2026 02:13
This commit simplified the timer expiration for hrtimer.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit updated the comments.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit simplified the hrtimer.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
@Fix-Point Fix-Point force-pushed the hrtimer_refactor_p2.6 branch from 1816155 to 6d840d0 Compare January 29, 2026 02:33
@github-actions github-actions bot added the Area: Drivers Drivers issues label Jan 29, 2026
This commit provided workaround for incorrect SCHED_RR behavior in
tickless mode.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Since the type of the `NSEC_PER_TICK` is long, we should convert it
explicitly, or the `div_const` will not work as intended (it requires
that the second argument `base` is `uint32_t`).

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
@Fix-Point Fix-Point force-pushed the hrtimer_refactor_p2.6 branch from 102480a to 9c8a9f3 Compare January 29, 2026 03:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Area: OS Components OS Components issues Size: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants