1 /* 2 * Copyright (c) 2016, 2017, 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 */ 24 25 /* 26 * @test HandshakeWalkStackTest 27 * @library /testlibrary /test/lib 28 * @build HandshakeWalkStackTest 29 * @run main ClassFileInstaller sun.hotspot.WhiteBox 30 * sun.hotspot.WhiteBox$WhiteBoxPermission 31 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI HandshakeWalkStackTest 32 */ 33 34 import jdk.test.lib.Asserts; 35 import sun.hotspot.WhiteBox; 36 37 public class HandshakeWalkStackTest { 38 39 public static void main(String... args) throws Exception { 40 int iterations = 3; 41 if (args.length > 0) { 42 iterations = Integer.parseInt(args[0]); 43 } 44 test(iterations); 45 } 46 47 private static void test(int iterations) throws Exception { 48 Thread loop_thread = new Thread(() -> run_loop(create_list())); 49 Thread alloc_thread = new Thread(() -> run_alloc()); 50 Thread wait_thread = new Thread(() -> run_wait(new Object() {})); 51 loop_thread.start(); 52 alloc_thread.start(); 53 wait_thread.start(); 54 55 WhiteBox wb = WhiteBox.getWhiteBox(); 56 int walked = 0; 57 for (int i = 0; i < iterations; i++) { 58 System.out.println("Iteration " + i); 59 System.out.flush(); 60 Thread.sleep(200); 61 walked = wb.handshakeWalkStack(loop_thread, false); 62 Asserts.assertEQ(walked, 1, "Must have walked one thread stack"); 63 Thread.sleep(200); 64 walked = wb.handshakeWalkStack(alloc_thread, false); 65 Asserts.assertEQ(walked, 1, "Must have walked one thread stack"); 66 Thread.sleep(200); 67 walked = wb.handshakeWalkStack(wait_thread, false); 68 Asserts.assertEQ(walked, 1, "Must have walked one thread stack"); 69 Thread.sleep(200); 70 walked = wb.handshakeWalkStack(Thread.currentThread(), false); 71 Asserts.assertEQ(walked, 1, "Must have walked one thread stack"); 72 } 73 Thread.sleep(200); 74 walked = wb.handshakeWalkStack(null, true); 75 Asserts.assertGT(walked, 4, "Must have walked more than three thread stacks"); 76 } 77 78 static class List { 79 List next; 80 81 List(List next) { 82 this.next = next; 83 } 84 } 85 86 public static List create_list() { 87 List head = new List(null); 88 List elem = new List(head); 89 List elem2 = new List(elem); 90 List elem3 = new List(elem2); 91 List elem4 = new List(elem3); 92 head.next = elem4; 93 94 return head; 95 } 96 97 public static void run_loop(List loop) { 98 while (loop.next != null) { 99 loop = loop.next; 100 } 101 } 102 103 public static byte[] array; 104 105 public static void run_alloc() { 106 while (true) { 107 // Write to public static to ensure the byte array escapes. 108 array = new byte[4096]; 109 } 110 } 111 112 public static void run_wait(Object lock) { 113 synchronized (lock) { 114 try { 115 lock.wait(); 116 } catch (InterruptedException ie) {} 117 } 118 } 119 }