| 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/jfrEvents.hpp" |
| 27 | #include "jfr/jni/jfrJavaSupport.hpp" |
| 28 | #include "jfr/periodic/jfrThreadCPULoadEvent.hpp" |
| 29 | #include "jfr/recorder/jfrRecorder.hpp" |
| 30 | #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp" |
| 31 | #include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp" |
| 32 | #include "jfr/recorder/service/jfrOptionSet.hpp" |
| 33 | #include "jfr/recorder/storage/jfrStorage.hpp" |
| 34 | #include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp" |
| 35 | #include "jfr/support/jfrThreadLocal.hpp" |
| 36 | #include "memory/allocation.inline.hpp" |
| 37 | #include "runtime/os.hpp" |
| 38 | #include "runtime/thread.inline.hpp" |
| 39 | #include "utilities/sizes.hpp" |
| 40 | |
| 41 | /* This data structure is per thread and only accessed by the thread itself, no locking required */ |
| 42 | JfrThreadLocal::JfrThreadLocal() : |
| 43 | _java_event_writer(NULL), |
| 44 | _java_buffer(NULL), |
| 45 | _native_buffer(NULL), |
| 46 | _shelved_buffer(NULL), |
| 47 | _stackframes(NULL), |
| 48 | _trace_id(JfrTraceId::assign_thread_id()), |
| 49 | _thread_cp(), |
| 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 | _dead(false) {} |
| 59 | |
| 60 | u8 JfrThreadLocal::add_data_lost(u8 value) { |
| 61 | _data_lost += value; |
| 62 | return _data_lost; |
| 63 | } |
| 64 | |
| 65 | bool JfrThreadLocal::has_thread_checkpoint() const { |
| 66 | return _thread_cp.valid(); |
| 67 | } |
| 68 | |
| 69 | void JfrThreadLocal::set_thread_checkpoint(const JfrCheckpointBlobHandle& ref) { |
| 70 | assert(!_thread_cp.valid(), "invariant" ); |
| 71 | _thread_cp = ref; |
| 72 | } |
| 73 | |
| 74 | const JfrCheckpointBlobHandle& JfrThreadLocal::thread_checkpoint() const { |
| 75 | return _thread_cp; |
| 76 | } |
| 77 | |
| 78 | static void send_java_thread_start_event(JavaThread* jt) { |
| 79 | EventThreadStart event; |
| 80 | event.set_thread(jt->jfr_thread_local()->thread_id()); |
| 81 | event.commit(); |
| 82 | } |
| 83 | |
| 84 | void JfrThreadLocal::on_start(Thread* t) { |
| 85 | assert(t != NULL, "invariant" ); |
| 86 | assert(Thread::current() == t, "invariant" ); |
| 87 | if (JfrRecorder::is_recording()) { |
| 88 | if (t->is_Java_thread()) { |
| 89 | send_java_thread_start_event((JavaThread*)t); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static void send_java_thread_end_events(traceid id, JavaThread* jt) { |
| 95 | assert(jt != NULL, "invariant" ); |
| 96 | assert(Thread::current() == jt, "invariant" ); |
| 97 | assert(jt->jfr_thread_local()->trace_id() == id, "invariant" ); |
| 98 | EventThreadEnd event; |
| 99 | event.set_thread(id); |
| 100 | event.commit(); |
| 101 | JfrThreadCPULoadEvent::send_event_for_thread(jt); |
| 102 | } |
| 103 | |
| 104 | void JfrThreadLocal::release(JfrThreadLocal* tl, Thread* t) { |
| 105 | assert(tl != NULL, "invariant" ); |
| 106 | assert(t != NULL, "invariant" ); |
| 107 | assert(Thread::current() == t, "invariant" ); |
| 108 | assert(!tl->is_dead(), "invariant" ); |
| 109 | assert(tl->shelved_buffer() == NULL, "invariant" ); |
| 110 | if (tl->has_native_buffer()) { |
| 111 | JfrStorage::release_thread_local(tl->native_buffer(), t); |
| 112 | } |
| 113 | if (tl->has_java_buffer()) { |
| 114 | JfrStorage::release_thread_local(tl->java_buffer(), t); |
| 115 | } |
| 116 | if (tl->has_java_event_writer()) { |
| 117 | assert(t->is_Java_thread(), "invariant" ); |
| 118 | JfrJavaSupport::destroy_global_jni_handle(tl->java_event_writer()); |
| 119 | } |
| 120 | if (tl->_stackframes != NULL) { |
| 121 | FREE_C_HEAP_ARRAY(JfrStackFrame, tl->_stackframes); |
| 122 | } |
| 123 | tl->_dead = true; |
| 124 | } |
| 125 | |
| 126 | void JfrThreadLocal::on_exit(Thread* t) { |
| 127 | assert(t != NULL, "invariant" ); |
| 128 | JfrThreadLocal * const tl = t->jfr_thread_local(); |
| 129 | assert(!tl->is_dead(), "invariant" ); |
| 130 | if (JfrRecorder::is_recording()) { |
| 131 | if (t->is_Java_thread()) { |
| 132 | send_java_thread_end_events(tl->thread_id(), (JavaThread*)t); |
| 133 | } |
| 134 | } |
| 135 | release(tl, Thread::current()); // because it could be that Thread::current() != t |
| 136 | } |
| 137 | |
| 138 | JfrBuffer* JfrThreadLocal::install_native_buffer() const { |
| 139 | assert(!has_native_buffer(), "invariant" ); |
| 140 | _native_buffer = JfrStorage::acquire_thread_local(Thread::current()); |
| 141 | return _native_buffer; |
| 142 | } |
| 143 | |
| 144 | JfrBuffer* JfrThreadLocal::install_java_buffer() const { |
| 145 | assert(!has_java_buffer(), "invariant" ); |
| 146 | assert(!has_java_event_writer(), "invariant" ); |
| 147 | _java_buffer = JfrStorage::acquire_thread_local(Thread::current()); |
| 148 | return _java_buffer; |
| 149 | } |
| 150 | |
| 151 | JfrStackFrame* JfrThreadLocal::install_stackframes() const { |
| 152 | assert(_stackframes == NULL, "invariant" ); |
| 153 | _stackframes = NEW_C_HEAP_ARRAY(JfrStackFrame, stackdepth(), mtTracing); |
| 154 | return _stackframes; |
| 155 | } |
| 156 | |
| 157 | ByteSize JfrThreadLocal::trace_id_offset() { |
| 158 | return in_ByteSize(offset_of(JfrThreadLocal, _trace_id)); |
| 159 | } |
| 160 | |
| 161 | ByteSize JfrThreadLocal::java_event_writer_offset() { |
| 162 | return in_ByteSize(offset_of(JfrThreadLocal, _java_event_writer)); |
| 163 | } |
| 164 | |
| 165 | u4 JfrThreadLocal::stackdepth() const { |
| 166 | return _stackdepth != 0 ? _stackdepth : (u4)JfrOptionSet::stackdepth(); |
| 167 | } |
| 168 | |