Mac OS X System.nanoTime Improvements

Staffan Larsen staffan.larsen at oracle.com
Tue May 14 22:46:37 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.

There is this little "flag" in the source, but I can't find a bug report for it.

#ifdef __APPLE__
void os::Bsd::clock_init() {
        // XXXDARWIN: Investigate replacement monotonic clock
}
#else


/Staffan

> 
>> 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.
> 
> 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