Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit ea34a48

Browse files
committed
Serialize rdtsc instruction on x86_64
Unfortunately rdtsc can produce skewed results without mfence sometimes. This is what GNU/Linux does in rdtsc_ordered function. ref: https://github.com/torvalds/linux/blob/03b9730b769fc4d87e40f6104f4c5b2e43889f19/arch/x86/include/asm/msr.h#L130-L154
1 parent 932d1c0 commit ea34a48

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

src/rt/trace.d

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,10 @@ else
862862
asm
863863
{
864864
naked ;
865+
// rdtsc can produce skewed results without preceding lfence/mfence.
866+
// this is what GNU/Linux does, but only use mfence here.
867+
// see https://github.com/torvalds/linux/blob/03b9730b769fc4d87e40f6104f4c5b2e43889f19/arch/x86/include/asm/msr.h#L130-L154
868+
mfence ; // serialize rdtsc instruction.
865869
rdtsc ;
866870
mov [RDI],EAX ;
867871
mov 4[RDI],EDX ;

0 commit comments

Comments
 (0)