hotspot/src/share/vm/runtime/timer.cpp
author mgerdin
Thu, 23 Feb 2012 14:58:35 +0100
changeset 12095 cc3d6f08a4c4
parent 10565 dc90c239f4ec
child 13754 81163e1274d7
permissions -rw-r--r--
7148152: Add whitebox testing API to HotSpot Summary: Add an internal testing API to HotSpot to enable more targeted testing of vm functionality Reviewed-by: phh, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
     2
 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#include "oops/oop.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
#include "runtime/timer.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "utilities/ostream.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#ifdef TARGET_OS_FAMILY_linux
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    30
# include "os_linux.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    31
#endif
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    32
#ifdef TARGET_OS_FAMILY_solaris
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    33
# include "os_solaris.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    34
#endif
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    35
#ifdef TARGET_OS_FAMILY_windows
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    36
# include "os_windows.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    37
#endif
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents: 7397
diff changeset
    38
#ifdef TARGET_OS_FAMILY_bsd
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents: 7397
diff changeset
    39
# include "os_bsd.inline.hpp"
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents: 7397
diff changeset
    40
#endif
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
void elapsedTimer::add(elapsedTimer t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  _counter += t._counter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
void elapsedTimer::start() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  if (!_active) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
    _active = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
    _start_counter = os::elapsed_counter();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
void elapsedTimer::stop() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  if (_active) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    _counter += os::elapsed_counter() - _start_counter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    _active = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
double elapsedTimer::seconds() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  double count = (double) _counter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  double freq  = (double) os::elapsed_frequency();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  return count/freq;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
jlong elapsedTimer::milliseconds() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  jlong ticks_per_ms = os::elapsed_frequency() / 1000;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  return _counter / ticks_per_ms;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
jlong elapsedTimer::active_ticks() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  if (!_active) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
    return ticks();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  jlong counter = _counter + os::elapsed_counter() - _start_counter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  return counter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
void TimeStamp::update_to(jlong ticks) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  _counter = ticks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  if (_counter == 0)  _counter = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  assert(is_updated(), "must not look clear");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
void TimeStamp::update() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  update_to(os::elapsed_counter());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
double TimeStamp::seconds() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  assert(is_updated(), "must not be clear");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  jlong new_count = os::elapsed_counter();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  double count = (double) new_count - _counter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  double freq  = (double) os::elapsed_frequency();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  return count/freq;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
jlong TimeStamp::milliseconds() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  assert(is_updated(), "must not be clear");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  jlong new_count = os::elapsed_counter();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  jlong count = new_count - _counter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  jlong ticks_per_ms = os::elapsed_frequency() / 1000;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  return count / ticks_per_ms;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
jlong TimeStamp::ticks_since_update() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  assert(is_updated(), "must not be clear");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  return os::elapsed_counter() - _counter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
TraceTime::TraceTime(const char* title,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
                     bool doit,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
                     bool print_cr,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
                     outputStream* logfile) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  _active   = doit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  _verbose  = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  _print_cr = print_cr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  _logfile = (logfile != NULL) ? logfile : tty;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  if (_active) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    _accum = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    if (PrintGCTimeStamps) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
      _logfile->stamp();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
      _logfile->print(": ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    _logfile->print("[%s", title);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    _logfile->flush();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    _t.start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
TraceTime::TraceTime(const char* title,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
                     elapsedTimer* accumulator,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
                     bool doit,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
                     bool verbose,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
                     outputStream* logfile) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  _active = doit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  _verbose = verbose;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  _print_cr = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  _logfile = (logfile != NULL) ? logfile : tty;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  if (_active) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    if (_verbose) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
      if (PrintGCTimeStamps) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
        _logfile->stamp();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
        _logfile->print(": ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
      _logfile->print("[%s", title);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
      _logfile->flush();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    _accum = accumulator;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    _t.start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
TraceTime::~TraceTime() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  if (_active) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
    _t.stop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    if (_accum!=NULL) _accum->add(_t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
    if (_verbose) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
      if (_print_cr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
        _logfile->print_cr(", %3.7f secs]", _t.seconds());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
        _logfile->print(", %3.7f secs]", _t.seconds());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
      _logfile->flush();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
TraceCPUTime::TraceCPUTime(bool doit,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
               bool print_cr,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
               outputStream *logfile) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
  _active(doit),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  _print_cr(print_cr),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  _starting_user_time(0.0),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  _starting_system_time(0.0),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  _starting_real_time(0.0),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  _logfile(logfile),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  _error(false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  if (_active) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    if (logfile != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
      _logfile = logfile;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
      _logfile = tty;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
    _error = !os::getTimesSecs(&_starting_real_time,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
                               &_starting_user_time,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
                               &_starting_system_time);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
TraceCPUTime::~TraceCPUTime() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  if (_active) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
    bool valid = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
    if (!_error) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
      double real_secs;                 // walk clock time
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
      double system_secs;               // system time
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
      double user_secs;                 // user time for all threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
      double real_time, user_time, system_time;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
      valid = os::getTimesSecs(&real_time, &user_time, &system_time);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
      if (valid) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
        user_secs = user_time - _starting_user_time;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
        system_secs = system_time - _starting_system_time;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
        real_secs = real_time - _starting_real_time;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
        _logfile->print(" [Times: user=%3.2f sys=%3.2f, real=%3.2f secs] ",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
          user_secs, system_secs, real_secs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
        _logfile->print("[Invalid result in TraceCPUTime]");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
      _logfile->print("[Error in TraceCPUTime]");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
     if (_print_cr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
      _logfile->print_cr("");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
}