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
2
votes
2 answers

Is mfence for rdtsc necessary on x86_64 platform?

unsigned int lo = 0; unsigned int hi = 0; __asm__ __volatile__ ( "mfence;rdtsc" : "=a"(lo), "=d"(hi) : : "memory" ); mfence in the above code, is it necessary? Based on my test, cpu reorder is not found. The fragment of test code is included…
Scy
  • 488
  • 3
  • 11
2
votes
1 answer

Seconds calculation using rdtsc

Here is the code to calculate CPU time but it's not correct because when I use gettimeofday it gives me correct time in ms. I am running my process on one processor and its clock runs at 800MHz. My knowledge about rdtsc is as follows: Rdtsc returns…
Anonymous
  • 93
  • 2
  • 9
2
votes
2 answers

__rdtscp calibration unstable under Linux on Intel Xeon X5550

I'm trying to use __rdtscp intrinsinc function to measure time intervals. Target platform is Linux x64, CPU Intel Xeon X5550. Although constant_tsc flag is set for this processor, calibrating __rdtscp gives very different results: $ taskset -c 1…
Rost
  • 8,779
  • 28
  • 50
2
votes
3 answers

What is the most reliable way to measure the number of cycles of my program in C?

I am familiar with two approaches, but both of them have their limitations. The first one is to use the instruction RDTSC. However, the problem is that it doesn't count the number of cycles of my program in isolation and is therefore sensitive to…
Ivaylo Toskov
  • 3,911
  • 3
  • 32
  • 48
2
votes
1 answer

How to benchmark in Qemu i386 system using rdtsc

Currently I am trying to measure number of clock cycles taken to complete an operation by two different programming languages on same environment. (without using an OS) Currently I am using Qemu-i386 emulator and using rdtsc to measure the clock…
madstr
  • 23
  • 6
2
votes
1 answer

Intercept rdtsc instruction from guest vm userspace in KVM

I'm stuck in the problem as the title says.I want to do this in VMM by adding the CPU_BASED_RDTSC_EXITING flag in vmx.c(arch/x86/kvm) in setup_vmcs_config function,and then handle the vm_exit by myself(ref this:mail list).The question is that I…
ioilala
  • 277
  • 2
  • 10
2
votes
1 answer

Measuring time: differences among gettimeofday, TSC and clock ticks

I am doing some performance profiling for part of my program. And I try to measure the execution with the following four methods. Interestingly they show different results and I don't fully understand their differences. My CPU is Intel(R) Core(TM)…
Neo1989
  • 285
  • 3
  • 14
2
votes
1 answer

Where is the code for RDTSC handler in QEMU source code?

I am working on an application which requires me to make some changes with the part of the QEMU source code which deals with RDTSC calls. However, I am not able to locate the same in the huge source code.
hardcoder
  • 415
  • 1
  • 5
  • 13
2
votes
1 answer

ASM clobber help. Do you need to clobber ax, dx or memory with rdtsc?

I don't understand a lot of ASM. I recently ran across inline gcc asm which reads: ("rdtsc;movl %%eax,%0":"=m"(x)::"ax","dx") It looks to me as though that puts the lower 32 bits of the counter in x, a 32-bit unsigned integer. What I don't…
loop
  • 3,460
  • 5
  • 34
  • 57
2
votes
3 answers

RDTSC on VisualStudio 2010 Express - C++ does not support default-int

I tried to test rdtsc on VisualStudio 2010. Heres my code: #include #include #include using namespace std; uint64_t rdtsc() { return __rdtsc(); } int main() { cout << rdtsc() << "\n"; cin.get(); …
Brian Brown
  • 3,873
  • 16
  • 48
  • 79
2
votes
7 answers

How do I ensure my program runs from beginning to end without interruption?

I'm attempting to time code using RDTSC (no other profiling software I've tried is able to time to the resolution I need) on Ubuntu 8.10. However, I keep getting outliers from task switches and interrupts firing, which are causing my statistics to…
Matthew Iselin
  • 10,400
  • 4
  • 51
  • 62
1
vote
2 answers

Coordinating Times From /sys/kernel/debug/tracing In Ubuntu

I am trying to gather data from multiple sources on an Ubuntu 10.10 machine programatically about a program's performance. For all of my other sources, I have been able to gather them using the RDTSC x86 instruction, and then scale them using…
Dave McGinnis
  • 585
  • 4
  • 17
1
vote
0 answers

rdtsc under WSL2 lacks nonstop_tsc?

I'm doing some microbenchmarking of short code snippets using the __rdtsc intrinsic inside WSL2 on Windows 11. I'm noticing that there is a lot more variance in the results than I'm used to from working on machines that are running Linux on bare…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
1
vote
0 answers

Why aren't mfence;RDTSCP timings closer to equal across repeated calls after warm-up? +-2% variation over ~3300 TSC counts

I am writing code to compare the execution times of different versions of a bigint add function, on AMD FX(tm)-8350 Eight-Core Processor 4.00 GHz. I need help to make sense of the machine behaviour that I’m observing. The code that I’m timing…
user1752563
  • 149
  • 9
1
vote
0 answers

Configure qemu KVM-SVM to not emulate rdtscp and get valid timestamp

I am trying to measure the cycle count of an instruction in a VM -- my code looks like this: start = rdtscp(); //complex_sequence_of_instructions end = rtdscp(); //complex_sequence_of_instructions took end-time cycles To the best of my knowledge,…