Does anybody know x86 instructions that can be used to measure time? Is the timer that leads to task switches accessible by software?
相关问题
- Timer vs. repetitive background worker
- Where can the code be more efficient for checking
- NASM x86 print integer using extern printf
- setInterval doesn't slow down on inactive tab
- “rdtsc”: “=a” (a0), “=d” (d0) what does this do? [
相关文章
- Is it possible to run 16 bit code in an operating
- x86 instruction encoding tables
- x86 Program Counter abstracted from microarchitect
- Is the HPET directly accessible in Windows?
- Assembler : why BCD exists?
- Visual Studio: How to properly build and specify t
- Blinking Button for Simon Says
- Timer Label not updated after switching views (swi
You can use
rdtsc
.Just deducts previous value from present value to calculate between time difference.This is a C code which implements this instruction :-
Here is a FORTRAN callable routine that does it:
The assembler instruction RDTSC returns a 64-bit integer which is the number of CPU clock counts since the year dot. If your FORTRAN has 64-bit integers, the argument KOUNT is just declared to be INTEGER*8. Else declare it to be an array of two 32-bit INTEGER*4's
So in your FORTRAN program you write
at the start, save the value of KOUNT, then repeat at the end. Then subtract the two 64-bit values to determine the elapsed time. I generally only bother to subtract the two lower words, as what I time is usually less than 2^32 system clocks
It's also callable from C, but I don't speak C.
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.)