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