Linux hrtimer定时器误差很大,可能会有哪些原因啊?
本文是原「蜗窝讨论区」的历史存档(2017-01-08),来自版块「Linux kernel技术问答」,共 4 帖。讨论区已停止服务,此处仅供查阅。
用的arm平台Linux,现在需要定时us级别。有事定时500us,实际可能达到700多。而且这个hrtimer是不是可能会被打断吗?我测试了hrtimer定时400us,跑2000次。期间不断触摸屏幕。结果是实际到期达到2ms多,比预期的400us多太多了。
444444+count1:813---difftime:408-
333333+count:814---difftime:407--
444444+count1:814---difftime:408-//定时400us正常
ly event->au16_y[i] =1060//有一个触摸屏的中断
333333+count:815---difftime:2979-//下次hrtimer到期达到近3ms
444444+count1:815---difftime:2979
333333+count:816---difftime:411--
444444+count1:816---difftime:416-
还有一个测试情况,开两个hrtimer,定时600us,实际老是会多出200us左右,并且定时一个小时也是会经常多出200us。但是定时时间非常短,就不会多出200us,比如定时300us以下,都是准确的。
[== Undefined ==]
444444+count1:23---difftime:847-
333333+count:24---difftime:841--
444444+count1:24---difftime:846-
333333+count:25---difftime:841--
444444+count1:25---difftime:846-
333333+count:26---difftime:842--
444444+count1:26---difftime:847-
333333+count:27---difftime:607--//正常600us
444444+count1:27---difftime:609-
333333+count:28---difftime:842--
444444+count1:28---difftime:846-//不正常
333333+count:29---difftime:840--
444444+count1:29---difftime:844-
333333+count:30---difftime:840--
444444+count1:30---difftime:845-
333333+count:31---difftime:606--
444444+count1:31---difftime:609-
333333+count:32---difftime:610--
444444+count1:32---difftime:613-
333333+count:33---difftime:842--
444444+count1:33---difftime:845-
333333+count:34---difftime:840--
444444+count1:34---difftime:845-
333333+count:35---difftime:841--
444444+count1:35---difftime:845-
333333+count:36---difftime:841--
444444+count1:36---difftime:845-
333333+count:37---difftime:608--
444444+count1:37---difftime:612-
333333+count:38---difftime:842--
444444+count1:38---difftime:846-
不知道有哪些原因会导致hrtimer这么的不准,这么多问题啊?
Linux kernel并不是一个硬实时的kernel,所以hrtimer不准很正常。 影响hrtimer准确度的最有可能的原因,就是kernel中那些关中断的临界区。中断都关闭了,什么timer都完蛋了…… 所以,不要纠结准不准了,尽量减少误差的影响,例如问问自己:需要这么精确的timer吗? 另外,如果需要一个周期性的timer,尽量使用hrtimer_forward_now重启timer,以避免误差累积。
是啊,有些驱动中经常 spin_lock_irqsave spin_unlock_irqrestore的,对Linux 实时性能也有影响的
楼主你的 hrtimer时钟选对了 吗还有mode 是相对还是绝对 hrtimer在100us以上还是很准的
