1 /*
  2  * Copyright (c) 1997, 2023, 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 "logging/log.hpp"
 27 #include "oops/oop.inline.hpp"
 28 #include "runtime/os.hpp"
 29 #include "runtime/thread.hpp"
 30 #include "runtime/timer.hpp"
 31 #include "utilities/ostream.hpp"
 32 
 33 double TimeHelper::counter_to_seconds(jlong counter) {
 34   double freq  = (double) os::elapsed_frequency();
 35   return (double)counter / freq;
 36 }
 37 
 38 double TimeHelper::counter_to_millis(jlong counter) {
 39   return counter_to_seconds(counter) * 1000.0;
 40 }
 41 
 42 jlong TimeHelper::millis_to_counter(jlong millis) {
 43   jlong freq = os::elapsed_frequency() / MILLIUNITS;
 44   return millis * freq;
 45 }
 46 
 47 jlong TimeHelper::micros_to_counter(jlong micros) {
 48   jlong freq = os::elapsed_frequency() / MICROUNITS;
 49   return micros * freq;
 50 }
 51 
 52 void BaseTimer::add(BaseTimer* t) {
 53   _counter += t->_counter;
 54 }
 55 
 56 void BaseTimer::add_nanoseconds(jlong ns) {
 57   jlong freq = os::elapsed_frequency() / NANOUNITS;
 58   _counter += ns * freq;
 59 }
 60 
 61 void BaseTimer::start() {
 62   if (!_active) {
 63     _active = true;
 64     _start_counter = read_counter();
 65   }
 66 }
 67 
 68 void BaseTimer::stop() {
 69   if (_active) {
 70     _counter += read_counter() - _start_counter;
 71     _active = false;
 72   }
 73 }
 74 
 75 double BaseTimer::seconds() const {
 76  return TimeHelper::counter_to_seconds(_counter);
 77 }
 78 
 79 jlong BaseTimer::milliseconds() const {
 80   return (jlong)TimeHelper::counter_to_millis(_counter);
 81 }
 82 
 83 jlong BaseTimer::active_ticks() const {
 84   if (!_active) {
 85     return ticks();
 86   }
 87   jlong counter = _counter + read_counter() - _start_counter;
 88   return counter;
 89 }
 90 
 91 jlong elapsedTimer::read_counter() const {
 92   return os::elapsed_counter();
 93 }
 94 
 95 ThreadTimer::ThreadTimer() {
 96   _owner = Thread::current();
 97 }
 98 
 99 void ThreadTimer::start() {
100   assert(_owner != nullptr, "timer must be bound to a thread");
101   Thread* current = Thread::current();
102   assert(current == _owner, "timer can only be started by the thread that owns it");
103   BaseTimer::start();
104 }
105 
106 void ThreadTimer::stop() {
107   assert(_owner != nullptr, "sanity check");
108   Thread* current = Thread::current();
109   assert(current == _owner, "timer can only be stopped by the thread that owns it");
110   if (_start_counter != -1) {
111     BaseTimer::stop();
112   }
113 }
114 
115 jlong ThreadTimer::read_counter() const {
116   assert(_owner != nullptr, "sanity check");
117   return os::thread_cpu_time(_owner);
118 }
119 
120 void TimeStamp::update_to(jlong ticks) {
121   _counter = ticks;
122   if (_counter == 0)  _counter = 1;
123   assert(is_updated(), "must not look clear");
124 }
125 
126 void TimeStamp::update() {
127   update_to(os::elapsed_counter());
128 }
129 
130 double TimeStamp::seconds() const {
131   assert(is_updated(), "must not be clear");
132   jlong new_count = os::elapsed_counter();
133   return TimeHelper::counter_to_seconds(new_count - _counter);
134 }
135 
136 jlong TimeStamp::milliseconds() const {
137   assert(is_updated(), "must not be clear");
138   jlong new_count = os::elapsed_counter();
139   return (jlong)TimeHelper::counter_to_millis(new_count - _counter);
140 }
141 
142 jlong TimeStamp::ticks_since_update() const {
143   assert(is_updated(), "must not be clear");
144   return os::elapsed_counter() - _counter;
145 }