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