grttimeofday和xtime的定时精度

本文探讨了gettimeofday函数在特定系统配置下(内核2.6.27.8,HZ=100)的精度问题,发现其微秒级时间戳的后四位始终为零,仅能提供10毫秒级别的精度。通过分析内核源代码,揭示了这种现象的原因在于xtime变量的更新机制及其与系统节拍的关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

客户反映,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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值