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