News

Time.....Obtaining Nano-Second Precision in Java

Time.....Obtaining Nano-Second Precision in Java

At TransFICC, for MiFID compliance (as explained in my previous blog) we need to obtain the time of an event within 100 microsecond accuracy with respect to UTC.

To achieve this in Java we first need to be able to obtain the time from the OS with better than millisecond precision. We can then make use of Precision Time Protocol (PTP) to ensure the system clock is synchronised to UTC with sufficient accuracy. Take a look at the excellent posts from Luke Bigum on how to achieve this.

In Java version 8 it is not possible to directly obtain the time with higher than millisecond precision. Java 8 System.currentTimeMillis() will return the time since the Unix epoch in milliseconds. Higher precision time is possible on some underlying systems such as Linux.

For details of the Linux code that OpenJDK JVM executes click here - https://wireddevelopment.blogspot.co.uk/2016/08/ob...

The Linux code makes the function call gettimeofday(). This returns the time since the Linux epoch in micro seconds however it is rounded to the nearest millisecond. On newer Linux kernels this is an alias for clock_gettime(). The POSIX spec has deprecated gettimeofday() for clock_gettime() so this should be used instead. On modern Linux systems the function call is implemented as a Virtual Dynamic Shared Object or (VDSO) as opposed to a system call for improved performance.

The accuracy and of the underlying clock is dependent on the clock source being used by the kernel.

Calling clock_gettime() to obtain a nanosecond precision time from the JVM can be achieved in Java using JNA or JNI. We implemented both clock_gettime() using JNA and JNI and performance compared with System.currentTimeMillis() as a baseline. The tests were conducted on a Intel Xeon CPU E5-1650 v3 @ 3.50GHz using the Java Benchmark Harness (JMH).

JNA has the advantage of not requiring the user to compile any native code but this comes at the cost of performance. For each call to obtain the time JNA constructs Structure objects that represent native structures. This results in a worse median latency as well as generating significant amounts of garbage degrading long tail latency. The JNA direct mapping showed improved performance past the 99.99 percentiles but was still significantly slower than JNI.


To read Judd's full post, which includes the code and benchmark results, visit - https://wireddevelopment.blogspot.co.uk/2016/08/ob...




Share
Filter by section
Most popular stories
Mob Rules - Moving to Virtual Pair Programming

Mob Rules - Moving to Virtual Pair Programming

Like all companies, lockdown has impacted the way we work at TransFICC. We have always championed Extreme Programming as our methodology of choice, so when we decided to close the office at the start of March, this raised some issues around how we would operate with everyone working remotely.


Read More Twitter LinkedIn
All Change - Technology & Efficiency Are The (Old) New Buzzwords in Fixed Income

All Change - Technology & Efficiency Are The (Old) New Buzzwords in Fixed Income

Steve writes for The Financial Technologist - published by Harrington Starr


Read More Twitter LinkedIn
Press Release - TransFICC Secures New Investment From AlbionVC, ING Ventures and HSBC

Press Release - TransFICC Secures New Investment From AlbionVC, ING Ventures and HSBC

TransFICC has closed its Series A investment round for £5.75 million. Led by AlbionVC, it included new strategic investments from ING Ventures and HSBC. The new investors join existing shareholders, Citi, Illuminate Financial, Main Incubator (the R&D unit of Commerzbank Group) and The FinLab.


Read More Twitter LinkedIn
Twitter Feed