Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time sources in x86 processors

Does anybody know x86 instructions that can be used to measure time? Is the timer that leads to task switches accessible by software?

like image 248
user1284064 Avatar asked Sep 01 '12 16:09

user1284064


People also ask

What is TSC clock source?

Reading Hardware Clock Sources. Reading from the TSC means reading a register from the processor. Reading from the HPET clock means reading a memory area. Reading from the TSC is faster, which provides a significant performance advantage when timestamping hundreds of thousands of messages per second.

What is kernel clock?

The other is the "system clock" (sometimes called the "kernel clock" or "software clock") which is a software counter based on the timer interrupt. It does not exist when the system is not running, so it has to be initialized from the RTC (or some other time source) at boot time.

What is HPET Linux?

The High Precision Event Timer (HPET) hardware follows a specification by Intel and Microsoft, revision 1. Each HPET has one fixed-rate counter (at 10+ MHz, hence “High Precision”) and up to 32 comparators.


2 Answers

Ways to measure time on an x86 platform:

  • Real Time Clock - The source of time and date for your OS. 1 second precision. The only time source in a standard PC that can be used to measure absolute time.

  • 8254 Counter/Timers - A standard counter/timer chip that has been present on motherboards since the dawn of PC (now a functional block inside the chipset). This is the traditional source of IRQ0, the timer interrupt that does task switching in most older OSes.

  • RDTSC assembly instruction - Counts CPU clock cycles. See the answer Anon ymous posted for some usage details. Pretty much the highest level of precision you can find for a time source on x86. However, it has some gotchas with accuracy. Also the most convenient option if you are writing in assembly.

  • RDTSCP assembly instruction - Similar to RDTSC, but is serialized, which resolves some of the accuracy issues with RDTSC. Only found on the newest processors.

  • HPET - Introduced around the Core Duo era of PCs. Intended to be a replacement for the venerable 8254. Modern OSes will use this as their task scheduling interrupt (Vista and later)

  • Proprietary Timers in the Chipset - Some chipsets have special timers built into them for Power Management and Multimedia functions. You can sometimes commandeer these for your own application, assuming you are dealing with a fixed-function embedded system and not a general purpose PC.

Note that not all of these options will be available to you depending on your OS and Hardware. If you are running under a modern OS (Windows, Linux), it will take control of the 8254/HPET for its own timing needs, and thus they will be unavailable for you.

Under a modern Operating System, it is usually best to use the OS-provided timing functions. The OS developers have probably worked out a lot of the issues that you will run into on your own if you try to use it. (Note that the OS may provide multiple timing functions. Pick the one that is suitable for your application.)

like image 103
myron-semack Avatar answered Jan 02 '23 21:01

myron-semack


You can use rdtsc.Just deducts previous value from present value to calculate between time difference.

Loads the current value of the processor’s time-stamp counter (a 64-bit MSR) into the EDX:EAX registers. The EDX register is loaded with the high-order 32 bits of the MSR and the EAX register is loaded with the low-order 32 bits. (On processors that support the Intel 64 architecture, the high-order 32 bits of each of RAX and RDX are cleared.)

This is a C code which implements this instruction :-

unsigned long long int rdtsc(void)
{
   unsigned long long int x;
   unsigned a, d;

   __asm__ volatile("rdtsc" : "=a" (a), "=d" (d));

   return ((unsigned long long)a) | (((unsigned long long)d) << 32);;
}
like image 38
perilbrain Avatar answered Jan 02 '23 20:01

perilbrain