src/hotspot/share/jfr/periodic/sampling/jfrThreadSampler.cpp
changeset 53255 61a385765c9b
parent 53103 67e3a8b3449c
child 54562 1af5c0e68381
equal deleted inserted replaced
53254:47bc06170313 53255:61a385765c9b
     1 /*
     1 /*
     2  * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
   465     jlong java_interval = _interval_java == 0 ? max_jlong : MAX2<jlong>(_interval_java, 10);
   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);
   466     jlong native_interval = _interval_native == 0 ? max_jlong : MAX2<jlong>(_interval_native, 10);
   467 
   467 
   468     jlong now_ms = get_monotonic_ms();
   468     jlong now_ms = get_monotonic_ms();
   469 
   469 
   470     jlong next_j = java_interval + last_java_ms - now_ms;
   470     /*
   471     jlong next_n = native_interval + last_native_ms - now_ms;
   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);
   472 
   481 
   473     jlong sleep_to_next = MIN2<jlong>(next_j, next_n);
   482     jlong sleep_to_next = MIN2<jlong>(next_j, next_n);
   474 
   483 
   475     if (sleep_to_next > 0) {
   484     if (sleep_to_next > 0) {
   476       os::naked_short_sleep(sleep_to_next);
   485       os::naked_short_sleep(sleep_to_next);