src/share/vm/runtime/timer.cpp

Wed, 09 Apr 2008 15:10:22 -0700

author
rasbold
date
Wed, 09 Apr 2008 15:10:22 -0700
changeset 544
9f4457a14b58
parent 435
a61af66fc99e
child 1907
c18cbe5936b8
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright 1997-2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 # include "incls/_precompiled.incl"
    26 # include "incls/_timer.cpp.incl"
    29 void elapsedTimer::add(elapsedTimer t) {
    30   _counter += t._counter;
    31 }
    33 void elapsedTimer::start() {
    34   if (!_active) {
    35     _active = true;
    36     _start_counter = os::elapsed_counter();
    37   }
    38 }
    40 void elapsedTimer::stop() {
    41   if (_active) {
    42     _counter += os::elapsed_counter() - _start_counter;
    43     _active = false;
    44   }
    45 }
    47 double elapsedTimer::seconds() const {
    48   double count = (double) _counter;
    49   double freq  = (double) os::elapsed_frequency();
    50   return count/freq;
    51 }
    53 jlong elapsedTimer::milliseconds() const {
    54   jlong ticks_per_ms = os::elapsed_frequency() / 1000;
    55   return _counter / ticks_per_ms;
    56 }
    58 jlong elapsedTimer::active_ticks() const {
    59   if (!_active) {
    60     return ticks();
    61   }
    62   jlong counter = _counter + os::elapsed_counter() - _start_counter;
    63   return counter;
    64 }
    66 void TimeStamp::update_to(jlong ticks) {
    67   _counter = ticks;
    68   if (_counter == 0)  _counter = 1;
    69   assert(is_updated(), "must not look clear");
    70 }
    72 void TimeStamp::update() {
    73   update_to(os::elapsed_counter());
    74 }
    76 double TimeStamp::seconds() const {
    77   assert(is_updated(), "must not be clear");
    78   jlong new_count = os::elapsed_counter();
    79   double count = (double) new_count - _counter;
    80   double freq  = (double) os::elapsed_frequency();
    81   return count/freq;
    82 }
    84 jlong TimeStamp::milliseconds() const {
    85   assert(is_updated(), "must not be clear");
    87   jlong new_count = os::elapsed_counter();
    88   jlong count = new_count - _counter;
    89   jlong ticks_per_ms = os::elapsed_frequency() / 1000;
    90   return count / ticks_per_ms;
    91 }
    93 jlong TimeStamp::ticks_since_update() const {
    94   assert(is_updated(), "must not be clear");
    95   return os::elapsed_counter() - _counter;
    96 }
    98 TraceTime::TraceTime(const char* title,
    99                      bool doit,
   100                      bool print_cr,
   101                      outputStream* logfile) {
   102   _active   = doit;
   103   _verbose  = true;
   104   _print_cr = print_cr;
   105   _logfile = (logfile != NULL) ? logfile : tty;
   107   if (_active) {
   108     _accum = NULL;
   109     if (PrintGCTimeStamps) {
   110       _logfile->stamp();
   111       _logfile->print(": ");
   112     }
   113     _logfile->print("[%s", title);
   114     _logfile->flush();
   115     _t.start();
   116   }
   117 }
   119 TraceTime::TraceTime(const char* title,
   120                      elapsedTimer* accumulator,
   121                      bool doit,
   122                      bool verbose,
   123                      outputStream* logfile) {
   124   _active = doit;
   125   _verbose = verbose;
   126   _print_cr = true;
   127   _logfile = (logfile != NULL) ? logfile : tty;
   128   if (_active) {
   129     if (_verbose) {
   130       if (PrintGCTimeStamps) {
   131         _logfile->stamp();
   132         _logfile->print(": ");
   133       }
   134       _logfile->print("[%s", title);
   135       _logfile->flush();
   136     }
   137     _accum = accumulator;
   138     _t.start();
   139   }
   140 }
   142 TraceTime::~TraceTime() {
   143   if (_active) {
   144     _t.stop();
   145     if (_accum!=NULL) _accum->add(_t);
   146     if (_verbose) {
   147       if (_print_cr) {
   148         _logfile->print_cr(", %3.7f secs]", _t.seconds());
   149       } else {
   150         _logfile->print(", %3.7f secs]", _t.seconds());
   151       }
   152       _logfile->flush();
   153     }
   154   }
   155 }
   157 TraceCPUTime::TraceCPUTime(bool doit,
   158                bool print_cr,
   159                outputStream *logfile) :
   160   _active(doit),
   161   _print_cr(print_cr),
   162   _starting_user_time(0.0),
   163   _starting_system_time(0.0),
   164   _starting_real_time(0.0),
   165   _logfile(logfile),
   166   _error(false) {
   167   if (_active) {
   168     if (logfile != NULL) {
   169       _logfile = logfile;
   170     } else {
   171       _logfile = tty;
   172     }
   174     _error = !os::getTimesSecs(&_starting_real_time,
   175                                &_starting_user_time,
   176                                &_starting_system_time);
   177   }
   178 }
   180 TraceCPUTime::~TraceCPUTime() {
   181   if (_active) {
   182     bool valid = false;
   183     if (!_error) {
   184       double real_secs;                 // walk clock time
   185       double system_secs;               // system time
   186       double user_secs;                 // user time for all threads
   188       double real_time, user_time, system_time;
   189       valid = os::getTimesSecs(&real_time, &user_time, &system_time);
   190       if (valid) {
   192         user_secs = user_time - _starting_user_time;
   193         system_secs = system_time - _starting_system_time;
   194         real_secs = real_time - _starting_real_time;
   196         _logfile->print(" [Times: user=%3.2f sys=%3.2f, real=%3.2f secs] ",
   197           user_secs, system_secs, real_secs);
   199       } else {
   200         _logfile->print("[Invalid result in TraceCPUTime]");
   201       }
   202     } else {
   203       _logfile->print("[Error in TraceCPUTime]");
   204     }
   205      if (_print_cr) {
   206       _logfile->print_cr("");
   207     }
   208   }
   209 }

mercurial