diff -r f8ebefc29b79 -r 162867fa0f8d src/hotspot/share/jfr/recorder/checkpoint/types/jfrType.cpp --- 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(os_thread != NULL ? os_thread->thread_id() : (u8)0); + _writer.write(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)0); // java thread id - _writer.write((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);