1 /*
   2  * Copyright (c) 1997, 2010, 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 "oops/oop.inline.hpp"
  27 #include "runtime/timer.hpp"
  28 #include "utilities/ostream.hpp"
  29 #ifdef TARGET_OS_FAMILY_linux
  30 # include "os_linux.inline.hpp"
  31 #endif
  32 #ifdef TARGET_OS_FAMILY_solaris
  33 # include "os_solaris.inline.hpp"
  34 #endif
  35 #ifdef TARGET_OS_FAMILY_windows
  36 # include "os_windows.inline.hpp"
  37 #endif
  38 #ifdef TARGET_OS_FAMILY_bsd
  39 # include "os_bsd.inline.hpp"
  40 #endif
  41 
  42 
  43 void elapsedTimer::add(elapsedTimer t) {
  44   _counter += t._counter;
  45 }
  46 
  47 void elapsedTimer::start() {
  48   if (!_active) {
  49     _active = true;
  50     _start_counter = os::elapsed_counter();
  51   }
  52 }
  53 
  54 void elapsedTimer::stop() {
  55   if (_active) {
  56     _counter += os::elapsed_counter() - _start_counter;
  57     _active = false;
  58   }
  59 }
  60 
  61 double elapsedTimer::seconds() const {
  62   double count = (double) _counter;
  63   double freq  = (double) os::elapsed_frequency();
  64   return count/freq;
  65 }
  66 
  67 jlong elapsedTimer::milliseconds() const {
  68   jlong ticks_per_ms = os::elapsed_frequency() / 1000;
  69   return _counter / ticks_per_ms;
  70 }
  71 
  72 jlong elapsedTimer::active_ticks() const {
  73   if (!_active) {
  74     return ticks();
  75   }
  76   jlong counter = _counter + os::elapsed_counter() - _start_counter;
  77   return counter;
  78 }
  79 
  80 void TimeStamp::update_to(jlong ticks) {
  81   _counter = ticks;
  82   if (_counter == 0)  _counter = 1;
  83   assert(is_updated(), "must not look clear");
  84 }
  85 
  86 void TimeStamp::update() {
  87   update_to(os::elapsed_counter());
  88 }
  89 
  90 double TimeStamp::seconds() const {
  91   assert(is_updated(), "must not be clear");
  92   jlong new_count = os::elapsed_counter();
  93   double count = (double) new_count - _counter;
  94   double freq  = (double) os::elapsed_frequency();
  95   return count/freq;
  96 }
  97 
  98 jlong TimeStamp::milliseconds() const {
  99   assert(is_updated(), "must not be clear");
 100 
 101   jlong new_count = os::elapsed_counter();
 102   jlong count = new_count - _counter;
 103   jlong ticks_per_ms = os::elapsed_frequency() / 1000;
 104   return count / ticks_per_ms;
 105 }
 106 
 107 jlong TimeStamp::ticks_since_update() const {
 108   assert(is_updated(), "must not be clear");
 109   return os::elapsed_counter() - _counter;
 110 }
 111 
 112 TraceTime::TraceTime(const char* title,
 113                      bool doit,
 114                      bool print_cr,
 115                      outputStream* logfile) {
 116   _active   = doit;
 117   _verbose  = true;
 118   _print_cr = print_cr;
 119   _logfile = (logfile != NULL) ? logfile : tty;
 120 
 121   if (_active) {
 122     _accum = NULL;
 123     _logfile->stamp(PrintGCTimeStamps);
 124     _logfile->print("[%s", title);
 125     _logfile->flush();
 126     _t.start();
 127   }
 128 }
 129 
 130 TraceTime::TraceTime(const char* title,
 131                      elapsedTimer* accumulator,
 132                      bool doit,
 133                      bool verbose,
 134                      outputStream* logfile) {
 135   _active = doit;
 136   _verbose = verbose;
 137   _print_cr = true;
 138   _logfile = (logfile != NULL) ? logfile : tty;
 139   if (_active) {
 140     if (_verbose) {
 141       _logfile->stamp(PrintGCTimeStamps);
 142       _logfile->print("[%s", title);
 143       _logfile->flush();
 144     }
 145     _accum = accumulator;
 146     _t.start();
 147   }
 148 }
 149 
 150 TraceTime::~TraceTime() {
 151   if (_active) {
 152     _t.stop();
 153     if (_accum!=NULL) _accum->add(_t);
 154     if (_verbose) {
 155       if (_print_cr) {
 156         _logfile->print_cr(", %3.7f secs]", _t.seconds());
 157       } else {
 158         _logfile->print(", %3.7f secs]", _t.seconds());
 159       }
 160       _logfile->flush();
 161     }
 162   }
 163 }
 164 
 165 TraceCPUTime::TraceCPUTime(bool doit,
 166                bool print_cr,
 167                outputStream *logfile) :
 168   _active(doit),
 169   _print_cr(print_cr),
 170   _starting_user_time(0.0),
 171   _starting_system_time(0.0),
 172   _starting_real_time(0.0),
 173   _logfile(logfile),
 174   _error(false) {
 175   if (_active) {
 176     if (logfile != NULL) {
 177       _logfile = logfile;
 178     } else {
 179       _logfile = tty;
 180     }
 181 
 182     _error = !os::getTimesSecs(&_starting_real_time,
 183                                &_starting_user_time,
 184                                &_starting_system_time);
 185   }
 186 }
 187 
 188 TraceCPUTime::~TraceCPUTime() {
 189   if (_active) {
 190     bool valid = false;
 191     if (!_error) {
 192       double real_secs;                 // walk clock time
 193       double system_secs;               // system time
 194       double user_secs;                 // user time for all threads
 195 
 196       double real_time, user_time, system_time;
 197       valid = os::getTimesSecs(&real_time, &user_time, &system_time);
 198       if (valid) {
 199 
 200         user_secs = user_time - _starting_user_time;
 201         system_secs = system_time - _starting_system_time;
 202         real_secs = real_time - _starting_real_time;
 203 
 204         _logfile->print(" [Times: user=%3.2f sys=%3.2f, real=%3.2f secs] ",
 205           user_secs, system_secs, real_secs);
 206 
 207       } else {
 208         _logfile->print("[Invalid result in TraceCPUTime]");
 209       }
 210     } else {
 211       _logfile->print("[Error in TraceCPUTime]");
 212     }
 213      if (_print_cr) {
 214       _logfile->print_cr("");
 215     }
 216   }
 217 }