您的位置:首页 > 其它

gettimeofday和xtime的定时精度

2010-11-20 13:05 453 查看
============================================

作者:yuanlulu
http://blog.csdn.net/yuanlulu
版权没有,但是转载请保留此段声明

============================================

客户反映,gettimeofday获取的时间us部分总是零。

自己修改测试代码有如下结果:

./lpc3250test 1 0 50000

divsor is 1 sec is 0 usec is 50000 ***

current time: sec = 1289836976 usec = 390000 ***

current time: sec = 1289836976 usec = 390000 ***

current time: sec = 1289836976 usec = 390000 ***

current time: sec = 1289836976 usec = 450000 cnt = 1 ***

current time: sec = 1289836976 usec = 450000 cnt = 1 ***

current time: sec = 1289836976 usec = 500000 cnt = 2 ***

current time: sec = 1289836976 usec = 500000 cnt = 2 ***

current time: sec = 1289836976 usec = 550000 cnt = 3 ***

current time: sec = 1289836976 usec = 550000 cnt = 3 ***

current time: sec = 1289836976 usec = 600000 cnt = 4 ***

current time: sec = 1289836976 usec = 600000 cnt = 4 ***

current time: sec = 1289836976 usec = 650000 cnt = 5 ***

current time: sec = 1289836976 usec = 650000 cnt = 5 ***

current time: sec = 1289836976 usec = 700000 cnt = 6 ***

current time: sec = 1289836976 usec = 700000 cnt = 6 ***

current time: sec = 1289836976 usec = 750000 cnt = 7 ***

current time: sec = 1289836976 usec = 750000 cnt = 7 ***

current time: sec = 1289836976 usec = 800000 cnt = 8 ***

current time: sec = 1289836976 usec = 800000 cnt = 8 ***

current time: sec = 1289836976 usec = 850000 cnt = 9 ***

可以发现gettimeofday的us后四位总为零,只能精确到10ms.

调用关系:gettimeofday->sys_gettimeofday->do_gettimeofday.

do_gettimeofday的代码在内核代码的kernel/time/timekeeping.c

函数定义在114行,do_gettimeofday又调用了同一文件中的getnstimeofday。

void getnstimeofday(struct timespec *ts)

{

unsigned long seq;

s64 nsecs;

do {

seq = read_seqbegin(&xtime_lock);

*ts = xtime;

nsecs = __get_nsec_offset();

} while (read_seqretry(&xtime_lock, seq));

timespec_add_ns(ts, nsecs);

}

可见getnstimeofday直接使用了xtime。

现在问题转化为xtime的精度到底如何。

在驱动模块中打印current_kernel_time(void)读取的xtime如下:

isoinit, sec:1289833417, ns:950000000

isoinit, sec:1289833417, ns:950000000

isoinit, sec:1289833417, ns:960000000

isoinit, sec:1289833417, ns:960000000

isoinit, sec:1289833417, ns:960000000

isoinit, sec:1289833417, ns:970000000

isoinit, sec:1289833417, ns:970000000

isoinit, sec:1289833425, ns:780000000

isoinit, sec:1289833425, ns:780000000

isoinit, sec:1289833425, ns:790000000

isoinit, sec:1289833425, ns:790000000

isoinit, sec:1289833425, ns:790000000

isoinit, sec:1289833425, ns:800000000

isoinit, sec:1289833425, ns:800000000

多次试验的结果都是最后七位总为零。所以xtime的精度不会超过10ms。

现在可以看到gettimeofday和xtime的精度同为10ms。与系统时钟的精度相同(HZ=100).

xtime是一个全局变量。

kernel/time/timekeeping.c中更新xtime的函数为update_wall_time(void)

/**

* update_wall_time - Uses the current clocksource to increment the wall time

*

* Called from the timer interrupt, must hold a write on xtime_lock.

*/

void update_wall_time(void)

{

。。。。。。。。。。。

/* store full nanoseconds into xtime */

xtime.tv_nsec = (s64)clock->xtime_nsec >> clock->shift;

clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;

。。。。。。。。。。。

}

由于此函数是由系统定时中断调用的,所以xtime精度和系统时钟精度相同也就不足为怪了。



总结:getrimeofday和xtime的精度和系统节拍有关,我的系统HZ=100,因此只能精确到10ms。(内核2.6.27.8)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: