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  protected:
  46   jlong _startTime;
  47   jlong _endTime;
  48 
  49  private:
  50   bool _started;
  51 #ifdef ASSERT
  52   bool _committed;
  53   bool _cancelled;
  54  protected:
  55   bool _ignore_check;
  56 #endif
  57 
  58  public:
  59   TraceEvent(EventStartTime timing=TIMED) :
  60     _startTime(0),
  61     _endTime(0),
  62     _started(false)
  63 #ifdef ASSERT
  64     ,
  65     _committed(false),
  66     _cancelled(false),
  67     _ignore_check(false)
  68 #endif
  69   {
  70     if (T::is_enabled()) {
  71       _started = true;
  72       if (timing == TIMED && !T::isInstant) {
  73         static_cast<T *>(this)->set_starttime(Tracing::time());
  74       }
  75     }
  76   }
  77 
  78   static bool is_enabled() {
  79     return Tracing::is_event_enabled(T::eventId);
  80   }
  81 
  82   bool should_commit() {
  83     return _started;
  84   }
  85 
  86   void ignoreCheck() {
  87     DEBUG_ONLY(_ignore_check = true);
  88   }
  89 
  90   void commit() {
  91     if (!should_commit()) {
  92         cancel();
  93         return;
  94     }
  95     if (_endTime == 0) {
  96       static_cast<T*>(this)->set_endtime(Tracing::time());
  97     }
  98     if (static_cast<T*>(this)->should_write()) {
  99       static_cast<T*>(this)->writeEvent();
 100     }
 101     set_commited();
 102   }
 103 
 104   void set_starttime(const TracingTime& time = Tracing::time()) {
 105     _startTime = time;
 106   }
 107 
 108   void set_endtime(const TracingTime& time = Tracing::time()) {
 109     _endTime = time;
 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 */