src/share/vm/runtime/timer.cpp

Print this page
rev 4773 : 8005849: JEP 167: Event-Based JVM Tracing
Reviewed-by: acorn, coleenp, sla
Contributed-by: Karen Kinnear <karen.kinnear@oracle.com>, Bengt Rutisson <bengt.rutisson@oracle.com>, Calvin Cheung <calvin.cheung@oracle.com>, Erik Gahlin <erik.gahlin@oracle.com>, Erik Helin <erik.helin@oracle.com>, Jesper Wilhelmsson <jesper.wilhelmsson@oracle.com>, Keith McGuigan <keith.mcguigan@oracle.com>, Mattias Tobiasson <mattias.tobiasson@oracle.com>, Markus Gronlund <markus.gronlund@oracle.com>, Mikael Auno <mikael.auno@oracle.com>, Nils Eliasson <nils.eliasson@oracle.com>, Nils Loodin <nils.loodin@oracle.com>, Rickard Backman <rickard.backman@oracle.com>, Staffan Larsen <staffan.larsen@oracle.com>, Stefan Karlsson <stefan.karlsson@oracle.com>, Yekaterina Kantserova <yekaterina.kantserova@oracle.com>
   1 /*
   2  * Copyright (c) 1997, 2012, 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     }


   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     }