src/hotspot/share/runtime/timer.cpp
author rehn
Fri, 29 Nov 2019 12:09:25 +0100
changeset 59325 3636bab5e81e
parent 53520 5178e4b58b17
permissions -rw-r--r--
8234086: VM operation can be simplified Reviewed-by: kbarrett, dholmes, dcubed

/*
 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

#include "precompiled.hpp"
#include "logging/log.hpp"
#include "oops/oop.inline.hpp"
#include "runtime/timer.hpp"
#include "utilities/ostream.hpp"

double TimeHelper::counter_to_seconds(jlong counter) {
  double freq  = (double) os::elapsed_frequency();
  return counter / freq;
}

double TimeHelper::counter_to_millis(jlong counter) {
  return counter_to_seconds(counter) * 1000.0;
}

jlong TimeHelper::millis_to_counter(jlong millis) {
  jlong freq = os::elapsed_frequency() / MILLIUNITS;
  return millis * freq;
}

elapsedTimer::elapsedTimer(jlong time, jlong timeUnitsPerSecond) {
  _active = false;
  jlong osTimeUnitsPerSecond = os::elapsed_frequency();
  assert(osTimeUnitsPerSecond % 1000 == 0, "must be");
  assert(timeUnitsPerSecond % 1000 == 0, "must be");
  while (osTimeUnitsPerSecond < timeUnitsPerSecond) {
    timeUnitsPerSecond /= 1000;
    time *= 1000;
  }
  while (osTimeUnitsPerSecond > timeUnitsPerSecond) {
    timeUnitsPerSecond *= 1000;
    time /= 1000;
  }
  _counter = time;
}

void elapsedTimer::add(elapsedTimer t) {
  _counter += t._counter;
}

void elapsedTimer::start() {
  if (!_active) {
    _active = true;
    _start_counter = os::elapsed_counter();
  }
}

void elapsedTimer::stop() {
  if (_active) {
    _counter += os::elapsed_counter() - _start_counter;
    _active = false;
  }
}

double elapsedTimer::seconds() const {
 return TimeHelper::counter_to_seconds(_counter);
}

jlong elapsedTimer::milliseconds() const {
  return (jlong)TimeHelper::counter_to_millis(_counter);
}

jlong elapsedTimer::active_ticks() const {
  if (!_active) {
    return ticks();
  }
  jlong counter = _counter + os::elapsed_counter() - _start_counter;
  return counter;
}

void TimeStamp::update_to(jlong ticks) {
  _counter = ticks;
  if (_counter == 0)  _counter = 1;
  assert(is_updated(), "must not look clear");
}

void TimeStamp::update() {
  update_to(os::elapsed_counter());
}

double TimeStamp::seconds() const {
  assert(is_updated(), "must not be clear");
  jlong new_count = os::elapsed_counter();
  return TimeHelper::counter_to_seconds(new_count - _counter);
}

jlong TimeStamp::milliseconds() const {
  assert(is_updated(), "must not be clear");
  jlong new_count = os::elapsed_counter();
  return (jlong)TimeHelper::counter_to_millis(new_count - _counter);
}

jlong TimeStamp::ticks_since_update() const {
  assert(is_updated(), "must not be clear");
  return os::elapsed_counter() - _counter;
}