1 /*
   2  * Copyright (c) 2012, 2019, 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_JFR_JFRTHREADDATA_HPP
  26 #define SHARE_VM_JFR_JFRTHREADDATA_HPP
  27 
  28 #include "jni.h"
  29 #include "jfr/recorder/checkpoint/jfrCheckpointBlob.hpp"
  30 #include "jfr/utilities/jfrTypes.hpp"
  31 #include "trace/traceMacros.hpp"
  32 
  33 class JavaThread;
  34 class JfrBuffer;
  35 class JfrStackFrame;
  36 
  37 class JfrThreadData {
  38  private:
  39   jobject _java_event_writer;
  40   mutable JfrBuffer* _java_buffer;
  41   mutable JfrBuffer* _native_buffer;
  42   JfrBuffer* _shelved_buffer;
  43   mutable JfrStackFrame* _stackframes;
  44   TRACE_DEFINE_TRACE_ID_FIELD;
  45   JfrCheckpointBlobHandle _thread_cp;
  46   u8 _data_lost;
  47   traceid _stack_trace_id;
  48   jlong _user_time;
  49   jlong _cpu_time;
  50   jlong _wallclock_time;
  51   unsigned int _stack_trace_hash;
  52   mutable u4 _stackdepth;
  53   volatile jint _entering_suspend_flag;
  54 
  55   JfrBuffer* install_native_buffer() const;
  56   JfrBuffer* install_java_buffer() const;
  57   JfrStackFrame* install_stackframes() const;
  58 
  59  public:
  60   JfrThreadData();
  61 
  62   JfrBuffer* native_buffer() const {
  63     return _native_buffer != NULL ? _native_buffer : install_native_buffer();
  64   }
  65 
  66   bool has_native_buffer() const {
  67     return _native_buffer != NULL;
  68   }
  69 
  70   void set_native_buffer(JfrBuffer* buffer) {
  71     _native_buffer = buffer;
  72   }
  73 
  74   JfrBuffer* java_buffer() const {
  75     return _java_buffer != NULL ? _java_buffer : install_java_buffer();
  76   }
  77 
  78   bool has_java_buffer() const {
  79     return _java_buffer != NULL;
  80   }
  81 
  82   void set_java_buffer(JfrBuffer* buffer) {
  83     _java_buffer = buffer;
  84   }
  85 
  86   JfrBuffer* shelved_buffer() const {
  87     return _shelved_buffer;
  88   }
  89 
  90   void shelve_buffer(JfrBuffer* buffer) {
  91     _shelved_buffer = buffer;
  92   }
  93 
  94   bool has_java_event_writer() const {
  95     return _java_event_writer != NULL;
  96   }
  97 
  98   jobject java_event_writer() {
  99     return _java_event_writer;
 100   }
 101 
 102   void set_java_event_writer(jobject java_event_writer) {
 103     _java_event_writer = java_event_writer;
 104   }
 105 
 106   JfrStackFrame* stackframes() const {
 107     return _stackframes != NULL ? _stackframes : install_stackframes();
 108   }
 109 
 110   void set_stackframes(JfrStackFrame* frames) {
 111     _stackframes = frames;
 112   }
 113 
 114   u4 stackdepth() const {
 115     return _stackdepth;
 116   }
 117 
 118   void set_stackdepth(u4 depth) {
 119     _stackdepth = depth;
 120   }
 121 
 122   traceid thread_id() const {
 123     return _trace_id;
 124   }
 125 
 126   void set_thread_id(traceid thread_id) {
 127     _trace_id = thread_id;
 128   }
 129 
 130   void set_cached_stack_trace_id(traceid id, unsigned int hash = 0) {
 131     _stack_trace_id = id;
 132     _stack_trace_hash = hash;
 133   }
 134 
 135   bool has_cached_stack_trace() const {
 136     return _stack_trace_id != max_julong;
 137   }
 138 
 139   void clear_cached_stack_trace() {
 140     _stack_trace_id = max_julong;
 141     _stack_trace_hash = 0;
 142   }
 143 
 144   traceid cached_stack_trace_id() const {
 145     return _stack_trace_id;
 146   }
 147 
 148   unsigned int cached_stack_trace_hash() const {
 149     return _stack_trace_hash;
 150   }
 151 
 152 
 153   void set_trace_block() {
 154     _entering_suspend_flag = 1;
 155   }
 156 
 157   void clear_trace_block() {
 158     _entering_suspend_flag = 0;
 159   }
 160 
 161   bool is_trace_block() const {
 162     return _entering_suspend_flag != 0;
 163   }
 164 
 165   u8 data_lost() const {
 166     return _data_lost;
 167   }
 168 
 169   u8 add_data_lost(u8 value);
 170 
 171   jlong get_user_time() const {
 172     return _user_time;
 173   }
 174 
 175   void set_user_time(jlong user_time) {
 176     _user_time = user_time;
 177   }
 178 
 179   jlong get_cpu_time() const {
 180     return _cpu_time;
 181   }
 182 
 183   void set_cpu_time(jlong cpu_time) {
 184     _cpu_time = cpu_time;
 185   }
 186 
 187   jlong get_wallclock_time() const {
 188     return _wallclock_time;
 189   }
 190 
 191   void set_wallclock_time(jlong wallclock_time) {
 192     _wallclock_time = wallclock_time;
 193   }
 194 
 195   bool has_thread_checkpoint() const;
 196   void set_thread_checkpoint(const JfrCheckpointBlobHandle& handle);
 197   const JfrCheckpointBlobHandle& thread_checkpoint() const;
 198 
 199   static JfrBuffer* acquire(Thread* t, size_t size = 0);
 200   static void release(JfrBuffer* buffer, Thread* t);
 201   static void destroy_stackframes(Thread* t);
 202   static void on_exit(JavaThread* t);
 203   static void on_destruct(Thread* t);
 204 
 205   TRACE_DEFINE_TRACE_ID_METHODS;
 206   // Code generation
 207   TRACE_DEFINE_THREAD_ID_OFFSET;
 208   TRACE_DEFINE_THREAD_ID_SIZE;
 209   TRACE_DEFINE_THREAD_DATA_WRITER_OFFSET;
 210 };
 211 
 212 #endif // SHARE_VM_JFR_JFRTHREADDATA_HPP