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