Time calculation with TSC (Time Stamp Counter)

前端 未结 4 1784
误落风尘
误落风尘 2021-01-05 19:41

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

For this purpose, I have tried rdtscl

4条回答
  •  Happy的楠姐
    2021-01-05 19:49

    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?]

    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.

    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.

    Edit: 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 );  
    }
    

    alernatitvely, as a function returning a value:

    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 );  
    }
    

    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.

提交回复
热议问题