1 /*
2  * Copyright (c) 2017, 2020, Red Hat, Inc. All rights reserved.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.
7  *
8  * This code is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * version 2 for more details (a copy is included in the LICENSE file that
12  * accompanied this code).
13  *
14  * You should have received a copy of the GNU General Public License version
15  * 2 along with this work; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19  * or visit www.oracle.com if you need additional information or have any
20  * questions.
21  *
22  */
23 
24 /*
25  * @test TestPeriodicGC
26  * @summary Test that periodic GC is working
27  * @key gc
28  * @requires vm.gc.Shenandoah & !vm.graal.enabled
29  * @library /test/lib
30  * @run driver TestPeriodicGC
31  */
32 
33 import java.util.*;
34 
35 import jdk.test.lib.Asserts;
36 import jdk.test.lib.process.ProcessTools;
37 import jdk.test.lib.process.OutputAnalyzer;
38 
39 public class TestPeriodicGC {
40 
testWith(String msg, boolean periodic, String... args)41     public static void testWith(String msg, boolean periodic, String... args) throws Exception {
42         String[] cmds = Arrays.copyOf(args, args.length + 2);
43         cmds[args.length] = TestPeriodicGC.class.getName();
44         cmds[args.length + 1] = "test";
45         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(cmds);
46 
47         OutputAnalyzer output = new OutputAnalyzer(pb.start());
48         output.shouldHaveExitValue(0);
49         if (periodic && !output.getOutput().contains("Trigger: Time since last GC")) {
50             throw new AssertionError(msg + ": Should have periodic GC in logs");
51         }
52         if (!periodic && output.getOutput().contains("Trigger: Time since last GC")) {
53             throw new AssertionError(msg + ": Should not have periodic GC in logs");
54         }
55     }
56 
main(String[] args)57     public static void main(String[] args) throws Exception {
58         if (args.length > 0 && args[0].equals("test")) {
59             Thread.sleep(5000); // stay idle
60             return;
61         }
62 
63         String[] enabled = new String[] {
64                 "adaptive",
65                 "compact",
66                 "static",
67         };
68 
69         for (String h : enabled) {
70             testWith("Zero interval with " + h,
71                     false,
72                     "-Xlog:gc",
73                     "-XX:+UnlockDiagnosticVMOptions",
74                     "-XX:+UnlockExperimentalVMOptions",
75                     "-XX:+UseShenandoahGC",
76                     "-XX:ShenandoahGCHeuristics=" + h,
77                     "-XX:ShenandoahGuaranteedGCInterval=0"
78             );
79 
80             testWith("Short interval with " + h,
81                     true,
82                     "-Xlog:gc",
83                     "-XX:+UnlockDiagnosticVMOptions",
84                     "-XX:+UnlockExperimentalVMOptions",
85                     "-XX:+UseShenandoahGC",
86                     "-XX:ShenandoahGCHeuristics=" + h,
87                     "-XX:ShenandoahGuaranteedGCInterval=1000"
88             );
89 
90             testWith("Long interval with " + h,
91                     false,
92                     "-Xlog:gc",
93                     "-XX:+UnlockDiagnosticVMOptions",
94                     "-XX:+UnlockExperimentalVMOptions",
95                     "-XX:+UseShenandoahGC",
96                     "-XX:ShenandoahGCHeuristics=" + h,
97                     "-XX:ShenandoahGuaranteedGCInterval=100000" // deliberately too long
98             );
99         }
100 
101         testWith("Zero interval with iu mode",
102                  false,
103                  "-Xlog:gc",
104                  "-XX:+UnlockDiagnosticVMOptions",
105                  "-XX:+UnlockExperimentalVMOptions",
106                  "-XX:+UseShenandoahGC",
107                  "-XX:ShenandoahGCMode=iu",
108                  "-XX:ShenandoahGuaranteedGCInterval=0"
109         );
110 
111         testWith("Short interval with iu mode",
112                  true,
113                  "-Xlog:gc",
114                  "-XX:+UnlockDiagnosticVMOptions",
115                  "-XX:+UnlockExperimentalVMOptions",
116                  "-XX:+UseShenandoahGC",
117                  "-XX:ShenandoahGCMode=iu",
118                  "-XX:ShenandoahGuaranteedGCInterval=1000"
119         );
120 
121         testWith("Long interval with iu mode",
122                  false,
123                  "-Xlog:gc",
124                  "-XX:+UnlockDiagnosticVMOptions",
125                  "-XX:+UnlockExperimentalVMOptions",
126                  "-XX:+UseShenandoahGC",
127                  "-XX:ShenandoahGCMode=iu",
128                  "-XX:ShenandoahGuaranteedGCInterval=100000" // deliberately too long
129         );
130 
131         testWith("Short interval with aggressive",
132                  false,
133                  "-Xlog:gc",
134                  "-XX:+UnlockDiagnosticVMOptions",
135                  "-XX:+UnlockExperimentalVMOptions",
136                  "-XX:+UseShenandoahGC",
137                  "-XX:ShenandoahGCHeuristics=aggressive",
138                  "-XX:ShenandoahGuaranteedGCInterval=1000"
139         );
140 
141         testWith("Zero interval with passive",
142                  false,
143                  "-Xlog:gc",
144                  "-XX:+UnlockDiagnosticVMOptions",
145                  "-XX:+UnlockExperimentalVMOptions",
146                  "-XX:+UseShenandoahGC",
147                  "-XX:ShenandoahGCMode=passive",
148                  "-XX:ShenandoahGuaranteedGCInterval=0"
149         );
150 
151         testWith("Short interval with passive",
152                  false,
153                  "-Xlog:gc",
154                  "-XX:+UnlockDiagnosticVMOptions",
155                  "-XX:+UnlockExperimentalVMOptions",
156                  "-XX:+UseShenandoahGC",
157                  "-XX:ShenandoahGCMode=passive",
158                  "-XX:ShenandoahGuaranteedGCInterval=1000"
159         );
160     }
161 
162 }
163