1 /*
   2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. 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 package gc;
  25 
  26 /*
  27  * @test TestAgeOutput
  28  * @bug 8164936
  29  * @summary Check that collectors using age table based aging print an age table even for the first garbage collection
  30  * @key gc
  31  * @requires vm.gc=="null"
  32  * @modules java.base/jdk.internal.misc
  33  * @library /test/lib
  34  * @build sun.hotspot.WhiteBox
  35  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  36  * @run main/othervm -XX:+UseSerialGC gc.TestAgeOutput UseSerialGC
  37  * @run main/othervm -XX:+UseG1GC gc.TestAgeOutput UseG1GC
  38  */
  39 
  40 /*
  41  * @test TestAgeOutputCMS
  42  * @bug 8164936
  43  * @key gc
  44  * @comment Graal does not support CMS
  45  * @requires vm.gc=="null" & !vm.graal.enabled
  46  * @modules java.base/jdk.internal.misc
  47  * @library /test/lib
  48  * @build sun.hotspot.WhiteBox
  49  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  50  * @run main/othervm -XX:+UseConcMarkSweepGC gc.TestAgeOutput UseConcMarkSweepGC
  51  */
  52 
  53 import sun.hotspot.WhiteBox;
  54 
  55 import java.util.regex.Matcher;
  56 import java.util.regex.Pattern;
  57 
  58 import jdk.test.lib.process.OutputAnalyzer;
  59 import jdk.test.lib.process.ProcessTools;
  60 
  61 public class TestAgeOutput {
  62 
  63     public static void checkPattern(String pattern, String what) throws Exception {
  64         Pattern r = Pattern.compile(pattern);
  65         Matcher m = r.matcher(what);
  66 
  67         if (!m.find()) {
  68             throw new RuntimeException("Could not find pattern " + pattern + " in output");
  69         }
  70     }
  71 
  72     public static void runTest(String gcArg) throws Exception {
  73         final String[] arguments = {
  74             "-Xbootclasspath/a:.",
  75             "-XX:+UnlockExperimentalVMOptions",
  76             "-XX:+UnlockDiagnosticVMOptions",
  77             "-XX:+WhiteBoxAPI",
  78             "-XX:+" + gcArg,
  79             "-Xmx10M",
  80             "-Xlog:gc+age=trace",
  81             GCTest.class.getName()
  82             };
  83 
  84         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(arguments);
  85         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  86 
  87         output.shouldHaveExitValue(0);
  88 
  89         System.out.println(output.getStdout());
  90 
  91         String stdout = output.getStdout();
  92 
  93         checkPattern(".*GC\\(0\\) .*Desired survivor size.*", stdout);
  94         checkPattern(".*GC\\(0\\) .*Age table with threshold.*", stdout);
  95         checkPattern(".*GC\\(0\\) .*- age   1:.*", stdout);
  96     }
  97 
  98     public static void main(String[] args) throws Exception {
  99         runTest(args[0]);
 100     }
 101 
 102     static class GCTest {
 103         private static final WhiteBox WB = WhiteBox.getWhiteBox();
 104 
 105         public static Object holder;
 106 
 107         public static void main(String [] args) {
 108             holder = new byte[100];
 109             WB.youngGC();
 110             System.out.println(holder);
 111         }
 112     }
 113 }
 114