X86 processor time sources

Does anyone know x86 instructions that can be used to measure time? Is a timer that leads to task switching software accessible?

+4
source share
3 answers

Methods of measuring time on the x86 platform:

  • Real-time clock - a source of time and date for your OS. Accuracy is 1 second. The only time source on a standard PC that can be used to measure absolute time.

  • 8254 Counter / Timers is a standard counter / timer chip that has been present on motherboards from the very beginning of the PC (now a function block inside the chipset). This is the traditional source of IRQ0, a timer interrupt that switches the task on most older operating systems.

  • Assembly instruction RDTSC - counts CPU clock cycles. See Anon ymous Answer for some usage details. A pretty high level of accuracy that you can find for an x86 time source. However, it has some gotchas with accuracy. Also the most convenient option if you write in the assembly.

  • RDTSCP assembly instruction - similar to RDTSC, but serialized, which eliminates some accuracy issues with RDTSC. Only on the latest processors.

  • HPET - Introduced in the era of Core Duo for PC. Designed to replace the venerable 8254. Modern operating systems will use this as their interruption in task scheduling (Vista and later).

  • Own timers in the chipset. Some chipsets have special timers built into them for power management and multimedia functions. Sometimes you can use them for your application, assuming that you are dealing with an embedded system with a fixed function, and not with a general-purpose PC.

Please note that not all of these options will be available to you depending on your OS and hardware. If you are running a modern OS (Windows, Linux), it will take over the management of 8254 / HPET for its own needs of the time, and therefore, they will not be available to you.

In a modern operating system, it is usually better to use the synchronization features with the OS. OS developers have probably developed many problems that you yourself will encounter if you try to use it. (Note that the OS may provide several synchronization functions. Choose the one that suits your application.)

+9
source

You can use rdtsc . Just subtract the previous value from the current value to calculate the time difference.

Loads the current processor tag time counter (a 64-bit MSR) into the EDX: EAX registers. The EDX register is loaded with 32-bit MSR and EAX register with 32 low-order bits. (On processors supporting Intel 64 architectures, 32 bits of each of the RAX and RDX are cleared.)

This is the C code that 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);; } 
+2
source

The following is the procedure to enable FORTRAN:

The RDTSC assembler instruction returns a 64-bit integer, which is the number of CPU clocks counted from the moment of the year. If your FORTRAN has 64-bit integers, the KOUNT argument is declared as INTEGER * 8. Else declares that it is an array of two 32-bit INTEGER * 4

So in your FORTRAN program you write

 CALL TIMERR(KOUNT) 

at the beginning, save the KOUNT value, then repeat at the end. Then subtract two 64-bit values ​​to determine elapsed time. Usually I just want to subtract the bottom two words, like the fact that I time is usually less than 2 ^ 32 system hours

It can also be called from C, but I do not say C.

 ; C This assembler routine looks to FORTRAN like this: ; SUBROUTINE TIMERR(KOUNT) ; INTEGER*4 KOUNT(2); or INTEGER*8 KOUNT ; ...get a 64-but system time value into KOUNT...... ; RETURN ; END ; .Code _TIMERR@4 : RDTSC Push Eax Push Ecx Push Edx Mov Ecx, [Esp + 16] Mov [Ecx], Eax Mov [Ecx + 4], Edx Pop Edx Pop Ecx Pop Eax Ret 4 
0
source

All Articles