From 92cfe74509010496feb367ef20e7dcf7c43546d6 Mon Sep 17 00:00:00 2001 From: Denghui Dong Date: Fri, 25 Sep 2020 11:17:59 +0800 Subject: [PATCH] [JFR] Fixed missing Threads_lock protection when traversing thread list Summary: TRACE_REQUEST_FUNC(ThreadAllocationStatistics) in jfrPeriodic.cpp void JfrCheckpointThreadClosure::do_thread(Thread* t) in jfrType.cpp Test Plan: jdk/test/jdk/jfr Reviewed-by: kelthuzadx, zhengxiaolinX Issue: https://github.com/alibaba/dragonwell8/issues/134 --- src/share/vm/jfr/periodic/jfrPeriodic.cpp | 17 +++++++++++------ .../jfr/recorder/checkpoint/types/jfrType.cpp | 1 + 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/share/vm/jfr/periodic/jfrPeriodic.cpp b/src/share/vm/jfr/periodic/jfrPeriodic.cpp index 67a6d614c..af2df2a7e 100644 --- a/src/share/vm/jfr/periodic/jfrPeriodic.cpp +++ b/src/share/vm/jfr/periodic/jfrPeriodic.cpp @@ -395,12 +395,17 @@ TRACE_REQUEST_FUNC(ThreadAllocationStatistics) { GrowableArray allocated(initial_size); GrowableArray thread_ids(initial_size); JfrTicks time_stamp = JfrTicks::now(); - JfrJavaThreadIterator iter; - while (iter.has_next()) { - JavaThread* const jt = iter.next(); - assert(jt != NULL, "invariant"); - allocated.append(jt->cooked_allocated_bytes()); - thread_ids.append(JFR_THREAD_ID(jt)); + + { + // Collect allocation statistics while holding threads lock + MutexLockerEx ml(Threads_lock); + JfrJavaThreadIterator iter; + while (iter.has_next()) { + JavaThread* const jt = iter.next(); + assert(jt != NULL, "invariant"); + allocated.append(jt->cooked_allocated_bytes()); + thread_ids.append(JFR_THREAD_ID(jt)); + } } // Write allocation statistics to buffer. diff --git a/src/share/vm/jfr/recorder/checkpoint/types/jfrType.cpp b/src/share/vm/jfr/recorder/checkpoint/types/jfrType.cpp index 2079fcb28..7b6024f75 100644 --- a/src/share/vm/jfr/recorder/checkpoint/types/jfrType.cpp +++ b/src/share/vm/jfr/recorder/checkpoint/types/jfrType.cpp @@ -108,6 +108,7 @@ void JfrCheckpointThreadClosure::do_thread(Thread* t) { void JfrThreadConstantSet::serialize(JfrCheckpointWriter& writer) { JfrCheckpointThreadClosure tc(writer); + MutexLockerEx ml(Threads_lock); JfrJavaThreadIterator javathreads; while (javathreads.has_next()) { tc.do_thread(javathreads.next()); -- GitLab