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