src/hotspot/share/runtime/timer.cpp
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 37161 hotspot/src/share/vm/runtime/timer.cpp@e881f320966e
child 47881 0ce0ac68ace7
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
36178
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35061
diff changeset
     2
 * Copyright (c) 1997, 2016, 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"
36178
9739f8c767da 8148630: Convert TraceStartupTime to Unified Logging
rprotacio
parents: 35061
diff changeset
    26
#include "logging/log.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
#include "oops/oop.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "runtime/timer.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#include "utilities/ostream.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 14634
diff changeset
    31
double TimeHelper::counter_to_seconds(jlong counter) {
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 14634
diff changeset
    32
  double freq  = (double) os::elapsed_frequency();
30608
d79880a5cf2f 8079561: Add a method to convert counters to milliseconds
brutisso
parents: 25468
diff changeset
    33
  return counter / freq;
d79880a5cf2f 8079561: Add a method to convert counters to milliseconds
brutisso
parents: 25468
diff changeset
    34
}
d79880a5cf2f 8079561: Add a method to convert counters to milliseconds
brutisso
parents: 25468
diff changeset
    35
d79880a5cf2f 8079561: Add a method to convert counters to milliseconds
brutisso
parents: 25468
diff changeset
    36
double TimeHelper::counter_to_millis(jlong counter) {
d79880a5cf2f 8079561: Add a method to convert counters to milliseconds
brutisso
parents: 25468
diff changeset
    37
  return counter_to_seconds(counter) * 1000.0;
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 14634
diff changeset
    38
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
33160
c59f1676d27e 8136421: JEP 243: Java-Level JVM Compiler Interface
twisti
parents: 31334
diff changeset
    40
elapsedTimer::elapsedTimer(jlong time, jlong timeUnitsPerSecond) {
c59f1676d27e 8136421: JEP 243: Java-Level JVM Compiler Interface
twisti
parents: 31334
diff changeset
    41
  _active = false;
c59f1676d27e 8136421: JEP 243: Java-Level JVM Compiler Interface
twisti
parents: 31334
diff changeset
    42
  jlong osTimeUnitsPerSecond = os::elapsed_frequency();
c59f1676d27e 8136421: JEP 243: Java-Level JVM Compiler Interface
twisti
parents: 31334
diff changeset
    43
  assert(osTimeUnitsPerSecond % 1000 == 0, "must be");
c59f1676d27e 8136421: JEP 243: Java-Level JVM Compiler Interface
twisti
parents: 31334
diff changeset
    44
  assert(timeUnitsPerSecond % 1000 == 0, "must be");
c59f1676d27e 8136421: JEP 243: Java-Level JVM Compiler Interface
twisti
parents: 31334
diff changeset
    45
  while (osTimeUnitsPerSecond < timeUnitsPerSecond) {
c59f1676d27e 8136421: JEP 243: Java-Level JVM Compiler Interface
twisti
parents: 31334
diff changeset
    46
    timeUnitsPerSecond /= 1000;
c59f1676d27e 8136421: JEP 243: Java-Level JVM Compiler Interface
twisti
parents: 31334
diff changeset
    47
    time *= 1000;
c59f1676d27e 8136421: JEP 243: Java-Level JVM Compiler Interface
twisti
parents: 31334
diff changeset
    48
  }
c59f1676d27e 8136421: JEP 243: Java-Level JVM Compiler Interface
twisti
parents: 31334
diff changeset
    49
  while (osTimeUnitsPerSecond > timeUnitsPerSecond) {
c59f1676d27e 8136421: JEP 243: Java-Level JVM Compiler Interface
twisti
parents: 31334
diff changeset
    50
    timeUnitsPerSecond *= 1000;
c59f1676d27e 8136421: JEP 243: Java-Level JVM Compiler Interface
twisti
parents: 31334
diff changeset
    51
    time /= 1000;
c59f1676d27e 8136421: JEP 243: Java-Level JVM Compiler Interface
twisti
parents: 31334
diff changeset
    52
  }
c59f1676d27e 8136421: JEP 243: Java-Level JVM Compiler Interface
twisti
parents: 31334
diff changeset
    53
  _counter = time;
c59f1676d27e 8136421: JEP 243: Java-Level JVM Compiler Interface
twisti
parents: 31334
diff changeset
    54
}
c59f1676d27e 8136421: JEP 243: Java-Level JVM Compiler Interface
twisti
parents: 31334
diff changeset
    55
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
void elapsedTimer::add(elapsedTimer t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  _counter += t._counter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
void elapsedTimer::start() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  if (!_active) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
    _active = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
    _start_counter = os::elapsed_counter();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
void elapsedTimer::stop() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  if (_active) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
    _counter += os::elapsed_counter() - _start_counter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
    _active = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
double elapsedTimer::seconds() const {
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 14634
diff changeset
    75
 return TimeHelper::counter_to_seconds(_counter);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
jlong elapsedTimer::milliseconds() const {
31334
d55c96b36b5f 8085975: Fix warning "converting to jlong from double" of gcc 4.1.2 after 8079561
goetz
parents: 30608
diff changeset
    79
  return (jlong)TimeHelper::counter_to_millis(_counter);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
jlong elapsedTimer::active_ticks() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  if (!_active) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
    return ticks();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  jlong counter = _counter + os::elapsed_counter() - _start_counter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  return 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
void TimeStamp::update_to(jlong ticks) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  _counter = ticks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  if (_counter == 0)  _counter = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  assert(is_updated(), "must not look clear");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
void TimeStamp::update() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  update_to(os::elapsed_counter());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
double TimeStamp::seconds() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  assert(is_updated(), "must not be clear");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  jlong new_count = os::elapsed_counter();
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 14634
diff changeset
   103
  return TimeHelper::counter_to_seconds(new_count - _counter);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
jlong TimeStamp::milliseconds() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  assert(is_updated(), "must not be clear");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  jlong new_count = os::elapsed_counter();
31334
d55c96b36b5f 8085975: Fix warning "converting to jlong from double" of gcc 4.1.2 after 8079561
goetz
parents: 30608
diff changeset
   109
  return (jlong)TimeHelper::counter_to_millis(new_count - _counter);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
jlong TimeStamp::ticks_since_update() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  assert(is_updated(), "must not be clear");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  return os::elapsed_counter() - _counter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
TraceCPUTime::TraceCPUTime(bool doit,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
               bool print_cr,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
               outputStream *logfile) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  _active(doit),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  _print_cr(print_cr),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  _starting_user_time(0.0),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  _starting_system_time(0.0),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  _starting_real_time(0.0),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  _logfile(logfile),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  _error(false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  if (_active) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    if (logfile != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
      _logfile = logfile;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
      _logfile = tty;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    _error = !os::getTimesSecs(&_starting_real_time,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
                               &_starting_user_time,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
                               &_starting_system_time);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
TraceCPUTime::~TraceCPUTime() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  if (_active) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
    bool valid = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    if (!_error) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
      double real_secs;                 // walk clock time
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
      double system_secs;               // system time
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
      double user_secs;                 // user time for all threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
      double real_time, user_time, system_time;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
      valid = os::getTimesSecs(&real_time, &user_time, &system_time);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
      if (valid) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
        user_secs = user_time - _starting_user_time;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
        system_secs = system_time - _starting_system_time;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
        real_secs = real_time - _starting_real_time;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
23223
d38d6390733f 8023899: Typo in TraceCPUTime message
jwilhelm
parents: 22827
diff changeset
   156
        _logfile->print(" [Times: user=%3.2f sys=%3.2f real=%3.2f secs] ",
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
          user_secs, system_secs, real_secs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
        _logfile->print("[Invalid result in TraceCPUTime]");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
      _logfile->print("[Error in TraceCPUTime]");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
    }
14634
fdd9909928ae 8004170: G1: Verbose GC output is not getting flushed to log file using JDK 8
johnc
parents: 13963
diff changeset
   165
    if (_print_cr) {
24424
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 23223
diff changeset
   166
      _logfile->cr();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    }
14634
fdd9909928ae 8004170: G1: Verbose GC output is not getting flushed to log file using JDK 8
johnc
parents: 13963
diff changeset
   168
    _logfile->flush();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
}
37161
e881f320966e 8150015: Integrate TraceTime with Unified Logging more seamlessly
rehn
parents: 37043
diff changeset
   171