src/share/vm/runtime/timer.cpp

changeset 435
a61af66fc99e
child 1907
c18cbe5936b8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/runtime/timer.cpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,209 @@
     1.4 +/*
     1.5 + * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +# include "incls/_precompiled.incl"
    1.29 +# include "incls/_timer.cpp.incl"
    1.30 +
    1.31 +
    1.32 +void elapsedTimer::add(elapsedTimer t) {
    1.33 +  _counter += t._counter;
    1.34 +}
    1.35 +
    1.36 +void elapsedTimer::start() {
    1.37 +  if (!_active) {
    1.38 +    _active = true;
    1.39 +    _start_counter = os::elapsed_counter();
    1.40 +  }
    1.41 +}
    1.42 +
    1.43 +void elapsedTimer::stop() {
    1.44 +  if (_active) {
    1.45 +    _counter += os::elapsed_counter() - _start_counter;
    1.46 +    _active = false;
    1.47 +  }
    1.48 +}
    1.49 +
    1.50 +double elapsedTimer::seconds() const {
    1.51 +  double count = (double) _counter;
    1.52 +  double freq  = (double) os::elapsed_frequency();
    1.53 +  return count/freq;
    1.54 +}
    1.55 +
    1.56 +jlong elapsedTimer::milliseconds() const {
    1.57 +  jlong ticks_per_ms = os::elapsed_frequency() / 1000;
    1.58 +  return _counter / ticks_per_ms;
    1.59 +}
    1.60 +
    1.61 +jlong elapsedTimer::active_ticks() const {
    1.62 +  if (!_active) {
    1.63 +    return ticks();
    1.64 +  }
    1.65 +  jlong counter = _counter + os::elapsed_counter() - _start_counter;
    1.66 +  return counter;
    1.67 +}
    1.68 +
    1.69 +void TimeStamp::update_to(jlong ticks) {
    1.70 +  _counter = ticks;
    1.71 +  if (_counter == 0)  _counter = 1;
    1.72 +  assert(is_updated(), "must not look clear");
    1.73 +}
    1.74 +
    1.75 +void TimeStamp::update() {
    1.76 +  update_to(os::elapsed_counter());
    1.77 +}
    1.78 +
    1.79 +double TimeStamp::seconds() const {
    1.80 +  assert(is_updated(), "must not be clear");
    1.81 +  jlong new_count = os::elapsed_counter();
    1.82 +  double count = (double) new_count - _counter;
    1.83 +  double freq  = (double) os::elapsed_frequency();
    1.84 +  return count/freq;
    1.85 +}
    1.86 +
    1.87 +jlong TimeStamp::milliseconds() const {
    1.88 +  assert(is_updated(), "must not be clear");
    1.89 +
    1.90 +  jlong new_count = os::elapsed_counter();
    1.91 +  jlong count = new_count - _counter;
    1.92 +  jlong ticks_per_ms = os::elapsed_frequency() / 1000;
    1.93 +  return count / ticks_per_ms;
    1.94 +}
    1.95 +
    1.96 +jlong TimeStamp::ticks_since_update() const {
    1.97 +  assert(is_updated(), "must not be clear");
    1.98 +  return os::elapsed_counter() - _counter;
    1.99 +}
   1.100 +
   1.101 +TraceTime::TraceTime(const char* title,
   1.102 +                     bool doit,
   1.103 +                     bool print_cr,
   1.104 +                     outputStream* logfile) {
   1.105 +  _active   = doit;
   1.106 +  _verbose  = true;
   1.107 +  _print_cr = print_cr;
   1.108 +  _logfile = (logfile != NULL) ? logfile : tty;
   1.109 +
   1.110 +  if (_active) {
   1.111 +    _accum = NULL;
   1.112 +    if (PrintGCTimeStamps) {
   1.113 +      _logfile->stamp();
   1.114 +      _logfile->print(": ");
   1.115 +    }
   1.116 +    _logfile->print("[%s", title);
   1.117 +    _logfile->flush();
   1.118 +    _t.start();
   1.119 +  }
   1.120 +}
   1.121 +
   1.122 +TraceTime::TraceTime(const char* title,
   1.123 +                     elapsedTimer* accumulator,
   1.124 +                     bool doit,
   1.125 +                     bool verbose,
   1.126 +                     outputStream* logfile) {
   1.127 +  _active = doit;
   1.128 +  _verbose = verbose;
   1.129 +  _print_cr = true;
   1.130 +  _logfile = (logfile != NULL) ? logfile : tty;
   1.131 +  if (_active) {
   1.132 +    if (_verbose) {
   1.133 +      if (PrintGCTimeStamps) {
   1.134 +        _logfile->stamp();
   1.135 +        _logfile->print(": ");
   1.136 +      }
   1.137 +      _logfile->print("[%s", title);
   1.138 +      _logfile->flush();
   1.139 +    }
   1.140 +    _accum = accumulator;
   1.141 +    _t.start();
   1.142 +  }
   1.143 +}
   1.144 +
   1.145 +TraceTime::~TraceTime() {
   1.146 +  if (_active) {
   1.147 +    _t.stop();
   1.148 +    if (_accum!=NULL) _accum->add(_t);
   1.149 +    if (_verbose) {
   1.150 +      if (_print_cr) {
   1.151 +        _logfile->print_cr(", %3.7f secs]", _t.seconds());
   1.152 +      } else {
   1.153 +        _logfile->print(", %3.7f secs]", _t.seconds());
   1.154 +      }
   1.155 +      _logfile->flush();
   1.156 +    }
   1.157 +  }
   1.158 +}
   1.159 +
   1.160 +TraceCPUTime::TraceCPUTime(bool doit,
   1.161 +               bool print_cr,
   1.162 +               outputStream *logfile) :
   1.163 +  _active(doit),
   1.164 +  _print_cr(print_cr),
   1.165 +  _starting_user_time(0.0),
   1.166 +  _starting_system_time(0.0),
   1.167 +  _starting_real_time(0.0),
   1.168 +  _logfile(logfile),
   1.169 +  _error(false) {
   1.170 +  if (_active) {
   1.171 +    if (logfile != NULL) {
   1.172 +      _logfile = logfile;
   1.173 +    } else {
   1.174 +      _logfile = tty;
   1.175 +    }
   1.176 +
   1.177 +    _error = !os::getTimesSecs(&_starting_real_time,
   1.178 +                               &_starting_user_time,
   1.179 +                               &_starting_system_time);
   1.180 +  }
   1.181 +}
   1.182 +
   1.183 +TraceCPUTime::~TraceCPUTime() {
   1.184 +  if (_active) {
   1.185 +    bool valid = false;
   1.186 +    if (!_error) {
   1.187 +      double real_secs;                 // walk clock time
   1.188 +      double system_secs;               // system time
   1.189 +      double user_secs;                 // user time for all threads
   1.190 +
   1.191 +      double real_time, user_time, system_time;
   1.192 +      valid = os::getTimesSecs(&real_time, &user_time, &system_time);
   1.193 +      if (valid) {
   1.194 +
   1.195 +        user_secs = user_time - _starting_user_time;
   1.196 +        system_secs = system_time - _starting_system_time;
   1.197 +        real_secs = real_time - _starting_real_time;
   1.198 +
   1.199 +        _logfile->print(" [Times: user=%3.2f sys=%3.2f, real=%3.2f secs] ",
   1.200 +          user_secs, system_secs, real_secs);
   1.201 +
   1.202 +      } else {
   1.203 +        _logfile->print("[Invalid result in TraceCPUTime]");
   1.204 +      }
   1.205 +    } else {
   1.206 +      _logfile->print("[Error in TraceCPUTime]");
   1.207 +    }
   1.208 +     if (_print_cr) {
   1.209 +      _logfile->print_cr("");
   1.210 +    }
   1.211 +  }
   1.212 +}

mercurial