8203943: eventThreadGroup was null in TestJavaBlockedEvent.java
authormgronlun
Sun, 08 Jul 2018 11:54:08 +0200
changeset 51004 162867fa0f8d
parent 51003 f8ebefc29b79
child 51005 0083d474b0e1
8203943: eventThreadGroup was null in TestJavaBlockedEvent.java Reviewed-by: egahlin
src/hotspot/share/jfr/jfr.cpp
src/hotspot/share/jfr/recorder/checkpoint/types/jfrType.cpp
src/hotspot/share/jfr/support/jfrThreadLocal.cpp
src/hotspot/share/jfr/support/jfrThreadLocal.hpp
--- a/src/hotspot/share/jfr/jfr.cpp	Fri Jul 06 12:10:43 2018 -0700
+++ b/src/hotspot/share/jfr/jfr.cpp	Sun Jul 08 11:54:08 2018 +0200
@@ -26,10 +26,10 @@
 #include "jfr/jfr.hpp"
 #include "jfr/leakprofiler/leakProfiler.hpp"
 #include "jfr/periodic/sampling/jfrThreadSampler.hpp"
-#include "jfr/recorder/service/jfrOptionSet.hpp"
 #include "jfr/recorder/jfrRecorder.hpp"
 #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
 #include "jfr/recorder/repository/jfrEmergencyDump.hpp"
+#include "jfr/recorder/service/jfrOptionSet.hpp"
 #include "jfr/support/jfrThreadLocal.hpp"
 #include "runtime/java.hpp"
 
@@ -64,9 +64,7 @@
 }
 
 void Jfr::on_thread_exit(JavaThread* thread) {
-  if (JfrRecorder::is_recording()) {
-    JfrThreadLocal::on_exit(thread);
-  }
+  JfrThreadLocal::on_exit(thread);
 }
 
 void Jfr::on_thread_destruct(Thread* thread) {
--- a/src/hotspot/share/jfr/recorder/checkpoint/types/jfrType.cpp	Fri Jul 06 12:10:43 2018 -0700
+++ b/src/hotspot/share/jfr/recorder/checkpoint/types/jfrType.cpp	Sun Jul 08 11:54:08 2018 +0200
@@ -60,22 +60,32 @@
 #include "gc/g1/g1YCTypes.hpp"
 #endif
 
-class JfrCheckpointThreadCountClosure : public ThreadClosure {
-private:
-  u4 _total_threads;
-public:
-  JfrCheckpointThreadCountClosure() : _total_threads(0) {}
-  u4 total_threads() { return _total_threads; }
-  void do_thread(Thread *t) { _total_threads++; }
-};
-
 // Requires a ResourceMark for get_thread_name/as_utf8
 class JfrCheckpointThreadClosure : public ThreadClosure {
  private:
   JfrCheckpointWriter& _writer;
-  Thread* _curthread;
+  JfrCheckpointContext _ctx;
+  const intptr_t _count_position;
+  Thread* const _curthread;
+  u4 _count;
+
  public:
-  JfrCheckpointThreadClosure(JfrCheckpointWriter& writer) : _writer(writer), _curthread(Thread::current()) {}
+  JfrCheckpointThreadClosure(JfrCheckpointWriter& writer) : _writer(writer),
+                                                            _ctx(writer.context()),
+                                                            _count_position(writer.reserve(sizeof(u4))),
+                                                            _curthread(Thread::current()),
+                                                            _count(0) {
+  }
+
+  ~JfrCheckpointThreadClosure() {
+    if (_count == 0) {
+      // restore
+      _writer.set_context(_ctx);
+      return;
+    }
+    _writer.write_count(_count, _count_position);
+  }
+
   void do_thread(Thread* t);
 };
 
@@ -83,10 +93,16 @@
 void JfrCheckpointThreadClosure::do_thread(Thread* t) {
   assert(t != NULL, "invariant");
   assert_locked_or_safepoint(Threads_lock);
-  _writer.write_key(t->jfr_thread_local()->thread_id());
+  const JfrThreadLocal* const tl = t->jfr_thread_local();
+  assert(tl != NULL, "invariant");
+  if (tl->is_dead()) {
+    return;
+  }
+  ++_count;
+  _writer.write_key(tl->thread_id());
   _writer.write(t->name());
   const OSThread* const os_thread = t->osthread();
-  _writer.write<traceid>(os_thread != NULL ? os_thread->thread_id() : (u8)0);
+  _writer.write<traceid>(os_thread != NULL ? os_thread->thread_id() : 0);
   if (t->is_Java_thread()) {
     JavaThread* const jt = (JavaThread*)t;
     _writer.write(jt->name());
@@ -97,17 +113,12 @@
     return;
   }
   _writer.write((const char*)NULL); // java name
-  _writer.write<traceid>((traceid)0); // java thread id
-  _writer.write<traceid>((traceid)0); // java thread group
+  _writer.write((traceid)0); // java thread id
+  _writer.write((traceid)0); // java thread group
 }
 
 void JfrThreadConstantSet::serialize(JfrCheckpointWriter& writer) {
   assert(SafepointSynchronize::is_at_safepoint(), "invariant");
-  JfrCheckpointThreadCountClosure tcc;
-  Threads::threads_do(&tcc);
-  const u4 total_threads = tcc.total_threads();
-  // THREADS
-  writer.write_count(total_threads);
   JfrCheckpointThreadClosure tc(writer);
   Threads::threads_do(&tc);
 }
@@ -334,7 +345,7 @@
   writer.write_count(1);
   writer.write_key(_thread->jfr_thread_local()->thread_id());
   writer.write(thread_name);
-  writer.write((u8)_thread->osthread()->thread_id());
+  writer.write((traceid)_thread->osthread()->thread_id());
   writer.write(thread_name);
   writer.write(java_lang_thread_id);
   writer.write(thread_group_id);
--- a/src/hotspot/share/jfr/support/jfrThreadLocal.cpp	Fri Jul 06 12:10:43 2018 -0700
+++ b/src/hotspot/share/jfr/support/jfrThreadLocal.cpp	Sun Jul 08 11:54:08 2018 +0200
@@ -23,8 +23,9 @@
  */
 
 #include "precompiled.hpp"
+#include "jfr/jni/jfrJavaSupport.hpp"
 #include "jfr/periodic/jfrThreadCPULoadEvent.hpp"
-#include "jfr/jni/jfrJavaSupport.hpp"
+#include "jfr/recorder/jfrRecorder.hpp"
 #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp"
 #include "jfr/recorder/service/jfrOptionSet.hpp"
@@ -51,7 +52,8 @@
   _wallclock_time(os::javaTimeNanos()),
   _stack_trace_hash(0),
   _stackdepth(0),
-  _entering_suspend_flag(0) {}
+  _entering_suspend_flag(0),
+  _dead(false) {}
 
 u8 JfrThreadLocal::add_data_lost(u8 value) {
   _data_lost += value;
@@ -71,9 +73,17 @@
   return _thread_cp;
 }
 
+void JfrThreadLocal::set_dead() {
+  assert(!is_dead(), "invariant");
+  _dead = true;
+}
+
 void JfrThreadLocal::on_exit(JavaThread* thread) {
-  JfrCheckpointManager::write_thread_checkpoint(thread);
-  JfrThreadCPULoadEvent::send_event_for_thread(thread);
+  if (JfrRecorder::is_recording()) {
+    JfrCheckpointManager::write_thread_checkpoint(thread);
+    JfrThreadCPULoadEvent::send_event_for_thread(thread);
+  }
+  thread->jfr_thread_local()->set_dead();
 }
 
 void JfrThreadLocal::on_destruct(Thread* thread) {
--- a/src/hotspot/share/jfr/support/jfrThreadLocal.hpp	Fri Jul 06 12:10:43 2018 -0700
+++ b/src/hotspot/share/jfr/support/jfrThreadLocal.hpp	Sun Jul 08 11:54:08 2018 +0200
@@ -50,11 +50,14 @@
   unsigned int _stack_trace_hash;
   mutable u4 _stackdepth;
   volatile jint _entering_suspend_flag;
+  bool _dead;
 
   JfrBuffer* install_native_buffer() const;
   JfrBuffer* install_java_buffer() const;
   JfrStackFrame* install_stackframes() const;
 
+  void set_dead();
+
  public:
   JfrThreadLocal();
 
@@ -202,6 +205,10 @@
     _trace_id = id;
   }
 
+  bool is_dead() const {
+    return _dead;
+  }
+
   bool has_thread_checkpoint() const;
   void set_thread_checkpoint(const JfrCheckpointBlobHandle& handle);
   const JfrCheckpointBlobHandle& thread_checkpoint() const;