duke@435: /* duke@435: * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // Timers for simple measurement. duke@435: duke@435: class elapsedTimer VALUE_OBJ_CLASS_SPEC { duke@435: friend class VMStructs; duke@435: private: duke@435: jlong _counter; duke@435: jlong _start_counter; duke@435: bool _active; duke@435: public: duke@435: elapsedTimer() { _active = false; reset(); } duke@435: void add(elapsedTimer t); duke@435: void start(); duke@435: void stop(); duke@435: void reset() { _counter = 0; } duke@435: double seconds() const; duke@435: jlong milliseconds() const; duke@435: jlong ticks() const { return _counter; } duke@435: jlong active_ticks() const; duke@435: bool is_active() const { return _active; } duke@435: }; duke@435: duke@435: // TimeStamp is used for recording when an event took place. duke@435: class TimeStamp VALUE_OBJ_CLASS_SPEC { duke@435: private: duke@435: jlong _counter; duke@435: public: duke@435: TimeStamp() { _counter = 0; } duke@435: void clear() { _counter = 0; } duke@435: // has the timestamp been updated since being created or cleared? duke@435: bool is_updated() const { return _counter != 0; } duke@435: // update to current elapsed time duke@435: void update(); duke@435: // update to given elapsed time duke@435: void update_to(jlong ticks); duke@435: // returns seconds since updated duke@435: // (must not be in a cleared state: must have been previously updated) duke@435: double seconds() const; duke@435: jlong milliseconds() const; duke@435: // ticks elapsed between VM start and last update duke@435: jlong ticks() const { return _counter; } duke@435: // ticks elapsed since last update duke@435: jlong ticks_since_update() const; duke@435: }; duke@435: duke@435: // TraceTime is used for tracing the execution time of a block duke@435: // Usage: duke@435: // { TraceTime t("block time") duke@435: // some_code(); duke@435: // } duke@435: // duke@435: duke@435: class TraceTime: public StackObj { duke@435: private: duke@435: bool _active; // do timing duke@435: bool _verbose; // report every timing duke@435: bool _print_cr; // add a CR to the end of the timer report duke@435: elapsedTimer _t; // timer duke@435: elapsedTimer* _accum; // accumulator duke@435: outputStream* _logfile; // output log file duke@435: public: duke@435: // Constuctors duke@435: TraceTime(const char* title, duke@435: bool doit = true, duke@435: bool print_cr = true, duke@435: outputStream *logfile = NULL); duke@435: TraceTime(const char* title, duke@435: elapsedTimer* accumulator, duke@435: bool doit = true, duke@435: bool verbose = false, duke@435: outputStream *logfile = NULL ); duke@435: ~TraceTime(); duke@435: duke@435: // Accessors duke@435: void set_verbose(bool verbose) { _verbose = verbose; } duke@435: bool verbose() const { return _verbose; } duke@435: duke@435: // Activation duke@435: void suspend() { if (_active) _t.stop(); } duke@435: void resume() { if (_active) _t.start(); } duke@435: }; duke@435: duke@435: class TraceCPUTime: public StackObj { duke@435: private: duke@435: bool _active; // true if times will be measured and printed duke@435: bool _print_cr; // if true print carriage return at end duke@435: double _starting_user_time; // user time at start of measurement duke@435: double _starting_system_time; // system time at start of measurement duke@435: double _starting_real_time; // real time at start of measurement duke@435: outputStream* _logfile; // output is printed to this stream duke@435: bool _error; // true if an error occurred, turns off output duke@435: duke@435: public: duke@435: TraceCPUTime(bool doit = true, duke@435: bool print_cr = true, duke@435: outputStream *logfile = NULL); duke@435: ~TraceCPUTime(); duke@435: };