Questions tagged [rdtsc]

RDTSC is the x86 read time stamp counter instruction.

RDTSC is the x86 read time stamp counter instruction often used for high resolution timing.

See How to Benchmark Code Execution Times on Intel® IA-32 and IA-64 Instruction Set Architectures.

Get CPU cycle count? has info on various caveats of using it: on modern x86, it measures reference cycles, not actual core clock cycles. (And also shows how to access it from C++.)

The earliest CPUs to support RDTSC had fixed clock frequency, and some OSes found it was more useful as a low-overhead time source time-of-day functions, so CPU vendors eventually changed it to be how it is now: a fixed-frequency nonstop counter.

It can be out-of-sync across different cores. (Some CPUs avoid that for cores in the same physical package.)

137 questions
3
votes
2 answers

Accessing high Performance Counter in 16-bit Turbo Pascal

I'm trying to use the high performance counter in a 16-bit Turbo Pascal 7 program (don't ask...) running in a WinXP DOS box. I can execute the RDTSC instruction with inline $0F, $31 and the contents of AX and DX are set to what look like sensible…
rossmcm
  • 5,493
  • 10
  • 55
  • 118
3
votes
1 answer

How are clock_gettime and getrusage related?

I am trying to understand how cpu time is computed in Linux. In particular, I would like to focus in this question on clock_gettime and on getrusage. I'm wondering how are these two related. Does one call the other one? Do they both rely on the…
Vincent
  • 57,703
  • 61
  • 205
  • 388
3
votes
3 answers

Time Stamp Counter

I am using time stamp counter in my C++ programme by querying the register. However, one problem I encounter is that the function to acquire the time stamp would acquire from different CPU. How could I ensure that my function would always acquire…
Leslieg
  • 31
  • 1
  • 3
3
votes
1 answer

What is the gcc cpu-type that includes support for RDTSCP?

I am using RDTSCP to replace LFENCE;RDTSC sequences and also get the processor ID back so that I know when I'm comparing TSC values after the thread was rescheduled to another CPU. To ensure I don't run RDTSCP on a too old machine I fallback to…
Peeter Joot
  • 7,848
  • 7
  • 48
  • 82
3
votes
3 answers

There is any way to trigger a legacy mode for RDTSC?

I rewrote the entire question, people clearly weren't understanding it. RDTSC used to count CPU cycles, and it varied with the CPU throttling. Currently, RDTSC don't vary with CPU throttling. Some old applications, expect RDTSC to vary with CPU…
speeder
  • 6,197
  • 5
  • 34
  • 51
3
votes
1 answer

How can I convert this assembly timestamp function to C++?

I am trying to convert someone else's project from 32bit to 64bit. Everything seems to be OK except one function, which uses assembly expressions which are not supported in Visual Studio when building x64: // Returns the Read Time Stamp Counter of…
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
3
votes
2 answers

Function __asm__ __volatile__("rdtsc");

I don't know what exactly does this code: int rdtsc(){ __asm__ __volatile__("rdtsc"); Please, someone can explain me? why "rdtsc"?
user17629
  • 159
  • 2
  • 6
3
votes
1 answer

How to fake RDTSC in linux

i am currently working on a crackme. RDTSC is used in x86 assemblies to get time stamp to match if it is slowed by a debugger or something.The crackme itself is elf32 stripped binary. I am currently working on Macos + VirtualBox Debian32. My…
Eren Yagdiran
  • 338
  • 2
  • 12
3
votes
1 answer

Use rdtsc function in Assembly

I am trying to profile an x86 Assembly program using Ubuntu 12.04. I'd like to use the rdtsc function. The problem is, according to a comment, that I should get the number of cycles in rdx but with the following code I get a too high number: SECTION…
Luigi Tiburzi
  • 4,265
  • 7
  • 32
  • 43
2
votes
1 answer

Read rdtsc in Python

Is there any way to read out the timestamp-counter on x86 CPUs in Python? I know that using rdtscp is bad and using rdtsc is even worse. But trust me I really need that value, or at least some approximation of that value. Any ideas?
Michael
  • 8,920
  • 3
  • 38
  • 56
2
votes
2 answers

what is the C++ equivalent for this assembly code

I'm trying to figure out how to read this assembly code in C++. This is the code: unsigned __int64 high_perf_time; unsigned __int64 *dest = &high_perf_time; __asm { _emit 0xf // these two bytes form the 'rdtsc' asm instruction, _emit…
maor03
  • 23
  • 5
2
votes
1 answer

How to measure the ACTUAL number of clock cycles elapsed on modern x86?

On recent x86, RDTSC returns some pseudo-counter that measures time instead of clock cycles. Given this, how do I measure actual clock cycles for the current thread/program? Platform-wise, I prefer Windows, but a Linux answer works too.
user541686
  • 205,094
  • 128
  • 528
  • 886
2
votes
1 answer

x86_64 - Why is timing a program with rdtsc/rdtscp giving unreasonably large numbers?

I'm trying to time a subroutine using rdtscp. This is my procedure: ; Setting up time rdtscp ; Getting time push rax ; Saving timestamp ; for(r9=0; r9
Luiz Martins
  • 1,644
  • 10
  • 24
2
votes
0 answers

Why is row-major matrix access runs slower than column-major matrix access in my C code?

I'm implementing a C code to compare the effect of spatial locality when accessing a matrix in a row-major order and in a column-major order. However, the row-major way got a slower result, which is not what I've learned. I'm running on…
tgudtk
  • 39
  • 5
2
votes
1 answer

solution to rdtsc out of order execution?

I am trying to replace clock_gettime(CLOCK_REALTIME, &ts) with rdtsc to benchmark code execution time in terms of cpu cycles rather than server time. The execution time of the bench-marking code is critical for the software. I have tried running…