< prev index next >

src/hotspot/share/jfr/periodic/sampling/jfrThreadSampler.cpp

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -445,10 +445,34 @@
 
 static jlong get_monotonic_ms() {
   return os::javaTimeNanos() / 1000000;
 }
 
+// calculate next sleep period so that it doesn't overflow
+static jlong calc_next(jlong interval, jlong last_ms, jlong now_ms) {
+    assert(last_ms > 0, "invariant");
+
+    jlong time_diff = last_ms - now_ms;
+    jlong next;
+    if (interval == 0) {
+      next = (time_diff < 0) ? (max_jlong + time_diff) : max_jlong;
+    } else {
+      jlong max_v = MAX2<jlong>(interval, 10);
+
+      if (time_diff > 0) {
+        if (max_v <= (max_jlong - time_diff)) {
+          next = max_v + time_diff;
+        } else {
+          next = max_jlong;
+        }
+      } else {
+        next = max_v + time_diff;
+      }
+    }
+    return next;
+}
+
 void JfrThreadSampler::run() {
   assert(_sampler_thread == NULL, "invariant");
 
   _sampler_thread = this;
 

@@ -460,17 +484,14 @@
       _sample.wait();
       last_java_ms = get_monotonic_ms();
       last_native_ms = last_java_ms;
     }
     _sample.signal();
-    jlong java_interval = _interval_java == 0 ? max_jlong : MAX2<jlong>(_interval_java, 10);
-    jlong native_interval = _interval_native == 0 ? max_jlong : MAX2<jlong>(_interval_native, 10);
-
     jlong now_ms = get_monotonic_ms();
 
-    jlong next_j = java_interval + last_java_ms - now_ms;
-    jlong next_n = native_interval + last_native_ms - now_ms;
+    jlong next_j = calc_next(_interval_java, last_java_ms, now_ms);
+    jlong next_n = calc_next(_interval_native, last_native_ms, now_ms);
 
     jlong sleep_to_next = MIN2<jlong>(next_j, next_n);
 
     if (sleep_to_next > 0) {
       os::naked_short_sleep(sleep_to_next);
< prev index next >