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