rt-thread icon indicating copy to clipboard operation
rt-thread copied to clipboard

[proposal] `rt_timer_check` 长时间关闭中断,影响系统实时性, 或许可以优化

Open a1012112796 opened this issue 1 year ago • 6 comments

目前 rt_timer_check 为了确保 timer list 的一致性,全程关闭中断 (时间长达数十us, 且理论上会随定时器数量的增加而变大),影响了系统的实时性,或许可以通过算法优化,尽可能减少中断关闭的时间。

此问题是社区微信群里的大佬提出的,个人认为确实值得优化,但暂时没想到太好的方案, mark 一下。

https://github.com/RT-Thread/rt-thread/blob/35231eb116c44165d881dd10ff15d27c4ca540d1/src/timer.c#L665

一些 tips:

  • 群里大佬提到了在执行 hook 的时候暂时恢复中断来减少关闭中断的时间,但我感觉不太可。。。示例如下:
@@ -685,9 +685,16 @@ void rt_timer_check(void)
             }
             /* add timer to temporary list  */
             rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
+
+            /* enable interrupt */
+            rt_hw_interrupt_enable(level);
+
             /* call timeout function */
             t->timeout_func(t->parameter);
 
+            /* disable interrupt */
+            level = rt_hw_interrupt_disable();
+
             /* re-get tick */
             current_tick = rt_tick_get();
  • 个人对别的实时操作系统了解不多,好奇 free-rtos 等其它实时操作系统是如何做的,是否有更好的方案.

a1012112796 avatar Sep 19 '23 10:09 a1012112796

related: https://github.com/RT-Thread/rt-thread/issues/7860

mysterywolf avatar Sep 19 '23 21:09 mysterywolf

freertos是基于queue实现的,task本身并不依赖于timer,其timer是一个可选项。

freertos对于定时器的处理是,所有的操作都通过push to queue去完成,之后在tmr svc线程中pop from queue依次执行所有的定时器操作,显然不存在竞争关系,因此可以收敛为单线程场景,不需要考虑同步问题,也就不需要关中断实现一个临界区了。

但rt-thread中thread依赖于timer的实现,因此不太好借鉴其实现,但或许可以考虑学习其思想,将timer收敛为单线程场景,这样就能去掉锁了?

Rei-4396 avatar Oct 10 '23 16:10 Rei-4396

image

引用链接

a1012112796 avatar Oct 11 '23 07:10 a1012112796

https://github.com/RT-Thread/rt-thread/pull/8042#discussion_r1391914034

a1012112796 avatar Nov 14 '23 02:11 a1012112796

#8042 (comment)

已验证此改动会造成死锁bug,此pr会进行修复 https://github.com/RT-Thread/rt-thread/pull/8369

xqyjlj avatar Dec 15 '23 09:12 xqyjlj

rt_timer_check 这个函数也确实要优化了,该函数会严重影响实时性,造成严重抖动。多核上效果更恐怖

xqyjlj avatar Dec 15 '23 09:12 xqyjlj