Skip to contentSkip to conversation

gettime: fix cpuclock-test on AMD platforms

PATCH In review Vincent Fu 2 participants 3 messages Feb 27, 2024

lore
Share & tools
lore

Vincent Fu

author
#0 lore raw

PATCH 1/1 gettime: fix cpuclock-test on AMD platforms

Starting with gcc 11 __sync_synchronize() compiles to

lock or QWORD PTR [rsp], 0

on x86_64 platforms. Previously it compiled to an mfence instruction.

See line 47 of https://godbolt.org/z/xfE18K7b4 for an example.

On Intel platforms this change does not affect the result of fio's CPU clock test. But on AMD platforms, this change causes fio's CPU clock test to fail and fio to fall back to clock_gettime() instead of using the CPU clock for timing.

This patch has fio explicitly use an mfence instruction instead of __sync_synchornize() in the CPU clock test code on x86_64 platforms in order to allow the CPU clock test to pass on AMD platforms.

---
 arch/arch-x86_64.h | 5 +++++
 arch/arch.h        | 7 +++++++
 gettime.c          | 2 +-
 3 files changed, 13 insertions(+), 1 deletion(-)
arch/arch-x86_64.h +5 -0
index 86ce1b7e..b402dc6d 100644
@@ -26,6 +26,11 @@ static inline unsigned long arch_ffz(unsigned long bitmask)
 	return bitmask;
 }
 
+static inline void tsc_barrier(void)
+{
+	__asm__ __volatile__("mfence":::"memory");
+}
+
 static inline unsigned long long get_cpu_clock(void)
 {
 	unsigned int lo, hi;
arch/arch.h +7 -0
index 3ee9b053..7e294ddf 100644
@@ -108,6 +108,13 @@ extern unsigned long arch_flags;
 #include "arch-generic.h"
 #endif
 
+#if !defined(__x86_64__) && defined(CONFIG_SYNC_SYNC)
+static inline void tsc_barrier(void)
+{
+	__sync_synchronize();
+}
+#endif
+
 #include "../lib/ffz.h"
 /* IWYU pragma: end_exports */
 
gettime.c +1 -1
index bc66a3ac..5ca31206 100644
@@ -623,7 +623,7 @@ static void *clock_thread_fn(void *data)
 			seq = *t->seq;
 			if (seq == UINT_MAX)
 				break;
-			__sync_synchronize();
+			tsc_barrier();
 			tsc = get_cpu_clock();
 		} while (seq != atomic32_compare_and_swap(t->seq, seq, seq + 1));
 
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Vincent Fu

Starting with gcc 11 __sync_synchronize() compiles to

lock or QWORD PTR [rsp], 0

on x86_64 platforms. Previously it compiled to an mfence instruction.

See line 47 of https://godbolt.org/z/xfE18K7b4 for an example.

Quoted 8 lines

On Intel platforms this change does not affect the result of fio's CPU clock test. But on AMD platforms, this change causes fio's CPU clock test to fail and fio to fall back to clock_gettime() instead of using the CPU clock for timing.

This patch has fio explicitly use an mfence instruction instead of __sync_synchornize() in the CPU clock test code on x86_64 platforms in order to allow the CPU clock test to pass on AMD platforms.

Looks good:

Signature · 1 line
Jens Axboe
author Applied
#2 lore raw
Quoted 14 lines · Jens Axboe
Quoted 13 lines · Vincent Fu

Starting with gcc 11 __sync_synchronize() compiles to

lock or QWORD PTR [rsp], 0

on x86_64 platforms. Previously it compiled to an mfence instruction.

See line 47 of https://godbolt.org/z/xfE18K7b4 for an example.

On Intel platforms this change does not affect the result of fio's CPU clock test. But on AMD platforms, this change causes fio's CPU clock test to fail and fio to fall back to clock_gettime() instead of using the CPU clock for timing.

Quoted 8 lines
Quoted 3 lines

This patch has fio explicitly use an mfence instruction instead of __sync_synchornize() in the CPU clock test code on x86_64 platforms in order to allow the CPU clock test to pass on AMD platforms.

Looks good:

Applied. Thanks.