1 /*
  2  * Copyright (c) 2011, 2021, 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/javaClasses.inline.hpp"
 28 #include "classfile/symbolTable.hpp"
 29 #include "classfile/systemDictionary.hpp"
 30 #include "jfr/jni/jfrJavaSupport.hpp"
 31 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp"
 32 #include "jfr/utilities/jfrTypes.hpp"
 33 #include "oops/access.inline.hpp"
 34 #include "oops/arrayKlass.inline.hpp"
 35 #include "oops/klass.inline.hpp"
 36 #include "oops/instanceKlass.inline.hpp"
 37 #include "oops/method.hpp"
 38 #include "oops/oop.inline.hpp"
 39 #include "runtime/atomic.hpp"
 40 #include "runtime/vm_version.hpp"
 41 #include "runtime/jniHandles.inline.hpp"
 42 #include "runtime/thread.inline.hpp"
 43 #include "utilities/debug.hpp"
 44 #include "utilities/exceptions.hpp"
 45 
 46 static int next_tid_offset = invalid_offset;
 47 static jclass thread_identifiers = NULL;
 48 
 49 static bool setup_thread_identifiers(TRAPS) {
 50   const char class_name[] = "java/lang/Thread$ThreadIdentifiers";
 51   Symbol * const k_sym = SymbolTable::new_symbol(class_name);
 52   assert(k_sym != NULL, "invariant");
 53   Klass * klass = SystemDictionary::resolve_or_fail(k_sym, true, CHECK_false);
 54   assert(klass != NULL, "invariant");
 55   assert(klass->is_instance_klass(), "invariant");
 56   assert(InstanceKlass::cast(klass)->is_initialized(), "invariant");
 57   const char next_tid_name[] = "nextTid";
 58   Symbol * const next_tid_symbol = SymbolTable::new_symbol(next_tid_name);
 59   assert(next_tid_symbol != NULL, "invariant");
 60   assert(invalid_offset == next_tid_offset, "invariant");
 61   if (!JfrJavaSupport::compute_field_offset(next_tid_offset, klass, next_tid_symbol, vmSymbols::long_signature(), true)) {
 62     return false;
 63   }
 64   assert(invalid_offset != next_tid_offset, "invariant");
 65   assert(thread_identifiers == NULL, "invariant");
 66   thread_identifiers = (jclass)JfrJavaSupport::global_jni_handle(klass->java_mirror(), THREAD);
 67   return thread_identifiers != NULL;
 68 }
 69 
 70 bool JfrTraceId::initialize() {
 71   static bool initialized = false;
 72   if (!initialized) {
 73     initialized = setup_thread_identifiers(JavaThread::current());
 74   }
 75   return initialized;
 76 }
 77 
 78 static traceid next_thread_id() {
 79   assert(thread_identifiers != NULL, "invariant");
 80   const oop base = JfrJavaSupport::resolve_non_null(thread_identifiers);
 81   traceid next;
 82   do {
 83     next = HeapAccess<>::load_at(base, (ptrdiff_t)next_tid_offset);
 84   } while (HeapAccess<>::atomic_cmpxchg_at(base, (ptrdiff_t)next_tid_offset, next, next + 1) != next);
 85   return next;
 86 }
 87 
 88 // returns updated value
 89 static traceid atomic_inc(traceid volatile* const dest, traceid stride = 1) {
 90   assert(VM_Version::supports_cx8(), "invariant");
 91   traceid compare_value;
 92   traceid exchange_value;
 93   do {
 94     compare_value = *dest;
 95     exchange_value = compare_value + stride;
 96   } while (Atomic::cmpxchg(dest, compare_value, exchange_value) != compare_value);
 97   return exchange_value;
 98 }
 99 
100 static traceid next_class_id() {
101   static volatile traceid class_id_counter = LAST_TYPE_ID + 1; // + 1 is for the void.class primitive
102   return atomic_inc(&class_id_counter) << TRACE_ID_SHIFT;
103 }
104 





105 static traceid next_module_id() {
106   static volatile traceid module_id_counter = 0;
107   return atomic_inc(&module_id_counter) << TRACE_ID_SHIFT;
108 }
109 
110 static traceid next_package_id() {
111   static volatile traceid package_id_counter = 0;
112   return atomic_inc(&package_id_counter) << TRACE_ID_SHIFT;
113 }
114 
115 static traceid next_class_loader_data_id() {
116   static volatile traceid cld_id_counter = 0;
117   return atomic_inc(&cld_id_counter) << TRACE_ID_SHIFT;
118 }
119 
120 static bool found_jdk_internal_event_klass = false;
121 static bool found_jdk_jfr_event_klass = false;
122 
123 static void check_klass(const Klass* klass) {
124   assert(klass != NULL, "invariant");
125   if (found_jdk_internal_event_klass && found_jdk_jfr_event_klass) {
126     return;
127   }
128   static const Symbol* jdk_internal_event_sym = NULL;
129   if (jdk_internal_event_sym == NULL) {
130     // setup when loading the first TypeArrayKlass (Universe::genesis) hence single threaded invariant
131     jdk_internal_event_sym = SymbolTable::new_permanent_symbol("jdk/internal/event/Event");
132   }
133   assert(jdk_internal_event_sym != NULL, "invariant");
134 
135   static const Symbol* jdk_jfr_event_sym = NULL;
136   if (jdk_jfr_event_sym == NULL) {
137     // setup when loading the first TypeArrayKlass (Universe::genesis) hence single threaded invariant
138     jdk_jfr_event_sym = SymbolTable::new_permanent_symbol("jdk/jfr/Event");
139   }
140   assert(jdk_jfr_event_sym != NULL, "invariant");
141   const Symbol* const klass_name = klass->name();
142 
143   if (!found_jdk_internal_event_klass) {
144     if (jdk_internal_event_sym == klass_name && klass->class_loader() == NULL) {
145       found_jdk_internal_event_klass = true;
146       JfrTraceId::tag_as_jdk_jfr_event(klass);
147       return;
148     }
149   }
150 
151   if (!found_jdk_jfr_event_klass) {
152     if (jdk_jfr_event_sym == klass_name && klass->class_loader() == NULL) {
153       found_jdk_jfr_event_klass = true;
154       JfrTraceId::tag_as_jdk_jfr_event(klass);
155       return;
156     }
157   }
158 }
159 
160 void JfrTraceId::assign(const Klass* klass) {
161   assert(klass != NULL, "invariant");
162   klass->set_trace_id(next_class_id());
163   check_klass(klass);
164   const Klass* const super = klass->super();
165   if (super == NULL) {
166     return;
167   }
168   if (IS_EVENT_KLASS(super)) {
169     tag_as_jdk_jfr_event_sub(klass);
170   }
171 }
172 
173 void JfrTraceId::assign(const ModuleEntry* module) {
174   assert(module != NULL, "invariant");
175   module->set_trace_id(next_module_id());
176 }
177 
178 void JfrTraceId::assign(const PackageEntry* package) {
179   assert(package != NULL, "invariant");
180   package->set_trace_id(next_package_id());
181 }
182 
183 void JfrTraceId::assign(const ClassLoaderData* cld) {
184   assert(cld != NULL, "invariant");
185   if (cld->has_class_mirror_holder()) {
186     cld->set_trace_id(0);
187     return;
188   }
189   cld->set_trace_id(next_class_loader_data_id());
190 }
191 
192 traceid JfrTraceId::assign_primitive_klass_id() {
193   return next_class_id();
194 }
195 
196 traceid JfrTraceId::assign_thread_id() {
197   return next_thread_id();
198 }
199 
200 // A mirror representing a primitive class (e.g. int.class) has no reified Klass*,
201 // instead it has an associated TypeArrayKlass* (e.g. int[].class).
202 // We can use the TypeArrayKlass* as a proxy for deriving the id of the primitive class.
203 // The exception is the void.class, which has neither a Klass* nor a TypeArrayKlass*.
204 // It will use a reserved constant.
205 static traceid load_primitive(const oop mirror) {
206   assert(java_lang_Class::is_primitive(mirror), "invariant");
207   const Klass* const tak = java_lang_Class::array_klass_acquire(mirror);
208   traceid id;
209   if (tak == NULL) {
210     // The first klass id is reserved for the void.class
211     id = LAST_TYPE_ID + 1;
212   } else {
213     id = JfrTraceId::load_raw(tak) + 1;
214   }
215   JfrTraceIdEpoch::set_changed_tag_state();
216   return id;
217 }
218 
219 traceid JfrTraceId::load(jclass jc, bool raw /* false */) {
220   assert(jc != NULL, "invariant");
221   assert(JavaThread::current()->thread_state() == _thread_in_vm, "invariant");
222   const oop mirror = JNIHandles::resolve(jc);
223   assert(mirror != NULL, "invariant");
224   const Klass* const k = java_lang_Class::as_Klass(mirror);
225   return k != NULL ? (raw ? load_raw(k) : load(k)) : load_primitive(mirror);
226 }
227 
228 traceid JfrTraceId::load_raw(jclass jc) {
229   return load(jc, true);
230 }
231 
232 // used by CDS / APPCDS as part of "remove_unshareable_info"
233 void JfrTraceId::remove(const Klass* k) {
234   assert(k != NULL, "invariant");
235   // Mask off and store the event flags.
236   // This mechanism will retain the event specific flags
237   // in the archive, allowing for event flag restoration
238   // when renewing the traceid on klass revival.
239   k->set_trace_id(EVENT_KLASS_MASK(k));
240 }
241 
242 // used by CDS / APPCDS as part of "remove_unshareable_info"
243 void JfrTraceId::remove(const Method* method) {
244   assert(method != NULL, "invariant");
245   // Clear all bits.
246   method->set_trace_flags(0);
247 }
248 
249 // used by CDS / APPCDS as part of "restore_unshareable_info"
250 void JfrTraceId::restore(const Klass* k) {
251   assert(k != NULL, "invariant");
252   if (IS_JDK_JFR_EVENT_KLASS(k)) {
253     found_jdk_jfr_event_klass = true;
254   }
255   const traceid event_flags = k->trace_id();
256   // get a fresh traceid and restore the original event flags
257   k->set_trace_id(next_class_id() | event_flags);
258   if (k->is_typeArray_klass()) {
259     // the next id is reserved for the corresponding primitive class
260     next_class_id();
261   }
262 }
263 
264 bool JfrTraceId::in_visible_set(const jclass jc) {
265   assert(jc != NULL, "invariant");
266   assert(JavaThread::current()->thread_state() == _thread_in_vm, "invariant");
267   const oop mirror = JNIHandles::resolve(jc);
268   assert(mirror != NULL, "invariant");
269   return in_visible_set(java_lang_Class::as_Klass(mirror));
270 }
271 
272 bool JfrTraceId::in_jdk_jfr_event_hierarchy(const jclass jc) {
273   assert(jc != NULL, "invariant");
274   const oop mirror = JNIHandles::resolve(jc);
275   assert(mirror != NULL, "invariant");
276   return in_jdk_jfr_event_hierarchy(java_lang_Class::as_Klass(mirror));
277 }
278 
279 bool JfrTraceId::is_jdk_jfr_event_sub(const jclass jc) {
280   assert(jc != NULL, "invariant");
281   const oop mirror = JNIHandles::resolve(jc);
282   assert(mirror != NULL, "invariant");
283   return is_jdk_jfr_event_sub(java_lang_Class::as_Klass(mirror));
284 }
285 
286 bool JfrTraceId::is_jdk_jfr_event(const jclass jc) {
287   assert(jc != NULL, "invariant");
288   const oop mirror = JNIHandles::resolve(jc);
289   assert(mirror != NULL, "invariant");
290   return is_jdk_jfr_event(java_lang_Class::as_Klass(mirror));
291 }
292 
293 bool JfrTraceId::is_event_host(const jclass jc) {
294   assert(jc != NULL, "invariant");
295   const oop mirror = JNIHandles::resolve(jc);
296   assert(mirror != NULL, "invariant");
297   return is_event_host(java_lang_Class::as_Klass(mirror));
298 }
299 
300 void JfrTraceId::tag_as_jdk_jfr_event_sub(const jclass jc) {
301   assert(jc != NULL, "invariant");
302   const oop mirror = JNIHandles::resolve(jc);
303   assert(mirror != NULL, "invariant");
304   const Klass* const k = java_lang_Class::as_Klass(mirror);
305   tag_as_jdk_jfr_event_sub(k);
306   assert(IS_JDK_JFR_EVENT_SUBKLASS(k), "invariant");
307 }
308 
309 void JfrTraceId::tag_as_event_host(const jclass jc) {
310   assert(jc != NULL, "invariant");
311   const oop mirror = JNIHandles::resolve(jc);
312   assert(mirror != NULL, "invariant");
313   const Klass* const k = java_lang_Class::as_Klass(mirror);
314   tag_as_event_host(k);
315   assert(IS_EVENT_HOST_KLASS(k), "invariant");
316 }
--- EOF ---