< prev index next >

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

Print this page




 445 
 446 static jlong get_monotonic_ms() {
 447   return os::javaTimeNanos() / 1000000;
 448 }
 449 
 450 void JfrThreadSampler::run() {
 451   assert(_sampler_thread == NULL, "invariant");
 452 
 453   _sampler_thread = this;
 454 
 455   jlong last_java_ms = get_monotonic_ms();
 456   jlong last_native_ms = last_java_ms;
 457   while (true) {
 458     if (!_sample.trywait()) {
 459       // disenrolled
 460       _sample.wait();
 461       last_java_ms = get_monotonic_ms();
 462       last_native_ms = last_java_ms;
 463     }
 464     _sample.signal();
 465     jlong java_interval = _interval_java == 0 ? max_jlong : MAX2<jlong>(_interval_java, 1);
 466     jlong native_interval = _interval_native == 0 ? max_jlong : MAX2<jlong>(_interval_native, 1);
 467 
 468     jlong now_ms = get_monotonic_ms();
 469 
 470     /*
 471      * Let I be java_interval or native_interval.
 472      * Let L be last_java_ms or last_native_ms.
 473      * Let N be now_ms.
 474      *
 475      * Interval, I, might be max_jlong so the addition
 476      * could potentially overflow without parenthesis (UB). Also note that
 477      * L - N < 0. Avoid UB, by adding parenthesis.
 478      */
 479     jlong next_j = java_interval + (last_java_ms - now_ms);
 480     jlong next_n = native_interval + (last_native_ms - now_ms);
 481 
 482     jlong sleep_to_next = MIN2<jlong>(next_j, next_n);
 483 
 484     if (sleep_to_next > 0) {
 485       os::naked_short_sleep(sleep_to_next);
 486     }




 445 
 446 static jlong get_monotonic_ms() {
 447   return os::javaTimeNanos() / 1000000;
 448 }
 449 
 450 void JfrThreadSampler::run() {
 451   assert(_sampler_thread == NULL, "invariant");
 452 
 453   _sampler_thread = this;
 454 
 455   jlong last_java_ms = get_monotonic_ms();
 456   jlong last_native_ms = last_java_ms;
 457   while (true) {
 458     if (!_sample.trywait()) {
 459       // disenrolled
 460       _sample.wait();
 461       last_java_ms = get_monotonic_ms();
 462       last_native_ms = last_java_ms;
 463     }
 464     _sample.signal();
 465     jlong java_interval = _interval_java == 0 ? max_jlong : MAX2<jlong>(_interval_java, 10);
 466     jlong native_interval = _interval_native == 0 ? max_jlong : MAX2<jlong>(_interval_native, 10);
 467 
 468     jlong now_ms = get_monotonic_ms();
 469 
 470     /*
 471      * Let I be java_interval or native_interval.
 472      * Let L be last_java_ms or last_native_ms.
 473      * Let N be now_ms.
 474      *
 475      * Interval, I, might be max_jlong so the addition
 476      * could potentially overflow without parenthesis (UB). Also note that
 477      * L - N < 0. Avoid UB, by adding parenthesis.
 478      */
 479     jlong next_j = java_interval + (last_java_ms - now_ms);
 480     jlong next_n = native_interval + (last_native_ms - now_ms);
 481 
 482     jlong sleep_to_next = MIN2<jlong>(next_j, next_n);
 483 
 484     if (sleep_to_next > 0) {
 485       os::naked_short_sleep(sleep_to_next);
 486     }


< prev index next >