1 /*
2 * Copyright (c) 2011, 2020, 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;
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_thread_id()151 traceid JfrTraceId::assign_thread_id() {
152 return next_thread_id();
153 }
154
load_raw(jclass jc)155 traceid JfrTraceId::load_raw(jclass jc) {
156 assert(jc != NULL, "invariant");
157 assert(JavaThread::current()->thread_state() == _thread_in_vm, "invariant");
158 const oop my_oop = JNIHandles::resolve(jc);
159 assert(my_oop != NULL, "invariant");
160 return load_raw(java_lang_Class::as_Klass(my_oop));
161 }
162
163 // used by CDS / APPCDS as part of "remove_unshareable_info"
remove(const Klass * k)164 void JfrTraceId::remove(const Klass* k) {
165 assert(k != NULL, "invariant");
166 // Mask off and store the event flags.
167 // This mechanism will retain the event specific flags
168 // in the archive, allowing for event flag restoration
169 // when renewing the traceid on klass revival.
170 k->set_trace_id(EVENT_KLASS_MASK(k));
171 }
172
173 // used by CDS / APPCDS as part of "remove_unshareable_info"
remove(const Method * method)174 void JfrTraceId::remove(const Method* method) {
175 assert(method != NULL, "invariant");
176 // Clear all bits.
177 method->set_trace_flags(0);
178 }
179
180 // used by CDS / APPCDS as part of "restore_unshareable_info"
restore(const Klass * k)181 void JfrTraceId::restore(const Klass* k) {
182 assert(k != NULL, "invariant");
183 if (IS_JDK_JFR_EVENT_KLASS(k)) {
184 found_jdk_jfr_event_klass = true;
185 }
186 const traceid event_flags = k->trace_id();
187 // get a fresh traceid and restore the original event flags
188 k->set_trace_id(next_class_id() | event_flags);
189 }
190
in_visible_set(const jclass jc)191 bool JfrTraceId::in_visible_set(const jclass jc) {
192 assert(jc != NULL, "invariant");
193 assert(JavaThread::current()->thread_state() == _thread_in_vm, "invariant");
194 const oop mirror = JNIHandles::resolve(jc);
195 assert(mirror != NULL, "invariant");
196 return in_visible_set(java_lang_Class::as_Klass(mirror));
197 }
198
in_jdk_jfr_event_hierarchy(const jclass jc)199 bool JfrTraceId::in_jdk_jfr_event_hierarchy(const jclass jc) {
200 assert(jc != NULL, "invariant");
201 const oop mirror = JNIHandles::resolve(jc);
202 assert(mirror != NULL, "invariant");
203 return in_jdk_jfr_event_hierarchy(java_lang_Class::as_Klass(mirror));
204 }
205
is_jdk_jfr_event_sub(const jclass jc)206 bool JfrTraceId::is_jdk_jfr_event_sub(const jclass jc) {
207 assert(jc != NULL, "invariant");
208 const oop mirror = JNIHandles::resolve(jc);
209 assert(mirror != NULL, "invariant");
210 return is_jdk_jfr_event_sub(java_lang_Class::as_Klass(mirror));
211 }
212
is_jdk_jfr_event(const jclass jc)213 bool JfrTraceId::is_jdk_jfr_event(const jclass jc) {
214 assert(jc != NULL, "invariant");
215 const oop mirror = JNIHandles::resolve(jc);
216 assert(mirror != NULL, "invariant");
217 return is_jdk_jfr_event(java_lang_Class::as_Klass(mirror));
218 }
219
is_event_host(const jclass jc)220 bool JfrTraceId::is_event_host(const jclass jc) {
221 assert(jc != NULL, "invariant");
222 const oop mirror = JNIHandles::resolve(jc);
223 assert(mirror != NULL, "invariant");
224 return is_event_host(java_lang_Class::as_Klass(mirror));
225 }
226
tag_as_jdk_jfr_event_sub(const jclass jc)227 void JfrTraceId::tag_as_jdk_jfr_event_sub(const jclass jc) {
228 assert(jc != NULL, "invariant");
229 const oop mirror = JNIHandles::resolve(jc);
230 assert(mirror != NULL, "invariant");
231 const Klass* const k = java_lang_Class::as_Klass(mirror);
232 tag_as_jdk_jfr_event_sub(k);
233 assert(IS_JDK_JFR_EVENT_SUBKLASS(k), "invariant");
234 }
235
tag_as_event_host(const jclass jc)236 void JfrTraceId::tag_as_event_host(const jclass jc) {
237 assert(jc != NULL, "invariant");
238 const oop mirror = JNIHandles::resolve(jc);
239 assert(mirror != NULL, "invariant");
240 const Klass* const k = java_lang_Class::as_Klass(mirror);
241 tag_as_event_host(k);
242 assert(IS_EVENT_HOST_KLASS(k), "invariant");
243 }
244