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/periodic/jfrThreadCPULoadEvent.hpp" 27 #include "jfr/jni/jfrJavaSupport.hpp" 28 #include "jfr/recorder/access/jfrbackend.hpp" 29 #include "jfr/recorder/access/jfrOptionSet.hpp" 30 #include "jfr/recorder/access/jfrThreadData.hpp" 31 #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp" 32 #include "jfr/recorder/checkpoint/constant/traceid/jfrTraceId.inline.hpp" 33 #include "jfr/recorder/storage/jfrStorage.hpp" 34 #include "jfr/recorder/stacktrace/jfrStackTraceRepository.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 JfrThreadData::JfrThreadData() : 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 57 u8 JfrThreadData::add_data_lost(u8 value) { 58 _data_lost += value; 59 return _data_lost; 60 } 61 62 bool JfrThreadData::has_thread_checkpoint() const { 63 return _thread_cp.valid(); 64 } 65 66 void JfrThreadData::set_thread_checkpoint(const JfrCheckpointBlobHandle& ref) { 67 assert(!_thread_cp.valid(), "invariant"); 68 _thread_cp = ref; 69 } 70 71 const JfrCheckpointBlobHandle& JfrThreadData::thread_checkpoint() const { 72 return _thread_cp; 73 } 74 75 void JfrThreadData::on_exit(JavaThread* thread) { 76 JfrCheckpointManager::write_thread_checkpoint(thread); 77 JfrThreadCPULoadEvent::send_event_for_thread(thread); 78 } 79 80 void JfrThreadData::on_destruct(Thread* thread) { 81 JfrThreadData* const thread_data = thread->trace_data(); 82 if (thread_data->has_native_buffer()) { 83 release(thread_data->native_buffer(), thread); 84 } 85 if (thread_data->has_java_buffer()) { 86 release(thread_data->java_buffer(), thread); 87 } 88 assert(thread_data->shelved_buffer() == NULL, "invariant"); 89 if (thread->trace_data()->has_java_event_writer()) { 90 JfrJavaSupport::destroy_global_jni_handle(thread_data->java_event_writer()); 91 } 92 destroy_stackframes(thread); 93 } 94 95 JfrBuffer* JfrThreadData::acquire(Thread* thread, size_t size) { 96 return JfrStorage::acquire_thread_local(thread, size); 97 } 98 99 void JfrThreadData::release(JfrBuffer* buffer, Thread* thread) { 100 assert(buffer != NULL, "invariant"); 101 JfrStorage::release_thread_local(buffer, thread); 102 } 103 104 JfrBuffer* JfrThreadData::install_native_buffer() const { 105 assert(!has_native_buffer(), "invariant"); 106 _native_buffer = acquire(Thread::current()); 107 return _native_buffer; 108 } 109 110 JfrBuffer* JfrThreadData::install_java_buffer() const { 111 assert(!has_java_buffer(), "invariant"); 112 assert(!has_java_event_writer(), "invariant"); 113 _java_buffer = acquire(Thread::current()); 114 return _java_buffer; 115 } 116 117 JfrStackFrame* JfrThreadData::install_stackframes() const { 118 assert(_stackframes == NULL, "invariant"); 119 _stackdepth = (u4)JfrOptionSet::stackdepth(); 120 guarantee(_stackdepth > 0, "Stackdepth must be > 0"); 121 _stackframes = NEW_C_HEAP_ARRAY(JfrStackFrame, _stackdepth, mtTracing); 122 return _stackframes; 123 } 124 125 void JfrThreadData::destroy_stackframes(Thread* thread) { 126 assert(thread != NULL, "invariant"); 127 JfrStackFrame* frames = thread->trace_data()->stackframes(); 128 if (frames != NULL) { 129 FREE_C_HEAP_ARRAY(JfrStackFrame, frames); 130 thread->trace_data()->set_stackframes(NULL); 131 } 132 }