[FFmpeg-cvslog] x86: Serialize rdtsc in read_time()

Henrik Gramner git at videolan.org
Thu Jul 9 12:38:54 CEST 2015


ffmpeg | branch: master | Henrik Gramner <henrik at gramner.com> | Wed Jul  8 22:33:53 2015 +0200| [d1a6cb195f610978ba5d2351e60f938f7f261d59] | committer: Luca Barbato

x86: Serialize rdtsc in read_time()

Improves the accuracy of measurements, especially in short sections.

To quote the Intel 64 and IA-32 Architectures Software Developer's Manual:
"The RDTSC instruction is not a serializing instruction. It does not necessarily
wait until all previous instructions have been executed before reading the counter.
Similarly, subsequent instructions may begin execution before the read operation
is performed. If software requires RDTSC to be executed only after all previous
instructions have completed locally, it can either use RDTSCP (if the processor
supports that instruction) or execute the sequence LFENCE;RDTSC."

SSE2 is a requirement for lfence so only use it on SSE2-capable systems.
Prefer lfence;rdtsc over rdtscp since rdtscp is supported on fewer systems.

Signed-off-by: Luca Barbato <lu_zero at gentoo.org>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=d1a6cb195f610978ba5d2351e60f938f7f261d59
---

 libavutil/x86/timer.h |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/libavutil/x86/timer.h b/libavutil/x86/timer.h
index cdd67dd..bb7c341 100644
--- a/libavutil/x86/timer.h
+++ b/libavutil/x86/timer.h
@@ -30,7 +30,12 @@
 static inline uint64_t read_time(void)
 {
     uint32_t a, d;
-    __asm__ volatile("rdtsc" : "=a" (a), "=d" (d));
+    __asm__ volatile(
+#if ARCH_X86_64 || defined(__SSE2__)
+                     "lfence \n\t"
+#endif
+                     "rdtsc  \n\t"
+                     : "=a" (a), "=d" (d));
     return ((uint64_t)d << 32) + a;
 }
 



More information about the ffmpeg-cvslog mailing list