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/jfrJavaThread.hpp"
 36 #include "jfr/support/jfrThreadLocal.hpp"
 37 #include "jfr/utilities/jfrSpinlockHelper.hpp"
 38 #include "memory/allocation.inline.hpp"
 39 #include "runtime/os.hpp"
 40 #include "runtime/thread.inline.hpp"
 41 #include "utilities/sizes.hpp"
 42 
 43 JfrThreadLocal::JfrThreadLocal() :
 44   _java_event_writer(NULL),
 45   _java_buffer(NULL),
 46   _native_buffer(NULL),
 47   _shelved_buffer(NULL),
 48   _load_barrier_buffer_epoch_0(NULL),
 49   _load_barrier_buffer_epoch_1(NULL),
 50   _checkpoint_buffer_epoch_0(NULL),
 51   _checkpoint_buffer_epoch_1(NULL),
 52   _stackframes(NULL),
 53   _thread(),
 54   _thread_id(0),
 55   _thread_id_alias(max_julong),
 56   _data_lost(0),
 57   _stack_trace_id(max_julong),
 58   _user_time(0),
 59   _cpu_time(0),
 60   _wallclock_time(os::javaTimeNanos()),
 61   _stack_trace_hash(0),
 62   _stackdepth(0),
 63   _entering_suspend_flag(0),
 64   _critical_section(0),
 65   _excluded(false),
 66   _dead(false) {
 67   Thread* thread = Thread::current_or_null();
 68   _parent_trace_id = thread != NULL ? thread->jfr_thread_local()->trace_id() : (traceid)0;
 69 }
 70 
 71 u8 JfrThreadLocal::add_data_lost(u8 value) {
 72   _data_lost += value;
 73   return _data_lost;
 74 }
 75 
 76 bool JfrThreadLocal::has_thread_blob() const {
 77   return _thread.valid();
 78 }
 79 
 80 void JfrThreadLocal::set_thread_blob(const JfrBlobHandle& ref) {
 81   assert(!_thread.valid(), "invariant");
 82   _thread = ref;
 83 }
 84 
 85 const JfrBlobHandle& JfrThreadLocal::thread_blob() const {
 86   return _thread;
 87 }
 88 
 89 static void send_java_thread_start_event(JavaThread* jt, jobject vthread) {
 90   assert(jt != NULL, "invariant");
 91   assert(Thread::current() == jt, "invariant");
 92   if (!JfrJavaSupport::on_thread_start(jt, vthread)) {
 93     // thread is excluded
 94     return;
 95   }
 96   if (JfrRecorder::is_recording()) {
 97     EventThreadStart event;
 98     event.set_thread(vthread != NULL ? JfrThreadLocal::virtual_thread_id(jt, JfrJavaSupport::resolve_non_null(vthread)) : JfrThreadLocal::vm_thread_id(jt));
 99     event.set_parentThread(jt->jfr_thread_local()->parent_thread_id());
100     event.commit();
101   }
102 }
103 
104 void JfrThreadLocal::on_start(Thread* t) {
105   if (JfrRecorder::is_recording()) {
106     JfrCheckpointManager::write_checkpoint(t);
107     if (t->is_Java_thread()) {
108       send_java_thread_start_event(JavaThread::cast(t), NULL);
109     }
110   }
111   if (t->jfr_thread_local()->has_cached_stack_trace()) {
112     t->jfr_thread_local()->clear_cached_stack_trace();
113   }
114 }
115 
116 void JfrThreadLocal::on_vthread_start(JavaThread* jt, jobject vthread) {
117   send_java_thread_start_event(jt, vthread);
118 }
119 
120 void JfrThreadLocal::release(Thread* t) {
121   if (has_java_event_writer()) {
122     assert(t->is_Java_thread(), "invariant");
123     JfrJavaSupport::destroy_global_jni_handle(java_event_writer());
124     _java_event_writer = NULL;
125   }
126   if (has_native_buffer()) {
127     JfrStorage::release_thread_local(native_buffer(), t);
128     _native_buffer = NULL;
129   }
130   if (has_java_buffer()) {
131     JfrStorage::release_thread_local(java_buffer(), t);
132     _java_buffer = NULL;
133   }
134   if (_stackframes != NULL) {
135     FREE_C_HEAP_ARRAY(JfrStackFrame, _stackframes);
136     _stackframes = NULL;
137   }
138   if (_load_barrier_buffer_epoch_0 != NULL) {
139     _load_barrier_buffer_epoch_0->set_retired();
140     _load_barrier_buffer_epoch_0 = NULL;
141   }
142   if (_load_barrier_buffer_epoch_1 != NULL) {
143     _load_barrier_buffer_epoch_1->set_retired();
144     _load_barrier_buffer_epoch_1 = NULL;
145   }
146   if (_checkpoint_buffer_epoch_0 != NULL) {
147     _checkpoint_buffer_epoch_0->set_retired();
148     _checkpoint_buffer_epoch_0 = NULL;
149   }
150   if (_checkpoint_buffer_epoch_1 != NULL) {
151     _checkpoint_buffer_epoch_1->set_retired();
152     _checkpoint_buffer_epoch_1 = NULL;
153   }
154 }
155 
156 void JfrThreadLocal::release(JfrThreadLocal* tl, Thread* t) {
157   assert(tl != NULL, "invariant");
158   assert(t != NULL, "invariant");
159   assert(Thread::current() == t, "invariant");
160   assert(!tl->is_dead(), "invariant");
161   assert(tl->shelved_buffer() == NULL, "invariant");
162   tl->_dead = true;
163   tl->release(t);
164 }
165 
166 static void send_java_thread_end_event(JavaThread* jt, traceid tid) {
167   assert(jt != NULL, "invariant");
168   assert(Thread::current() == jt, "invariant");
169   assert(tid != 0, "invariant");
170   if (JfrRecorder::is_recording()) {
171     EventThreadEnd event;
172     event.set_thread(tid);
173     event.commit();
174     ObjectSampleCheckpoint::on_thread_exit(tid);
175   }
176 }
177 
178 void JfrThreadLocal::on_exit(Thread* t) {
179   assert(t != NULL, "invariant");
180   JfrThreadLocal * const tl = t->jfr_thread_local();
181   assert(!tl->is_dead(), "invariant");
182   if (t->is_Java_thread()) {
183     JavaThread* const jt = JavaThread::cast(t);
184     send_java_thread_end_event(jt, JfrThreadLocal::thread_id(jt));
185     JfrThreadCPULoadEvent::send_event_for_thread(jt);
186   }
187   release(tl, Thread::current()); // because it could be that Thread::current() != t
188 }
189 
190 void JfrThreadLocal::on_vthread_exit(JavaThread* jt, jobject vthread) {
191   assert(vthread != NULL, "invariant");
192   const traceid id = JfrJavaThread::virtual_thread_id(JfrJavaSupport::resolve_non_null(vthread), jt);
193   JfrThreadLocal::impersonate(jt, id);
194   send_java_thread_end_event(jt, id);
195   JfrThreadLocal::stop_impersonating(jt);
196 }
197 
198 static JfrBuffer* acquire_buffer(bool excluded) {
199   JfrBuffer* const buffer = JfrStorage::acquire_thread_local(Thread::current());
200   if (buffer != NULL && excluded) {
201     buffer->set_excluded();
202   }
203   return buffer;
204 }
205 
206 JfrBuffer* JfrThreadLocal::install_native_buffer() const {
207   assert(!has_native_buffer(), "invariant");
208   _native_buffer = acquire_buffer(_excluded);
209   return _native_buffer;
210 }
211 
212 JfrBuffer* JfrThreadLocal::install_java_buffer() const {
213   assert(!has_java_buffer(), "invariant");
214   assert(!has_java_event_writer(), "invariant");
215   _java_buffer = acquire_buffer(_excluded);
216   return _java_buffer;
217 }
218 
219 JfrStackFrame* JfrThreadLocal::install_stackframes() const {
220   assert(_stackframes == NULL, "invariant");
221   _stackframes = NEW_C_HEAP_ARRAY(JfrStackFrame, stackdepth(), mtTracing);
222   return _stackframes;
223 }
224 
225 ByteSize JfrThreadLocal::trace_id_offset() {
226   return in_ByteSize(offset_of(JfrThreadLocal, _thread_id));
227 }
228 
229 ByteSize JfrThreadLocal::java_event_writer_offset() {
230   return in_ByteSize(offset_of(JfrThreadLocal, _java_event_writer));
231 }
232 
233 void JfrThreadLocal::exclude(Thread* t) {
234   assert(t != NULL, "invariant");
235   t->jfr_thread_local()->_excluded = true;
236   t->jfr_thread_local()->release(t);
237 }
238 
239 void JfrThreadLocal::include(Thread* t) {
240   assert(t != NULL, "invariant");
241   t->jfr_thread_local()->_excluded = false;
242   t->jfr_thread_local()->release(t);
243 }
244 
245 u4 JfrThreadLocal::stackdepth() const {
246   return _stackdepth != 0 ? _stackdepth : (u4)JfrOptionSet::stackdepth();
247 }
248 
249 bool JfrThreadLocal::is_impersonating(const Thread* t) {
250   return t->jfr_thread_local()->_thread_id_alias != max_julong;
251 }
252 
253 void JfrThreadLocal::impersonate(const Thread* t, traceid other_thread_id) {
254   assert(t != NULL, "invariant");
255   assert(other_thread_id != 0, "invariant");
256   JfrThreadLocal* const tl = t->jfr_thread_local();
257   tl->_thread_id_alias = other_thread_id;
258 }
259 
260 void JfrThreadLocal::stop_impersonating(const Thread* t) {
261   assert(t != NULL, "invariant");
262   JfrThreadLocal* const tl = t->jfr_thread_local();
263   if (is_impersonating(t)) {
264     tl->_thread_id_alias = max_julong;
265   }
266   assert(!is_impersonating(t), "invariant");
267 }
268 
269 traceid JfrThreadLocal::thread_id(const Thread* t, bool* is_virtual) {
270   assert(t != NULL, "invariant");
271   if (is_impersonating(t)) {
272     return t->jfr_thread_local()->_thread_id_alias;
273   }
274   return t->is_Java_thread() ? JfrJavaThread::contextual_thread_id(JavaThread::cast(t), is_virtual) : vm_thread_id(t);
275 }
276 
277 traceid JfrThreadLocal::virtual_thread_id(const Thread* t, oop vthread) {
278   assert(t != NULL, "invariant");
279   assert(vthread != NULL, "invariant");
280   return JfrJavaThread::virtual_thread_id(vthread, (JavaThread*)t);
281 }
282 
283 traceid JfrThreadLocal::assign_thread_id(const Thread* t) {
284   assert(t != NULL, "invariant");
285   JfrThreadLocal* const tl = t->jfr_thread_local();
286   JfrSpinlockHelper spinlock(&tl->_critical_section);
287   traceid tid = tl->_thread_id;
288   if (tid == 0) {
289     tid = t->is_Java_thread() ? JfrJavaThread::java_thread_id((JavaThread*)t) :
290                                 JfrTraceId::assign_thread_id();
291     tl->_thread_id = tid;
292   }
293   assert(tid != 0, "invariant");
294   return tid;
295 }
296 
297 traceid JfrThreadLocal::vm_thread_id(const Thread* t) {
298   assert(t != NULL, "invariant");
299   JfrThreadLocal* const tl = t->jfr_thread_local();
300   return tl->_thread_id != 0 ? tl->_thread_id : JfrThreadLocal::assign_thread_id(t);
301 }