Mac OS X System.nanoTime Improvements

Staffan Larsen staffan.larsen at oracle.com
Wed May 15 00:27:27 PDT 2013


On 15 maj 2013, at 05:00, David Holmes <David.Holmes at oracle.com> wrote:

> On 15/05/2013 6:44 AM, Денис Ушаков wrote:
>> Hi,
>> 
>> couple of days ago Aleksey Shipilev mentioned that System.nanoTime() is very slow on OS X and is non-monotonic. After some research I found that there's no clock_gettime() function in OS X and current implementation falls back to gettimeofday().
> 
> Wow! If that is the case that is truly horrible and the OSX porters should have been aware of that and flagged it. :( A nanoTime() based on gettimeofday would not be monotonic if ntp adjustments are being made.
> 
>> OS X provides a monotonic clock function mach_absolute_time(). According to documentation (https://developer.apple.com/library/mac/#qa/qa1398/_index.html) it can be converted to nanoseconds using results of mach_timebase_info().
> 
> We need to know more about how this works, and in particular whether it is using the TSC and so is subject to the well known problems using the TSC as a "clock" base. The API sounds similar to that on Windows: QueryPerformanceCounter + QueryPerformanceFrequency.

The source is browsable here:

mach_absolute_time:
http://www.opensource.apple.com/source/xnu/xnu-2050.22.13/osfmk/i386/rtclock.c

_rtc_nanotime_read:
http://www.opensource.apple.com/source/xnu/xnu-2050.22.13/osfmk/x86_64/machine_routines_asm.s

PAL_RTC_NANOTIME_READ_FAST:
http://www.opensource.apple.com/source/xnu/xnu-2050.22.13/osfmk/i386/rtclock_asm_native.h


/Staffan


> 
> Thanks,
> David
> -----
> 
>> Here's draft patch that uses mach_absolute_time() to provide fast and monotonic System.nanoTime() for OS X:
>> https://gist.github.com/denofevil/db57fa547510b2dd87fd
>> 
>> You can find benchmark written by Aleksey Shipilev here: https://github.com/shipilev/jmh-nanotime.
>> Benchmark results with system specs: https://gist.github.com/denofevil/5026294ee6de42699b41
>> 
>> Thanks,
>> Dennis
>> 



More information about the hotspot-dev mailing list