Path: blob/master/src/hotspot/share/jfr/utilities/jfrTimeConverter.cpp
41152 views
/*1* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "jfr/utilities/jfrTimeConverter.hpp"26#include "jfr/utilities/jfrTime.hpp"27#include "runtime/os.hpp"2829#include OS_HEADER_INLINE(os)3031static double ft_counter_to_nanos_factor = .0;32static double nanos_to_ft_counter_factor = .0;33static double os_counter_to_nanos_factor = .0;34static double nanos_to_os_counter_factor = .0;3536const double JfrTimeConverter::NANOS_PER_SEC = 1000000000.0;37const double JfrTimeConverter::NANOS_PER_MILLISEC = 1000000.0;38const double JfrTimeConverter::NANOS_PER_MICROSEC = 1000.0;3940static bool initialized = false;4142void JfrTimeConverter::initialize() {43if (!initialized) {44nanos_to_os_counter_factor = (double)os::elapsed_frequency() / NANOS_PER_SEC;45assert(nanos_to_os_counter_factor != .0, "error in conversion!");46os_counter_to_nanos_factor = (double)1.0 / nanos_to_os_counter_factor;47assert(os_counter_to_nanos_factor != .0, "error in conversion!");48if (JfrTime::is_ft_enabled()) {49nanos_to_ft_counter_factor = (double)JfrTime::frequency() / NANOS_PER_SEC;50assert(nanos_to_ft_counter_factor != .0, "error in conversion!");51ft_counter_to_nanos_factor = (double)1.0 / nanos_to_ft_counter_factor;52assert(ft_counter_to_nanos_factor != .0, "error in conversion!");53}54initialized = true;55}56}5758double JfrTimeConverter::counter_to_nano_multiplier(bool is_os_time) {59if (!initialized) {60initialize();61}62return JfrTime::is_ft_enabled() && !is_os_time ? ft_counter_to_nanos_factor : os_counter_to_nanos_factor;63}6465double JfrTimeConverter::nano_to_counter_multiplier(bool is_os_time) {66if (!initialized) {67initialize();68}69return JfrTime::is_ft_enabled() && !is_os_time ? nanos_to_ft_counter_factor : nanos_to_os_counter_factor;70}7172double JfrTimeConverter::counter_to_nanos_internal(jlong c, bool is_os_time) {73return (double)c * counter_to_nano_multiplier(is_os_time);74}7576double JfrTimeConverter::counter_to_millis_internal(jlong c, bool is_os_time) {77return (counter_to_nanos_internal(c, is_os_time) / NANOS_PER_MILLISEC);78}7980jlong JfrTimeConverter::counter_to_nanos(jlong c, bool is_os_time) {81return (jlong)counter_to_nanos_internal(c, is_os_time);82}8384jlong JfrTimeConverter::counter_to_millis(jlong c, bool is_os_time) {85return (jlong)counter_to_millis_internal(c, is_os_time);86}8788jlong JfrTimeConverter::nanos_to_countertime(jlong nanos, bool as_os_time) {89return nanos <= 0 ? 0 : (jlong)((double)nanos * nano_to_counter_multiplier(as_os_time));90}919293