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