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

Is there any way to fake __rdtsc on Windows

I just wonder to know, is there any change to manipulate it or break it on Windows? For example ivgot a code like that: while (1) { auto v6 = __rdtsc(); switch (v6 & 0xF) { case 1ui64: …
ayya
  • 71
  • 5
1
vote
0 answers

C# executable (get rdtsc timestamp) crashes intermittently

I have copied this code that queries rdtsc timestamp in C#. The equivalent in C++ is just __rdtsc() using System.Diagnostics; using System.Runtime.InteropServices; using System.Security; public static class Rdtsc { …
Eduard Rostomyan
  • 7,050
  • 2
  • 37
  • 76
1
vote
2 answers

In a busy loop, two continuously getting time encounters a big time gap

In a busy loop, some codes contiguously get the current time twice and count their time interval. The time interval is supposed to be small, since the codes belong to a SCHED_FIFO process with highest priority(99) and could not be interrupted by…
foool
  • 1,462
  • 1
  • 15
  • 29
1
vote
1 answer

How to specify %edx to be the output instead of conventional %eax in inline assembly in C?

I am trying to use the following inline assembly in C to read the high word (%edx) of Time Stamp Counter for time measurement: unsigned int tsc; asm volatile ("rdtscp; xchgl %%edx, %%eax" : "=r" (tsc)); Unfortunately, the code crashes. If the xchgl…
zzzhhh
  • 319
  • 1
  • 8
1
vote
0 answers

What is the difference between inline assembly call and direct call?

What is the difference between using thet= __rdtsc() function directly and using asm __volatile__ ( "rdtsc \n" : "=a" (t)); I have seen on stackoverflow that many people do not recommend using inline assembly, but…
Gerrie
  • 736
  • 3
  • 18
1
vote
0 answers

How to use the output of RDTSC

My code is very simple: global start extern printf, Sleep section .data: string db 'End: %llu', 0Ah, 'Start: %llu', 0 section .text: start: mfence lfence rdtsc sub rsp, 8 mov [rsp + 4], eax mov [rsp], edx mov rcx,…
DarkAtom
  • 2,589
  • 1
  • 11
  • 27
1
vote
1 answer

Loop takes less than 1 cycle despite dependency between iterations

I wanted to benchmark the time needed to do a single addition on my Skylake (i5-6500) CPU. C is low-level enough for me, so I wrote the following code: // Initializing stuffs int a = rand(); int b = rand(); const unsigned long loop_count =…
Dada
  • 6,313
  • 7
  • 24
  • 43
1
vote
1 answer

Why is nop not taking one clock cycle

I wrote a basic code to find out the amount of clock cycles taken by nop. We know nop takes one clock cycle. #include #include #include int main(void) { uint32_t low1, low2, high1, high2; uint64_t…
md.jamal
  • 4,067
  • 8
  • 45
  • 108
1
vote
0 answers

How to count cycles when TscInvariant = True?

The command rdtsc should be reliable source of time if the "TscInvariant" CPUID feature is enabled (see for example this link). On a dynamic frequency rescaling context, a reliable source of time cannot be a reliable source of cycles. On…
diegor
  • 542
  • 2
  • 15
1
vote
1 answer

Program error that measures the parameters of matrix multiplication

I'm writing a program that measures the performance of matrix multiplication with rdtsc(), Read Time Stamp Counter. However, when I combined the matrix multiplication program and performance measurement program, errors appeared. I would appreciate…
NPP
  • 299
  • 2
  • 4
  • 11
1
vote
1 answer

RDTSC and system calls, sys_read and sys_write

I'm trying to count, using rdtsc, how many cycles it takes write something using sys_write. I was able to test the printf and scanf functions. They worked correctly, and now I have a problem with system calls. In my opinion, the problem is with %eax…
delvian
  • 15
  • 3
1
vote
1 answer

assembler benchmarking on intel using rdtsc is giving strange answers, why?

A while ago, I asked a question on stack overflow and was shown how to execute the rdtsc opcode in C++. I recently created a benchmark function using rdtsc as follows: inline unsigned long long rdtsc() { unsigned int lo, hi; asm volatile ( …
Dov
  • 8,000
  • 8
  • 46
  • 75
1
vote
0 answers

VMX performance issue with rdtsc (no rdtsc exiting, using rdtsc offseting)

I am working a Linux kernel module (VMM) to test Intel VMX, to run a self-made VM (The VM starts in real-mode, then switches to 32bit protected mode with Paging enabled). The VMM is configured to NOT use rdtsc exit, and use rdtsc offsetting. Then,…
wangt13
  • 959
  • 7
  • 17
1
vote
0 answers

how to use rdtscp correctly?

according to 《How to Benchmark Code Execution Times on Intel® IA-32 and IA-64 Instruction Set Architectures》, i use code below: static inline uint64_t bench_start(void) { unsigned cycles_low, cycles_high; asm volatile("CPUID\n\t" …
JunChan
  • 11
  • 2
1
vote
1 answer

Converting a uint64_t rdtsc value to a uint32_t

I have a RNG function xorshift128plus that takes a Xorshift128PlusKey: /** * \brief Keys for scalar xorshift128. Must be non-zero. * These are modified by xorshift128plus. */ struct Xorshift128PlusKey { …
Coder32
  • 181
  • 1
  • 10