apetushkov@9858: /* apetushkov@9858: * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved. apetushkov@9858: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. apetushkov@9858: * apetushkov@9858: * This code is free software; you can redistribute it and/or modify it apetushkov@9858: * under the terms of the GNU General Public License version 2 only, as apetushkov@9858: * published by the Free Software Foundation. apetushkov@9858: * apetushkov@9858: * This code is distributed in the hope that it will be useful, but WITHOUT apetushkov@9858: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or apetushkov@9858: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License apetushkov@9858: * version 2 for more details (a copy is included in the LICENSE file that apetushkov@9858: * accompanied this code). apetushkov@9858: * apetushkov@9858: * You should have received a copy of the GNU General Public License version apetushkov@9858: * 2 along with this work; if not, write to the Free Software Foundation, apetushkov@9858: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. apetushkov@9858: * apetushkov@9858: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA apetushkov@9858: * or visit www.oracle.com if you need additional information or have any apetushkov@9858: * questions. apetushkov@9858: * apetushkov@9858: */ apetushkov@9858: apetushkov@9858: #include "precompiled.hpp" apetushkov@9858: #include "jfr/jni/jfrJavaSupport.hpp" apetushkov@9858: #include "jfr/periodic/jfrThreadCPULoadEvent.hpp" apetushkov@9858: #include "jfr/recorder/jfrRecorder.hpp" apetushkov@9858: #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp" apetushkov@9858: #include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp" apetushkov@9858: #include "jfr/recorder/service/jfrOptionSet.hpp" apetushkov@9858: #include "jfr/recorder/storage/jfrStorage.hpp" apetushkov@9858: #include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp" apetushkov@9858: #include "jfr/support/jfrThreadLocal.hpp" apetushkov@9858: #include "memory/allocation.inline.hpp" apetushkov@9858: #include "runtime/os.hpp" apetushkov@9858: #include "runtime/thread.inline.hpp" apetushkov@9858: apetushkov@9858: /* This data structure is per thread and only accessed by the thread itself, no locking required */ apetushkov@9858: JfrThreadLocal::JfrThreadLocal() : apetushkov@9858: _java_event_writer(NULL), apetushkov@9858: _java_buffer(NULL), apetushkov@9858: _native_buffer(NULL), apetushkov@9858: _shelved_buffer(NULL), apetushkov@9858: _stackframes(NULL), apetushkov@9858: _trace_id(JfrTraceId::assign_thread_id()), apetushkov@9858: _thread_cp(), apetushkov@9858: _data_lost(0), apetushkov@9858: _stack_trace_id(max_julong), apetushkov@9858: _user_time(0), apetushkov@9858: _cpu_time(0), apetushkov@9858: _wallclock_time(os::javaTimeNanos()), apetushkov@9858: _stack_trace_hash(0), apetushkov@9858: _stackdepth(0), apetushkov@9858: _entering_suspend_flag(0), apetushkov@9858: _dead(false) {} apetushkov@9858: apetushkov@9858: u8 JfrThreadLocal::add_data_lost(u8 value) { apetushkov@9858: _data_lost += value; apetushkov@9858: return _data_lost; apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrThreadLocal::has_thread_checkpoint() const { apetushkov@9858: return _thread_cp.valid(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrThreadLocal::set_thread_checkpoint(const JfrCheckpointBlobHandle& ref) { apetushkov@9858: assert(!_thread_cp.valid(), "invariant"); apetushkov@9858: _thread_cp = ref; apetushkov@9858: } apetushkov@9858: apetushkov@9858: const JfrCheckpointBlobHandle& JfrThreadLocal::thread_checkpoint() const { apetushkov@9858: return _thread_cp; apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrThreadLocal::set_dead() { apetushkov@9858: assert(!is_dead(), "invariant"); apetushkov@9858: _dead = true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrThreadLocal::on_exit(JavaThread* thread) { apetushkov@9858: if (JfrRecorder::is_recording()) { apetushkov@9858: JfrCheckpointManager::write_thread_checkpoint(thread); apetushkov@9858: JfrThreadCPULoadEvent::send_event_for_thread(thread); apetushkov@9858: } apetushkov@9858: thread->jfr_thread_local()->set_dead(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrThreadLocal::on_destruct(Thread* thread) { apetushkov@9858: JfrThreadLocal* const tl = thread->jfr_thread_local(); apetushkov@9858: if (tl->has_native_buffer()) { apetushkov@9858: release(tl->native_buffer(), thread); apetushkov@9858: } apetushkov@9858: if (tl->has_java_buffer()) { apetushkov@9858: release(tl->java_buffer(), thread); apetushkov@9858: } apetushkov@9858: assert(tl->shelved_buffer() == NULL, "invariant"); apetushkov@9858: if (thread->jfr_thread_local()->has_java_event_writer()) { apetushkov@9858: JfrJavaSupport::destroy_global_jni_handle(tl->java_event_writer()); apetushkov@9858: } apetushkov@9858: destroy_stackframes(thread); apetushkov@9858: } apetushkov@9858: apetushkov@9858: JfrBuffer* JfrThreadLocal::acquire(Thread* thread, size_t size) { apetushkov@9858: return JfrStorage::acquire_thread_local(thread, size); apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrThreadLocal::release(JfrBuffer* buffer, Thread* thread) { apetushkov@9858: assert(buffer != NULL, "invariant"); apetushkov@9858: JfrStorage::release_thread_local(buffer, thread); apetushkov@9858: } apetushkov@9858: apetushkov@9858: JfrBuffer* JfrThreadLocal::install_native_buffer() const { apetushkov@9858: assert(!has_native_buffer(), "invariant"); apetushkov@9858: _native_buffer = acquire(Thread::current()); apetushkov@9858: return _native_buffer; apetushkov@9858: } apetushkov@9858: apetushkov@9858: JfrBuffer* JfrThreadLocal::install_java_buffer() const { apetushkov@9858: assert(!has_java_buffer(), "invariant"); apetushkov@9858: assert(!has_java_event_writer(), "invariant"); apetushkov@9858: _java_buffer = acquire(Thread::current()); apetushkov@9858: return _java_buffer; apetushkov@9858: } apetushkov@9858: apetushkov@9858: JfrStackFrame* JfrThreadLocal::install_stackframes() const { apetushkov@9858: assert(_stackframes == NULL, "invariant"); apetushkov@9858: _stackdepth = (u4)JfrOptionSet::stackdepth(); apetushkov@9858: guarantee(_stackdepth > 0, "Stackdepth must be > 0"); apetushkov@9858: _stackframes = NEW_C_HEAP_ARRAY(JfrStackFrame, _stackdepth, mtTracing); apetushkov@9858: return _stackframes; apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrThreadLocal::destroy_stackframes(Thread* thread) { apetushkov@9858: assert(thread != NULL, "invariant"); apetushkov@9858: JfrStackFrame* frames = thread->jfr_thread_local()->stackframes(); apetushkov@9858: if (frames != NULL) { apetushkov@9858: FREE_C_HEAP_ARRAY(JfrStackFrame, frames, mtTracing); apetushkov@9858: thread->jfr_thread_local()->set_stackframes(NULL); apetushkov@9858: } apetushkov@9858: }