Time calculation with TSC (Time Stamp Counter)

前端 未结 4 1763
误落风尘
误落风尘 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条回答
  •  太阳男子
    2021-01-05 19:59

    The same WikiPedia article did said about issues with the TSC as below,

    With the advent of multi-core/hyper-threaded CPUs, systems with multiple CPUs, and 
    hibernating operating systems, the TSC cannot be relied on to provide accurate results 
    — unless great care is taken to correct the possible flaws: rate of tick and whether 
    all cores (processors) have identical values in their time-keeping registers. **There 
    is no promise that the timestamp counters of multiple CPUs on a single motherboard will 
    be synchronized**. In such cases, programmers can only get reliable results by locking 
    their code to a single CPU. Even then, the CPU speed may change due to power-saving 
    measures taken by the OS or BIOS, or the system may be hibernated and later resumed 
    (resetting the time stamp counter). In those latter cases, to stay relevant, the 
    counter must be recalibrated periodically (according to the time resolution your 
    application requires).
    

    Meaning modern CPU's can alter their CPU clock rate to save power which can affect the TSC value. Also TSC would never increment in situations like, when kernel may execute HALT and stop processor until an external interrupt is received.

    the second question is that i have intel xeon i3 processor which has 4 processors & 
    each having 2 cores then measuring the clock ticks will give the ticks of single 
    processor or addition of all 4 processors..?
    

    This may lead to a situation where a process could read a time on one processor, move to a second processor and encounter a time earlier than the one it read on the first processor which results in TSC as unstable time source.

提交回复
热议问题