1 /* 2 * Copyright (c) 2011, 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 "classfile/classLoaderData.inline.hpp" 27 #include "classfile/symbolTable.hpp" 28 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp" 29 #include "jfr/utilities/jfrTypes.hpp" 30 #include "oops/arrayKlass.inline.hpp" 31 #include "oops/klass.inline.hpp" 32 #include "oops/instanceKlass.inline.hpp" 33 #include "oops/method.hpp" 34 #include "oops/oop.inline.hpp" 35 #include "runtime/atomic.hpp" 36 #include "runtime/orderAccess.hpp" 37 #include "runtime/vm_version.hpp" 38 #include "runtime/jniHandles.inline.hpp" 39 #include "runtime/thread.inline.hpp" 40 #include "utilities/debug.hpp" 41 42 // returns updated value 43 static traceid atomic_inc(traceid volatile* const dest) { 44 assert(VM_Version::supports_cx8(), "invariant"); 45 traceid compare_value; 46 traceid exchange_value; 47 do { 48 compare_value = OrderAccess::load_acquire(dest); 49 exchange_value = compare_value + 1; 50 } while (Atomic::cmpxchg(exchange_value, dest, compare_value) != compare_value); 51 return exchange_value; 52 } 53 54 static traceid next_class_id() { 55 static volatile traceid class_id_counter = MaxJfrEventId + 100; 56 return atomic_inc(&class_id_counter) << TRACE_ID_SHIFT; 57 } 58 59 static traceid next_thread_id() { 60 static volatile traceid thread_id_counter = 0; 61 return atomic_inc(&thread_id_counter); 62 } 63 64 static traceid next_module_id() { 65 static volatile traceid module_id_counter = 1; 66 return atomic_inc(&module_id_counter) << TRACE_ID_SHIFT; 67 } 68 69 static traceid next_package_id() { 70 static volatile traceid package_id_counter = 1; 71 return atomic_inc(&package_id_counter) << TRACE_ID_SHIFT; 72 } 73 74 static traceid next_class_loader_data_id() { 75 static volatile traceid cld_id_counter = 1; 76 return atomic_inc(&cld_id_counter) << TRACE_ID_SHIFT; 77 } 78 79 static bool found_jdk_jfr_event_klass = false; 80 81 static void check_klass(const Klass* klass) { 82 assert(klass != NULL, "invariant"); 83 if (found_jdk_jfr_event_klass) { 84 return; 85 } 86 static const Symbol* jdk_jfr_event_sym = NULL; 87 if (jdk_jfr_event_sym == NULL) { 88 // setup when loading the first TypeArrayKlass (Universe::genesis) hence single threaded invariant 89 jdk_jfr_event_sym = SymbolTable::new_permanent_symbol("jdk/jfr/Event", Thread::current()); 90 } 91 assert(jdk_jfr_event_sym != NULL, "invariant"); 92 if (jdk_jfr_event_sym == klass->name() && klass->class_loader() == NULL) { 93 found_jdk_jfr_event_klass = true; 94 JfrTraceId::tag_as_jdk_jfr_event(klass); 95 } 96 } 97 98 void JfrTraceId::assign(const Klass* klass) { 99 assert(klass != NULL, "invariant"); 100 klass->set_trace_id(next_class_id()); 101 check_klass(klass); 102 const Klass* const super = klass->super(); 103 if (super == NULL) { 104 return; 105 } 106 if (IS_EVENT_KLASS(super)) { 107 tag_as_jdk_jfr_event_sub(klass); 108 } 109 } 110 111 void JfrTraceId::assign(const ModuleEntry* module) { 112 assert(module != NULL, "invariant"); 113 module->set_trace_id(next_module_id()); 114 } 115 116 void JfrTraceId::assign(const PackageEntry* package) { 117 assert(package != NULL, "invariant"); 118 package->set_trace_id(next_package_id()); 119 } 120 121 void JfrTraceId::assign(const ClassLoaderData* cld) { 122 assert(cld != NULL, "invariant"); 123 if (cld->is_anonymous()) { 124 cld->set_trace_id(0); 125 return; 126 } 127 cld->set_trace_id(next_class_loader_data_id()); 128 } 129 130 traceid JfrTraceId::assign_thread_id() { 131 return next_thread_id(); 132 } 133 134 // used by CDS / APPCDS as part of "remove_unshareable_info" 135 void JfrTraceId::remove(const Klass* k) { 136 assert(k != NULL, "invariant"); 137 // Mask off and store the event flags. 138 // This mechanism will retain the event specific flags 139 // in the archive, allowing for event flag restoration 140 // when renewing the traceid on klass revival. 141 k->set_trace_id(EVENT_FLAGS_MASK(k)); 142 } 143 144 // used by CDS / APPCDS as part of "restore_unshareable_info" 145 void JfrTraceId::restore(const Klass* k) { 146 assert(k != NULL, "invariant"); 147 if (IS_JDK_JFR_EVENT_KLASS(k)) { 148 found_jdk_jfr_event_klass = true; 149 } 150 const traceid event_flags = k->trace_id(); 151 // get a fresh traceid and restore the original event flags 152 k->set_trace_id(next_class_id() | event_flags); 153 } 154 155 traceid JfrTraceId::get(jclass jc) { 156 assert(jc != NULL, "invariant"); 157 assert(((JavaThread*)Thread::current())->thread_state() == _thread_in_vm, "invariant"); 158 const oop my_oop = JNIHandles::resolve(jc); 159 assert(my_oop != NULL, "invariant"); 160 return get(java_lang_Class::as_Klass(my_oop)); 161 } 162 163 traceid JfrTraceId::use(jclass jc, bool leakp /* false */) { 164 assert(jc != NULL, "invariant"); 165 assert(((JavaThread*)Thread::current())->thread_state() == _thread_in_vm, "invariant"); 166 const oop my_oop = JNIHandles::resolve(jc); 167 assert(my_oop != NULL, "invariant"); 168 return use(java_lang_Class::as_Klass(my_oop), leakp); 169 } 170 171 bool JfrTraceId::in_visible_set(const jclass jc) { 172 assert(jc != NULL, "invariant"); 173 assert(((JavaThread*)Thread::current())->thread_state() == _thread_in_vm, "invariant"); 174 const oop mirror = JNIHandles::resolve(jc); 175 assert(mirror != NULL, "invariant"); 176 return in_visible_set(java_lang_Class::as_Klass(mirror)); 177 } 178 179 bool JfrTraceId::in_jdk_jfr_event_hierarchy(const jclass jc) { 180 assert(jc != NULL, "invariant"); 181 const oop mirror = JNIHandles::resolve(jc); 182 assert(mirror != NULL, "invariant"); 183 return in_jdk_jfr_event_hierarchy(java_lang_Class::as_Klass(mirror)); 184 } 185 186 bool JfrTraceId::is_jdk_jfr_event_sub(const jclass jc) { 187 assert(jc != NULL, "invariant"); 188 const oop mirror = JNIHandles::resolve(jc); 189 assert(mirror != NULL, "invariant"); 190 return is_jdk_jfr_event_sub(java_lang_Class::as_Klass(mirror)); 191 } 192 193 bool JfrTraceId::is_jdk_jfr_event(const jclass jc) { 194 assert(jc != NULL, "invariant"); 195 const oop mirror = JNIHandles::resolve(jc); 196 assert(mirror != NULL, "invariant"); 197 return is_jdk_jfr_event(java_lang_Class::as_Klass(mirror)); 198 } 199 200 bool JfrTraceId::is_event_host(const jclass jc) { 201 assert(jc != NULL, "invariant"); 202 const oop mirror = JNIHandles::resolve(jc); 203 assert(mirror != NULL, "invariant"); 204 return is_event_host(java_lang_Class::as_Klass(mirror)); 205 } 206 207 void JfrTraceId::tag_as_jdk_jfr_event_sub(const jclass jc) { 208 assert(jc != NULL, "invariant"); 209 const oop mirror = JNIHandles::resolve(jc); 210 assert(mirror != NULL, "invariant"); 211 const Klass* const k = java_lang_Class::as_Klass(mirror); 212 tag_as_jdk_jfr_event_sub(k); 213 assert(IS_JDK_JFR_EVENT_SUBKLASS(k), "invariant"); 214 } 215 216 void JfrTraceId::tag_as_event_host(const jclass jc) { 217 assert(jc != NULL, "invariant"); 218 const oop mirror = JNIHandles::resolve(jc); 219 assert(mirror != NULL, "invariant"); 220 const Klass* const k = java_lang_Class::as_Klass(mirror); 221 tag_as_event_host(k); 222 assert(IS_EVENT_HOST_KLASS(k), "invariant"); 223 }