1 /*
2  * Copyright (c) 2019, 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 package gc.g1;
25 
26 /* @test
27  * @bug 8218880
28  * @summary Test that issuing a periodic collection while the GC locker is
29  * held does not crash the VM.
30  * @key gc
31  * @requires vm.gc.G1
32  * @modules java.base
33  * @run main/othervm/native
34  *    -Xbootclasspath/a:.
35  *    -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
36  *    -XX:+UseG1GC -XX:G1PeriodicGCInterval=100
37  *    -XX:+G1PeriodicGCInvokesConcurrent
38  *    -Xlog:gc,gc+periodic=debug
39  *    gc.g1.TestPeriodicCollectionJNI
40  * @run main/othervm/native
41  *    -Xbootclasspath/a:.
42  *    -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
43  *    -XX:+UseG1GC -XX:G1PeriodicGCInterval=100
44  *    -XX:-G1PeriodicGCInvokesConcurrent
45  *    -Xlog:gc,gc+periodic=debug
46  *    gc.g1.TestPeriodicCollectionJNI
47  */
48 
49 public class TestPeriodicCollectionJNI {
50     static { System.loadLibrary("TestPeriodicCollectionJNI"); }
51 
blockInNative(byte[] array)52     private static native boolean blockInNative(byte[] array);
unblock()53     private static native void unblock();
54 
block()55     public static void block() {
56         if (!blockInNative(new byte[0])) {
57             throw new RuntimeException("failed to acquire lock to dummy object");
58         }
59     }
60 
main(String[] args)61     public static void main(String[] args) throws InterruptedException {
62         long timeout = 2000;
63         long startTime = System.currentTimeMillis();
64 
65         // Start thread doing JNI call
66         BlockInNative blocker = new BlockInNative();
67         blocker.start();
68 
69         try {
70             // Wait for periodic GC timeout to trigger
71             while (System.currentTimeMillis() < startTime + timeout) {
72                 System.out.println("Sleeping to let periodic GC trigger...");
73                 Thread.sleep(200);
74             }
75         } finally {
76             unblock();
77         }
78     }
79 }
80 
81 class BlockInNative extends Thread {
82 
run()83     public void run() {
84         TestPeriodicCollectionJNI.block();
85     }
86 
unlock()87     native void unlock();
88 }
89