为什么udelay和ndelay在Linux内核中不准确? [英] Why udelay and ndelay is not accurate in linux kernel?

查看:899
本文介绍了为什么udelay和ndelay在Linux内核中不准确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做这样的函数

trace_printk("111111");
udelay(4000);
trace_printk("222222");

并且日志显示为4.01毫秒,没关系

但是当我这样打电话

trace_printk("111111");
ndelay(10000);
ndelay(10000);
ndelay(10000);
ndelay(10000);
....
....//totally 400 ndelay calls
trace_printk("222222");

日志将显示4.7毫秒.这是不可接受的. 为什么ndelay的错误如此之大?

深入内核代码,我发现这两个函数的实现

void __udelay(unsigned long usecs)
{
    __const_udelay(usecs * 0x10C7UL); /* 2**32 / 1000000 (rounded up) */
}

void __ndelay(unsigned long nsecs)
{
    __const_udelay(nsecs * 0x5UL); /* 2**32 / 1000000000 (rounded up) */
}

我认为udelay会是ndelay的1000倍,但事实并非如此,

解决方案

正如您已经注意到的,由于使用了0x5恒定因子,因此与毫秒延迟相比,纳秒延迟实现是一个粗略的近似值. 0x10c7 / 0x5大约是859.使用0x4会更接近1000(大约1073).

但是,使用0x4会使ndelay小于 所请求的纳秒数.通常,延迟功能旨在至少在用户要求的时间内提供延迟 (请参见此处:

and the log shows it's 4.01 ms , it'OK

But when i call like this

trace_printk("111111");
ndelay(10000);
ndelay(10000);
ndelay(10000);
ndelay(10000);
....
....//totally 400 ndelay calls
trace_printk("222222");

the log will shows 4.7 ms. It's not acceptable. Why the error of ndelay is so huge like this?

Look deep in the kernel code i found the implemention of this two functions

void __udelay(unsigned long usecs)
{
    __const_udelay(usecs * 0x10C7UL); /* 2**32 / 1000000 (rounded up) */
}

void __ndelay(unsigned long nsecs)
{
    __const_udelay(nsecs * 0x5UL); /* 2**32 / 1000000000 (rounded up) */
}

I thought udelay will be 1000 times than ndelay, but it's not, why?

解决方案

As you've already noticed, the nanosecond delay implementation is quite a coarse approximation compared to the millisecond delay, because of the 0x5 constant factor used. 0x10c7 / 0x5 is approximately 859. Using 0x4 would be closer to 1000 (approximately 1073).

However, using 0x4 would cause the ndelay to be less than the number of nanoseconds requested. In general, delay functions aim to provide a delay at least as long as requested by the user (see here: http://practicepeople.blogspot.jp/2013/08/kernel-programming-busy-waiting-delay.html).

这篇关于为什么udelay和ndelay在Linux内核中不准确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆