src/share/vm/runtime/timer.cpp

Sun, 01 Jan 2012 11:17:59 -0500

author
phh
date
Sun, 01 Jan 2012 11:17:59 -0500
changeset 3378
7ab5f6318694
parent 3156
f08d439fab8c
child 4063
9646b7ff4d14
permissions
-rw-r--r--

7125934: Add a fast unordered timestamp capability to Hotspot on x86/x64
Summary: Add rdtsc detection and inline generation.
Reviewed-by: kamg, dholmes
Contributed-by: karen.kinnear@oracle.com

     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  */
    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
    43 void elapsedTimer::add(elapsedTimer t) {
    44   _counter += t._counter;
    45 }
    47 void elapsedTimer::start() {
    48   if (!_active) {
    49     _active = true;
    50     _start_counter = os::elapsed_counter();
    51   }
    52 }
    54 void elapsedTimer::stop() {
    55   if (_active) {
    56     _counter += os::elapsed_counter() - _start_counter;
    57     _active = false;
    58   }
    59 }
    61 double elapsedTimer::seconds() const {
    62   double count = (double) _counter;
    63   double freq  = (double) os::elapsed_frequency();
    64   return count/freq;
    65 }
    67 jlong elapsedTimer::milliseconds() const {
    68   jlong ticks_per_ms = os::elapsed_frequency() / 1000;
    69   return _counter / ticks_per_ms;
    70 }
    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 }
    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 }
    86 void TimeStamp::update() {
    87   update_to(os::elapsed_counter());
    88 }
    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 }
    98 jlong TimeStamp::milliseconds() const {
    99   assert(is_updated(), "must not be clear");
   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 }
   107 jlong TimeStamp::ticks_since_update() const {
   108   assert(is_updated(), "must not be clear");
   109   return os::elapsed_counter() - _counter;
   110 }
   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;
   121   if (_active) {
   122     _accum = NULL;
   123     if (PrintGCTimeStamps) {
   124       _logfile->stamp();
   125       _logfile->print(": ");
   126     }
   127     _logfile->print("[%s", title);
   128     _logfile->flush();
   129     _t.start();
   130   }
   131 }
   133 TraceTime::TraceTime(const char* title,
   134                      elapsedTimer* accumulator,
   135                      bool doit,
   136                      bool verbose,
   137                      outputStream* logfile) {
   138   _active = doit;
   139   _verbose = verbose;
   140   _print_cr = true;
   141   _logfile = (logfile != NULL) ? logfile : tty;
   142   if (_active) {
   143     if (_verbose) {
   144       if (PrintGCTimeStamps) {
   145         _logfile->stamp();
   146         _logfile->print(": ");
   147       }
   148       _logfile->print("[%s", title);
   149       _logfile->flush();
   150     }
   151     _accum = accumulator;
   152     _t.start();
   153   }
   154 }
   156 TraceTime::~TraceTime() {
   157   if (_active) {
   158     _t.stop();
   159     if (_accum!=NULL) _accum->add(_t);
   160     if (_verbose) {
   161       if (_print_cr) {
   162         _logfile->print_cr(", %3.7f secs]", _t.seconds());
   163       } else {
   164         _logfile->print(", %3.7f secs]", _t.seconds());
   165       }
   166       _logfile->flush();
   167     }
   168   }
   169 }
   171 TraceCPUTime::TraceCPUTime(bool doit,
   172                bool print_cr,
   173                outputStream *logfile) :
   174   _active(doit),
   175   _print_cr(print_cr),
   176   _starting_user_time(0.0),
   177   _starting_system_time(0.0),
   178   _starting_real_time(0.0),
   179   _logfile(logfile),
   180   _error(false) {
   181   if (_active) {
   182     if (logfile != NULL) {
   183       _logfile = logfile;
   184     } else {
   185       _logfile = tty;
   186     }
   188     _error = !os::getTimesSecs(&_starting_real_time,
   189                                &_starting_user_time,
   190                                &_starting_system_time);
   191   }
   192 }
   194 TraceCPUTime::~TraceCPUTime() {
   195   if (_active) {
   196     bool valid = false;
   197     if (!_error) {
   198       double real_secs;                 // walk clock time
   199       double system_secs;               // system time
   200       double user_secs;                 // user time for all threads
   202       double real_time, user_time, system_time;
   203       valid = os::getTimesSecs(&real_time, &user_time, &system_time);
   204       if (valid) {
   206         user_secs = user_time - _starting_user_time;
   207         system_secs = system_time - _starting_system_time;
   208         real_secs = real_time - _starting_real_time;
   210         _logfile->print(" [Times: user=%3.2f sys=%3.2f, real=%3.2f secs] ",
   211           user_secs, system_secs, real_secs);
   213       } else {
   214         _logfile->print("[Invalid result in TraceCPUTime]");
   215       }
   216     } else {
   217       _logfile->print("[Error in TraceCPUTime]");
   218     }
   219      if (_print_cr) {
   220       _logfile->print_cr("");
   221     }
   222   }
   223 }

mercurial