aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "runtime/timer.hpp" aoqi@0: #include "utilities/ostream.hpp" aoqi@0: #ifdef TARGET_OS_FAMILY_linux aoqi@0: # include "os_linux.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_solaris aoqi@0: # include "os_solaris.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_windows aoqi@0: # include "os_windows.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_aix aoqi@0: # include "os_aix.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_bsd aoqi@0: # include "os_bsd.inline.hpp" aoqi@0: #endif aoqi@0: aoqi@0: double TimeHelper::counter_to_seconds(jlong counter) { aoqi@0: double count = (double) counter; aoqi@0: double freq = (double) os::elapsed_frequency(); aoqi@0: return counter/freq; aoqi@0: } aoqi@0: aoqi@0: void elapsedTimer::add(elapsedTimer t) { aoqi@0: _counter += t._counter; aoqi@0: } aoqi@0: aoqi@0: void elapsedTimer::start() { aoqi@0: if (!_active) { aoqi@0: _active = true; aoqi@0: _start_counter = os::elapsed_counter(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void elapsedTimer::stop() { aoqi@0: if (_active) { aoqi@0: _counter += os::elapsed_counter() - _start_counter; aoqi@0: _active = false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: double elapsedTimer::seconds() const { aoqi@0: return TimeHelper::counter_to_seconds(_counter); aoqi@0: } aoqi@0: aoqi@0: jlong elapsedTimer::milliseconds() const { aoqi@0: jlong ticks_per_ms = os::elapsed_frequency() / 1000; aoqi@0: return _counter / ticks_per_ms; aoqi@0: } aoqi@0: aoqi@0: jlong elapsedTimer::active_ticks() const { aoqi@0: if (!_active) { aoqi@0: return ticks(); aoqi@0: } aoqi@0: jlong counter = _counter + os::elapsed_counter() - _start_counter; aoqi@0: return counter; aoqi@0: } aoqi@0: aoqi@0: void TimeStamp::update_to(jlong ticks) { aoqi@0: _counter = ticks; aoqi@0: if (_counter == 0) _counter = 1; aoqi@0: assert(is_updated(), "must not look clear"); aoqi@0: } aoqi@0: aoqi@0: void TimeStamp::update() { aoqi@0: update_to(os::elapsed_counter()); aoqi@0: } aoqi@0: aoqi@0: double TimeStamp::seconds() const { aoqi@0: assert(is_updated(), "must not be clear"); aoqi@0: jlong new_count = os::elapsed_counter(); aoqi@0: return TimeHelper::counter_to_seconds(new_count - _counter); aoqi@0: } aoqi@0: aoqi@0: jlong TimeStamp::milliseconds() const { aoqi@0: assert(is_updated(), "must not be clear"); aoqi@0: aoqi@0: jlong new_count = os::elapsed_counter(); aoqi@0: jlong count = new_count - _counter; aoqi@0: jlong ticks_per_ms = os::elapsed_frequency() / 1000; aoqi@0: return count / ticks_per_ms; aoqi@0: } aoqi@0: aoqi@0: jlong TimeStamp::ticks_since_update() const { aoqi@0: assert(is_updated(), "must not be clear"); aoqi@0: return os::elapsed_counter() - _counter; aoqi@0: } aoqi@0: aoqi@0: TraceTime::TraceTime(const char* title, aoqi@0: bool doit) { aoqi@0: _active = doit; aoqi@0: _verbose = true; aoqi@0: aoqi@0: if (_active) { aoqi@0: _accum = NULL; aoqi@0: tty->stamp(PrintGCTimeStamps); aoqi@0: tty->print("[%s", title); aoqi@0: tty->flush(); aoqi@0: _t.start(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: TraceTime::TraceTime(const char* title, aoqi@0: elapsedTimer* accumulator, aoqi@0: bool doit, aoqi@0: bool verbose) { aoqi@0: _active = doit; aoqi@0: _verbose = verbose; aoqi@0: if (_active) { aoqi@0: if (_verbose) { aoqi@0: tty->stamp(PrintGCTimeStamps); aoqi@0: tty->print("[%s", title); aoqi@0: tty->flush(); aoqi@0: } aoqi@0: _accum = accumulator; aoqi@0: _t.start(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: TraceTime::~TraceTime() { aoqi@0: if (_active) { aoqi@0: _t.stop(); aoqi@0: if (_accum!=NULL) _accum->add(_t); aoqi@0: if (_verbose) { aoqi@0: tty->print_cr(", %3.7f secs]", _t.seconds()); aoqi@0: tty->flush(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: TraceCPUTime::TraceCPUTime(bool doit, aoqi@0: bool print_cr, aoqi@0: outputStream *logfile) : aoqi@0: _active(doit), aoqi@0: _print_cr(print_cr), aoqi@0: _starting_user_time(0.0), aoqi@0: _starting_system_time(0.0), aoqi@0: _starting_real_time(0.0), aoqi@0: _logfile(logfile), aoqi@0: _error(false) { aoqi@0: if (_active) { aoqi@0: if (logfile != NULL) { aoqi@0: _logfile = logfile; aoqi@0: } else { aoqi@0: _logfile = tty; aoqi@0: } aoqi@0: aoqi@0: _error = !os::getTimesSecs(&_starting_real_time, aoqi@0: &_starting_user_time, aoqi@0: &_starting_system_time); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: TraceCPUTime::~TraceCPUTime() { aoqi@0: if (_active) { aoqi@0: bool valid = false; aoqi@0: if (!_error) { aoqi@0: double real_secs; // walk clock time aoqi@0: double system_secs; // system time aoqi@0: double user_secs; // user time for all threads aoqi@0: aoqi@0: double real_time, user_time, system_time; aoqi@0: valid = os::getTimesSecs(&real_time, &user_time, &system_time); aoqi@0: if (valid) { aoqi@0: aoqi@0: user_secs = user_time - _starting_user_time; aoqi@0: system_secs = system_time - _starting_system_time; aoqi@0: real_secs = real_time - _starting_real_time; aoqi@0: aoqi@0: _logfile->print(" [Times: user=%3.2f sys=%3.2f, real=%3.2f secs] ", aoqi@0: user_secs, system_secs, real_secs); aoqi@0: aoqi@0: } else { aoqi@0: _logfile->print("[Invalid result in TraceCPUTime]"); aoqi@0: } aoqi@0: } else { aoqi@0: _logfile->print("[Error in TraceCPUTime]"); aoqi@0: } aoqi@0: if (_print_cr) { aoqi@0: _logfile->cr(); aoqi@0: } aoqi@0: _logfile->flush(); aoqi@0: } aoqi@0: }