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