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/jni/jfrJavaSupport.hpp"
  27 #include "jfr/periodic/jfrThreadCPULoadEvent.hpp"
  28 #include "jfr/recorder/jfrRecorder.hpp"
  29 #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
  30 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp"
  31 #include "jfr/recorder/service/jfrOptionSet.hpp"
  32 #include "jfr/recorder/storage/jfrStorage.hpp"
  33 #include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"
  34 #include "jfr/support/jfrThreadLocal.hpp"
  35 #include "memory/allocation.inline.hpp"
  36 #include "runtime/os.hpp"
  37 #include "runtime/thread.inline.hpp"
  38 
  39 /* This data structure is per thread and only accessed by the thread itself, no locking required */
  40 JfrThreadLocal::JfrThreadLocal() :
  41   _java_event_writer(NULL),
  42   _java_buffer(NULL),
  43   _native_buffer(NULL),
  44   _shelved_buffer(NULL),
  45   _stackframes(NULL),
  46   _trace_id(JfrTraceId::assign_thread_id()),
  47   _thread_cp(),
  48   _data_lost(0),
  49   _stack_trace_id(max_julong),
  50   _user_time(0),
  51   _cpu_time(0),
  52   _wallclock_time(os::javaTimeNanos()),
  53   _stack_trace_hash(0),
  54   _stackdepth(0),
  55   _entering_suspend_flag(0),
  56   _dead(false) {}
  57 
  58 u8 JfrThreadLocal::add_data_lost(u8 value) {
  59   _data_lost += value;
  60   return _data_lost;
  61 }
  62 
  63 bool JfrThreadLocal::has_thread_checkpoint() const {
  64   return _thread_cp.valid();
  65 }
  66 
  67 void JfrThreadLocal::set_thread_checkpoint(const JfrCheckpointBlobHandle& ref) {
  68   assert(!_thread_cp.valid(), "invariant");
  69   _thread_cp = ref;
  70 }
  71 
  72 const JfrCheckpointBlobHandle& JfrThreadLocal::thread_checkpoint() const {
  73   return _thread_cp;
  74 }
  75 
  76 void JfrThreadLocal::set_dead() {
  77   assert(!is_dead(), "invariant");
  78   _dead = true;
  79 }
  80 
  81 void JfrThreadLocal::on_exit(JavaThread* thread) {
  82   if (JfrRecorder::is_recording()) {
  83     JfrCheckpointManager::write_thread_checkpoint(thread);
  84     JfrThreadCPULoadEvent::send_event_for_thread(thread);
  85   }
  86   thread->jfr_thread_local()->set_dead();
  87 }
  88 
  89 void JfrThreadLocal::on_destruct(Thread* thread) {
  90   JfrThreadLocal* const tl = thread->jfr_thread_local();
  91   if (tl->has_native_buffer()) {
  92     release(tl->native_buffer(), thread);
  93   }
  94   if (tl->has_java_buffer()) {
  95     release(tl->java_buffer(), thread);
  96   }
  97   assert(tl->shelved_buffer() == NULL, "invariant");
  98   if (thread->jfr_thread_local()->has_java_event_writer()) {
  99     JfrJavaSupport::destroy_global_jni_handle(tl->java_event_writer());
 100   }
 101   destroy_stackframes(thread);
 102 }
 103 
 104 JfrBuffer* JfrThreadLocal::acquire(Thread* thread, size_t size) {
 105   return JfrStorage::acquire_thread_local(thread, size);
 106 }
 107 
 108 void JfrThreadLocal::release(JfrBuffer* buffer, Thread* thread) {
 109   assert(buffer != NULL, "invariant");
 110   JfrStorage::release_thread_local(buffer, thread);
 111 }
 112 
 113 JfrBuffer* JfrThreadLocal::install_native_buffer() const {
 114   assert(!has_native_buffer(), "invariant");
 115   _native_buffer = acquire(Thread::current());
 116   return _native_buffer;
 117 }
 118 
 119 JfrBuffer* JfrThreadLocal::install_java_buffer() const {
 120   assert(!has_java_buffer(), "invariant");
 121   assert(!has_java_event_writer(), "invariant");
 122   _java_buffer = acquire(Thread::current());
 123   return _java_buffer;
 124 }
 125 
 126 JfrStackFrame* JfrThreadLocal::install_stackframes() const {
 127   assert(_stackframes == NULL, "invariant");
 128   _stackdepth = (u4)JfrOptionSet::stackdepth();
 129   guarantee(_stackdepth > 0, "Stackdepth must be > 0");
 130   _stackframes = NEW_C_HEAP_ARRAY(JfrStackFrame, _stackdepth, mtTracing);
 131   return _stackframes;
 132 }
 133 
 134 void JfrThreadLocal::destroy_stackframes(Thread* thread) {
 135   assert(thread != NULL, "invariant");
 136   JfrStackFrame* frames = thread->jfr_thread_local()->stackframes();
 137   if (frames != NULL) {
 138     FREE_C_HEAP_ARRAY(JfrStackFrame, frames, mtTracing);
 139     thread->jfr_thread_local()->set_stackframes(NULL);
 140   }
 141 }