1 /*
  2  * Copyright (c) 2012, 2021, 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 #include "precompiled.hpp"
 26 #include "jfr/jfrEvents.hpp"
 27 #include "jfr/jni/jfrJavaSupport.hpp"
 28 #include "jfr/leakprofiler/checkpoint/objectSampleCheckpoint.hpp"
 29 #include "jfr/periodic/jfrThreadCPULoadEvent.hpp"
 30 #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
 31 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceIdEpoch.hpp"
 32 #include "jfr/recorder/jfrRecorder.hpp"
 33 #include "jfr/recorder/service/jfrOptionSet.hpp"
 34 #include "jfr/recorder/storage/jfrStorage.hpp"
 35 #include "jfr/support/jfrThreadLocal.hpp"
 36 #include "memory/allocation.inline.hpp"
 37 #include "runtime/os.hpp"
 38 #include "runtime/thread.inline.hpp"
 39 #include "utilities/sizes.hpp"
 40 
 41 JfrThreadLocal::JfrThreadLocal() :
 42   _java_event_writer(NULL),
 43   _java_buffer(NULL),
 44   _native_buffer(NULL),
 45   _shelved_buffer(NULL),
 46   _load_barrier_buffer_epoch_0(NULL),
 47   _load_barrier_buffer_epoch_1(NULL),
 48   _stackframes(NULL),
 49   _trace_id(JfrTraceId::assign_thread_id()),
 50   _thread(),
 51   _data_lost(0),
 52   _stack_trace_id(max_julong),
 53   _user_time(0),
 54   _cpu_time(0),
 55   _wallclock_time(os::javaTimeNanos()),
 56   _stack_trace_hash(0),
 57   _stackdepth(0),
 58   _entering_suspend_flag(0),
 59   _excluded(false),
 60   _dead(false) {
 61   Thread* thread = Thread::current_or_null();
 62   _parent_trace_id = thread != NULL ? thread->jfr_thread_local()->trace_id() : (traceid)0;
 63 }
 64 
 65 u8 JfrThreadLocal::add_data_lost(u8 value) {
 66   _data_lost += value;
 67   return _data_lost;
 68 }
 69 
 70 bool JfrThreadLocal::has_thread_blob() const {
 71   return _thread.valid();
 72 }
 73 
 74 void JfrThreadLocal::set_thread_blob(const JfrBlobHandle& ref) {
 75   assert(!_thread.valid(), "invariant");
 76   _thread = ref;
 77 }
 78 
 79 const JfrBlobHandle& JfrThreadLocal::thread_blob() const {
 80   return _thread;
 81 }
 82 
 83 static void send_java_thread_start_event(JavaThread* jt) {
 84   EventThreadStart event;
 85   event.set_thread(jt->jfr_thread_local()->thread_id());
 86   event.set_parentThread(jt->jfr_thread_local()->parent_thread_id());
 87   event.commit();
 88 }
 89 
 90 void JfrThreadLocal::on_start(Thread* t) {
 91   assert(t != NULL, "invariant");
 92   assert(Thread::current() == t, "invariant");
 93   JfrJavaSupport::on_thread_start(t);
 94   if (JfrRecorder::is_recording()) {
 95     JfrCheckpointManager::write_thread_checkpoint(t);
 96     if (!t->jfr_thread_local()->is_excluded()) {
 97       if (t->is_Java_thread()) {
 98         send_java_thread_start_event(JavaThread::cast(t));
 99       }
100     }
101   }
102   if (t->jfr_thread_local()->has_cached_stack_trace()) {
103     t->jfr_thread_local()->clear_cached_stack_trace();
104   }
105 }
106 
107 static void send_java_thread_end_events(traceid id, JavaThread* jt) {
108   assert(jt != NULL, "invariant");
109   assert(Thread::current() == jt, "invariant");
110   assert(jt->jfr_thread_local()->trace_id() == id, "invariant");
111   if (JfrRecorder::is_recording()) {
112     EventThreadEnd event;
113     event.set_thread(id);
114     event.commit();
115     JfrThreadCPULoadEvent::send_event_for_thread(jt);
116   }
117 }
118 
119 void JfrThreadLocal::release(Thread* t) {
120   if (has_java_event_writer()) {
121     assert(t->is_Java_thread(), "invariant");
122     JfrJavaSupport::destroy_global_jni_handle(java_event_writer());
123     _java_event_writer = NULL;
124   }
125   if (has_native_buffer()) {
126     JfrStorage::release_thread_local(native_buffer(), t);
127     _native_buffer = NULL;
128   }
129   if (has_java_buffer()) {
130     JfrStorage::release_thread_local(java_buffer(), t);
131     _java_buffer = NULL;
132   }
133   if (_stackframes != NULL) {
134     FREE_C_HEAP_ARRAY(JfrStackFrame, _stackframes);
135     _stackframes = NULL;
136   }
137   if (_load_barrier_buffer_epoch_0 != NULL) {
138     _load_barrier_buffer_epoch_0->set_retired();
139     _load_barrier_buffer_epoch_0 = NULL;
140   }
141   if (_load_barrier_buffer_epoch_1 != NULL) {
142     _load_barrier_buffer_epoch_1->set_retired();
143     _load_barrier_buffer_epoch_1 = NULL;
144   }
145 }
146 
147 void JfrThreadLocal::release(JfrThreadLocal* tl, Thread* t) {
148   assert(tl != NULL, "invariant");
149   assert(t != NULL, "invariant");
150   assert(Thread::current() == t, "invariant");
151   assert(!tl->is_dead(), "invariant");
152   assert(tl->shelved_buffer() == NULL, "invariant");
153   tl->_dead = true;
154   tl->release(t);
155 }
156 
157 void JfrThreadLocal::on_exit(Thread* t) {
158   assert(t != NULL, "invariant");
159   JfrThreadLocal * const tl = t->jfr_thread_local();
160   assert(!tl->is_dead(), "invariant");
161   if (JfrRecorder::is_recording()) {
162     if (t->is_Java_thread()) {
163       JavaThread* const jt = JavaThread::cast(t);
164       ObjectSampleCheckpoint::on_thread_exit(jt);
165       send_java_thread_end_events(tl->thread_id(), jt);
166     }
167   }
168   release(tl, Thread::current()); // because it could be that Thread::current() != t
169 }
170 
171 static JfrBuffer* acquire_buffer(bool excluded) {
172   JfrBuffer* const buffer = JfrStorage::acquire_thread_local(Thread::current());
173   if (buffer != NULL && excluded) {
174     buffer->set_excluded();
175   }
176   return buffer;
177 }
178 
179 JfrBuffer* JfrThreadLocal::install_native_buffer() const {
180   assert(!has_native_buffer(), "invariant");
181   _native_buffer = acquire_buffer(_excluded);
182   return _native_buffer;
183 }
184 
185 JfrBuffer* JfrThreadLocal::install_java_buffer() const {
186   assert(!has_java_buffer(), "invariant");
187   assert(!has_java_event_writer(), "invariant");
188   _java_buffer = acquire_buffer(_excluded);
189   return _java_buffer;
190 }
191 
192 JfrStackFrame* JfrThreadLocal::install_stackframes() const {
193   assert(_stackframes == NULL, "invariant");
194   _stackframes = NEW_C_HEAP_ARRAY(JfrStackFrame, stackdepth(), mtTracing);
195   return _stackframes;
196 }
197 
198 ByteSize JfrThreadLocal::trace_id_offset() {
199   return in_ByteSize(offset_of(JfrThreadLocal, _trace_id));
200 }
201 
202 ByteSize JfrThreadLocal::java_event_writer_offset() {
203   return in_ByteSize(offset_of(JfrThreadLocal, _java_event_writer));
204 }
205 
206 void JfrThreadLocal::exclude(Thread* t) {
207   assert(t != NULL, "invariant");
208   t->jfr_thread_local()->_excluded = true;
209   t->jfr_thread_local()->release(t);
210 }
211 
212 void JfrThreadLocal::include(Thread* t) {
213   assert(t != NULL, "invariant");
214   t->jfr_thread_local()->_excluded = false;
215   t->jfr_thread_local()->release(t);
216 }
217 
218 u4 JfrThreadLocal::stackdepth() const {
219   return _stackdepth != 0 ? _stackdepth : (u4)JfrOptionSet::stackdepth();
220 }