This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 970920a58838a1c72e9bc9492c7bcf6e8d622d0a
Author: ouyangxiangzhen <[email protected]>
AuthorDate: Tue Oct 27 17:41:50 2026 +0800

    sched/hrtimer: Skip redundant reprogram via head expiration comparison
    
    The is_head results of hrtimer_insert()/hrtimer_remove() only track
    queue head identity: restarting the head timer with the same deadline
    still reprograms the hardware even though the earliest expiration is
    unchanged.  The g_hrtimer_expected cache handles the value case but
    needs an invalidation protocol on hrtimer_process() entry to stay
    consistent.
    
    Instead, snapshot the earliest expiration before the queue change in
    hrtimer_start_absolute()/hrtimer_cancel() and compare it with the one
    after.  If unchanged, the hardware timer is already programmed for that
    target and hrtimer_reprogram() is skipped.  The local comparison needs
    no persistent state and covers both head identity and head value
    changes, so hrtimer_insert()/hrtimer_remove() lose their is_head return
    values and g_hrtimer_expected and its invalidation are removed.
    
    hrtimer_process() keeps reprogramming unconditionally at the end:
    after the one-shot timer has fired, the hardware must always be
    re-armed.  The expired fixup for hrtimer_gettime() now only runs for
    pending timers.
    
    Measured on qemu-intel64 (TSC deadline, -O3, CONFIG_HRTIMER_LIST):
    restarting the queue head with the same deadline drops from 1178 to 39
    cycles per call; other start/cancel paths stay at ~40 cycles.
    
    Assisted-by: mimo-v2.6-pro
    Signed-off-by: ouyangxiangzhen <[email protected]>
---
 sched/hrtimer/hrtimer.h            | 95 ++++++++++++++++----------------------
 sched/hrtimer/hrtimer_cancel.c     | 24 +++++++---
 sched/hrtimer/hrtimer_initialize.c |  4 --
 sched/hrtimer/hrtimer_start.c      | 20 +++++---
 4 files changed, 71 insertions(+), 72 deletions(-)

diff --git a/sched/hrtimer/hrtimer.h b/sched/hrtimer/hrtimer.h
index 4971a9f4605..af5e11bd48c 100644
--- a/sched/hrtimer/hrtimer.h
+++ b/sched/hrtimer/hrtimer.h
@@ -104,10 +104,6 @@ extern struct FAR hrtimer_s *g_hrtimer_head;
 extern struct list_node g_hrtimer_list;
 #endif
 
-#ifdef CONFIG_SCHED_TICKLESS
-extern uint64_t g_hrtimer_expected;
-#endif
-
 #ifdef CONFIG_SMP
 /* Array of pointers to currently running high-resolution timers
  * for each CPU in SMP configurations. Index corresponds to CPU ID.
@@ -223,46 +219,42 @@ static inline_function void hrtimer_reprogram(uint64_t 
next_expired)
   uint64_t        delay;
   uint64_t        expired = hrtimer_last_expired();
 
-  if (g_hrtimer_expected != next_expired)
-    {
-      g_hrtimer_expected = next_expired;
 #  if CONFIG_HRTIMER_PRECISION_SHIFT > 0
-      /* Ceiling-align the target expiration time to the precision boundary.
-       * This coarsens the timer resolution to (1 << SHIFT) nanoseconds,
-       * reducing the frequency of hardware timer reprogramming.
-       */
+  /* Ceiling-align the target expiration time to the precision boundary.
+   * This coarsens the timer resolution to (1 << SHIFT) nanoseconds,
+   * reducing the frequency of hardware timer reprogramming.
+   */
 
-      next_expired = (next_expired + HRTIMER_PRECISION_MASK) &
-                     ~HRTIMER_PRECISION_MASK;
+  next_expired = (next_expired + HRTIMER_PRECISION_MASK) &
+                 ~HRTIMER_PRECISION_MASK;
 #  endif
 
-      delay = HRTIMER_TIME_BEFORE_EQ(expired, next_expired) ?
-              next_expired - expired : 0u;
+  delay = HRTIMER_TIME_BEFORE_EQ(expired, next_expired) ?
+          next_expired - expired : 0u;
 
 #  ifdef CONFIG_SCHED_TICKLESS_LIMIT_MAX_SLEEP
-      delay = delay < TICK2NSEC((uint64_t)g_oneshot_maxticks) ?
-              delay : TICK2NSEC((uint64_t)g_oneshot_maxticks);
+  delay = delay < TICK2NSEC((uint64_t)g_oneshot_maxticks) ?
+          delay : TICK2NSEC((uint64_t)g_oneshot_maxticks);
 #  endif
 
-      /* Even if overflow occurs here, the functional correctness
-       * should still be fine.
-       */
+  /* Even if overflow occurs here, the functional correctness
+   * should still be fine.
+   */
 
-      delay -= CONFIG_TIMER_ADJUST_NSEC;
+  delay -= CONFIG_TIMER_ADJUST_NSEC;
 
-      clock_nsec2time(&ts, expired + delay);
+  clock_nsec2time(&ts, expired + delay);
 
 #  ifdef CONFIG_SCHED_TICKLESS_ALARM
-      ret = up_alarm_start(&ts);
+  ret = up_alarm_start(&ts);
 #  else
-      struct timespec current;
-      up_timer_gettime(&current);
-      clock_timespec_subtract(&ts, &current, &ts);
-      ret = up_timer_start(&ts);
+  struct timespec current;
+  up_timer_gettime(&current);
+  clock_timespec_subtract(&ts, &current, &ts);
+  ret = up_timer_start(&ts);
 #  endif
 
-      DEBUGASSERT(ret == 0);
-    }
+  DEBUGASSERT(ret == 0);
 #endif
 }
 
@@ -319,37 +311,28 @@ RB_PROTOTYPE(hrtimer_tree_s, hrtimer_s, node, 
hrtimer_compare);
  * Description:
  *   Remove a timer from the queue and mark it as dequeued.
  *
- * Returned Value:
- *   true if the timer is the head of the queue, otherwise false.
- *
  * Assumption:
  *   The caller must hold the queue lock.
  *   The caller must ensure that the timer is in the queue.
  *
  ****************************************************************************/
 
-static inline_function bool hrtimer_remove(FAR hrtimer_t *hrtimer)
+static inline_function void hrtimer_remove(FAR hrtimer_t *hrtimer)
 {
-  bool is_head;
 #ifdef CONFIG_HRTIMER_TREE
-  is_head = g_hrtimer_head == hrtimer;
-
   RB_REMOVE(hrtimer_tree_s, &g_hrtimer_tree, hrtimer);
 
-  if (is_head)
+  if (g_hrtimer_head == hrtimer)
     {
       g_hrtimer_head = RB_MIN(hrtimer_tree_s, &g_hrtimer_tree);
     }
 #else
-  is_head = list_is_head(&g_hrtimer_list, &hrtimer->node);
   list_delete_fast(&hrtimer->node);
 #endif
 
   /* Explicitly mark the timer as dequeued. */
 
   hrtimer->func = NULL;
-
-  return is_head;
 }
 
 /****************************************************************************
@@ -358,28 +341,21 @@ static inline_function bool hrtimer_remove(FAR hrtimer_t 
*hrtimer)
  * Description:
  *   Insert a timer into the hrtimer queue according to its expiration time.
  *
- * Returned Value:
- *   true if the timer is added to the head of the queue, otherwise false.
- *
  * Assumption:
  *   The caller must hold the queue lock.
  *   The caller must ensure that the timer is in the queue.
  *
  ****************************************************************************/
 
-static inline_function bool hrtimer_insert(FAR hrtimer_t *hrtimer)
+static inline_function void hrtimer_insert(FAR hrtimer_t *hrtimer)
 {
 #ifdef CONFIG_HRTIMER_TREE
-  bool is_head = false;
   RB_INSERT(hrtimer_tree_s, &g_hrtimer_tree, hrtimer);
 
   if (HRTIMER_TIME_BEFORE(hrtimer->expired, g_hrtimer_head->expired))
     {
       g_hrtimer_head = hrtimer;
-      is_head        = true;
     }
-
-  return is_head;
 #else
   FAR hrtimer_t *curr;
   uint64_t expired = hrtimer->expired;
@@ -395,7 +371,6 @@ static inline_function bool hrtimer_insert(FAR hrtimer_t 
*hrtimer)
     }
 
   list_add_before(&curr->node, &hrtimer->node);
-  return list_is_head(&g_hrtimer_list, &hrtimer->node);
 #endif
 }
 
@@ -419,6 +394,22 @@ static inline_function FAR hrtimer_t 
*hrtimer_get_first(void)
 #endif
 }
 
+/****************************************************************************
+ * Name: hrtimer_next_expired
+ *
+ * Description:
+ *   Return the expiration time of the earliest expiring pending timer.
+ *
+ * Returned Value:
+ *   The expiration time in nanoseconds.
+ *
+ ****************************************************************************/
+
+static inline_function uint64_t hrtimer_next_expired(void)
+{
+  return hrtimer_get_first()->expired;
+}
+
 /****************************************************************************
  * Name: hrtimer_read_64/32
  *
@@ -673,12 +664,6 @@ static inline_function void hrtimer_process(uint64_t now)
   uint64_t        expired = hrtimer->expired;
   hrtimer_entry_t    func = hrtimer->func;
 
-#ifdef CONFIG_SCHED_TICKLESS
-  /* Invalidate the g_hrtimer_expected */
-
-  g_hrtimer_expected = now;
-#endif
-
   /* Acquire the lock and seize the ownership of the hrtimer queue.
    * Then check if the timer has expired.
    */
diff --git a/sched/hrtimer/hrtimer_cancel.c b/sched/hrtimer/hrtimer_cancel.c
index 8ec67afa511..1d5747365dd 100644
--- a/sched/hrtimer/hrtimer_cancel.c
+++ b/sched/hrtimer/hrtimer_cancel.c
@@ -74,9 +74,8 @@
 
 int hrtimer_cancel(FAR hrtimer_t *hrtimer)
 {
-  FAR hrtimer_t *first;
   irqstate_t flags;
-  int ret;
+  int        ret;
 
   DEBUGASSERT(hrtimer != NULL);
 
@@ -90,12 +89,25 @@ int hrtimer_cancel(FAR hrtimer_t *hrtimer)
 
   if (hrtimer_is_pending(hrtimer))
     {
-      /* Update the hardware timer if the queue head changed. */
+      uint64_t prev_expired = hrtimer_next_expired();
+      uint64_t next_expired;
 
-      if (hrtimer_remove(hrtimer))
+      /* Update the expired time to ensure the cancelled timer will return 0
+       * in hrtimer_gettime.
+       */
+
+      hrtimer->expired = hrtimer_last_expired();
+
+      hrtimer_remove(hrtimer);
+
+      /* Reprogram the hardware timer only if the earliest expiration
+       * changed.
+       */
+
+      next_expired = hrtimer_next_expired();
+      if (prev_expired != next_expired)
         {
-          first = hrtimer_get_first();
-          hrtimer_reprogram(first->expired);
+          hrtimer_reprogram(next_expired);
         }
     }
 
diff --git a/sched/hrtimer/hrtimer_initialize.c 
b/sched/hrtimer/hrtimer_initialize.c
index eac9ec26a8b..0b8beaf53cb 100644
--- a/sched/hrtimer/hrtimer_initialize.c
+++ b/sched/hrtimer/hrtimer_initialize.c
@@ -32,10 +32,6 @@
  * Public Data
  ****************************************************************************/
 
-#ifdef CONFIG_SCHED_TICKLESS
-uint64_t g_hrtimer_expected;
-#endif
-
 #ifdef CONFIG_SMP
 /* Array of pointers to currently running high-resolution timers
  * for each CPU in SMP configurations. Index corresponds to CPU ID.
diff --git a/sched/hrtimer/hrtimer_start.c b/sched/hrtimer/hrtimer_start.c
index 719cb3a0e54..8603952110a 100644
--- a/sched/hrtimer/hrtimer_start.c
+++ b/sched/hrtimer/hrtimer_start.c
@@ -59,8 +59,9 @@ int hrtimer_start_absolute(FAR hrtimer_t *hrtimer, 
hrtimer_entry_t func,
                            uint64_t expired)
 {
   irqstate_t flags;
-  bool       reprogram = false;
-  int        ret       = OK;
+  uint64_t   prev_expired;
+  uint64_t   next_expired;
+  int        ret = OK;
 
   DEBUGASSERT(hrtimer != NULL && func != NULL);
 
@@ -72,9 +73,13 @@ int hrtimer_start_absolute(FAR hrtimer_t *hrtimer, 
hrtimer_entry_t func,
 
   hrtimer_cancel_running(hrtimer);
 
+  /* Remember the earliest expiration before the queue is changed. */
+
+  prev_expired = hrtimer_next_expired();
+
   if (hrtimer_is_pending(hrtimer))
     {
-      reprogram = hrtimer_remove(hrtimer);
+      hrtimer_remove(hrtimer);
     }
 
   hrtimer->func    = func;
@@ -82,13 +87,14 @@ int hrtimer_start_absolute(FAR hrtimer_t *hrtimer, 
hrtimer_entry_t func,
 
   /* Insert the timer into the hrtimer queue. */
 
-  reprogram |= hrtimer_insert(hrtimer);
+  hrtimer_insert(hrtimer);
 
-  /* If the inserted timer is now the earliest, start hardware timer. */
+  /* Reprogram the hardware timer only if the earliest expiration changed. */
 
-  if (reprogram)
+  next_expired = hrtimer_next_expired();
+  if (prev_expired != next_expired)
     {
-      hrtimer_reprogram(hrtimer_get_first()->expired);
+      hrtimer_reprogram(next_expired);
     }
 
   /* Release the lock and give up the ownership of the hrtimer queue. */

Reply via email to