MPS issue job003969

TitleEvent clock implementation may not be reliable
Statusopen
Priorityoptional
Assigned userGareth Rees
OrganizationRavenbrook
DescriptionThe event clock implementation reads the Time Stamp Counter via the RDTSC instruction. But this may not be reliable.

Wikipedia says [1]: "With the advent of multi-core/hyper-threaded CPUs, systems with multiple CPUs, and hibernating operating systems, the TSC cannot be relied on to provide accurate results. ... There is no promise that the timestamp counters of multiple CPUs on a single motherboard will be synchronized. In such cases, programmers can only get reliable results by locking their code to a single CPU."

Windows documentation says [2]: "We strongly discourage using the RDTSC or RDTSCP processor instruction to directly query the TSC because you won't get reliable results on some versions of Windows, across live migrations of virtual machines, and on hardware systems without invariant or tightly synchronized TSCs."
AnalysisIt looks as if QueryPerformanceCounter [3] may be the right API on Windows, and clock_gettime(CLOCK_MONOTONIC) [4] on POSIX. But are these fast enough? If they use system calls then there are not so good.

See "Pitfalls of TSC usage" [5], which discusses the situation on Linux.
How foundinspection
Evidence[1] <https://en.wikipedia.org/wiki/Time_Stamp_Counter>
[2] <https://msdn.microsoft.com/en-us/libra...3408%28v=vs.85%29.aspx#direct_tsc_usage>
[3] <https://msdn.microsoft.com/en-us/library/windows/desktop/ms644904.aspx>
[4] <http://pubs.opengroup.org/onlinepubs/009695399/functions/clock_getres.html>
[5] http://oliveryang.net/2015/09/pitfalls-of-TSC-usage/
Created byGareth Rees
Created on2016-03-06 21:51:32
Last modified byGareth Rees
Last modified on2016-09-18 20:35:57
History2016-03-06 GDR Created.