Comparing System.nanoTime () values ​​received from different machines

Is it right to compare two values ​​as a result of calling System.nanoTime () on two different machines? I would say no, because System.nanoTime () returns an exact time equal to a nanosecond relative to some arbitrary time using a time counter (TSC) which is processor dependent.

If I'm right, is there a way (in Java) to take a moment on two different machines and compare (safely) these values, at least accurate to microseconds or even accurate to nanotemes?

System.currentTimeMillis () is not a solution because it does not return a linearly increasing number of time stamps. A user or services, such as NTP, can change the system clock at any time, and the time will move back and forth.

+4
source share
4 answers

You might want to learn different clock synchronization algorithms. Apparently, Precision Time Protocol can get you within sub-microsecond accuracy on a local network.

If you do not need a specific time value, but rather would like to know the order of various events, you can, for example, use Lamport timestamps .

+3
source

It depends on the processor and the Q operating system. For example, when viewing a POSIX clock, there are high-precision timestamps (for example, CLOCK_REALTIME returns a nano-era value) and high-precision arbitrary timestamps (for example CLOCK_MONOTONIC) (NB: the difference between the two is well explained in this answer ).

The latter often happens just from the moment the box is loaded, and therefore it is not possible to accurately compare them between servers if you do not have high-frequency clock synchronization (for example, PTP, as indicated in another answer) in the first place (since then you can share the offset between them). A.

Whether NTP is good enough for you depends on what you are trying to measure. For example, if you are trying to measure an interval of several hundred microns (for example, boxes connected to the same switch), then your results will be rude, and on another extreme NTP it might be fine if your servers are located in different geographical points entirely ( for example, from London to New York), which means that the synchronized effect of synchronization (until it is turned off) damps the delay between locations.

The FWIW JNI needed to access such a watch from java is pretty trivial.

0
source

You cannot use nanoTime between two different machines. For Java API Docs :

This method can only be used to measure elapsed time and is not used associated with any other concept of system or wall time. The value returned is nanoseconds, since there is some fixed, but arbitrary time (possibly in the future, so the values ​​can be negative).

There is no guarantee that nanoTime applies to any time base.

0
source

You can synchronize the time with the current millisecond of time. However, even if you use NTP, it can drift for 1 ms to 10 ms between machines. The only way microsecond synchronization between machines is to use specialized equipment.

nanoTime is guaranteed to be detected in the same way or has the same resolution on two different operating systems.

0
source

Source: https://habr.com/ru/post/1379544/


All Articles