使用TSC(时间戳计数器)进行时间计算 [英] Time calculation with TSC (Time Stamp Counter)

查看:658
本文介绍了使用TSC(时间戳计数器)进行时间计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测量Linux内核模块中Linux内核中的某些代码所花费的时间.

I am trying to measure the time taken by some code inside Linux kernel at very high accuracy by a Linux kernel module.

为此,我尝试了 rdtscl(),它给出了以下代码中使用的时钟滴答数:

For this purpose, I have tried rdtscl() which gives the number of clock ticks used in the code as given below:

unsigned long ini, end;
rdtscl(ini);
//some code...
rdtscl(end);
printk("time taken=%lu ticks",end-ini);

正如我所提到的 http://en.wikipedia.org/wiki/Time_Stamp_Counter表示 TSC是奔腾以来所有x86处理器上都存在的64位寄存器.那么,如果我有双核处理器,那么这个计数器会出现在两个核中,还是因为只有一个处理器却是双核而只有一个?

As I have refered to http://en.wikipedia.org/wiki/Time_Stamp_Counter which says that TSC is a 64-bit register present on all x86 processors since the Pentium. So, if I have dual core processor, will this counter be present in both cores or there will be only one since it is only one processor but dual core?

第二个问题是:我有Intel Xeon i3处理器,该处理器具有4个处理器,每个处理器具有2个内核.然后,测量时钟滴答声,会给出单个处理器的滴答声还是所有4个处理器的滴答声?

The second question is that: I have Intel Xeon i3 processor which has 4 processors, each of them having 2 cores. Then, measuring the clock ticks, will give the ticks of single processor or addition of all 4 processors?

推荐答案

如果没有时钟滴答声,则说明您的代码存在严重问题.您是否编写了自己的rdtscl [或从不是很好来源的地方复制了它?]

If you get NO clock ticks, then there's something seriously wrong with your code. Did you write your own rdtscl [or copy it from somewhere that isn't a good source?]

顺便说一句,现代的Intel(和AMD)处理器很可能具有恒定TSC",因此停顿,睡眠,运行速度较慢等处理器仍会以与其他处理器相同的速率跳动-可能仍然不同步,但这是另一回事.

By the way, modern Intel (and AMD) processors may well have "constant TSC", so a processor that is halted, sleeping, running slower, etc, will still tick away at the same rate as the others - it may not be in sync still, but that's a different matter.

尝试仅运行一个循环,该循环从计数器中打印出值-仅RDTSC指令本身应花费大约30-50个时钟周期,因此您应该看到它在移动.

Try running just a loop that prints the value from your counter - just the RDTSC instruction itself should take some 30-50 clock cycles, so you should see it moving.

这是我的rdtsc函数:

Here's my rdtsc function:

void rdtscl(unsigned long long *ll)
{
    unsigned int lo, hi;
    __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));                        
    *ll = ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );  
}

作为返回值的函数:

unsigned long long rdtscl(void)
{
    unsigned int lo, hi;
    __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));                        
    return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );  
}

我注意到您的代码没有传递无符号长整数的指针,这使我怀疑您实际上没有将时间戳计数器BACK传递给调用者,而是只是保留了它恰好具有的任何值-这可能两个值都一样.

I notice that your code doesn't pass a pointer of your unsigned long, which makes me suspect that you are not actually passing the timestamp counter BACK to the caller, but rather just keeping whatever value it happens to have - which may well be the same for both values.

这篇关于使用TSC(时间戳计数器)进行时间计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆