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 | #include OS_HEADER_INLINE(os) |
31 | |
32 | static double ft_counter_to_nanos_factor = .0; |
33 | static double nanos_to_ft_counter_factor = .0; |
34 | static double os_counter_to_nanos_factor = .0; |
35 | static double nanos_to_os_counter_factor = .0; |
36 | |
37 | const double JfrTimeConverter::NANOS_PER_SEC = 1000000000.0; |
38 | const double JfrTimeConverter::NANOS_PER_MILLISEC = 1000000.0; |
39 | const double JfrTimeConverter::NANOS_PER_MICROSEC = 1000.0; |
40 | |
41 | static bool initialized = false; |
42 | |
43 | void JfrTimeConverter::initialize() { |
44 | if (!initialized) { |
45 | nanos_to_os_counter_factor = (double)os::elapsed_frequency() / NANOS_PER_SEC; |
46 | assert(nanos_to_os_counter_factor != .0, "error in conversion!" ); |
47 | os_counter_to_nanos_factor = (double)1.0 / nanos_to_os_counter_factor; |
48 | assert(os_counter_to_nanos_factor != .0, "error in conversion!" ); |
49 | if (JfrTime::is_ft_enabled()) { |
50 | nanos_to_ft_counter_factor = (double)JfrTime::frequency() / NANOS_PER_SEC; |
51 | assert(nanos_to_ft_counter_factor != .0, "error in conversion!" ); |
52 | ft_counter_to_nanos_factor = (double)1.0 / nanos_to_ft_counter_factor; |
53 | assert(ft_counter_to_nanos_factor != .0, "error in conversion!" ); |
54 | } |
55 | initialized = true; |
56 | } |
57 | } |
58 | |
59 | double JfrTimeConverter::counter_to_nano_multiplier(bool is_os_time) { |
60 | if (!initialized) { |
61 | initialize(); |
62 | } |
63 | return JfrTime::is_ft_enabled() && !is_os_time ? ft_counter_to_nanos_factor : os_counter_to_nanos_factor; |
64 | } |
65 | |
66 | double JfrTimeConverter::nano_to_counter_multiplier(bool is_os_time) { |
67 | if (!initialized) { |
68 | initialize(); |
69 | } |
70 | return JfrTime::is_ft_enabled() && !is_os_time ? nanos_to_ft_counter_factor : nanos_to_os_counter_factor; |
71 | } |
72 | |
73 | double JfrTimeConverter::counter_to_nanos_internal(jlong c, bool is_os_time) { |
74 | return (double)c * counter_to_nano_multiplier(is_os_time); |
75 | } |
76 | |
77 | double JfrTimeConverter::counter_to_millis_internal(jlong c, bool is_os_time) { |
78 | return (counter_to_nanos_internal(c, is_os_time) / NANOS_PER_MILLISEC); |
79 | } |
80 | |
81 | jlong JfrTimeConverter::counter_to_nanos(jlong c, bool is_os_time) { |
82 | return (jlong)counter_to_nanos_internal(c, is_os_time); |
83 | } |
84 | |
85 | jlong JfrTimeConverter::counter_to_millis(jlong c, bool is_os_time) { |
86 | return (jlong)counter_to_millis_internal(c, is_os_time); |
87 | } |
88 | |
89 | jlong JfrTimeConverter::nanos_to_countertime(jlong nanos, bool as_os_time) { |
90 | return nanos <= 0 ? 0 : (jlong)((double)nanos * nano_to_counter_multiplier(as_os_time)); |
91 | } |
92 | |