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