grttimeofday和xtime的定时精度
来源:互联网 发布:身份证借去开淘宝店 编辑:程序博客网 时间:2024/06/05 07:32
============================================
作者: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)
- grttimeofday和xtime的定时精度
- gettimeofday和xtime的定时精度
- boost::xtime和::xtime会冲突
- 容易混淆LINUX时钟的xtime和jiffie…
- 解决嵌入式系统用户态和内核态定时时间精度不够的问题
- 单精度和双精度的区别
- windows 时间 定时 精度
- Windows定时精度比较
- Python写的一个优美的定时器,定时精度可调
- ADC的精度和分辨率
- ADC的精度和分辨率
- float 和 double 的精度
- bigDecimal的精度和四舍五入
- VB6中双精度和单精度数据类型的比较
- 单精度实型和双精度实型的有效位数
- Windows中的精度定时问题
- Qt QTimer测试定时精度
- float和double的范围和精度
- 扫描可以使用的NFS Server目录
- 类似于QQ游戏百万人同时在线的服务器架构实现
- 奇艺QSV文件格式简单分析
- 离散数学复习笔记
- 封装PHP mail函数发送HTML邮件并使用中文发件人且自定义发件人邮箱地址
- grttimeofday和xtime的定时精度
- SQL Server内部的内存管理
- 数据结构---队列实现
- 检验字段值是否符合规则
- SQLMAP参数中文解说
- Windows Server 2003 SP2 企业版 ISO 下载 629M
- 深入浅出URL编码
- 好几台机子跑 puppet 出错,郁闷啊
- IOS中的sqlite数据库的使用