Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/jfr/support/jfrThreadLocal.cpp
38920 views
/*1* Copyright (c) 2012, 2018, 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/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#include "utilities/sizes.hpp"3940/* This data structure is per thread and only accessed by the thread itself, no locking required */41JfrThreadLocal::JfrThreadLocal() :42_java_event_writer(NULL),43_java_buffer(NULL),44_native_buffer(NULL),45_shelved_buffer(NULL),46_stackframes(NULL),47_trace_id(JfrTraceId::assign_thread_id()),48_thread_cp(),49_data_lost(0),50_stack_trace_id(max_julong),51_user_time(0),52_cpu_time(0),53_wallclock_time(os::javaTimeNanos()),54_stack_trace_hash(0),55_stackdepth(0),56_entering_suspend_flag(0),57_dead(false) {}5859u8 JfrThreadLocal::add_data_lost(u8 value) {60_data_lost += value;61return _data_lost;62}6364bool JfrThreadLocal::has_thread_checkpoint() const {65return _thread_cp.valid();66}6768void JfrThreadLocal::set_thread_checkpoint(const JfrCheckpointBlobHandle& ref) {69assert(!_thread_cp.valid(), "invariant");70_thread_cp = ref;71}7273const JfrCheckpointBlobHandle& JfrThreadLocal::thread_checkpoint() const {74return _thread_cp;75}7677static void send_java_thread_start_event(JavaThread* jt) {78EventThreadStart event;79event.set_thread(jt->jfr_thread_local()->thread_id());80event.commit();81}8283void JfrThreadLocal::on_start(Thread* t) {84assert(t != NULL, "invariant");85assert(Thread::current() == t, "invariant");86if (JfrRecorder::is_recording()) {87if (t->is_Java_thread()) {88send_java_thread_start_event((JavaThread*)t);89}90}91}9293static void send_java_thread_end_events(traceid id, JavaThread* jt) {94assert(jt != NULL, "invariant");95assert(Thread::current() == jt, "invariant");96assert(jt->jfr_thread_local()->trace_id() == id, "invariant");97EventThreadEnd event;98event.set_thread(id);99event.commit();100JfrThreadCPULoadEvent::send_event_for_thread(jt);101}102103void JfrThreadLocal::release(JfrThreadLocal* tl, Thread* t) {104assert(tl != NULL, "invariant");105assert(t != NULL, "invariant");106assert(Thread::current() == t, "invariant");107assert(!tl->is_dead(), "invariant");108assert(tl->shelved_buffer() == NULL, "invariant");109if (tl->has_native_buffer()) {110JfrStorage::release_thread_local(tl->native_buffer(), t);111}112if (tl->has_java_buffer()) {113JfrStorage::release_thread_local(tl->java_buffer(), t);114}115if (tl->has_java_event_writer()) {116assert(t->is_Java_thread(), "invariant");117JfrJavaSupport::destroy_global_jni_handle(tl->java_event_writer());118}119if (tl->_stackframes != NULL) {120FREE_C_HEAP_ARRAY(JfrStackFrame, tl->_stackframes, mtTracing);121}122tl->_dead = true;123}124125void JfrThreadLocal::on_exit(Thread* t) {126assert(t != NULL, "invariant");127JfrThreadLocal * const tl = t->jfr_thread_local();128assert(!tl->is_dead(), "invariant");129if (JfrRecorder::is_recording()) {130if (t->is_Java_thread()) {131send_java_thread_end_events(tl->thread_id(), (JavaThread*)t);132}133}134release(tl, Thread::current()); // because it could be that Thread::current() != t135}136137JfrBuffer* JfrThreadLocal::install_native_buffer() const {138assert(!has_native_buffer(), "invariant");139_native_buffer = JfrStorage::acquire_thread_local(Thread::current());140return _native_buffer;141}142143JfrBuffer* JfrThreadLocal::install_java_buffer() const {144assert(!has_java_buffer(), "invariant");145assert(!has_java_event_writer(), "invariant");146_java_buffer = JfrStorage::acquire_thread_local(Thread::current());147return _java_buffer;148}149150JfrStackFrame* JfrThreadLocal::install_stackframes() const {151assert(_stackframes == NULL, "invariant");152_stackframes = NEW_C_HEAP_ARRAY(JfrStackFrame, stackdepth(), mtTracing);153return _stackframes;154}155156ByteSize JfrThreadLocal::trace_id_offset() {157return in_ByteSize(offset_of(JfrThreadLocal, _trace_id));158}159160ByteSize JfrThreadLocal::java_event_writer_offset() {161return in_ByteSize(offset_of(JfrThreadLocal, _java_event_writer));162}163164u4 JfrThreadLocal::stackdepth() const {165return _stackdepth != 0 ? _stackdepth : (u4)JfrOptionSet::stackdepth();166}167168169