< prev index next >

src/hotspot/share/trace/traceEventClasses.xsl

Print this page
rev 49619 : JEP 328 : Flight Recorder open source preview

@@ -34,127 +34,127 @@
 
 // On purpose outside the INCLUDE_TRACE
 // Some parts of traceEvent.hpp are used outside of
 // INCLUDE_TRACE
 
+#include "trace/traceEvent.hpp"
 #include "tracefiles/traceTypes.hpp"
 #include "utilities/macros.hpp"
-
 #if INCLUDE_TRACE
-#include "trace/traceEvent.hpp"
-#include "trace/traceStream.hpp"
-#include "utilities/ostream.hpp"
+#include "jfr/recorder/service/jfrEvent.hpp"
+/*
+ * Each event class has an assert member function verify() which is invoked
+ * just before the engine writes the event and its fields to the data stream.
+ * The purpose of verify() is to ensure that all fields in the event are initialized
+ * and set before attempting to commit.
+ *
+ * We enforce this requirement because events are generally stack allocated and therefore
+ * *not* initialized to default values. This prevents us from inadvertently committing
+ * uninitialized values to the data stream.
+ *
+ * The assert message contains both the index (zero based) as well as the name of the field.
+ */
 
-  <xsl:apply-templates select="trace/events/struct" mode="trace"/>
-  <xsl:apply-templates select="trace/events/event" mode="trace"/>
+<xsl:apply-templates select="trace/events/struct" mode="jfr"/>
+<xsl:apply-templates select="trace/events/event" mode="jfr"/>
 
 #else // !INCLUDE_TRACE
 
 class TraceEvent {
-public:
+ public:
   TraceEvent() {}
-  void set_starttime(const Ticks&amp; ignore) {}
-  void set_endtime(const Ticks&amp; ignore) {}
+  void set_starttime(const JfrTraceTime&amp; ignore) const {}
+  void set_endtime(const JfrTraceTime&amp; ignore) const {}
+  void set_starttime(const Ticks&amp; ignore) const {}
+  void set_endtime(const Ticks&amp; ignore) const {}
   bool should_commit() const { return false; }
   static bool is_enabled() { return false; }
   void commit() {}
 };
 
   <xsl:apply-templates select="trace/events/struct" mode="empty"/>
   <xsl:apply-templates select="trace/events/event" mode="empty"/>
 
 #endif // INCLUDE_TRACE
-#endif // TRACEFILES_TRACEEVENTCLASSES_HPP
+#endif // JFRFILES_JFREVENTCLASSES_HPP
 </xsl:template>
 
-<xsl:template match="struct" mode="trace">
+<xsl:template match="struct" mode="jfr">
 struct TraceStruct<xsl:value-of select="@id"/>
 {
-private:
+ private:
 <xsl:apply-templates select="value" mode="write-fields"/>
-public:
+
+ public:
 <xsl:apply-templates select="value" mode="write-setters"/>
 
-  void writeStruct(TraceStream&amp; ts) {
+  template &lt;typename Writer&gt;
+  void writeData(Writer&amp; w) {
 <xsl:apply-templates select="value" mode="write-data"/>
   }
 };
 
 </xsl:template>
 
 <xsl:template match="struct" mode="empty">
 struct TraceStruct<xsl:value-of select="@id"/> 
 {
-public:
+ public:
 <xsl:apply-templates select="value" mode="write-empty-setters"/>
 };
-</xsl:template>
 
+</xsl:template>
 
 <xsl:template match="event" mode="empty">
   <xsl:value-of select="concat('class Event', @id, ' : public TraceEvent')"/>
 {
  public:
-<xsl:value-of select="concat('  Event', @id, '(bool ignore=true) {}')"/>
-<xsl:text>
+<xsl:value-of select="concat('  Event', @id, '(EventStartTime ignore=TIMED) {}')"/>
+  <xsl:text>
 </xsl:text>
 
-<xsl:apply-templates select="value|structvalue|transition_value|relation" mode="write-empty-setters"/>
+<xsl:apply-templates select="value|structvalue|transition_value|relation" mode="write-empty-setters">
+  <xsl:with-param name="cls" select="concat('Event',@id)"/>
+</xsl:apply-templates>
 };
 
 </xsl:template>
 
-
-<xsl:template match="event" mode="trace">
-  <xsl:value-of select="concat('class Event', @id, ' : public TraceEvent&lt;Event', @id, '&gt;')"/>
+<xsl:template match="event" mode="jfr">
+  <xsl:value-of select="concat('class Event', @id, ' : public JfrTraceEvent&lt;Event', @id, '&gt;')"/>
 {
+ private:
+<xsl:apply-templates select="value|structvalue|transition_value|relation" mode="write-fields"/>
+
  public:
   static const bool hasThread = <xsl:value-of select="@has_thread"/>;
   static const bool hasStackTrace = <xsl:value-of select="@has_stacktrace"/>;
   static const bool isInstant = <xsl:value-of select="@is_instant"/>;
+  static const bool hasCutoff = <xsl:value-of select="@cutoff"/>;
   static const bool isRequestable = <xsl:value-of select="@is_requestable"/>;
   static const TraceEventId eventId = <xsl:value-of select="concat('Trace', @id, 'Event')"/>;
 
- private:
-<xsl:apply-templates select="value|structvalue|transition_value|relation" mode="write-fields"/>
+<xsl:value-of select="concat('  Event', @id, '(EventStartTime timing=TIMED) : JfrTraceEvent&lt;Event', @id, '&gt;(timing) {}

')"/>
 
-  void writeEventContent(void) {
-    TraceStream ts;
-    ts.print("<xsl:value-of select="@label"/>: [");
-<xsl:apply-templates select="value|structvalue" mode="write-data"/>
-    ts.print("]\n");
-  }
+<xsl:apply-templates select="value|structvalue|transition_value|relation" mode="write-setters-with-verification"/>
 
- public:
-<xsl:apply-templates select="value|structvalue|transition_value|relation" mode="write-setters"/>
-
-  bool should_write(void) {
-    return true;
-  }
-<xsl:text>
-
-</xsl:text>
-  <xsl:value-of select="concat('  Event', @id, '(EventStartTime timing=TIMED) : TraceEvent&lt;Event', @id, '&gt;(timing) {}', $newline)"/>
-  void writeEvent(void) {
-    if (UseLockedTracing) {
-      ttyLocker lock;
-      writeEventContent();
-    } else {
-      writeEventContent();
-    }
+  template &lt;typename Writer&gt;
+  void writeData(Writer&amp; w) {
+<xsl:apply-templates select="value|structvalue|transition_value|relation" mode="write-data"/>
   }
 
-  using <xsl:value-of select="concat('TraceEvent&lt;Event', @id, '&gt;')"/>::commit; // else commit() is hidden by overloaded versions in this class
+  using TraceEvent::commit; // else commit() is hidden by overloaded versions in this class
 
 <xsl:variable name="instant" select="@is_instant"/>
+
 <!-- non static method (only for non instant events)-->
 <xsl:if test="$instant='false'">
   <xsl:value-of select="concat('  Event', @id)"/>(
     <xsl:for-each select="value|structvalue|transition_value|relation">
     <xsl:apply-templates select="." mode="cpp-type"/><xsl:value-of select="concat(' ', @field)"/>
     <xsl:if test="position() != last()">,
-    </xsl:if></xsl:for-each>) : TraceEvent&lt;<xsl:value-of select="concat('Event', @id)"/>&gt;(TIMED) {
+    </xsl:if></xsl:for-each>) : JfrTraceEvent&lt;<xsl:value-of select="concat('Event', @id)"/>&gt;(TIMED) {
     if (should_commit()) {<xsl:for-each select="value|structvalue|transition_value|relation">
       set_<xsl:value-of select="@field"/>(<xsl:value-of select="@field"/>);</xsl:for-each>
     }
   }
 

@@ -183,113 +183,149 @@
       </xsl:if>
       <xsl:for-each select="value|structvalue|transition_value|relation">me.set_<xsl:value-of select="@field"/>(<xsl:value-of select="@field"/>);
       </xsl:for-each>me.commit();
     }
   }
-};
 
+#ifdef ASSERT
+  void verify() const {
+<xsl:apply-templates select="value" mode="write-verify"/>
+  }
+#endif
+};
+<xsl:text>
+</xsl:text>
 </xsl:template>
 
 <xsl:template match="value|transition_value|relation" mode="write-empty-setters">
   <xsl:param name="cls"/>
   <xsl:variable name="type" select="@type"/>
   <xsl:variable name="wt" select="//primary_type[@symbol=$type]/@type"/>
-  <xsl:value-of select="concat('  void set_', @field, '(', $wt, ' value) { }')"/>
+  <xsl:value-of select="concat('  void set_', @field, '(', $wt, ' ignore) { }')"/>
   <xsl:if test="position() != last()">
     <xsl:text>
 </xsl:text>
   </xsl:if>
 </xsl:template>
 
 <xsl:template match="structvalue" mode="write-empty-setters">
   <xsl:param name="cls"/>
-  <xsl:value-of select="concat('  void set_', @field, '(const TraceStruct', @type, '&amp; value) { }')"/>
+  <xsl:value-of select="concat('  void set_', @field, '(const TraceStruct', @type, '&amp; ignore) { }')"/>
   <xsl:if test="position() != last()">
     <xsl:text>
 </xsl:text>
   </xsl:if>
 </xsl:template>
 
 <xsl:template match="value[@type='TICKS']" mode="write-setters">
-#if INCLUDE_TRACE
-<xsl:value-of select="concat('  void set_', @field, '(const Ticks&amp; time) { _', @field, ' = time; }')"/>
-#else
-<xsl:value-of select="concat('  void set_', @field, '(const Ticks&amp; ignore) {}')"/>
-#endif
+  <xsl:value-of select="concat('  void set_', @field, '(const Ticks&amp; time) {
    this->_', @field, ' = time; }')"/>
+  <xsl:text>
</xsl:text>
+</xsl:template>
+
+<xsl:template match="value[@type='TICKS']" mode="write-setters-with-verification">
+  <xsl:value-of select="concat('  void set_', @field, '(const Ticks&amp; time) {
    this->_', @field, ' = time;
')"/>
+  <xsl:value-of select="concat('    DEBUG_ONLY(set_field_bit(', position() - 1, '));
  }')"/>
+  <xsl:text>
</xsl:text>
 </xsl:template>
 
 <xsl:template match="value[@type='TICKSPAN']" mode="write-setters">
-#if INCLUDE_TRACE
-  <xsl:value-of select="concat('  void set_', @field, '(const Tickspan&amp; time) { _', @field, ' = time; }')"/>
-#else
-  <xsl:value-of select="concat('  void set_', @field, '(const Tickspan&amp; ignore) {}')"/>
-#endif
+  <xsl:value-of select="concat('  void set_', @field, '(const Tickspan&amp; time) {
    this->_', @field, ' = time; }')"/>
+  <xsl:text>
</xsl:text>
 </xsl:template>
 
+<xsl:template match="value[@type='TICKSPAN']" mode="write-setters-with-verification">
+  <xsl:value-of select="concat('  void set_', @field, '(const Tickspan&amp; time) {
    this->_', @field, ' = time;
')"/>
+  <xsl:value-of select="concat('    DEBUG_ONLY(set_field_bit(', position() - 1, '));
  }')"/>
+  <xsl:text>
</xsl:text>
+</xsl:template>
 
-<xsl:template match="value" mode="write-fields">
+<xsl:template match="value|transition_value|relation" mode="write-setters">
+  <xsl:param name="cls"/>
   <xsl:variable name="type" select="@type"/>
   <xsl:variable name="wt" select="//primary_type[@symbol=$type]/@type"/>
-  <xsl:value-of select="concat('  ', $wt, ' _', @field, ';')"/>
+  <xsl:value-of select="concat('  void set_', @field, '(', $wt, ' new_value) { this->_', @field, ' = new_value; }')"/>
   <xsl:if test="position() != last()">
     <xsl:text> 
 </xsl:text>
   </xsl:if>
 </xsl:template>
 
-<xsl:template match="structvalue" mode="write-fields">
-  <xsl:value-of select="concat('  TraceStruct', @type, ' _', @field, ';')"/>
+<xsl:template match="value|transition|relation" mode="write-setters-with-verification">
+  <xsl:param name="cls"/>
+  <xsl:variable name="type" select="@type"/>
+  <xsl:variable name="wt" select="//primary_type[@symbol=$type]/@type"/>
+  <xsl:value-of select="concat('  void set_', @field, '(', $wt, ' new_value) {
    this->_', @field, ' = new_value;
')"/>
+  <xsl:value-of select="concat('    DEBUG_ONLY(set_field_bit(', position() - 1, '));
  }')"/>
+  <xsl:if test="position() != last()">
   <xsl:text>
 </xsl:text>
+  </xsl:if>
 </xsl:template>
 
-<xsl:template match="value|transition_value|relation" mode="write-setters">
+<xsl:template match="structvalue" mode="write-setters">
+  <xsl:param name="cls"/>
+  <xsl:value-of select="concat('  void set_', @field, '(const TraceStruct', @type, '&amp; value) { this->_', @field, ' = value; }')"/>
+  <xsl:if test="position() != last()">
+    <xsl:text>
+</xsl:text>
+  </xsl:if>
+</xsl:template>
+
+<xsl:template match="structvalue" mode="write-setters-with-verification">
   <xsl:param name="cls"/>
+  <xsl:value-of select="concat('  void set_', @field, '(const TraceStruct', @type, '&amp; value) {
    this->_', @field, ' = value;
')"/>
+  <xsl:value-of select="concat('    DEBUG_ONLY(set_field_bit(', position() - 1, '));
')"/>
+  <xsl:text>  }</xsl:text>
+  <xsl:if test="position() != last()">
+    <xsl:text>
+</xsl:text>
+  </xsl:if>
+</xsl:template>
+
+<xsl:template match="value|transition_value|relation" mode="write-fields">
   <xsl:variable name="type" select="@type"/>
   <xsl:variable name="wt" select="//primary_type[@symbol=$type]/@type"/>
-  <xsl:value-of select="concat('  void set_', @field, '(', $wt, ' value) { this->_', @field, ' = value; }')"/>
+  <xsl:value-of select="concat('  ', $wt, ' _', @field, ';')"/>
   <xsl:if test="position() != last()">
     <xsl:text>
 </xsl:text>
   </xsl:if>
 </xsl:template>
 
-<xsl:template match="structvalue" mode="write-setters">
+<xsl:template match="structvalue" mode="write-fields">
+  <xsl:value-of select="concat('  TraceStruct', @type, ' _', @field, ';')"/>
+  <xsl:if test="position() != last()">
+    <xsl:text>
+</xsl:text>
+  </xsl:if>
+</xsl:template>
+
+<xsl:template match="value" mode="write-verify">
   <xsl:param name="cls"/>
-  <xsl:value-of select="concat('  void set_', @field, '(const TraceStruct', @type, '&amp; value) { this->_', @field, ' = value; }')"/>
+  <xsl:text>    </xsl:text>
+  <xsl:value-of select="concat('assert(verify_field_bit(', position() - 1, '), ' , $quote, 'Attempting to write an uninitialized event field: %s', $quote,', ', $quote, '_', @field, $quote, ');')"/>
   <xsl:if test="position() != last()">
     <xsl:text>
 </xsl:text>
   </xsl:if>
 </xsl:template>
 
-<xsl:template match="value" mode="write-data">
+<xsl:template match="value|transition_value|relation" mode="write-data">
   <xsl:variable name="type" select="@type"/>
-  <xsl:variable name="wt" select="//primary_type[@symbol=$type]/@writetype"/>
-  <xsl:choose>
-    <xsl:when test="@type='TICKSPAN'">
-      <xsl:value-of select="concat('    ts.print_val(&quot;', @label, '&quot;, _', @field, '.value());')"/>
-    </xsl:when>
-    <xsl:when test="@type='TICKS'">
-      <xsl:value-of select="concat('    ts.print_val(&quot;', @label, '&quot;, _', @field, '.value());')"/>
-    </xsl:when>
-    <xsl:otherwise>
-      <xsl:value-of select="concat('    ts.print_val(&quot;', @label, '&quot;, _', @field, ');')"/>
-    </xsl:otherwise>
-  </xsl:choose>
+  <xsl:value-of select="concat('    w.write(_', @field, ');')"/>
   <xsl:if test="position() != last()">
     <xsl:text>
-    ts.print(", ");
 </xsl:text>
   </xsl:if>
 </xsl:template>
 
 <xsl:template match="structvalue" mode="write-data">
-  <xsl:value-of select="concat('    _', @field, '.writeStruct(ts);')"/>
+  <xsl:variable name="structtype" select="@type"/>
+  <xsl:variable name="structname" select="@field"/>
+  <xsl:value-of select="concat('    _', @field, '.writeData(w);')"/>
   <xsl:if test="position() != last()">
     <xsl:text>
-    ts.print(", ");
 </xsl:text>
   </xsl:if>
 </xsl:template>
 
 
< prev index next >