src/share/vm/jfr/support/jfrThreadLocal.cpp

Thu, 15 Nov 2018 11:10:04 +0100

author
mgronlun
date
Thu, 15 Nov 2018 11:10:04 +0100
changeset 9868
69fb91513217
parent 9858
b985cbb00e68
child 9885
8e875c964f41
permissions
-rw-r--r--

8210024: JFR calls virtual is_Java_thread from ~Thread()
Reviewed-by: kbarrett, dholmes, dcubed, egahlin

apetushkov@9858 1 /*
apetushkov@9858 2 * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
apetushkov@9858 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
apetushkov@9858 4 *
apetushkov@9858 5 * This code is free software; you can redistribute it and/or modify it
apetushkov@9858 6 * under the terms of the GNU General Public License version 2 only, as
apetushkov@9858 7 * published by the Free Software Foundation.
apetushkov@9858 8 *
apetushkov@9858 9 * This code is distributed in the hope that it will be useful, but WITHOUT
apetushkov@9858 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
apetushkov@9858 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
apetushkov@9858 12 * version 2 for more details (a copy is included in the LICENSE file that
apetushkov@9858 13 * accompanied this code).
apetushkov@9858 14 *
apetushkov@9858 15 * You should have received a copy of the GNU General Public License version
apetushkov@9858 16 * 2 along with this work; if not, write to the Free Software Foundation,
apetushkov@9858 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
apetushkov@9858 18 *
apetushkov@9858 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
apetushkov@9858 20 * or visit www.oracle.com if you need additional information or have any
apetushkov@9858 21 * questions.
apetushkov@9858 22 *
apetushkov@9858 23 */
apetushkov@9858 24
apetushkov@9858 25 #include "precompiled.hpp"
mgronlun@9868 26 #include "jfr/jfrEvents.hpp"
apetushkov@9858 27 #include "jfr/jni/jfrJavaSupport.hpp"
apetushkov@9858 28 #include "jfr/periodic/jfrThreadCPULoadEvent.hpp"
apetushkov@9858 29 #include "jfr/recorder/jfrRecorder.hpp"
apetushkov@9858 30 #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
apetushkov@9858 31 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp"
apetushkov@9858 32 #include "jfr/recorder/service/jfrOptionSet.hpp"
apetushkov@9858 33 #include "jfr/recorder/storage/jfrStorage.hpp"
apetushkov@9858 34 #include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"
apetushkov@9858 35 #include "jfr/support/jfrThreadLocal.hpp"
apetushkov@9858 36 #include "memory/allocation.inline.hpp"
apetushkov@9858 37 #include "runtime/os.hpp"
apetushkov@9858 38 #include "runtime/thread.inline.hpp"
mgronlun@9868 39 #include "utilities/sizes.hpp"
apetushkov@9858 40
apetushkov@9858 41 /* This data structure is per thread and only accessed by the thread itself, no locking required */
apetushkov@9858 42 JfrThreadLocal::JfrThreadLocal() :
apetushkov@9858 43 _java_event_writer(NULL),
apetushkov@9858 44 _java_buffer(NULL),
apetushkov@9858 45 _native_buffer(NULL),
apetushkov@9858 46 _shelved_buffer(NULL),
apetushkov@9858 47 _stackframes(NULL),
apetushkov@9858 48 _trace_id(JfrTraceId::assign_thread_id()),
apetushkov@9858 49 _thread_cp(),
apetushkov@9858 50 _data_lost(0),
apetushkov@9858 51 _stack_trace_id(max_julong),
apetushkov@9858 52 _user_time(0),
apetushkov@9858 53 _cpu_time(0),
apetushkov@9858 54 _wallclock_time(os::javaTimeNanos()),
apetushkov@9858 55 _stack_trace_hash(0),
apetushkov@9858 56 _stackdepth(0),
apetushkov@9858 57 _entering_suspend_flag(0),
apetushkov@9858 58 _dead(false) {}
apetushkov@9858 59
apetushkov@9858 60 u8 JfrThreadLocal::add_data_lost(u8 value) {
apetushkov@9858 61 _data_lost += value;
apetushkov@9858 62 return _data_lost;
apetushkov@9858 63 }
apetushkov@9858 64
apetushkov@9858 65 bool JfrThreadLocal::has_thread_checkpoint() const {
apetushkov@9858 66 return _thread_cp.valid();
apetushkov@9858 67 }
apetushkov@9858 68
apetushkov@9858 69 void JfrThreadLocal::set_thread_checkpoint(const JfrCheckpointBlobHandle& ref) {
apetushkov@9858 70 assert(!_thread_cp.valid(), "invariant");
apetushkov@9858 71 _thread_cp = ref;
apetushkov@9858 72 }
apetushkov@9858 73
apetushkov@9858 74 const JfrCheckpointBlobHandle& JfrThreadLocal::thread_checkpoint() const {
apetushkov@9858 75 return _thread_cp;
apetushkov@9858 76 }
apetushkov@9858 77
mgronlun@9868 78 static void send_java_thread_start_event(JavaThread* jt) {
mgronlun@9868 79 EventThreadStart event;
mgronlun@9868 80 event.set_thread(jt->jfr_thread_local()->thread_id());
mgronlun@9868 81 event.commit();
apetushkov@9858 82 }
apetushkov@9858 83
mgronlun@9868 84 void JfrThreadLocal::on_start(Thread* t) {
mgronlun@9868 85 assert(t != NULL, "invariant");
mgronlun@9868 86 assert(Thread::current() == t, "invariant");
apetushkov@9858 87 if (JfrRecorder::is_recording()) {
mgronlun@9868 88 if (t->is_Java_thread()) {
mgronlun@9868 89 send_java_thread_start_event((JavaThread*)t);
mgronlun@9868 90 }
apetushkov@9858 91 }
apetushkov@9858 92 }
apetushkov@9858 93
mgronlun@9868 94 static void send_java_thread_end_events(traceid id, JavaThread* jt) {
mgronlun@9868 95 assert(jt != NULL, "invariant");
mgronlun@9868 96 assert(Thread::current() == jt, "invariant");
mgronlun@9868 97 assert(jt->jfr_thread_local()->trace_id() == id, "invariant");
mgronlun@9868 98 EventThreadEnd event;
mgronlun@9868 99 event.set_thread(id);
mgronlun@9868 100 event.commit();
mgronlun@9868 101 JfrThreadCPULoadEvent::send_event_for_thread(jt);
mgronlun@9868 102 }
mgronlun@9868 103
mgronlun@9868 104 void JfrThreadLocal::release(JfrThreadLocal* tl, Thread* t) {
mgronlun@9868 105 assert(tl != NULL, "invariant");
mgronlun@9868 106 assert(t != NULL, "invariant");
mgronlun@9868 107 assert(Thread::current() == t, "invariant");
mgronlun@9868 108 assert(!tl->is_dead(), "invariant");
mgronlun@9868 109 assert(tl->shelved_buffer() == NULL, "invariant");
apetushkov@9858 110 if (tl->has_native_buffer()) {
mgronlun@9868 111 JfrStorage::release_thread_local(tl->native_buffer(), t);
apetushkov@9858 112 }
apetushkov@9858 113 if (tl->has_java_buffer()) {
mgronlun@9868 114 JfrStorage::release_thread_local(tl->java_buffer(), t);
apetushkov@9858 115 }
mgronlun@9868 116 if (tl->has_java_event_writer()) {
mgronlun@9868 117 assert(t->is_Java_thread(), "invariant");
apetushkov@9858 118 JfrJavaSupport::destroy_global_jni_handle(tl->java_event_writer());
apetushkov@9858 119 }
mgronlun@9868 120 if (tl->_stackframes != NULL) {
mgronlun@9868 121 FREE_C_HEAP_ARRAY(JfrStackFrame, tl->_stackframes, mtTracing);
mgronlun@9868 122 }
mgronlun@9868 123 tl->_dead = true;
apetushkov@9858 124 }
apetushkov@9858 125
mgronlun@9868 126 void JfrThreadLocal::on_exit(Thread* t) {
mgronlun@9868 127 assert(t != NULL, "invariant");
mgronlun@9868 128 JfrThreadLocal * const tl = t->jfr_thread_local();
mgronlun@9868 129 assert(!tl->is_dead(), "invariant");
mgronlun@9868 130 if (JfrRecorder::is_recording()) {
mgronlun@9868 131 if (t->is_Java_thread()) {
mgronlun@9868 132 send_java_thread_end_events(tl->thread_id(), (JavaThread*)t);
mgronlun@9868 133 }
mgronlun@9868 134 }
mgronlun@9868 135 release(tl, Thread::current()); // because it could be that Thread::current() != t
apetushkov@9858 136 }
apetushkov@9858 137
apetushkov@9858 138 JfrBuffer* JfrThreadLocal::install_native_buffer() const {
apetushkov@9858 139 assert(!has_native_buffer(), "invariant");
mgronlun@9868 140 _native_buffer = JfrStorage::acquire_thread_local(Thread::current());
apetushkov@9858 141 return _native_buffer;
apetushkov@9858 142 }
apetushkov@9858 143
apetushkov@9858 144 JfrBuffer* JfrThreadLocal::install_java_buffer() const {
apetushkov@9858 145 assert(!has_java_buffer(), "invariant");
apetushkov@9858 146 assert(!has_java_event_writer(), "invariant");
mgronlun@9868 147 _java_buffer = JfrStorage::acquire_thread_local(Thread::current());
apetushkov@9858 148 return _java_buffer;
apetushkov@9858 149 }
apetushkov@9858 150
apetushkov@9858 151 JfrStackFrame* JfrThreadLocal::install_stackframes() const {
apetushkov@9858 152 assert(_stackframes == NULL, "invariant");
apetushkov@9858 153 _stackdepth = (u4)JfrOptionSet::stackdepth();
apetushkov@9858 154 guarantee(_stackdepth > 0, "Stackdepth must be > 0");
apetushkov@9858 155 _stackframes = NEW_C_HEAP_ARRAY(JfrStackFrame, _stackdepth, mtTracing);
apetushkov@9858 156 return _stackframes;
apetushkov@9858 157 }
apetushkov@9858 158
mgronlun@9868 159 ByteSize JfrThreadLocal::trace_id_offset() {
mgronlun@9868 160 return in_ByteSize(offset_of(JfrThreadLocal, _trace_id));
apetushkov@9858 161 }
mgronlun@9868 162
mgronlun@9868 163 ByteSize JfrThreadLocal::java_event_writer_offset() {
mgronlun@9868 164 return in_ByteSize(offset_of(JfrThreadLocal, _java_event_writer));
mgronlun@9868 165 }

mercurial