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