Accuracy of System.nanoTime() to measure time elapsed decreases after a call to Thread.sleep()

后端 未结 3 2009
醉酒成梦
醉酒成梦 2021-01-21 08:38

I\'m encountering a really unusual issue here. It seems that the calling of Thread.sleep(n), where n > 0 would cause the following System.nanoTime() calls to be less predictable

相关标签:
3条回答
  • 2021-01-21 09:00

    The inconsistencies probably arise not from Java, but from the different OSs and VMs "atomic-" or system- clocks themselves.

    According to the official .nanoTime() documentation:

    no guarantees are made except that the resolution is at least as good as that of currentTimeMillis()

    source

    ...I can tell from personal knowledge that this is because in some OSs and VMs, the system itself doesn't support "atomic" clocks, which are necessary for higher resolutions. (I will post the link to source this information as soon as I find it again...It's been a long time.)

    0 讨论(0)
  • 2021-01-21 09:08

    I can suggest at least two possible reasons of such behavior:

    1. Power saving. When executing a busy loop, CPU runs at its maximum performance state. However, after Thread.sleep it is likely to fall into one of power-saving states, with frequency and voltage reduced. After than CPU won't return to its maximum performance immediately, this may take from several nanoseconds to microseconds.
    2. Scheduling. After a thread is descheduled due to Thread.sleep, it will be scheduled for execution again after a timer event which might be related to the timer used for System.nanoTime.

    In both cases you can't directly work around this - I mean Thread.sleep will also affect timings in your real application. But if the amount of useful work measured is large enough, the inaccuracy will be negligible.

    0 讨论(0)
  • 2021-01-21 09:19

    This is a complex topic because the timers used by the JVM are highly CPU- and OS-dependent and also change with JVM versions (e.g. by using newer OS APIs). Virtual machines may also limit the CPU capabilities they pass through to guests, which may alter the choices in comparison to a bare metal setup.

    On x86 the RDTSC instruction provides the lowest latency and best granularity of all clocks, but under some configurations it's not available or reliable enough as a time source.

    On linux you should check kernel startup messages (dmesg), the tsc-related /proc/cpuinfo flags and the selected /sys/devices/system/clocksource/*/current_clocksource. The kernel will try to use TSC by default, if it doesn't there may be a reason for that.

    For some history you may want to read the following, but note that some of those articles may be a bit dated, TSC reliability has improved a lot over the years:

    • OpenJDK Bug 8068730 exposing more precise system clocks in Java 9 through the Date and Time APIs introduced in java 8
    • http://shipilev.net/blog/2014/nanotrusting-nanotime/ (mentions the -XX:+AssumeMonotonicOSTimers manual override/footgun)
    • https://blog.packagecloud.io/eng/2017/03/14/using-strace-to-understand-java-performance-improvement/ (mentions the similar option for linux UseLinuxPosixThreadCPUClocks)
    • https://btorpey.github.io/blog/2014/02/18/clock-sources-in-linux/
    • https://stas-blogspot.blogspot.de/2012/02/what-is-behind-systemnanotime.html
    • https://en.wikipedia.org/wiki/Time_Stamp_Counter (especially CPU capabilities, constant_tsc tsc_reliable nonstop_tsc in linux nomenclature)
    • http://vanillajava.blogspot.de/2012/04/yield-sleep0-wait01-and-parknanos1.html
    0 讨论(0)
提交回复
热议问题