Wed, 19 Sep 2012 15:24:32 +0100
7196045: Possible JVM deadlock in ThreadTimesClosure when using HotspotInternal non-public API.
Reviewed-by: sspitsyn, dholmes
src/share/vm/services/management.cpp | file | annotate | diff | comparison | revisions | |
test/runtime/7196045/Test7196045.java | file | annotate | diff | comparison | revisions |
1.1 --- a/src/share/vm/services/management.cpp Tue Sep 18 11:37:26 2012 -0700 1.2 +++ b/src/share/vm/services/management.cpp Wed Sep 19 15:24:32 2012 +0100 1.3 @@ -1804,31 +1804,37 @@ 1.4 1.5 class ThreadTimesClosure: public ThreadClosure { 1.6 private: 1.7 - objArrayOop _names; 1.8 + objArrayHandle _names_strings; 1.9 + char **_names_chars; 1.10 typeArrayOop _times; 1.11 int _names_len; 1.12 int _times_len; 1.13 int _count; 1.14 1.15 public: 1.16 - ThreadTimesClosure(objArrayOop names, typeArrayOop times); 1.17 + ThreadTimesClosure(objArrayHandle names, typeArrayOop times); 1.18 + ~ThreadTimesClosure(); 1.19 virtual void do_thread(Thread* thread); 1.20 + void do_unlocked(); 1.21 int count() { return _count; } 1.22 }; 1.23 1.24 -ThreadTimesClosure::ThreadTimesClosure(objArrayOop names, 1.25 +ThreadTimesClosure::ThreadTimesClosure(objArrayHandle names, 1.26 typeArrayOop times) { 1.27 - assert(names != NULL, "names was NULL"); 1.28 + assert(names() != NULL, "names was NULL"); 1.29 assert(times != NULL, "times was NULL"); 1.30 - _names = names; 1.31 + _names_strings = names; 1.32 _names_len = names->length(); 1.33 + _names_chars = NEW_C_HEAP_ARRAY(char*, _names_len, mtInternal); 1.34 _times = times; 1.35 _times_len = times->length(); 1.36 _count = 0; 1.37 } 1.38 1.39 +// 1.40 +// Called with Threads_lock held 1.41 +// 1.42 void ThreadTimesClosure::do_thread(Thread* thread) { 1.43 - Handle s; 1.44 assert(thread != NULL, "thread was NULL"); 1.45 1.46 // exclude externally visible JavaThreads 1.47 @@ -1842,16 +1848,32 @@ 1.48 } 1.49 1.50 EXCEPTION_MARK; 1.51 + ResourceMark rm(THREAD); // thread->name() uses ResourceArea 1.52 1.53 assert(thread->name() != NULL, "All threads should have a name"); 1.54 - s = java_lang_String::create_from_str(thread->name(), CHECK); 1.55 - _names->obj_at_put(_count, s()); 1.56 - 1.57 + _names_chars[_count] = strdup(thread->name()); 1.58 _times->long_at_put(_count, os::is_thread_cpu_time_supported() ? 1.59 os::thread_cpu_time(thread) : -1); 1.60 _count++; 1.61 } 1.62 1.63 +// Called without Threads_lock, we can allocate String objects. 1.64 +void ThreadTimesClosure::do_unlocked() { 1.65 + 1.66 + EXCEPTION_MARK; 1.67 + for (int i = 0; i < _count; i++) { 1.68 + Handle s = java_lang_String::create_from_str(_names_chars[i], CHECK); 1.69 + _names_strings->obj_at_put(i, s()); 1.70 + } 1.71 +} 1.72 + 1.73 +ThreadTimesClosure::~ThreadTimesClosure() { 1.74 + for (int i = 0; i < _count; i++) { 1.75 + free(_names_chars[i]); 1.76 + } 1.77 + FREE_C_HEAP_ARRAY(char *, _names_chars, mtInternal); 1.78 +} 1.79 + 1.80 // Fills names with VM internal thread names and times with the corresponding 1.81 // CPU times. If names or times is NULL, a NullPointerException is thrown. 1.82 // If the element type of names is not String, an IllegalArgumentException is 1.83 @@ -1878,12 +1900,12 @@ 1.84 typeArrayOop ta = typeArrayOop(JNIHandles::resolve_non_null(times)); 1.85 typeArrayHandle times_ah(THREAD, ta); 1.86 1.87 - ThreadTimesClosure ttc(names_ah(), times_ah()); 1.88 + ThreadTimesClosure ttc(names_ah, times_ah()); 1.89 { 1.90 MutexLockerEx ml(Threads_lock); 1.91 Threads::threads_do(&ttc); 1.92 } 1.93 - 1.94 + ttc.do_unlocked(); 1.95 return ttc.count(); 1.96 JVM_END 1.97
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/runtime/7196045/Test7196045.java Wed Sep 19 15:24:32 2012 +0100 2.3 @@ -0,0 +1,78 @@ 2.4 +/* 2.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + * 2.26 + */ 2.27 + 2.28 +/* 2.29 + * @test 2.30 + * @bug 7196045 2.31 + * @summary Possible JVM deadlock in ThreadTimesClosure when using HotspotInternal non-public API. 2.32 + * @run main/othervm 2.33 + */ 2.34 + 2.35 +import java.lang.management.ManagementFactory; 2.36 +import javax.management.JMException; 2.37 +import javax.management.MBeanServer; 2.38 +import javax.management.MalformedObjectNameException; 2.39 +import javax.management.ObjectName; 2.40 + 2.41 +public class Test7196045 { 2.42 + 2.43 + public static long duration = 1000 * 60 * 2; 2.44 + private static final String HOTSPOT_INTERNAL = "sun.management:type=HotspotInternal"; 2.45 + 2.46 + public static void main(String[] args) { 2.47 + 2.48 + MBeanServer server = ManagementFactory.getPlatformMBeanServer(); 2.49 + ObjectName objName= null; 2.50 + try { 2.51 + ObjectName hotspotInternal = new ObjectName(HOTSPOT_INTERNAL); 2.52 + try { 2.53 + server.registerMBean(new sun.management.HotspotInternal(), hotspotInternal); 2.54 + } catch (JMException e) { 2.55 + throw new RuntimeException("HotSpotWatcher: Failed to register the HotspotInternal MBean" + e); 2.56 + } 2.57 + objName= new ObjectName("sun.management:type=HotspotThreading"); 2.58 + 2.59 + } catch (MalformedObjectNameException e1) { 2.60 + throw new RuntimeException("Bad object name" + e1); 2.61 + } 2.62 + 2.63 + long endTime = System.currentTimeMillis() + duration; 2.64 + long i = 0; 2.65 + while (true) { 2.66 + try { 2.67 + server.getAttribute(objName, "InternalThreadCpuTimes"); 2.68 + } catch (Exception ex) { 2.69 + System.err.println("Exception while getting attribute: " + ex); 2.70 + } 2.71 + i++; 2.72 + if (i % 10000 == 0) { 2.73 + System.out.println("Successful iterations: " + i); 2.74 + } 2.75 + if (System.currentTimeMillis() > endTime) { 2.76 + break; 2.77 + } 2.78 + } 2.79 + System.out.println("PASSED."); 2.80 + } 2.81 +}