clock_gettime 不能立即更新 [英] clock_gettime can not update instantly
问题描述
更新
检查时间分辨率后,我们尝试在内核空间调试问题.
After checking the time resolution, we tried to debug the problem in kernel space.
unsigned long long task_sched_runtime(struct task_struct *p)
{
unsigned long flags;
struct rq *rq;
u64 ns = 0;
rq = task_rq_lock(p, &flags);
ns = p->se.sum_exec_runtime + do_task_delta_exec(p, rq);
task_rq_unlock(rq, &flags);
//printk("task_sched runtime\n");
return ns;
}
我们的新实验表明时间 p->se.sum_exec_runtime
不会立即更新.但是如果我们在函数内部添加 printk()
.时间会即时更新.
Our new experiment shows that the time p->se.sum_exec_runtime
is not updated instantly. But if we add printk()
inside the function. the time will be updated instantly.
旧
我们正在开发一个 Android 程序.但是,函数 threadCpuTimenanos()
测量的时间在我们的平台上并不总是正确的.
We are developing an Android program.
However, the time measured by the function threadCpuTimenanos()
is not always correct on our platform.
经过实验,我们发现clock_gettime
返回的时间不是即时更新的.即使经过几次 while 循环迭代,我们得到的时间仍然没有改变.
After experimenting, we found that the time returned from clock_gettime
is not updated instantly.
Even after several while loop iterations, the time we get still doesn't change.
这是我们的示例代码:
while(1)
{
test = 1;
test = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now);
printf(" clock gettime test 1 %lx, %lx , ret = %d\n",now.tv_sec , now.tv_nsec,test );
pre = now.tv_nsec;
sleep(1);
}
此代码在 x86 PC 上运行正常.但是它不能在我们的内核为 2.6.35.13 的嵌入式平台 ARM Cortex-A9 中正确运行.
This code runs okay on an x86 PC. But it does not run correctly in our embedded platform ARM Cortex-A9 with kernel 2.6.35.13.
有什么想法吗?
推荐答案
我更改了 clock_gettime 以使用 CLOCK_MONOTONIC_RAW ,将线程分配给一个 CPU,但我得到了不同的值.我也在用双皮质-A9
I changed the clock_gettime to use the CLOCK_MONOTONIC_RAW , assigned the thread to one CPU and I get different values. I am also working with a dual cortex-A9
while(1)
{
test = 1;
test = clock_gettime(CLOCK_MONOTONIC_RAW, &now);
printf(" clock gettime test 1 %lx, %lx , ret = %d\n",now.tv_sec , now.tv_nsec, test );
pre = now.tv_nsec;
sleep(1);
}
这篇关于clock_gettime 不能立即更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!