Skip to content

Commit b9c1863

Browse files
TejeshPalaTomTheBear
authored andcommitted
Fix RDTSC measurements for accuracy:
- serialization - OOE Signed-off-by: Tejesh Pala <tejesh.pala@fau.de>
1 parent ec5a648 commit b9c1863

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/timer.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,29 @@ void (*TSTOP)(TscCounter*) = NULL;
6262
static void
6363
fRDTSC(TscCounter* cpu_c)
6464
{
65-
__asm__ volatile("xor %%eax,%%eax\n\t" \
66-
"cpuid\n\t" \
67-
"rdtsc\n\t" \
68-
"movl %%eax, %0\n\t" \
69-
"movl %%edx, %1\n\t" \
65+
__asm__ volatile(
66+
"mfence\n\t" \
67+
"lfence\n\t" \
68+
"rdtsc\n\t" \
69+
"movl %%eax, %0\n\t" \
70+
"movl %%edx, %1\n\t" \
71+
"lfence\n\t" \
7072
: "=r" ((cpu_c)->int32.lo), "=r" ((cpu_c)->int32.hi) \
71-
: : "%eax","%ebx","%ecx","%edx");
73+
: : "%rax", "rbx", "rcx", "%rdx", "memory");
7274
}
7375

7476
static void
7577
fRDTSC_CR(TscCounter* cpu_c)
7678
{
77-
__asm__ volatile( \
78-
"rdtsc\n\t" \
79-
"movl %%eax, %0\n\t" \
80-
"movl %%edx, %1\n\t" \
79+
__asm__ volatile(
80+
"lfence\n\t" \
81+
"rdtscp\n\t" \
82+
"movl %%eax, %0\n\t" \
83+
"movl %%edx, %1\n\t" \
84+
"lfence\n\t" \
85+
"cpuid\n\t" \
8186
: "=r" ((cpu_c)->int32.lo), "=r" ((cpu_c)->int32.hi) \
82-
: : "%eax","%ebx","%ecx","%edx");
87+
: : "%rax", "rbx", "rcx", "%rdx", "memory");
8388
}
8489
#ifndef __MIC__
8590
static void

0 commit comments

Comments
 (0)