1 /* 2 * Copyright (c) 2012, 2013, 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 #ifndef SHARE_VM_TRACE_TRACEEVENT_HPP 26 #define SHARE_VM_TRACE_TRACEEVENT_HPP 27 28 #include "utilities/macros.hpp" 29 30 enum EventStartTime { 31 UNTIMED, 32 TIMED 33 }; 34 35 #if INCLUDE_TRACE 36 37 #include "trace/traceBackend.hpp" 38 #include "trace/tracing.hpp" 39 #include "tracefiles/traceEventIds.hpp" 40 #include "tracefiles/traceTypes.hpp" 41 #include "utilities/ticks.hpp" 42 43 template<typename T> 44 class TraceEvent : public StackObj { 45 private: 46 bool _started; 47 #ifdef ASSERT 48 bool _committed; 49 bool _cancelled; 50 protected: 51 bool _ignore_check; 52 #endif 53 54 protected: 55 jlong _startTime; 56 jlong _endTime; 57 58 void set_starttime(const TracingTime& time) { 59 _startTime = time; 60 } 61 62 void set_endtime(const TracingTime& time) { 63 _endTime = time; 64 } 65 66 public: 67 TraceEvent(EventStartTime timing=TIMED) : 68 _startTime(0), 69 _endTime(0), 70 _started(false) 71 #ifdef ASSERT 72 , 73 _committed(false), 74 _cancelled(false), 75 _ignore_check(false) 76 #endif 77 { 78 if (T::is_enabled()) { 79 _started = true; 80 if (timing == TIMED && !T::isInstant) { 81 static_cast<T *>(this)->set_starttime(Tracing::time()); 82 } 83 } 84 } 85 86 static bool is_enabled() { 87 return Tracing::is_event_enabled(T::eventId); 88 } 89 90 bool should_commit() { 91 return _started; 92 } 93 94 void ignoreCheck() { 95 DEBUG_ONLY(_ignore_check = true); 96 } 97 98 void commit() { 99 if (!should_commit()) { 100 cancel(); 101 return; 102 } 103 if (_endTime == 0) { 104 static_cast<T*>(this)->set_endtime(Tracing::time()); 105 } 106 if (static_cast<T*>(this)->should_write()) { 107 static_cast<T*>(this)->writeEvent(); 108 } 109 set_commited(); 110 } 111 112 void set_starttime(const Ticks& time) { 113 _startTime = time.value(); 114 } 115 116 void set_endtime(const Ticks& time) { 117 _endTime = time.value(); 118 } 119 120 TraceEventId id() const { 121 return T::eventId; 122 } 123 124 bool is_instant() const { 125 return T::isInstant; 126 } 127 128 bool is_requestable() const { 129 return T::isRequestable; 130 } 131 132 bool has_thread() const { 133 return T::hasThread; 134 } 135 136 bool has_stacktrace() const { 137 return T::hasStackTrace; 138 } 139 140 void cancel() { 141 assert(!_committed && !_cancelled, "event was already committed/cancelled"); 142 DEBUG_ONLY(_cancelled = true); 143 } 144 145 void set_commited() { 146 assert(!_committed, "event has already been committed"); 147 DEBUG_ONLY(_committed = true); 148 } 149 150 ~TraceEvent() { 151 if (_started) { 152 assert(_ignore_check || _committed || _cancelled, "event was not committed/cancelled"); 153 } 154 } 155 }; 156 157 #endif /* INCLUDE_TRACE */ 158 159 #endif /* SHARE_VM_TRACE_TRACEEVENT_HPP */