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  * @library /testlibrary
  29  *
  30  * @run driver TestPeriodicGC
  31  */
  32 
  33 import java.util.*;
  34 
  35 import com.oracle.java.testlibrary.*;
  36 
  37 public class TestPeriodicGC {
  38 
  39     public static void testWith(String msg, boolean periodic, String... args) throws Exception {
  40         String[] cmds = Arrays.copyOf(args, args.length + 2);
  41         cmds[args.length] = TestPeriodicGC.class.getName();
  42         cmds[args.length + 1] = "test";
  43         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(cmds);
  44 
  45         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  46         output.shouldHaveExitValue(0);
  47         if (periodic && !output.getOutput().contains("Trigger: Time since last GC")) {
  48             throw new AssertionError(msg + ": Should have periodic GC in logs");
  49         }
  50         if (!periodic && output.getOutput().contains("Trigger: Time since last GC")) {
  51             throw new AssertionError(msg + ": Should not have periodic GC in logs");
  52         }
  53     }
  54 
  55     public static void main(String[] args) throws Exception {
  56         if (args.length > 0 && args[0].equals("test")) {
  57             Thread.sleep(5000); // stay idle
  58             return;
  59         }
  60 
  61         String[] enabled = new String[] {
  62                 "adaptive",
  63                 "compact",
  64                 "static"
  65         };
  66 
  67         for (String h : enabled) {
  68             testWith("Zero interval with " + h,
  69                     false,
  70                     "-verbose:gc",
  71                     "-XX:+UnlockDiagnosticVMOptions",
  72                     "-XX:+UnlockExperimentalVMOptions",
  73                     "-XX:+UseShenandoahGC",
  74                     "-XX:ShenandoahGCHeuristics=" + h,
  75                     "-XX:ShenandoahGuaranteedGCInterval=0"
  76             );
  77 
  78             testWith("Short interval with " + h,
  79                     true,
  80                     "-verbose:gc",
  81                     "-XX:+UnlockDiagnosticVMOptions",
  82                     "-XX:+UnlockExperimentalVMOptions",
  83                     "-XX:+UseShenandoahGC",
  84                     "-XX:ShenandoahGCHeuristics=" + h,
  85                     "-XX:ShenandoahGuaranteedGCInterval=1000"
  86             );
  87 
  88             testWith("Long interval with " + h,
  89                     false,
  90                     "-verbose:gc",
  91                     "-XX:+UnlockDiagnosticVMOptions",
  92                     "-XX:+UnlockExperimentalVMOptions",
  93                     "-XX:+UseShenandoahGC",
  94                     "-XX:ShenandoahGCHeuristics=" + h,
  95                     "-XX:ShenandoahGuaranteedGCInterval=100000" // deliberately too long
  96             );
  97         }
  98 
  99         testWith("Zero interval with iu mode",
 100                  false,
 101                  "-verbose:gc",
 102                  "-XX:+UnlockDiagnosticVMOptions",
 103                  "-XX:+UnlockExperimentalVMOptions",
 104                  "-XX:+UseShenandoahGC",
 105                  "-XX:ShenandoahGCMode=iu",
 106                  "-XX:ShenandoahGuaranteedGCInterval=0"
 107         );
 108 
 109         testWith("Short interval with iu mode",
 110                  true,
 111                  "-verbose:gc",
 112                  "-XX:+UnlockDiagnosticVMOptions",
 113                  "-XX:+UnlockExperimentalVMOptions",
 114                  "-XX:+UseShenandoahGC",
 115                  "-XX:ShenandoahGCMode=iu",
 116                  "-XX:ShenandoahGuaranteedGCInterval=1000"
 117         );
 118 
 119         testWith("Long interval with iu mode",
 120                  false,
 121                  "-verbose:gc",
 122                  "-XX:+UnlockDiagnosticVMOptions",
 123                  "-XX:+UnlockExperimentalVMOptions",
 124                  "-XX:+UseShenandoahGC",
 125                  "-XX:ShenandoahGCMode=iu",
 126                  "-XX:ShenandoahGuaranteedGCInterval=100000" // deliberately too long
 127         );
 128 
 129         testWith("Short interval with aggressive",
 130                  false,
 131                  "-verbose:gc",
 132                  "-XX:+UnlockDiagnosticVMOptions",
 133                  "-XX:+UnlockExperimentalVMOptions",
 134                  "-XX:+UseShenandoahGC",
 135                  "-XX:ShenandoahGCHeuristics=aggressive",
 136                  "-XX:ShenandoahGuaranteedGCInterval=1000"
 137         );
 138 
 139         testWith("Zero interval with passive",
 140                  false,
 141                  "-verbose:gc",
 142                  "-XX:+UnlockDiagnosticVMOptions",
 143                  "-XX:+UnlockExperimentalVMOptions",
 144                  "-XX:+UseShenandoahGC",
 145                  "-XX:ShenandoahGCMode=passive",
 146                  "-XX:ShenandoahGuaranteedGCInterval=0"
 147         );
 148 
 149         testWith("Short interval with passive",
 150                  false,
 151                  "-verbose:gc",
 152                  "-XX:+UnlockDiagnosticVMOptions",
 153                  "-XX:+UnlockExperimentalVMOptions",
 154                  "-XX:+UseShenandoahGC",
 155                  "-XX:ShenandoahGCMode=passive",
 156                  "-XX:ShenandoahGuaranteedGCInterval=1000"
 157         );
 158     }
 159 
 160 }