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
1
vote
2 answers

Using rdtsc + rdtscp across context switch

I am trying to write a program to measure context switch. I have gone through this Intel's manual about rdtsc + rdtscp instructions. Now, I want to use these time-stamp instructions across context switch. I have the general skeleton as follows: //…
1
vote
1 answer

RDTSC slow in Ubuntu

I have a piece of inlined assembly that I compile with clang++: asm volatile ("LFENCE\n\t" "RDTSC\n\t" "shl $32, %%rdx\n\t" "or %%rdx, %%rax\n\t" : "=a" (retval) :: "%rax",…
Thomas Kejser
  • 1,264
  • 1
  • 10
  • 30
1
vote
2 answers

Cycle counts for nanosleep using RDTSC

I am trying to see how many cycles does 1 nanosecond takes on my laptop. Laptop Config : Processor : Intel Centrino Duo 2.0 GHz ( T7200 ) Memory : 2 GB Following are the counts : 1 second takes : 1995198000 ( which is close to 2 billion ticks,…
dudedev
  • 451
  • 1
  • 5
  • 19
1
vote
2 answers

Experienced strange rdtsc behavior comparing physical hardware and kvm-based VMs

I have a following problem. I run several stress tests on a Linux machine $ uname -a Linux debian 3.14-2-686-pae #1 SMP Debian 3.14.15-2 (2014-08-09) i686 GNU/Linux It's an Intel i5 Intel(R) Core(TM) i5-2400 CPU @ 3.10GHz, 8 G RAM, 300 G…
Eryk
  • 11
  • 2
1
vote
1 answer

How to detect rdtscp support in Visual C++?

I have a piece of code running on MSVC 2012: #include #include UINT64 gettime() { try { unsigned int ui; return __rdtscp(&ui); } catch (...) { return __rdtsc(); } } I was trying to use __rdtscp() to get…
stanleyli
  • 1,427
  • 1
  • 11
  • 28
1
vote
1 answer

Why rdtscp doesn't return the core id?

I have this code #include #include unsigned long long rdtscp(unsigned int* aux) { // For IA32 unsigned long long x; asm volatile("rdtscp" : "=c" (*aux), "=A" (x) ::); return x; } int main() { unsigned int …
e271p314
  • 3,841
  • 7
  • 36
  • 61
1
vote
1 answer

How to modify the implementation of x86 assembly instructions on QEMU Virtual Machine

I want to modify the implementation of rdtsc assembly instruction, i.e., I want to modify at the fundamental level, what happens when rdtsc assembly instruction is invoked. I am working on a QEMU Virtual Machine running on an Intel Core2 Duo…
hardcoder
  • 415
  • 1
  • 5
  • 13
1
vote
0 answers

How to use rdtsc in c#?

I'm working on a performance counter, which will be always running inside the app, ever a release version. So, it needs to be as fast as possible. I did compare c#'s StopWatch and c++ rdtsc(), and with 100 000 000 cycles taking 3 secs rdtsc() is a…
Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224
1
vote
1 answer

rdtsc() giving strange results

I have written a very simple C program in an attempt to understand rdtsc in C (Linux).The program is given below. #include static inline unsigned long long tick() { unsigned long long d; __asm__ __volatile__ ("rdtsc" :…
liv2hak
  • 14,472
  • 53
  • 157
  • 270
1
vote
2 answers

Assembly rdtsc x64 ubuntu

I'm trying to use rdtsc function but i've got weird numbers. I'm trying to call this function from C code and pass the tick back to function. Can you tell me if im doing it right or not ? Asm code: .text .globl czas .type czas,…
Gravian
  • 957
  • 2
  • 11
  • 20
1
vote
1 answer

Error profiling with embedded assembly language in C++ code

I found this article on the efficiency of std::vector::push_back, the associated code can be found here. I tried it myself and I got an illegal instruction (core dumped), gdb indicates the error occurs on line 37. I compiled using gcc 4.7.2, on a…
mkm
  • 673
  • 5
  • 21
1
vote
1 answer

Undefined reference to rdtsc

I'm writing code that creates trees and times different methods of creating trees. I can't seem to get rdtsc to function properly, though. Here's my code: #include #include #define SIZE 10 struct tnode { …
Greg
  • 65
  • 4
  • 11
0
votes
0 answers

how to use `__rdtsc` properly in linux x86 gcc

In linux, the gcc compiler has the intrinsic function __rdtsc to measure the cpu cycles. So I don't need to use inline asm code, which I am not familiar with. On the other hand, when reading posts about the asm code, I saw people saying that rdtsc…
doraemon
  • 2,296
  • 1
  • 17
  • 36
0
votes
1 answer

Best practice of using __rdtsc

I am new to system programing, and I have some doubts about how to use __rdtsc. Here is a quote from Microsoft Learn: Generates the rdtsc instruction, which returns the processor time stamp. The processor time stamp records the number of clock…
chenzhongpu
  • 6,193
  • 8
  • 41
  • 79
0
votes
1 answer

How stable / constant is the timestamp counter (RDTSC) with temperature and over time?

x86-CPUs have invariant TSCs for a long time, i.e. they change the timestamp counter according to a constant frequency, usually the base-clock of the CPU. If Windows detects an invariant TSC it depends it's QueryPerformanceCounter() on this…
Bonita Montero
  • 2,817
  • 9
  • 22