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         ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(cmds);
 47 
 48         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 49         output.shouldHaveExitValue(0);
 50         if (periodic && !output.getOutput().contains("Trigger (GLOBAL): Time since last GC")) {
 51             throw new AssertionError(msg + ": Should have periodic GC in logs");
 52         }
 53         if (!periodic && output.getOutput().contains("Trigger (GLOBAL): Time since last GC")) {
 54             throw new AssertionError(msg + ": Should not have periodic GC in logs");
 55         }
 56     }
 57 
 58     public static void testGenerational(boolean periodic, String... args) throws Exception {
 59         String[] cmds = Arrays.copyOf(args, args.length + 2);
 60         cmds[args.length] = TestPeriodicGC.class.getName();
 61         cmds[args.length + 1] = "test";
 62         ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(cmds);
 63 
 64         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 65         output.shouldHaveExitValue(0);
 66         if (periodic) {
 67             if (!output.getOutput().contains("Trigger (YOUNG): Time since last GC")) {
 68                 throw new AssertionError("Generational mode: Should have periodic young GC in logs");
 69             }
 70             if (!output.getOutput().contains("Trigger (OLD): Time since last GC")) {
 71                 throw new AssertionError("Generational mode: Should have periodic old GC in logs");
 72             }
 73         } else {
 74             if (output.getOutput().contains("Trigger (YOUNG): Time since last GC")) {
 75                 throw new AssertionError("Generational mode: Should not have periodic young GC in logs");
 76             }
 77             if (output.getOutput().contains("Trigger (OLD): Time since last GC")) {
 78                 throw new AssertionError("Generational mode: Should not have periodic old GC in logs");
 79             }
 80         }
 81     }
 82 
 83     public static void main(String[] args) throws Exception {
 84         if (args.length > 0 && args[0].equals("test")) {
 85             Thread.sleep(5000); // stay idle
 86             return;
 87         }
 88 
 89         String[] enabled = new String[] {
 90                 "adaptive",
 91                 "compact",
 92                 "static",
 93         };
 94 
 95         for (String h : enabled) {
 96             testWith("Zero interval with " + h,
 97                     false,
 98                     "-Xlog:gc",
 99                     "-XX:+UnlockDiagnosticVMOptions",
100                     "-XX:+UnlockExperimentalVMOptions",
101                     "-XX:+UseShenandoahGC",
102                     "-XX:ShenandoahGCHeuristics=" + h,
103                     "-XX:ShenandoahGuaranteedGCInterval=0"
104             );
105 
106             testWith("Short interval with " + h,
107                     true,
108                     "-Xlog:gc",
109                     "-XX:+UnlockDiagnosticVMOptions",
110                     "-XX:+UnlockExperimentalVMOptions",
111                     "-XX:+UseShenandoahGC",
112                     "-XX:ShenandoahGCHeuristics=" + h,
113                     "-XX:ShenandoahGuaranteedGCInterval=1000"
114             );
115 
116             testWith("Long interval with " + h,
117                     false,
118                     "-Xlog:gc",
119                     "-XX:+UnlockDiagnosticVMOptions",
120                     "-XX:+UnlockExperimentalVMOptions",
121                     "-XX:+UseShenandoahGC",
122                     "-XX:ShenandoahGCHeuristics=" + h,
123                     "-XX:ShenandoahGuaranteedGCInterval=100000" // deliberately too long
124             );
125         }
126 
127         testWith("Zero interval with iu mode",
128                  false,
129                  "-Xlog:gc",
130                  "-XX:+UnlockDiagnosticVMOptions",
131                  "-XX:+UnlockExperimentalVMOptions",
132                  "-XX:+UseShenandoahGC",
133                  "-XX:ShenandoahGCMode=iu",
134                  "-XX:ShenandoahGuaranteedGCInterval=0"
135         );
136 
137         testWith("Short interval with iu mode",
138                  true,
139                  "-Xlog:gc",
140                  "-XX:+UnlockDiagnosticVMOptions",
141                  "-XX:+UnlockExperimentalVMOptions",
142                  "-XX:+UseShenandoahGC",
143                  "-XX:ShenandoahGCMode=iu",
144                  "-XX:ShenandoahGuaranteedGCInterval=1000"
145         );
146 
147         testWith("Long interval with iu mode",
148                  false,
149                  "-Xlog:gc",
150                  "-XX:+UnlockDiagnosticVMOptions",
151                  "-XX:+UnlockExperimentalVMOptions",
152                  "-XX:+UseShenandoahGC",
153                  "-XX:ShenandoahGCMode=iu",
154                  "-XX:ShenandoahGuaranteedGCInterval=100000" // deliberately too long
155         );
156 
157         testWith("Short interval with aggressive",
158                  false,
159                  "-Xlog:gc",
160                  "-XX:+UnlockDiagnosticVMOptions",
161                  "-XX:+UnlockExperimentalVMOptions",
162                  "-XX:+UseShenandoahGC",
163                  "-XX:ShenandoahGCHeuristics=aggressive",
164                  "-XX:ShenandoahGuaranteedGCInterval=1000"
165         );
166 
167         testWith("Zero interval with passive",
168                  false,
169                  "-Xlog:gc",
170                  "-XX:+UnlockDiagnosticVMOptions",
171                  "-XX:+UnlockExperimentalVMOptions",
172                  "-XX:+UseShenandoahGC",
173                  "-XX:ShenandoahGCMode=passive",
174                  "-XX:ShenandoahGuaranteedGCInterval=0"
175         );
176 
177         testWith("Short interval with passive",
178                  false,
179                  "-Xlog:gc",
180                  "-XX:+UnlockDiagnosticVMOptions",
181                  "-XX:+UnlockExperimentalVMOptions",
182                  "-XX:+UseShenandoahGC",
183                  "-XX:ShenandoahGCMode=passive",
184                  "-XX:ShenandoahGuaranteedGCInterval=1000"
185         );
186 
187         testGenerational(true,
188                          "-Xlog:gc",
189                          "-XX:+UnlockDiagnosticVMOptions",
190                          "-XX:+UnlockExperimentalVMOptions",
191                          "-XX:+UseShenandoahGC",
192                          "-XX:ShenandoahGCMode=generational",
193                          "-XX:ShenandoahGuaranteedYoungGCInterval=1000",
194                          "-XX:ShenandoahGuaranteedOldGCInterval=1500"
195         );
196 
197         testGenerational(false,
198                          "-Xlog:gc",
199                          "-XX:+UnlockDiagnosticVMOptions",
200                          "-XX:+UnlockExperimentalVMOptions",
201                          "-XX:+UseShenandoahGC",
202                          "-XX:ShenandoahGCMode=generational",
203                          "-XX:ShenandoahGuaranteedYoungGCInterval=0",
204                          "-XX:ShenandoahGuaranteedOldGCInterval=0"
205         );
206     }
207 
208 }
--- EOF ---