src/os/solaris/vm/os_solaris.cpp

Print this page
rev 6280 : 8040140: System.nanoTime() is slow and non-monotonic on OS X
Reviewed-by: sspitsyn, shade

@@ -1393,27 +1393,32 @@
 // gethrtime can move backwards if read from one cpu and then a different cpu
 // getTimeNanos is guaranteed to not move backward on Solaris
 inline hrtime_t getTimeNanos() {
   if (VM_Version::supports_cx8()) {
     const hrtime_t now = gethrtime();
-    // Use atomic long load since 32-bit x86 uses 2 registers to keep long.
-    const hrtime_t prev = Atomic::load((volatile jlong*)&max_hrtime);
-    if (now <= prev)  return prev;   // same or retrograde time;
+    if (AssumeMonotonicOSTimers) {
+      return now;
+    }
+
+    const hrtime_t prev = max_hrtime;
+    if (now <= prev) {
+      return prev;   // same or retrograde time;
+    }
     const hrtime_t obsv = Atomic::cmpxchg(now, (volatile jlong*)&max_hrtime, prev);
     assert(obsv >= prev, "invariant");   // Monotonicity
     // If the CAS succeeded then we're done and return "now".
-    // If the CAS failed and the observed value "obs" is >= now then
-    // we should return "obs".  If the CAS failed and now > obs > prv then
+    // If the CAS failed and the observed value "obsv" is >= now then
+    // we should return "obsv".  If the CAS failed and now > obsv > prv then
     // some other thread raced this thread and installed a new value, in which case
     // we could either (a) retry the entire operation, (b) retry trying to install now
-    // or (c) just return obs.  We use (c).   No loop is required although in some cases
+    // or (c) just return obsv.  We use (c).   No loop is required although in some cases
     // we might discard a higher "now" value in deference to a slightly lower but freshly
-    // installed obs value.   That's entirely benign -- it admits no new orderings compared
+    // installed obsv value.   That's entirely benign -- it admits no new orderings compared
     // to (a) or (b) -- and greatly reduces coherence traffic.
-    // We might also condition (c) on the magnitude of the delta between obs and now.
+    // We might also condition (c) on the magnitude of the delta between obsv and now.
     // Avoiding excessive CAS operations to hot RW locations is critical.
-    // See http://blogs.sun.com/dave/entry/cas_and_cache_trivia_invalidate
+    // See https://blogs.oracle.com/dave/entry/cas_and_cache_trivia_invalidate
     return (prev == obsv) ? now : obsv ;
   } else {
     return oldgetTimeNanos();
   }
 }