< prev index next >

src/hotspot/share/jfr/writers/jfrJavaEventWriter.cpp

Print this page

 31 #include "jfr/recorder/storage/jfrStorage.hpp"
 32 #include "jfr/support/jfrThreadId.hpp"
 33 #include "jfr/utilities/jfrTypes.hpp"
 34 #include "jfr/writers/jfrJavaEventWriter.hpp"
 35 #include "memory/iterator.hpp"
 36 #include "oops/instanceKlass.hpp"
 37 #include "oops/oop.inline.hpp"
 38 #include "runtime/fieldDescriptor.inline.hpp"
 39 #include "runtime/handles.inline.hpp"
 40 #include "runtime/jniHandles.inline.hpp"
 41 #include "runtime/thread.inline.hpp"
 42 
 43 static int start_pos_offset = invalid_offset;
 44 static int start_pos_address_offset = invalid_offset;
 45 static int current_pos_offset = invalid_offset;
 46 static int max_pos_offset = invalid_offset;
 47 static int notified_offset = invalid_offset;
 48 static int thread_id_offset = invalid_offset;
 49 static int valid_offset = invalid_offset;
 50 
 51 static bool find_field(InstanceKlass* ik,
 52                        Symbol* name_symbol,
 53                        Symbol* signature_symbol,
 54                        fieldDescriptor* fd,
 55                        bool is_static = false,
 56                        bool allow_super = false) {
 57   if (allow_super || is_static) {
 58     return ik->find_field(name_symbol, signature_symbol, is_static, fd) != NULL;
 59   } else {
 60     return ik->find_local_field(name_symbol, signature_symbol, fd);
 61   }
 62 }
 63 
 64 static void compute_offset(int &dest_offset,
 65                            Klass* klass,
 66                            Symbol* name_symbol,
 67                            Symbol* signature_symbol,
 68                            bool is_static = false, bool allow_super = false) {
 69   fieldDescriptor fd;
 70   InstanceKlass* ik = InstanceKlass::cast(klass);
 71   if (!find_field(ik, name_symbol, signature_symbol, &fd, is_static, allow_super)) {
 72     assert(false, "invariant");
 73   }
 74   dest_offset = fd.offset();
 75 }
 76 
 77 static bool setup_event_writer_offsets(TRAPS) {
 78   const char class_name[] = "jdk/jfr/internal/EventWriter";
 79   Symbol* const k_sym = SymbolTable::new_symbol(class_name);
 80   assert(k_sym != NULL, "invariant");
 81   Klass* klass = SystemDictionary::resolve_or_fail(k_sym, true, CHECK_false);
 82   assert(klass != NULL, "invariant");
 83 
 84   const char start_pos_name[] = "startPosition";
 85   Symbol* const start_pos_sym = SymbolTable::new_symbol(start_pos_name);
 86   assert(start_pos_sym != NULL, "invariant");
 87   assert(invalid_offset == start_pos_offset, "invariant");
 88   compute_offset(start_pos_offset, klass, start_pos_sym, vmSymbols::long_signature());
 89   assert(start_pos_offset != invalid_offset, "invariant");
 90 
 91   const char start_pos_address_name[] = "startPositionAddress";
 92   Symbol* const start_pos_address_sym = SymbolTable::new_symbol(start_pos_address_name);
 93   assert(start_pos_address_sym != NULL, "invariant");
 94   assert(invalid_offset == start_pos_address_offset, "invariant");
 95   compute_offset(start_pos_address_offset, klass, start_pos_address_sym, vmSymbols::long_signature());
 96   assert(start_pos_address_offset != invalid_offset, "invariant");
 97 
 98   const char event_pos_name[] = "currentPosition";
 99   Symbol* const event_pos_sym = SymbolTable::new_symbol(event_pos_name);
100   assert(event_pos_sym != NULL, "invariant");
101   assert(invalid_offset == current_pos_offset, "invariant");
102   compute_offset(current_pos_offset, klass, event_pos_sym,vmSymbols::long_signature());
103   assert(current_pos_offset != invalid_offset, "invariant");
104 
105   const char max_pos_name[] = "maxPosition";
106   Symbol* const max_pos_sym = SymbolTable::new_symbol(max_pos_name);
107   assert(max_pos_sym != NULL, "invariant");
108   assert(invalid_offset == max_pos_offset, "invariant");
109   compute_offset(max_pos_offset, klass, max_pos_sym, vmSymbols::long_signature());
110   assert(max_pos_offset != invalid_offset, "invariant");
111 
112   const char notified_name[] = "notified";
113   Symbol* const notified_sym = SymbolTable::new_symbol(notified_name);
114   assert (notified_sym != NULL, "invariant");
115   assert(invalid_offset == notified_offset, "invariant");
116   compute_offset(notified_offset, klass, notified_sym, vmSymbols::bool_signature());
117   assert(notified_offset != invalid_offset, "invariant");
118 







119   const char valid_name[] = "valid";
120   Symbol* const valid_sym = SymbolTable::new_symbol(valid_name);
121   assert (valid_sym != NULL, "invariant");
122   assert(invalid_offset == valid_offset, "invariant");
123   compute_offset(valid_offset, klass, valid_sym, vmSymbols::bool_signature());
124   assert(valid_offset != invalid_offset, "invariant");
125   return true;
126 }
127 
128 bool JfrJavaEventWriter::initialize() {
129   static bool initialized = false;
130   if (!initialized) {
131     initialized = setup_event_writer_offsets(JavaThread::current());
132   }
133   return initialized;
134 }
135 
136 jboolean JfrJavaEventWriter::flush(jobject writer, jint used, jint requested, JavaThread* jt) {
137   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(jt));
138   assert(writer != NULL, "invariant");
139   oop const w = JNIHandles::resolve_non_null(writer);
140   assert(w != NULL, "invariant");
141   JfrBuffer* const current = jt->jfr_thread_local()->java_buffer();
142   assert(current != NULL, "invariant");
143   JfrBuffer* const buffer = JfrStorage::flush(current, used, requested, false, jt);

194 
195 static jobject create_new_event_writer(JfrBuffer* buffer, TRAPS) {
196   assert(buffer != NULL, "invariant");
197   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));
198   HandleMark hm(THREAD);
199   static const char klass[] = "jdk/jfr/internal/EventWriter";
200   static const char method[] = "<init>";
201   static const char signature[] = "(JJJJZ)V";
202   JavaValue result(T_OBJECT);
203   JfrJavaArguments args(&result, klass, method, signature, CHECK_NULL);
204   // parameters
205   args.push_long((jlong)buffer->pos());
206   args.push_long((jlong)buffer->end());
207   args.push_long((jlong)buffer->pos_address());
208   args.push_long((jlong)JFR_THREAD_ID(THREAD));
209   args.push_int((int)JNI_TRUE);
210   JfrJavaSupport::new_object_global_ref(&args, CHECK_NULL);
211   return result.get_jobject();
212 }
213 
214 jobject JfrJavaEventWriter::event_writer(JavaThread* t) {
215   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(t));
216   JfrThreadLocal* const tl = t->jfr_thread_local();
217   assert(tl->shelved_buffer() == NULL, "invariant");
218   return tl->java_event_writer();











219 }
220 
221 jobject JfrJavaEventWriter::new_event_writer(TRAPS) {
222   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));
223   assert(event_writer(THREAD) == NULL, "invariant");
224   JfrThreadLocal* const tl = THREAD->jfr_thread_local();
225   assert(!tl->has_java_buffer(), "invariant");
226   JfrBuffer* const buffer = tl->java_buffer();
227   if (buffer == NULL) {
228     JfrJavaSupport::throw_out_of_memory_error("OOME for thread local buffer", THREAD);
229     return NULL;
230   }
231   jobject java_event_writer = create_new_event_writer(buffer, CHECK_NULL);
232   tl->set_java_event_writer(java_event_writer);
233   assert(tl->has_java_event_writer(), "invariant");
234   return java_event_writer;
235 }

 31 #include "jfr/recorder/storage/jfrStorage.hpp"
 32 #include "jfr/support/jfrThreadId.hpp"
 33 #include "jfr/utilities/jfrTypes.hpp"
 34 #include "jfr/writers/jfrJavaEventWriter.hpp"
 35 #include "memory/iterator.hpp"
 36 #include "oops/instanceKlass.hpp"
 37 #include "oops/oop.inline.hpp"
 38 #include "runtime/fieldDescriptor.inline.hpp"
 39 #include "runtime/handles.inline.hpp"
 40 #include "runtime/jniHandles.inline.hpp"
 41 #include "runtime/thread.inline.hpp"
 42 
 43 static int start_pos_offset = invalid_offset;
 44 static int start_pos_address_offset = invalid_offset;
 45 static int current_pos_offset = invalid_offset;
 46 static int max_pos_offset = invalid_offset;
 47 static int notified_offset = invalid_offset;
 48 static int thread_id_offset = invalid_offset;
 49 static int valid_offset = invalid_offset;
 50 


























 51 static bool setup_event_writer_offsets(TRAPS) {
 52   const char class_name[] = "jdk/jfr/internal/EventWriter";
 53   Symbol* const k_sym = SymbolTable::new_symbol(class_name);
 54   assert(k_sym != NULL, "invariant");
 55   Klass* klass = SystemDictionary::resolve_or_fail(k_sym, true, CHECK_false);
 56   assert(klass != NULL, "invariant");
 57 
 58   const char start_pos_name[] = "startPosition";
 59   Symbol* const start_pos_sym = SymbolTable::new_symbol(start_pos_name);
 60   assert(start_pos_sym != NULL, "invariant");
 61   assert(invalid_offset == start_pos_offset, "invariant");
 62   JfrJavaSupport::compute_field_offset(start_pos_offset, klass, start_pos_sym, vmSymbols::long_signature());
 63   assert(start_pos_offset != invalid_offset, "invariant");
 64 
 65   const char start_pos_address_name[] = "startPositionAddress";
 66   Symbol* const start_pos_address_sym = SymbolTable::new_symbol(start_pos_address_name);
 67   assert(start_pos_address_sym != NULL, "invariant");
 68   assert(invalid_offset == start_pos_address_offset, "invariant");
 69   JfrJavaSupport::compute_field_offset(start_pos_address_offset, klass, start_pos_address_sym, vmSymbols::long_signature());
 70   assert(start_pos_address_offset != invalid_offset, "invariant");
 71 
 72   const char event_pos_name[] = "currentPosition";
 73   Symbol* const event_pos_sym = SymbolTable::new_symbol(event_pos_name);
 74   assert(event_pos_sym != NULL, "invariant");
 75   assert(invalid_offset == current_pos_offset, "invariant");
 76   JfrJavaSupport::compute_field_offset(current_pos_offset, klass, event_pos_sym,vmSymbols::long_signature());
 77   assert(current_pos_offset != invalid_offset, "invariant");
 78 
 79   const char max_pos_name[] = "maxPosition";
 80   Symbol* const max_pos_sym = SymbolTable::new_symbol(max_pos_name);
 81   assert(max_pos_sym != NULL, "invariant");
 82   assert(invalid_offset == max_pos_offset, "invariant");
 83   JfrJavaSupport::compute_field_offset(max_pos_offset, klass, max_pos_sym, vmSymbols::long_signature());
 84   assert(max_pos_offset != invalid_offset, "invariant");
 85 
 86   const char notified_name[] = "notified";
 87   Symbol* const notified_sym = SymbolTable::new_symbol(notified_name);
 88   assert (notified_sym != NULL, "invariant");
 89   assert(invalid_offset == notified_offset, "invariant");
 90   JfrJavaSupport::compute_field_offset(notified_offset, klass, notified_sym, vmSymbols::bool_signature());
 91   assert(notified_offset != invalid_offset, "invariant");
 92 
 93   const char threadID_name[] = "threadID";
 94   Symbol * const threadID_sym = SymbolTable::new_symbol(threadID_name);
 95   assert(threadID_sym != NULL, "invariant");
 96   assert(invalid_offset == thread_id_offset, "invariant");
 97   JfrJavaSupport::compute_field_offset(thread_id_offset, klass, threadID_sym, vmSymbols::long_signature());
 98   assert(thread_id_offset != invalid_offset, "invariant");
 99 
100   const char valid_name[] = "valid";
101   Symbol* const valid_sym = SymbolTable::new_symbol(valid_name);
102   assert (valid_sym != NULL, "invariant");
103   assert(invalid_offset == valid_offset, "invariant");
104   JfrJavaSupport::compute_field_offset(valid_offset, klass, valid_sym, vmSymbols::bool_signature());
105   assert(valid_offset != invalid_offset, "invariant");
106   return true;
107 }
108 
109 bool JfrJavaEventWriter::initialize() {
110   static bool initialized = false;
111   if (!initialized) {
112     initialized = setup_event_writer_offsets(JavaThread::current());
113   }
114   return initialized;
115 }
116 
117 jboolean JfrJavaEventWriter::flush(jobject writer, jint used, jint requested, JavaThread* jt) {
118   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(jt));
119   assert(writer != NULL, "invariant");
120   oop const w = JNIHandles::resolve_non_null(writer);
121   assert(w != NULL, "invariant");
122   JfrBuffer* const current = jt->jfr_thread_local()->java_buffer();
123   assert(current != NULL, "invariant");
124   JfrBuffer* const buffer = JfrStorage::flush(current, used, requested, false, jt);

175 
176 static jobject create_new_event_writer(JfrBuffer* buffer, TRAPS) {
177   assert(buffer != NULL, "invariant");
178   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));
179   HandleMark hm(THREAD);
180   static const char klass[] = "jdk/jfr/internal/EventWriter";
181   static const char method[] = "<init>";
182   static const char signature[] = "(JJJJZ)V";
183   JavaValue result(T_OBJECT);
184   JfrJavaArguments args(&result, klass, method, signature, CHECK_NULL);
185   // parameters
186   args.push_long((jlong)buffer->pos());
187   args.push_long((jlong)buffer->end());
188   args.push_long((jlong)buffer->pos_address());
189   args.push_long((jlong)JFR_THREAD_ID(THREAD));
190   args.push_int((int)JNI_TRUE);
191   JfrJavaSupport::new_object_global_ref(&args, CHECK_NULL);
192   return result.get_jobject();
193 }
194 
195 jobject JfrJavaEventWriter::event_writer(JavaThread* t, traceid tid /* 0 */) {
196   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(t));
197   JfrThreadLocal* const tl = t->jfr_thread_local();
198   assert(tl->shelved_buffer() == NULL, "invariant");
199   jobject h_writer = tl->java_event_writer();
200   if (h_writer != NULL) {
201     oop writer = JNIHandles::resolve_non_null(h_writer);
202     assert(writer != NULL, "invariant");
203     // primarily for supporting Virtual Threads
204     const jlong event_writer_tid = writer->long_field(thread_id_offset);
205     const jlong current_tid = tid != 0 ? tid : (jlong)JFR_THREAD_ID(t);
206     if (event_writer_tid != current_tid) {
207       writer->long_field_put(thread_id_offset, current_tid);
208     }
209   }
210   return h_writer;
211 }
212 
213 jobject JfrJavaEventWriter::new_event_writer(TRAPS) {
214   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));
215   assert(event_writer(THREAD) == NULL, "invariant");
216   JfrThreadLocal* const tl = THREAD->jfr_thread_local();
217   assert(!tl->has_java_buffer(), "invariant");
218   JfrBuffer* const buffer = tl->java_buffer();
219   if (buffer == NULL) {
220     JfrJavaSupport::throw_out_of_memory_error("OOME for thread local buffer", THREAD);
221     return NULL;
222   }
223   jobject h_writer = create_new_event_writer(buffer, CHECK_NULL);
224   tl->set_java_event_writer(h_writer);
225   assert(tl->has_java_event_writer(), "invariant");
226   return h_writer;
227 }
< prev index next >