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