Path: blob/master/src/hotspot/share/jfr/support/jfrThreadLocal.cpp
41152 views
/*1* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "jfr/jfrEvents.hpp"26#include "jfr/jni/jfrJavaSupport.hpp"27#include "jfr/leakprofiler/checkpoint/objectSampleCheckpoint.hpp"28#include "jfr/periodic/jfrThreadCPULoadEvent.hpp"29#include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"30#include "jfr/recorder/checkpoint/types/traceid/jfrTraceIdEpoch.hpp"31#include "jfr/recorder/jfrRecorder.hpp"32#include "jfr/recorder/service/jfrOptionSet.hpp"33#include "jfr/recorder/storage/jfrStorage.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#include "utilities/sizes.hpp"3940JfrThreadLocal::JfrThreadLocal() :41_java_event_writer(NULL),42_java_buffer(NULL),43_native_buffer(NULL),44_shelved_buffer(NULL),45_load_barrier_buffer_epoch_0(NULL),46_load_barrier_buffer_epoch_1(NULL),47_stackframes(NULL),48_trace_id(JfrTraceId::assign_thread_id()),49_thread(),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_excluded(false),59_dead(false) {60Thread* thread = Thread::current_or_null();61_parent_trace_id = thread != NULL ? thread->jfr_thread_local()->trace_id() : (traceid)0;62}6364u8 JfrThreadLocal::add_data_lost(u8 value) {65_data_lost += value;66return _data_lost;67}6869bool JfrThreadLocal::has_thread_blob() const {70return _thread.valid();71}7273void JfrThreadLocal::set_thread_blob(const JfrBlobHandle& ref) {74assert(!_thread.valid(), "invariant");75_thread = ref;76}7778const JfrBlobHandle& JfrThreadLocal::thread_blob() const {79return _thread;80}8182static void send_java_thread_start_event(JavaThread* jt) {83EventThreadStart event;84event.set_thread(jt->jfr_thread_local()->thread_id());85event.set_parentThread(jt->jfr_thread_local()->parent_thread_id());86event.commit();87}8889void JfrThreadLocal::on_start(Thread* t) {90assert(t != NULL, "invariant");91assert(Thread::current() == t, "invariant");92JfrJavaSupport::on_thread_start(t);93if (JfrRecorder::is_recording()) {94JfrCheckpointManager::write_thread_checkpoint(t);95if (!t->jfr_thread_local()->is_excluded()) {96if (t->is_Java_thread()) {97send_java_thread_start_event(t->as_Java_thread());98}99}100}101if (t->jfr_thread_local()->has_cached_stack_trace()) {102t->jfr_thread_local()->clear_cached_stack_trace();103}104}105106static void send_java_thread_end_events(traceid id, JavaThread* jt) {107assert(jt != NULL, "invariant");108assert(Thread::current() == jt, "invariant");109assert(jt->jfr_thread_local()->trace_id() == id, "invariant");110if (JfrRecorder::is_recording()) {111EventThreadEnd event;112event.set_thread(id);113event.commit();114JfrThreadCPULoadEvent::send_event_for_thread(jt);115}116}117118void JfrThreadLocal::release(Thread* t) {119if (has_java_event_writer()) {120assert(t->is_Java_thread(), "invariant");121JfrJavaSupport::destroy_global_jni_handle(java_event_writer());122_java_event_writer = NULL;123}124if (has_native_buffer()) {125JfrStorage::release_thread_local(native_buffer(), t);126_native_buffer = NULL;127}128if (has_java_buffer()) {129JfrStorage::release_thread_local(java_buffer(), t);130_java_buffer = NULL;131}132if (_stackframes != NULL) {133FREE_C_HEAP_ARRAY(JfrStackFrame, _stackframes);134_stackframes = NULL;135}136if (_load_barrier_buffer_epoch_0 != NULL) {137_load_barrier_buffer_epoch_0->set_retired();138_load_barrier_buffer_epoch_0 = NULL;139}140if (_load_barrier_buffer_epoch_1 != NULL) {141_load_barrier_buffer_epoch_1->set_retired();142_load_barrier_buffer_epoch_1 = NULL;143}144}145146void JfrThreadLocal::release(JfrThreadLocal* tl, Thread* t) {147assert(tl != NULL, "invariant");148assert(t != NULL, "invariant");149assert(Thread::current() == t, "invariant");150assert(!tl->is_dead(), "invariant");151assert(tl->shelved_buffer() == NULL, "invariant");152tl->_dead = true;153tl->release(t);154}155156void JfrThreadLocal::on_exit(Thread* t) {157assert(t != NULL, "invariant");158JfrThreadLocal * const tl = t->jfr_thread_local();159assert(!tl->is_dead(), "invariant");160if (JfrRecorder::is_recording()) {161if (t->is_Java_thread()) {162JavaThread* const jt = t->as_Java_thread();163ObjectSampleCheckpoint::on_thread_exit(jt);164send_java_thread_end_events(tl->thread_id(), jt);165}166}167release(tl, Thread::current()); // because it could be that Thread::current() != t168}169170static JfrBuffer* acquire_buffer(bool excluded) {171JfrBuffer* const buffer = JfrStorage::acquire_thread_local(Thread::current());172if (buffer != NULL && excluded) {173buffer->set_excluded();174}175return buffer;176}177178JfrBuffer* JfrThreadLocal::install_native_buffer() const {179assert(!has_native_buffer(), "invariant");180_native_buffer = acquire_buffer(_excluded);181return _native_buffer;182}183184JfrBuffer* JfrThreadLocal::install_java_buffer() const {185assert(!has_java_buffer(), "invariant");186assert(!has_java_event_writer(), "invariant");187_java_buffer = acquire_buffer(_excluded);188return _java_buffer;189}190191JfrStackFrame* JfrThreadLocal::install_stackframes() const {192assert(_stackframes == NULL, "invariant");193_stackframes = NEW_C_HEAP_ARRAY(JfrStackFrame, stackdepth(), mtTracing);194return _stackframes;195}196197ByteSize JfrThreadLocal::trace_id_offset() {198return in_ByteSize(offset_of(JfrThreadLocal, _trace_id));199}200201ByteSize JfrThreadLocal::java_event_writer_offset() {202return in_ByteSize(offset_of(JfrThreadLocal, _java_event_writer));203}204205void JfrThreadLocal::exclude(Thread* t) {206assert(t != NULL, "invariant");207t->jfr_thread_local()->_excluded = true;208t->jfr_thread_local()->release(t);209}210211void JfrThreadLocal::include(Thread* t) {212assert(t != NULL, "invariant");213t->jfr_thread_local()->_excluded = false;214t->jfr_thread_local()->release(t);215}216217u4 JfrThreadLocal::stackdepth() const {218return _stackdepth != 0 ? _stackdepth : (u4)JfrOptionSet::stackdepth();219}220221222