< prev index next >

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

Print this page
@@ -46,36 +46,10 @@
  static int max_pos_offset = invalid_offset;
  static int notified_offset = invalid_offset;
  static int thread_id_offset = invalid_offset;
  static int valid_offset = invalid_offset;
  
- static bool find_field(InstanceKlass* ik,
-                        Symbol* name_symbol,
-                        Symbol* signature_symbol,
-                        fieldDescriptor* fd,
-                        bool is_static = false,
-                        bool allow_super = false) {
-   if (allow_super || is_static) {
-     return ik->find_field(name_symbol, signature_symbol, is_static, fd) != NULL;
-   } else {
-     return ik->find_local_field(name_symbol, signature_symbol, fd);
-   }
- }
- 
- static void compute_offset(int &dest_offset,
-                            Klass* klass,
-                            Symbol* name_symbol,
-                            Symbol* signature_symbol,
-                            bool is_static = false, bool allow_super = false) {
-   fieldDescriptor fd;
-   InstanceKlass* ik = InstanceKlass::cast(klass);
-   if (!find_field(ik, name_symbol, signature_symbol, &fd, is_static, allow_super)) {
-     assert(false, "invariant");
-   }
-   dest_offset = fd.offset();
- }
- 
  static bool setup_event_writer_offsets(TRAPS) {
    const char class_name[] = "jdk/jfr/internal/EventWriter";
    Symbol* const k_sym = SymbolTable::new_symbol(class_name);
    assert(k_sym != NULL, "invariant");
    Klass* klass = SystemDictionary::resolve_or_fail(k_sym, true, CHECK_false);

@@ -83,46 +57,53 @@
  
    const char start_pos_name[] = "startPosition";
    Symbol* const start_pos_sym = SymbolTable::new_symbol(start_pos_name);
    assert(start_pos_sym != NULL, "invariant");
    assert(invalid_offset == start_pos_offset, "invariant");
-   compute_offset(start_pos_offset, klass, start_pos_sym, vmSymbols::long_signature());
+   JfrJavaSupport::compute_field_offset(start_pos_offset, klass, start_pos_sym, vmSymbols::long_signature());
    assert(start_pos_offset != invalid_offset, "invariant");
  
    const char start_pos_address_name[] = "startPositionAddress";
    Symbol* const start_pos_address_sym = SymbolTable::new_symbol(start_pos_address_name);
    assert(start_pos_address_sym != NULL, "invariant");
    assert(invalid_offset == start_pos_address_offset, "invariant");
-   compute_offset(start_pos_address_offset, klass, start_pos_address_sym, vmSymbols::long_signature());
+   JfrJavaSupport::compute_field_offset(start_pos_address_offset, klass, start_pos_address_sym, vmSymbols::long_signature());
    assert(start_pos_address_offset != invalid_offset, "invariant");
  
    const char event_pos_name[] = "currentPosition";
    Symbol* const event_pos_sym = SymbolTable::new_symbol(event_pos_name);
    assert(event_pos_sym != NULL, "invariant");
    assert(invalid_offset == current_pos_offset, "invariant");
-   compute_offset(current_pos_offset, klass, event_pos_sym,vmSymbols::long_signature());
+   JfrJavaSupport::compute_field_offset(current_pos_offset, klass, event_pos_sym,vmSymbols::long_signature());
    assert(current_pos_offset != invalid_offset, "invariant");
  
    const char max_pos_name[] = "maxPosition";
    Symbol* const max_pos_sym = SymbolTable::new_symbol(max_pos_name);
    assert(max_pos_sym != NULL, "invariant");
    assert(invalid_offset == max_pos_offset, "invariant");
-   compute_offset(max_pos_offset, klass, max_pos_sym, vmSymbols::long_signature());
+   JfrJavaSupport::compute_field_offset(max_pos_offset, klass, max_pos_sym, vmSymbols::long_signature());
    assert(max_pos_offset != invalid_offset, "invariant");
  
    const char notified_name[] = "notified";
    Symbol* const notified_sym = SymbolTable::new_symbol(notified_name);
    assert (notified_sym != NULL, "invariant");
    assert(invalid_offset == notified_offset, "invariant");
-   compute_offset(notified_offset, klass, notified_sym, vmSymbols::bool_signature());
+   JfrJavaSupport::compute_field_offset(notified_offset, klass, notified_sym, vmSymbols::bool_signature());
    assert(notified_offset != invalid_offset, "invariant");
  
+   const char threadID_name[] = "threadID";
+   Symbol * const threadID_sym = SymbolTable::new_symbol(threadID_name);
+   assert(threadID_sym != NULL, "invariant");
+   assert(invalid_offset == thread_id_offset, "invariant");
+   JfrJavaSupport::compute_field_offset(thread_id_offset, klass, threadID_sym, vmSymbols::long_signature());
+   assert(thread_id_offset != invalid_offset, "invariant");
+ 
    const char valid_name[] = "valid";
    Symbol* const valid_sym = SymbolTable::new_symbol(valid_name);
    assert (valid_sym != NULL, "invariant");
    assert(invalid_offset == valid_offset, "invariant");
-   compute_offset(valid_offset, klass, valid_sym, vmSymbols::bool_signature());
+   JfrJavaSupport::compute_field_offset(valid_offset, klass, valid_sym, vmSymbols::bool_signature());
    assert(valid_offset != invalid_offset, "invariant");
    return true;
  }
  
  bool JfrJavaEventWriter::initialize() {

@@ -209,15 +190,26 @@
    args.push_int((int)JNI_TRUE);
    JfrJavaSupport::new_object_global_ref(&args, CHECK_NULL);
    return result.get_jobject();
  }
  
- jobject JfrJavaEventWriter::event_writer(JavaThread* t) {
+ jobject JfrJavaEventWriter::event_writer(JavaThread* t, traceid tid /* 0 */) {
    DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(t));
    JfrThreadLocal* const tl = t->jfr_thread_local();
    assert(tl->shelved_buffer() == NULL, "invariant");
-   return tl->java_event_writer();
+   jobject h_writer = tl->java_event_writer();
+   if (h_writer != NULL) {
+     oop writer = JNIHandles::resolve_non_null(h_writer);
+     assert(writer != NULL, "invariant");
+     // primarily for supporting Virtual Threads
+     const jlong event_writer_tid = writer->long_field(thread_id_offset);
+     const jlong current_tid = tid != 0 ? tid : (jlong)JFR_THREAD_ID(t);
+     if (event_writer_tid != current_tid) {
+       writer->long_field_put(thread_id_offset, current_tid);
+     }
+   }
+   return h_writer;
  }
  
  jobject JfrJavaEventWriter::new_event_writer(TRAPS) {
    DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));
    assert(event_writer(THREAD) == NULL, "invariant");

@@ -226,10 +218,10 @@
    JfrBuffer* const buffer = tl->java_buffer();
    if (buffer == NULL) {
      JfrJavaSupport::throw_out_of_memory_error("OOME for thread local buffer", THREAD);
      return NULL;
    }
-   jobject java_event_writer = create_new_event_writer(buffer, CHECK_NULL);
-   tl->set_java_event_writer(java_event_writer);
+   jobject h_writer = create_new_event_writer(buffer, CHECK_NULL);
+   tl->set_java_event_writer(h_writer);
    assert(tl->has_java_event_writer(), "invariant");
-   return java_event_writer;
+   return h_writer;
  }
< prev index next >