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