1 /*
  2  * Copyright (c) 2017, 2020, Red Hat, Inc. 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 
 25 /*
 26  * @test
 27  * @summary Test that periodic GC is working
 28  * @requires vm.gc.Shenandoah
 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 
 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         OutputAnalyzer output = ProcessTools.executeLimitedTestJava(cmds);
 46 
 47         output.shouldHaveExitValue(0);
 48         if (periodic && !output.getOutput().contains("Trigger: Time since last GC")) {
 49             throw new AssertionError(msg + ": Should have periodic GC in logs");
 50         }
 51         if (!periodic && output.getOutput().contains("Trigger: Time since last GC")) {
 52             throw new AssertionError(msg + ": Should not have periodic GC in logs");
 53         }
 54     }
 55 
 56     public static void main(String[] args) throws Exception {
 57         if (args.length > 0 && args[0].equals("test")) {
 58             Thread.sleep(5000); // stay idle
 59             return;
 60         }
 61 
 62         String[] enabled = new String[] {
 63                 "adaptive",
 64                 "compact",
 65                 "static",
 66         };
 67 
 68         for (String h : enabled) {
 69             testWith("Zero interval with " + h,
 70                     false,
 71                     "-Xlog:gc",
 72                     "-XX:+UnlockDiagnosticVMOptions",
 73                     "-XX:+UnlockExperimentalVMOptions",
 74                     "-XX:+UseShenandoahGC",
 75                     "-XX:ShenandoahGCHeuristics=" + h,
 76                     "-XX:ShenandoahGuaranteedGCInterval=0"
 77             );
 78 
 79             testWith("Short interval with " + h,
 80                     true,
 81                     "-Xlog:gc",
 82                     "-XX:+UnlockDiagnosticVMOptions",
 83                     "-XX:+UnlockExperimentalVMOptions",
 84                     "-XX:+UseShenandoahGC",
 85                     "-XX:ShenandoahGCHeuristics=" + h,
 86                     "-XX:ShenandoahGuaranteedGCInterval=1000"
 87             );
 88 
 89             testWith("Long interval with " + h,
 90                     false,
 91                     "-Xlog:gc",
 92                     "-XX:+UnlockDiagnosticVMOptions",
 93                     "-XX:+UnlockExperimentalVMOptions",
 94                     "-XX:+UseShenandoahGC",
 95                     "-XX:ShenandoahGCHeuristics=" + h,
 96                     "-XX:ShenandoahGuaranteedGCInterval=100000" // deliberately too long
 97             );
 98         }
 99 
100         testWith("Short interval with aggressive",
101                  false,
102                  "-Xlog:gc",
103                  "-XX:+UnlockDiagnosticVMOptions",
104                  "-XX:+UnlockExperimentalVMOptions",
105                  "-XX:+UseShenandoahGC",
106                  "-XX:ShenandoahGCHeuristics=aggressive",
107                  "-XX:ShenandoahGuaranteedGCInterval=1000"
108         );
109 
110         testWith("Zero interval with passive",
111                  false,
112                  "-Xlog:gc",
113                  "-XX:+UnlockDiagnosticVMOptions",
114                  "-XX:+UnlockExperimentalVMOptions",
115                  "-XX:+UseShenandoahGC",
116                  "-XX:ShenandoahGCMode=passive",
117                  "-XX:ShenandoahGuaranteedGCInterval=0"
118         );
119 
120         testWith("Short interval with passive",
121                  false,
122                  "-Xlog:gc",
123                  "-XX:+UnlockDiagnosticVMOptions",
124                  "-XX:+UnlockExperimentalVMOptions",
125                  "-XX:+UseShenandoahGC",
126                  "-XX:ShenandoahGCMode=passive",
127                  "-XX:ShenandoahGuaranteedGCInterval=1000"
128         );
129     }
130 
131 }