1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 import java.lang.management.*; 24 25 /* 26 * @test 27 * @bug 8205878 8206954 28 * @requires os.family != "windows" 29 * @summary Basic test of Thread and ThreadMXBean queries on a natively 30 * attached thread that has failed to detach before terminating. 31 * @comment The native code only supports POSIX so no windows testing 32 * @run main/othervm/native TestTerminatedThread 33 */ 34 35 public class TestTerminatedThread { 36 37 static native Thread createTerminatedThread(); 38 39 static { 40 System.loadLibrary("terminatedThread"); 41 } 42 43 private static ThreadMXBean mbean = ManagementFactory.getThreadMXBean(); 44 45 public static void main(String[] args) throws Throwable { 46 47 Thread t = createTerminatedThread(); 48 49 if (!t.isAlive()) 50 throw new Error("Thread is only supposed to terminate at native layer!"); 51 52 // Now invoke the various functions on this thread to 53 // make sure the VM handles it okay. The focus is on 54 // functions with an underlying native OS implementation. 55 // Generally as long as we don't crash or throw unexpected 56 // exceptions then the test passes. In some cases we know exactly 57 // what a function should return and so can check that. 58 59 System.out.println("Working with thread: " + t + 60 ", in state: " + t.getState()); 61 62 System.out.println("Calling suspend ..."); 63 t.suspend(); 64 System.out.println("Calling resume ..."); 65 t.resume(); 66 System.out.println("Calling getStackTrace ..."); 67 StackTraceElement[] stack = t.getStackTrace(); 68 System.out.println(java.util.Arrays.toString(stack)); 69 if (stack.length != 0) 70 throw new Error("Terminated thread should have empty java stack trace"); 71 System.out.println("Calling setName(\"NewName\") ..."); 72 t.setName("NewName"); 73 System.out.println("Calling interrupt ..."); 74 t.interrupt(); 75 System.out.println("Calling stop ..."); 76 t.stop(); 77 78 // Now the ThreadMXBean functions 79 80 if (mbean.isThreadCpuTimeSupported() && 81 mbean.isThreadCpuTimeEnabled() ) { 82 System.out.println("Calling getThreadCpuTime ..."); 83 long t1 = mbean.getThreadCpuTime(t.getId()); 84 if (t1 != -1) { 85 // At least on PPC, we know threads can still be around a short 86 // instant. In some stress scenarios we seem to grab times of 87 // new threads started with the same thread id. In these cases 88 // we get here. 89 System.out.println("Unexpected: thread still reports CPU time: " + t1); 90 } else { 91 System.out.println("Okay: getThreadCpuTime() reported -1 as expected"); 92 } 93 } else { 94 System.out.println("Skipping Thread CPU time test as it's not supported"); 95 } 96 97 System.out.println("Calling getThreadUserTime ..."); 98 long t1 = mbean.getThreadUserTime(t.getId()); 99 if (t1 != -1) { 100 // At least on PPC, we know threads can still be around a short 101 // instant. In some stress scenarios we seem to grab times of 102 // new threads started with the same thread id. In these cases 103 // we get here. 104 System.out.println("Unexpected: thread still reports User time: " + t1); 105 } else { 106 System.out.println("Okay: getThreadUserTime() reported -1 as expected"); 107 } 108 109 System.out.println("Calling getThreadInfo ..."); 110 ThreadInfo info = mbean.getThreadInfo(t.getId()); 111 System.out.println(info); 112 113 System.out.println("Calling getThreadInfo with stack ..."); 114 info = mbean.getThreadInfo(t.getId(), Integer.MAX_VALUE); 115 System.out.println(info); 116 } 117 }