1 /* 2 * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 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 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "jfr/utilities/jfrTimeConverter.hpp" 27 #include "jfr/utilities/jfrTime.hpp" 28 #include "runtime/os.hpp" 29 30 static double ft_counter_to_nanos_factor = .0; 31 static double nanos_to_ft_counter_factor = .0; 32 static double os_counter_to_nanos_factor = .0; 33 static double nanos_to_os_counter_factor = .0; 34 35 const double JfrTimeConverter::NANOS_PER_SEC = 1000000000.0; 36 const double JfrTimeConverter::NANOS_PER_MILLISEC = 1000000.0; 37 const double JfrTimeConverter::NANOS_PER_MICROSEC = 1000.0; 38 39 static bool initialized = false; 40 41 void JfrTimeConverter::initialize() { 42 if (!initialized) { 43 nanos_to_os_counter_factor = (double)os::elapsed_frequency() / NANOS_PER_SEC; 44 assert(nanos_to_os_counter_factor != .0, "error in conversion!"); 45 os_counter_to_nanos_factor = (double)1.0 / nanos_to_os_counter_factor; 46 assert(os_counter_to_nanos_factor != .0, "error in conversion!"); 47 if (JfrTime::is_ft_enabled()) { 48 nanos_to_ft_counter_factor = (double)JfrTime::frequency() / NANOS_PER_SEC; 49 assert(nanos_to_ft_counter_factor != .0, "error in conversion!"); 50 ft_counter_to_nanos_factor = (double)1.0 / nanos_to_ft_counter_factor; 51 assert(ft_counter_to_nanos_factor != .0, "error in conversion!"); 52 } 53 initialized = true; 54 } 55 } 56 57 double JfrTimeConverter::counter_to_nano_multiplier(bool is_os_time) { 58 if (!initialized) { 59 initialize(); 60 } 61 return JfrTime::is_ft_enabled() && !is_os_time ? ft_counter_to_nanos_factor : os_counter_to_nanos_factor; 62 } 63 64 double JfrTimeConverter::nano_to_counter_multiplier(bool is_os_time) { 65 if (!initialized) { 66 initialize(); 67 } 68 return JfrTime::is_ft_enabled() && !is_os_time ? nanos_to_ft_counter_factor : nanos_to_os_counter_factor; 69 } 70 71 double JfrTimeConverter::counter_to_nanos_internal(jlong c, bool is_os_time) { 72 return (double)c * counter_to_nano_multiplier(is_os_time); 73 } 74 75 double JfrTimeConverter::counter_to_millis_internal(jlong c, bool is_os_time) { 76 return (counter_to_nanos_internal(c, is_os_time) / NANOS_PER_MILLISEC); 77 } 78 79 jlong JfrTimeConverter::counter_to_nanos(jlong c, bool is_os_time) { 80 return (jlong)counter_to_nanos_internal(c, is_os_time); 81 } 82 83 jlong JfrTimeConverter::counter_to_millis(jlong c, bool is_os_time) { 84 return (jlong)counter_to_millis_internal(c, is_os_time); 85 } 86 87 jlong JfrTimeConverter::nanos_to_countertime(jlong nanos, bool as_os_time) { 88 return nanos <= 0 ? 0 : (jlong)((double)nanos * nano_to_counter_multiplier(as_os_time)); 89 }