gettimeofday和xtime的定时精度
来源:互联网 发布:caffe 加载预训练模型 编辑:程序博客网 时间:2024/05/16 02:01
============================================
作者: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)
- gettimeofday和xtime的定时精度
- grttimeofday和xtime的定时精度
- boost::xtime和::xtime会冲突
- 容易混淆LINUX时钟的xtime和jiffie…
- linux墙上时间xtime与高精度时钟gettimeofday
- gettimeofday和clock_gettime的不同
- gettimeofday和clock_gettime的不同
- gettimeofday和clock_gettime的不同
- time和gettimeofday的性能差异
- 解决嵌入式系统用户态和内核态定时时间精度不够的问题
- 单精度和双精度的区别
- linux下的定时或计时操作(gettimeofday等的用法,秒,微妙,纳秒)
- linux下的定时或计时操作(gettimeofday等的用法)
- linux的查询时间实现秒定时+gettimeofday()函数的用法
- linux下的定时或计时操作(gettimeofday等的用法,秒,微妙,纳秒
- clock()、time()、clock_gettime()和gettimeofday()函数的用法和区别
- clock()、time()、clock_gettime()和gettimeofday()函数的用法和区别
- clock()、time()、clock_gettime()和gettimeofday()函数的用法和区别
- 虚拟化
- 如何在Linux和windows的控制台里面输入EOF以测试自己的程序
- c# 中对于每次修改的程序 都必须重新手动生成 才能编译的问题
- VS 2008调试C程序
- Hibernate常见面试题汇总
- gettimeofday和xtime的定时精度
- 一种未来可能的专家问询系统 (1.0 beta)
- login控件“您的登录尝试不成功。请重试”的解决方法
- 나 이나用法
- 读《Windows程序设计》笔记 第二章
- L4操作系统(L4/Fiasco介绍)
- 五种JSP页面跳转方法详解
- ZJUOJ1293——细菌的繁殖
- 心理学上的暗示