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