hotspot/src/share/vm/prims/jvm.cpp
changeset 28737 ca4b6a6e5cc8
parent 28731 f7339cba0a6a
child 28744 4283637ac96e
child 28947 2ea471384931
--- a/hotspot/src/share/vm/prims/jvm.cpp	Wed Jan 28 09:27:49 2015 +0100
+++ b/hotspot/src/share/vm/prims/jvm.cpp	Wed Jan 28 17:52:48 2015 +0100
@@ -300,6 +300,48 @@
   return os::javaTimeNanos();
 JVM_END
 
+// The function below is actually exposed by sun.misc.VM and not
+// java.lang.System, but we choose to keep it here so that it stays next
+// to JVM_CurrentTimeMillis and JVM_NanoTime
+
+const jlong MAX_DIFF_SECS = 0x0100000000;   //  2^32
+const jlong MIN_DIFF_SECS = -MAX_DIFF_SECS; // -2^32
+
+JVM_LEAF(jlong, JVM_GetNanoTimeAdjustment(JNIEnv *env, jclass ignored, jlong offset_secs))
+  JVMWrapper("JVM_GetNanoTimeAdjustment");
+  jlong seconds;
+  jlong nanos;
+
+  os::javaTimeSystemUTC(seconds, nanos);
+
+  // We're going to verify that the result can fit in a long.
+  // For that we need the difference in seconds between 'seconds'
+  // and 'offset_secs' to be such that:
+  //     |seconds - offset_secs| < (2^63/10^9)
+  // We're going to approximate 10^9 ~< 2^30 (1000^3 ~< 1024^3)
+  // which makes |seconds - offset_secs| < 2^33
+  // and we will prefer +/- 2^32 as the maximum acceptable diff
+  // as 2^32 has a more natural feel than 2^33...
+  //
+  // So if |seconds - offset_secs| >= 2^32 - we return a special
+  // sentinel value (-1) which the caller should take as an
+  // exception value indicating that the offset given to us is
+  // too far from range of the current time - leading to too big
+  // a nano adjustment. The caller is expected to recover by
+  // computing a more accurate offset and calling this method
+  // again. (For the record 2^32 secs is ~136 years, so that
+  // should rarely happen)
+  //
+  jlong diff = seconds - offset_secs;
+  if (diff >= MAX_DIFF_SECS || diff <= MIN_DIFF_SECS) {
+     return -1; // sentinel value: the offset is too far off the target
+  }
+
+  // return the adjustment. If you compute a time by adding
+  // this number of nanoseconds along with the number of seconds
+  // in the offset you should get the current UTC time.
+  return (diff * (jlong)1000000000) + nanos;
+JVM_END
 
 JVM_ENTRY(void, JVM_ArrayCopy(JNIEnv *env, jclass ignored, jobject src, jint src_pos,
                                jobject dst, jint dst_pos, jint length))