1 /* 2 * Copyright (c) 2011, 2019, 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/jfrTraceTime.hpp" 28 #include "runtime/os.hpp" 29 30 #ifdef TARGET_OS_FAMILY_linux 31 # include "os_linux.inline.hpp" 32 # include "os_posix.hpp" 33 #endif 34 #ifdef TARGET_OS_FAMILY_solaris 35 # include "os_solaris.inline.hpp" 36 # include "os_posix.hpp" 37 #endif 38 #ifdef TARGET_OS_FAMILY_windows 39 # include "os_windows.inline.hpp" 40 #endif 41 #ifdef TARGET_OS_FAMILY_aix 42 # include "os_aix.inline.hpp" 43 # include "os_posix.hpp" 44 #endif 45 #ifdef TARGET_OS_FAMILY_bsd 46 # include "os_posix.hpp" 47 # include "os_bsd.inline.hpp" 48 #endif 49 50 static double ft_counter_to_nanos_factor = .0; 51 static double nanos_to_ft_counter_factor = .0; 52 static double os_counter_to_nanos_factor = .0; 53 static double nanos_to_os_counter_factor = .0; 54 55 const double JfrTimeConverter::NANOS_PER_SEC = 1000000000.0; 56 const double JfrTimeConverter::NANOS_PER_MILLISEC = 1000000.0; 57 const double JfrTimeConverter::NANOS_PER_MICROSEC = 1000.0; 58 59 static bool initialized = false; 60 61 void JfrTimeConverter::initialize() { 62 if (!initialized) { 63 nanos_to_os_counter_factor = (double)os::elapsed_frequency() / NANOS_PER_SEC; 64 assert(nanos_to_os_counter_factor != .0, "error in conversion!"); 65 os_counter_to_nanos_factor = (double)1.0 / nanos_to_os_counter_factor; 66 assert(os_counter_to_nanos_factor != .0, "error in conversion!"); 67 if (JfrTraceTime::is_ft_enabled()) { 68 nanos_to_ft_counter_factor = (double)JfrTraceTime::frequency() / NANOS_PER_SEC; 69 assert(nanos_to_ft_counter_factor != .0, "error in conversion!"); 70 ft_counter_to_nanos_factor = (double)1.0 / nanos_to_ft_counter_factor; 71 assert(ft_counter_to_nanos_factor != .0, "error in conversion!"); 72 } 73 initialized = true; 74 } 75 } 76 77 double JfrTimeConverter::counter_to_nano_multiplier(bool is_os_time) { 78 if (!initialized) { 79 initialize(); 80 } 81 return JfrTraceTime::is_ft_enabled() && !is_os_time ? ft_counter_to_nanos_factor : os_counter_to_nanos_factor; 82 } 83 84 double JfrTimeConverter::nano_to_counter_multiplier(bool is_os_time) { 85 if (!initialized) { 86 initialize(); 87 } 88 return JfrTraceTime::is_ft_enabled() && !is_os_time ? nanos_to_ft_counter_factor : nanos_to_os_counter_factor; 89 } 90 91 double JfrTimeConverter::counter_to_nanos_internal(jlong c, bool is_os_time) { 92 return (double)c * counter_to_nano_multiplier(is_os_time); 93 } 94 95 double JfrTimeConverter::counter_to_millis_internal(jlong c, bool is_os_time) { 96 return (counter_to_nanos_internal(c, is_os_time) / NANOS_PER_MILLISEC); 97 } 98 99 jlong JfrTimeConverter::counter_to_nanos(jlong c, bool is_os_time) { 100 return (jlong)counter_to_nanos_internal(c, is_os_time); 101 } 102 103 jlong JfrTimeConverter::counter_to_millis(jlong c, bool is_os_time) { 104 return (jlong)counter_to_millis_internal(c, is_os_time); 105 } 106 107 jlong JfrTimeConverter::nanos_to_countertime(jlong nanos, bool as_os_time) { 108 return nanos <= 0 ? 0 : (jlong)((double)nanos * nano_to_counter_multiplier(as_os_time)); 109 }