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 dc24bf89ebefa8334f79343c3a1eca511f15c4b2
Author: ouyangxiangzhen <[email protected]>
AuthorDate: Wed Oct 28 11:17:33 2026 +0800

    sched/hrtimer: Remove the guard sentinel from the timer queue
    
    The guard timer kept hrtimer_get_first() non-NULL by occupying a
    permanent queue node at a never-firing expiration, but the sentinel
    state leaked into the hot paths:
    
    - When the queue drained, the hardware timer was reprogrammed to the
      guard's expiration for nothing, and hrtimer_process() did the same
      on every run with nothing pending (1370 cycles measured).
    - The guard node carried a NULL callback which hrtimer_process() would
      invoke if its expiration were ever reached.
    - A timer expiring after the guard silently never became the queue
      head.
    
    Make an empty queue a first-class state instead.  hrtimer_get_first()
    returns NULL, and hrtimer_next_expired() returns
    hrtimer_last_expired() + INT64_MAX when the queue is empty: a
    far-future deadline anchored at the last hrtimer_process() time, so it
    cannot collide with a real expiration requested by the user and never
    fires in practice.  hrtimer_last_expired() reads the new per-CPU
    g_hrtimer_last_expired recorded by hrtimer_process().
    
    hrtimer_process() reprograms the hardware only when a timer remains
    armed (77 cycles with an empty queue instead of 1370), and its loop
    now fetches the earliest pending timer in the loop condition and uses
    it for the final reprogram.  Cancel of the last timer reprograms the
    far-future deadline to disarm the hardware, keeping
    hrtimer_reprogram() unconditional and the implementation simple.
    
    Measured on qemu-intel64 (TSC deadline, -O3, CONFIG_HRTIMER_LIST):
    hrtimer_process() on an empty queue drops from 1370 to 77 cycles;
    start/cancel/restart paths are unchanged within noise.
    
    Assisted-by: mimo-v2.6-pro
    Signed-off-by: ouyangxiangzhen <[email protected]>
---
 include/nuttx/hrtimer.h            |  11 ----
 sched/hrtimer/hrtimer.h            | 131 +++++++++++++++++++++----------------
 sched/hrtimer/hrtimer_initialize.c |  23 ++-----
 3 files changed, 78 insertions(+), 87 deletions(-)

diff --git a/include/nuttx/hrtimer.h b/include/nuttx/hrtimer.h
index d7c42d9c449..8afd6806cec 100644
--- a/include/nuttx/hrtimer.h
+++ b/include/nuttx/hrtimer.h
@@ -43,19 +43,8 @@
  * hrtimer in the queue, HRTIMER_TIME_BEFORE/AFTER might be incorrect, so we
  * limited the delay to (INT64_MAX >> 1) + 1u, assuming all expired hrtimer
  * can be processed within HRTIMER_MAX_DELAY - 1u.
- *
- * g_hrtimer_guard.expired is set to now + HRTIMER_GUARD_DELAY in the
- * hrtimer_process. The guard delay is intentionally set to
- * INT64_MAX - (1ull << 48) rather than INT64_MAX for the following reason:
- * In SMP configurations, each CPU core independently updates shared
- * g_hrtimer_guard.expired. Due to interrupt and lock latency, the current
- * time across cores are not perfectly synchronized and may exhibit small
- * forward/backward drift.
- * The (1ull << 48) headroom (~0.3 days in nanoseconds) accommodates
- * this inter-core time desynchronization.
  */
 
-#define HRTIMER_GUARD_DELAY ((uint64_t)INT64_MAX - (1ull << 48))
 #define HRTIMER_MAX_DELAY   (((uint64_t)INT64_MAX >> 1) + 1u)
 
 /****************************************************************************
diff --git a/sched/hrtimer/hrtimer.h b/sched/hrtimer/hrtimer.h
index af5e11bd48c..947f455c1b1 100644
--- a/sched/hrtimer/hrtimer.h
+++ b/sched/hrtimer/hrtimer.h
@@ -91,7 +91,11 @@ RB_HEAD(hrtimer_tree_s, hrtimer_s);
 
 extern seqcount_t g_hrtimer_lock;
 
-extern struct hrtimer_s g_hrtimer_guard;
+/* The time of the last hrtimer_process() run, used as the reference
+ * point for hrtimer_last_expired().
+ */
+
+extern uint64_t g_hrtimer_last_expired;
 
 #ifdef CONFIG_HRTIMER_TREE
 /* Red-Black tree containing all active high-resolution timers */
@@ -166,6 +170,27 @@ void hrtimer_unlock(irqstate_t flags)
 #endif
 }
 
+/****************************************************************************
+ * Name: hrtimer_get_first
+ *
+ * Description:
+ *   Return the earliest expiring pending timer.
+ *
+ * Returned Value:
+ *   Pointer to the earliest timer, or NULL if none are pending.
+ *
+ ****************************************************************************/
+
+static inline_function FAR hrtimer_t *hrtimer_get_first(void)
+{
+#ifdef CONFIG_HRTIMER_TREE
+  return g_hrtimer_head;
+#else
+  return list_is_empty(&g_hrtimer_list) ?
+         NULL : list_first_entry(&g_hrtimer_list, FAR hrtimer_t, node);
+#endif
+}
+
 /****************************************************************************
  * Name: hrtimer_last_expired
  *
@@ -182,7 +207,28 @@ void hrtimer_unlock(irqstate_t flags)
 
 static inline_function uint64_t hrtimer_last_expired(void)
 {
-  return g_hrtimer_guard.expired - HRTIMER_GUARD_DELAY;
+  return g_hrtimer_last_expired;
+}
+
+/****************************************************************************
+ * Name: hrtimer_next_expired
+ *
+ * Description:
+ *   Return the expiration time of the earliest expiring pending timer.
+ *
+ * Returned Value:
+ *   The expiration time in nanoseconds.  When the queue is empty,
+ *   hrtimer_last_expired() + INT64_MAX is returned: a far-future deadline
+ *   that never fires in practice and cannot collide with a real
+ *   expiration requested by the user.
+ *
+ ****************************************************************************/
+
+static inline_function uint64_t hrtimer_next_expired(void)
+{
+  FAR hrtimer_t *first = hrtimer_get_first();
+
+  return first != NULL ? first->expired : hrtimer_last_expired() + INT64_MAX;
 }
 
 /****************************************************************************
@@ -352,7 +398,8 @@ static inline_function void hrtimer_insert(FAR hrtimer_t 
*hrtimer)
 #ifdef CONFIG_HRTIMER_TREE
   RB_INSERT(hrtimer_tree_s, &g_hrtimer_tree, hrtimer);
 
-  if (HRTIMER_TIME_BEFORE(hrtimer->expired, g_hrtimer_head->expired))
+  if (g_hrtimer_head == NULL ||
+      HRTIMER_TIME_BEFORE(hrtimer->expired, g_hrtimer_head->expired))
     {
       g_hrtimer_head = hrtimer;
     }
@@ -374,42 +421,6 @@ static inline_function void hrtimer_insert(FAR hrtimer_t 
*hrtimer)
 #endif
 }
 
-/****************************************************************************
- * Name: hrtimer_get_first
- *
- * Description:
- *   Return the earliest expiring pending timer.
- *
- * Returned Value:
- *   Pointer to the earliest timer, or NULL if none are pending.
- *
- ****************************************************************************/
-
-static inline_function FAR hrtimer_t *hrtimer_get_first(void)
-{
-#ifdef CONFIG_HRTIMER_TREE
-  return g_hrtimer_head;
-#else
-  return list_first_entry(&g_hrtimer_list, FAR hrtimer_t, node);
-#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
  *
@@ -657,19 +668,30 @@ static inline_function void hrtimer_wait(FAR hrtimer_t 
*timer)
 
 static inline_function void hrtimer_process(uint64_t now)
 {
+  uint64_t        expired;
   uint64_t          delay;
+  hrtimer_entry_t    func;
+  FAR hrtimer_t  *hrtimer;
   int                 cpu = this_cpu();
   irqstate_t        flags = hrtimer_lock();
-  FAR hrtimer_t  *hrtimer = hrtimer_get_first();
-  uint64_t        expired = hrtimer->expired;
-  hrtimer_entry_t    func = hrtimer->func;
 
-  /* Acquire the lock and seize the ownership of the hrtimer queue.
-   * Then check if the timer has expired.
+  /* Record the current time as the reference point for
+   * hrtimer_last_expired().
    */
 
-  while (HRTIMER_TIME_BEFORE_EQ(expired, now))
+  g_hrtimer_last_expired = now;
+
+  /* Process every expired timer.  The earliest pending timer is
+   * re-fetched on each iteration since the callback may have modified
+   * the queue.
+   */
+
+  while ((hrtimer = hrtimer_get_first()) != NULL &&
+         HRTIMER_TIME_BEFORE_EQ(hrtimer->expired, now))
     {
+      expired = hrtimer->expired;
+      func    = hrtimer->func;
+
       /* Remove the expired timer from the timer queue */
 
       hrtimer_remove(hrtimer);
@@ -704,23 +726,18 @@ static inline_function void hrtimer_process(uint64_t now)
           hrtimer->func    = func;
           hrtimer_insert(hrtimer);
         }
-
-      /* Fetch the earliest active timer */
-
-      hrtimer = hrtimer_get_first();
-      expired = hrtimer->expired;
-      func    = hrtimer->func;
     }
 
   hrtimer_unmark_running(cpu);
 
-  /* Note that the g_hrtimer_guard.expired is not monotonic. */
-
-  g_hrtimer_guard.expired = now + HRTIMER_GUARD_DELAY;
-
-  /* Schedule the next timer expiration */
+  /* Arm the hardware for the earliest remaining timer.  When the queue
+   * is empty nothing needs to fire and the hardware is left alone.
+   */
 
-  hrtimer_reprogram(expired);
+  if (hrtimer)
+    {
+      hrtimer_reprogram(hrtimer->expired);
+    }
 
   /* Release the lock and give up the ownership of the hrtimer queue. */
 
diff --git a/sched/hrtimer/hrtimer_initialize.c 
b/sched/hrtimer/hrtimer_initialize.c
index 0b8beaf53cb..e89db89691c 100644
--- a/sched/hrtimer/hrtimer_initialize.c
+++ b/sched/hrtimer/hrtimer_initialize.c
@@ -58,28 +58,13 @@ seqcount_t g_hrtimer_lock = SEQLOCK_INITIALIZER;
  * both configurations.
  */
 
-struct hrtimer_s g_hrtimer_guard =
-{
-#ifdef CONFIG_HRTIMER_TREE
-  { NULL },
-#else
-  { &g_hrtimer_list, &g_hrtimer_list },
-#endif
-  NULL,
-  HRTIMER_GUARD_DELAY
-};
+uint64_t g_hrtimer_last_expired;
 
 #ifdef CONFIG_HRTIMER_TREE
-struct hrtimer_tree_s g_hrtimer_tree =
-{
-  &g_hrtimer_guard
-};
-struct FAR hrtimer_s *g_hrtimer_head = &g_hrtimer_guard;
+struct hrtimer_tree_s g_hrtimer_tree;
+struct FAR hrtimer_s *g_hrtimer_head;
 #else
-struct list_node g_hrtimer_list =
-{
-  &g_hrtimer_guard.node, &g_hrtimer_guard.node
-};
+struct list_node g_hrtimer_list = LIST_INITIAL_VALUE(g_hrtimer_list);
 #endif
 
 /****************************************************************************

Reply via email to