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 #include "precompiled.hpp"
  26 #include "jfr/jfrEvents.hpp"
  27 #include "jfr/jni/jfrJavaSupport.hpp"
  28 #include "jfr/periodic/jfrThreadCPULoadEvent.hpp"
  29 #include "jfr/recorder/jfrRecorder.hpp"
  30 #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
  31 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp"
  32 #include "jfr/recorder/service/jfrOptionSet.hpp"
  33 #include "jfr/recorder/storage/jfrStorage.hpp"
  34 #include "jfr/recorder/stacktrace/jfrStackTraceRepository.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 /* This data structure is per thread and only accessed by the thread itself, no locking required */
  42 JfrThreadLocal::JfrThreadLocal() :
  43   _java_event_writer(NULL),
  44   _java_buffer(NULL),
  45   _native_buffer(NULL),
  46   _shelved_buffer(NULL),
  47   _stackframes(NULL),
  48   _trace_id(JfrTraceId::assign_thread_id()),
  49   _thread_cp(),
  50   _data_lost(0),
  51   _stack_trace_id(max_julong),
  52   _user_time(0),
  53   _cpu_time(0),
  54   _wallclock_time(os::javaTimeNanos()),
  55   _stack_trace_hash(0),
  56   _stackdepth(0),
  57   _entering_suspend_flag(0),
  58   _dead(false) {
  59 
  60   Thread* thread = Thread::current_or_null();
  61   _parent_trace_id = thread != NULL ? thread->jfr_thread_local()->trace_id() : (traceid)0;
  62 }
  63 
  64 u8 JfrThreadLocal::add_data_lost(u8 value) {
  65   _data_lost += value;
  66   return _data_lost;
  67 }
  68 
  69 bool JfrThreadLocal::has_thread_checkpoint() const {
  70   return _thread_cp.valid();
  71 }
  72 
  73 void JfrThreadLocal::set_thread_checkpoint(const JfrCheckpointBlobHandle& ref) {
  74   assert(!_thread_cp.valid(), "invariant");
  75   _thread_cp = ref;
  76 }
  77 
  78 const JfrCheckpointBlobHandle& JfrThreadLocal::thread_checkpoint() const {
  79   return _thread_cp;
  80 }
  81 
  82 static void send_java_thread_start_event(JavaThread* jt) {
  83   EventThreadStart event;
  84   event.set_thread(jt->jfr_thread_local()->thread_id());
  85   event.set_parentThread(jt->jfr_thread_local()->parent_thread_id());
  86   event.commit();
  87 }
  88 
  89 void JfrThreadLocal::on_start(Thread* t) {
  90   assert(t != NULL, "invariant");
  91   assert(Thread::current() == t, "invariant");
  92   if (JfrRecorder::is_recording()) {
  93     if (t->is_Java_thread()) {
  94       send_java_thread_start_event((JavaThread*)t);
  95     }
  96   }
  97   if (t->jfr_thread_local()->has_cached_stack_trace()) {
  98     t->jfr_thread_local()->clear_cached_stack_trace();
  99   }
 100 }
 101 
 102 static void send_java_thread_end_events(traceid id, JavaThread* jt) {
 103   assert(jt != NULL, "invariant");
 104   assert(Thread::current() == jt, "invariant");
 105   assert(jt->jfr_thread_local()->trace_id() == id, "invariant");
 106   EventThreadEnd event;
 107   event.set_thread(id);
 108   event.commit();
 109   JfrThreadCPULoadEvent::send_event_for_thread(jt);
 110 }
 111 
 112 void JfrThreadLocal::release(JfrThreadLocal* tl, Thread* t) {
 113   assert(tl != NULL, "invariant");
 114   assert(t != NULL, "invariant");
 115   assert(Thread::current() == t, "invariant");
 116   assert(!tl->is_dead(), "invariant");
 117   assert(tl->shelved_buffer() == NULL, "invariant");
 118   if (tl->has_native_buffer()) {
 119     JfrStorage::release_thread_local(tl->native_buffer(), t);
 120   }
 121   if (tl->has_java_buffer()) {
 122     JfrStorage::release_thread_local(tl->java_buffer(), t);
 123   }
 124   if (tl->has_java_event_writer()) {
 125     assert(t->is_Java_thread(), "invariant");
 126     JfrJavaSupport::destroy_global_jni_handle(tl->java_event_writer());
 127   }
 128   if (tl->_stackframes != NULL) {
 129     FREE_C_HEAP_ARRAY(JfrStackFrame, tl->_stackframes, mtTracing);
 130   }
 131   tl->_dead = true;
 132 }
 133 
 134 void JfrThreadLocal::on_exit(Thread* t) {
 135   assert(t != NULL, "invariant");
 136   JfrThreadLocal * const tl = t->jfr_thread_local();
 137   assert(!tl->is_dead(), "invariant");
 138   if (JfrRecorder::is_recording()) {
 139     if (t->is_Java_thread()) {
 140       send_java_thread_end_events(tl->thread_id(), (JavaThread*)t);
 141     }
 142   }
 143   release(tl, Thread::current()); // because it could be that Thread::current() != t
 144 }
 145 
 146 JfrBuffer* JfrThreadLocal::install_native_buffer() const {
 147   assert(!has_native_buffer(), "invariant");
 148   _native_buffer = JfrStorage::acquire_thread_local(Thread::current());
 149   return _native_buffer;
 150 }
 151 
 152 JfrBuffer* JfrThreadLocal::install_java_buffer() const {
 153   assert(!has_java_buffer(), "invariant");
 154   assert(!has_java_event_writer(), "invariant");
 155   _java_buffer = JfrStorage::acquire_thread_local(Thread::current());
 156   return _java_buffer;
 157 }
 158 
 159 JfrStackFrame* JfrThreadLocal::install_stackframes() const {
 160   assert(_stackframes == NULL, "invariant");
 161   _stackframes = NEW_C_HEAP_ARRAY(JfrStackFrame, stackdepth(), mtTracing);
 162   return _stackframes;
 163 }
 164 
 165 ByteSize JfrThreadLocal::trace_id_offset() {
 166   return in_ByteSize(offset_of(JfrThreadLocal, _trace_id));
 167 }
 168 
 169 ByteSize JfrThreadLocal::java_event_writer_offset() {
 170   return in_ByteSize(offset_of(JfrThreadLocal, _java_event_writer));
 171 }
 172 
 173 u4 JfrThreadLocal::stackdepth() const {
 174   return _stackdepth != 0 ? _stackdepth : (u4)JfrOptionSet::stackdepth();
 175 }