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 "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp"
30 #include "jfr/utilities/jfrTypes.hpp"
31 #include "oops/arrayKlass.inline.hpp"
32 #include "oops/klass.inline.hpp"
33 #include "oops/instanceKlass.inline.hpp"
34 #include "oops/method.hpp"
35 #include "oops/oop.inline.hpp"
36 #include "runtime/atomic.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
atomic_inc(traceid volatile * const dest)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 = *dest;
49 exchange_value = compare_value + 1;
50 } while (Atomic::cmpxchg(dest, compare_value, exchange_value) != compare_value);
51 return exchange_value;
52 }
53
next_class_id()54 static traceid next_class_id() {
55 static volatile traceid class_id_counter = LAST_TYPE_ID + 1; // + 1 is for the void.class primitive
56 return atomic_inc(&class_id_counter) << TRACE_ID_SHIFT;
57 }
58
next_thread_id()59 static traceid next_thread_id() {
60 static volatile traceid thread_id_counter = 0;
61 return atomic_inc(&thread_id_counter);
62 }
63
next_module_id()64 static traceid next_module_id() {
65 static volatile traceid module_id_counter = 0;
66 return atomic_inc(&module_id_counter) << TRACE_ID_SHIFT;
67 }
68
next_package_id()69 static traceid next_package_id() {
70 static volatile traceid package_id_counter = 0;
71 return atomic_inc(&package_id_counter) << TRACE_ID_SHIFT;
72 }
73
next_class_loader_data_id()74 static traceid next_class_loader_data_id() {
75 static volatile traceid cld_id_counter = 0;
76 return atomic_inc(&cld_id_counter) << TRACE_ID_SHIFT;
77 }
78
79 static bool found_jdk_internal_event_klass = false;
80 static bool found_jdk_jfr_event_klass = false;
81
check_klass(const Klass * klass)82 static void check_klass(const Klass* klass) {
83 assert(klass != NULL, "invariant");
84 if (found_jdk_internal_event_klass && found_jdk_jfr_event_klass) {
85 return;
86 }
87 static const Symbol* jdk_internal_event_sym = NULL;
88 if (jdk_internal_event_sym == NULL) {
89 // setup when loading the first TypeArrayKlass (Universe::genesis) hence single threaded invariant
90 jdk_internal_event_sym = SymbolTable::new_permanent_symbol("jdk/internal/event/Event");
91 }
92 assert(jdk_internal_event_sym != NULL, "invariant");
93
94 static const Symbol* jdk_jfr_event_sym = NULL;
95 if (jdk_jfr_event_sym == NULL) {
96 // setup when loading the first TypeArrayKlass (Universe::genesis) hence single threaded invariant
97 jdk_jfr_event_sym = SymbolTable::new_permanent_symbol("jdk/jfr/Event");
98 }
99 assert(jdk_jfr_event_sym != NULL, "invariant");
100 const Symbol* const klass_name = klass->name();
101
102 if (!found_jdk_internal_event_klass) {
103 if (jdk_internal_event_sym == klass_name && klass->class_loader() == NULL) {
104 found_jdk_internal_event_klass = true;
105 JfrTraceId::tag_as_jdk_jfr_event(klass);
106 return;
107 }
108 }
109
110 if (!found_jdk_jfr_event_klass) {
111 if (jdk_jfr_event_sym == klass_name && klass->class_loader() == NULL) {
112 found_jdk_jfr_event_klass = true;
113 JfrTraceId::tag_as_jdk_jfr_event(klass);
114 return;
115 }
116 }
117 }
118
assign(const Klass * klass)119 void JfrTraceId::assign(const Klass* klass) {
120 assert(klass != NULL, "invariant");
121 klass->set_trace_id(next_class_id());
122 check_klass(klass);
123 const Klass* const super = klass->super();
124 if (super == NULL) {
125 return;
126 }
127 if (IS_EVENT_KLASS(super)) {
128 tag_as_jdk_jfr_event_sub(klass);
129 }
130 }
131
assign(const ModuleEntry * module)132 void JfrTraceId::assign(const ModuleEntry* module) {
133 assert(module != NULL, "invariant");
134 module->set_trace_id(next_module_id());
135 }
136
assign(const PackageEntry * package)137 void JfrTraceId::assign(const PackageEntry* package) {
138 assert(package != NULL, "invariant");
139 package->set_trace_id(next_package_id());
140 }
141
assign(const ClassLoaderData * cld)142 void JfrTraceId::assign(const ClassLoaderData* cld) {
143 assert(cld != NULL, "invariant");
144 if (cld->has_class_mirror_holder()) {
145 cld->set_trace_id(0);
146 return;
147 }
148 cld->set_trace_id(next_class_loader_data_id());
149 }
150
assign_primitive_klass_id()151 traceid JfrTraceId::assign_primitive_klass_id() {
152 return next_class_id();
153 }
154
assign_thread_id()155 traceid JfrTraceId::assign_thread_id() {
156 return next_thread_id();
157 }
158
159 // A mirror representing a primitive class (e.g. int.class) has no reified Klass*,
160 // instead it has an associated TypeArrayKlass* (e.g. int[].class).
161 // We can use the TypeArrayKlass* as a proxy for deriving the id of the primitive class.
162 // The exception is the void.class, which has neither a Klass* nor a TypeArrayKlass*.
163 // It will use a reserved constant.
load_primitive(const oop mirror)164 static traceid load_primitive(const oop mirror) {
165 assert(java_lang_Class::is_primitive(mirror), "invariant");
166 const Klass* const tak = java_lang_Class::array_klass_acquire(mirror);
167 traceid id;
168 if (tak == NULL) {
169 // The first klass id is reserved for the void.class
170 id = LAST_TYPE_ID + 1;
171 } else {
172 id = JfrTraceId::load_raw(tak) + 1;
173 }
174 JfrTraceIdEpoch::set_changed_tag_state();
175 return id;
176 }
177
load(jclass jc,bool raw)178 traceid JfrTraceId::load(jclass jc, bool raw /* false */) {
179 assert(jc != NULL, "invariant");
180 assert(JavaThread::current()->thread_state() == _thread_in_vm, "invariant");
181 const oop mirror = JNIHandles::resolve(jc);
182 assert(mirror != NULL, "invariant");
183 const Klass* const k = java_lang_Class::as_Klass(mirror);
184 return k != NULL ? (raw ? load_raw(k) : load(k)) : load_primitive(mirror);
185 }
186
load_raw(jclass jc)187 traceid JfrTraceId::load_raw(jclass jc) {
188 return load(jc, true);
189 }
190
191 // used by CDS / APPCDS as part of "remove_unshareable_info"
remove(const Klass * k)192 void JfrTraceId::remove(const Klass* k) {
193 assert(k != NULL, "invariant");
194 // Mask off and store the event flags.
195 // This mechanism will retain the event specific flags
196 // in the archive, allowing for event flag restoration
197 // when renewing the traceid on klass revival.
198 k->set_trace_id(EVENT_KLASS_MASK(k));
199 }
200
201 // used by CDS / APPCDS as part of "remove_unshareable_info"
remove(const Method * method)202 void JfrTraceId::remove(const Method* method) {
203 assert(method != NULL, "invariant");
204 // Clear all bits.
205 method->set_trace_flags(0);
206 }
207
208 // used by CDS / APPCDS as part of "restore_unshareable_info"
restore(const Klass * k)209 void JfrTraceId::restore(const Klass* k) {
210 assert(k != NULL, "invariant");
211 if (IS_JDK_JFR_EVENT_KLASS(k)) {
212 found_jdk_jfr_event_klass = true;
213 }
214 const traceid event_flags = k->trace_id();
215 // get a fresh traceid and restore the original event flags
216 k->set_trace_id(next_class_id() | event_flags);
217 if (k->is_typeArray_klass()) {
218 // the next id is reserved for the corresponding primitive class
219 next_class_id();
220 }
221 }
222
in_visible_set(const jclass jc)223 bool JfrTraceId::in_visible_set(const jclass jc) {
224 assert(jc != NULL, "invariant");
225 assert(JavaThread::current()->thread_state() == _thread_in_vm, "invariant");
226 const oop mirror = JNIHandles::resolve(jc);
227 assert(mirror != NULL, "invariant");
228 return in_visible_set(java_lang_Class::as_Klass(mirror));
229 }
230
in_jdk_jfr_event_hierarchy(const jclass jc)231 bool JfrTraceId::in_jdk_jfr_event_hierarchy(const jclass jc) {
232 assert(jc != NULL, "invariant");
233 const oop mirror = JNIHandles::resolve(jc);
234 assert(mirror != NULL, "invariant");
235 return in_jdk_jfr_event_hierarchy(java_lang_Class::as_Klass(mirror));
236 }
237
is_jdk_jfr_event_sub(const jclass jc)238 bool JfrTraceId::is_jdk_jfr_event_sub(const jclass jc) {
239 assert(jc != NULL, "invariant");
240 const oop mirror = JNIHandles::resolve(jc);
241 assert(mirror != NULL, "invariant");
242 return is_jdk_jfr_event_sub(java_lang_Class::as_Klass(mirror));
243 }
244
is_jdk_jfr_event(const jclass jc)245 bool JfrTraceId::is_jdk_jfr_event(const jclass jc) {
246 assert(jc != NULL, "invariant");
247 const oop mirror = JNIHandles::resolve(jc);
248 assert(mirror != NULL, "invariant");
249 return is_jdk_jfr_event(java_lang_Class::as_Klass(mirror));
250 }
251
is_event_host(const jclass jc)252 bool JfrTraceId::is_event_host(const jclass jc) {
253 assert(jc != NULL, "invariant");
254 const oop mirror = JNIHandles::resolve(jc);
255 assert(mirror != NULL, "invariant");
256 return is_event_host(java_lang_Class::as_Klass(mirror));
257 }
258
tag_as_jdk_jfr_event_sub(const jclass jc)259 void JfrTraceId::tag_as_jdk_jfr_event_sub(const jclass jc) {
260 assert(jc != NULL, "invariant");
261 const oop mirror = JNIHandles::resolve(jc);
262 assert(mirror != NULL, "invariant");
263 const Klass* const k = java_lang_Class::as_Klass(mirror);
264 tag_as_jdk_jfr_event_sub(k);
265 assert(IS_JDK_JFR_EVENT_SUBKLASS(k), "invariant");
266 }
267
tag_as_event_host(const jclass jc)268 void JfrTraceId::tag_as_event_host(const jclass jc) {
269 assert(jc != NULL, "invariant");
270 const oop mirror = JNIHandles::resolve(jc);
271 assert(mirror != NULL, "invariant");
272 const Klass* const k = java_lang_Class::as_Klass(mirror);
273 tag_as_event_host(k);
274 assert(IS_EVENT_HOST_KLASS(k), "invariant");
275 }
276