1 /*
  2  * Copyright (c) 2020, 2023, 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  * @test
 26  * @bug 8284161 8289284
 27  * @summary Basic test of debugging option to trace pinned threads
 28  * @requires vm.continuations
 29  * @library /test/lib
 30  * @run junit/othervm -Djdk.tracePinnedThreads=full TracePinnedThreads
 31  * @run junit/othervm -Djdk.tracePinnedThreads=short TracePinnedThreads
 32  */
 33 
 34 import java.io.ByteArrayOutputStream;
 35 import java.io.PrintStream;
 36 import java.time.Duration;
 37 import java.util.concurrent.locks.LockSupport;
 38 
 39 import jdk.test.lib.thread.VThreadRunner;
 40 import org.junit.jupiter.api.Test;
 41 import static org.junit.jupiter.api.Assertions.*;
 42 
 43 class TracePinnedThreads {
 44     static final Object lock = new Object();
 45 
 46     /**
 47      * Parks current thread for 1 second.
 48      */
 49     private static void park() {
 50         long nanos = Duration.ofSeconds(1).toNanos();
 51         LockSupport.parkNanos(nanos);
 52     }
 53 
 54     /**
 55      * Invokes the park method through a native frame to park the current
 56      * thread for 1 second.
 57      */
 58     private static native void invokePark();
 59 
 60     /**
 61      * Test parking inside synchronized block.
 62      */
 63     @Test
 64     void testPinnedCausedBySynchronizedBlock() throws Exception {
 65         String output = run(() -> {
 66             synchronized (lock) {
 67                 park();
 68             }
 69         });
 70         assertContains(output, "<== monitors:1");
 71         assertDoesNotContain(output, "(Native Method)");
 72     }
 73 
 74     /**
 75      * Test parking with native frame on stack.
 76      */
 77     @Test
 78     void testPinnedCausedByNativeMethod() throws Exception {
 79         System.loadLibrary("TracePinnedThreads");
 80         String output = run(() -> invokePark());
 81         assertContains(output, "(Native Method)");
 82         assertDoesNotContain(output, "<== monitors");
 83     }
 84 
 85     /**
 86      * Run a task in a virtual thread, returning a String with any output printed
 87      * to standard output.
 88      */
 89     private static String run(Runnable task) throws Exception {
 90         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 91         PrintStream original = System.out;
 92         System.setOut(new PrintStream(baos, true));
 93         try {
 94             VThreadRunner.run(task::run);
 95         } finally {
 96             System.setOut(original);
 97         }
 98         String output = new String(baos.toByteArray());
 99         System.out.println(output);
100         return output;
101     }
102 
103     /**
104      * Tests that s1 contains s2.
105      */
106     private static void assertContains(String s1, String s2) {
107         assertTrue(s1.contains(s2), s2 + " not found!!!");
108     }
109 
110     /**
111      * Tests that s1 does not contain s2.
112      */
113     private static void assertDoesNotContain(String s1, String s2) {
114         assertFalse(s1.contains(s2), s2 + " found!!");
115     }
116 }