1 /*
   2  * Copyright (c) 2014, 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 /*
  25  * @test TestGCId
  26  * @bug 8043607
  27  * @summary Ensure that the GCId is logged
  28  * @key gc
  29  * @library /testlibrary
  30  */
  31 
  32 import com.oracle.java.testlibrary.ProcessTools;
  33 import com.oracle.java.testlibrary.OutputAnalyzer;
  34 
  35 public class TestGCId {
  36   public static void main(String[] args) throws Exception {
  37     testGCId("UseParallelGC", "PrintGC");
  38     testGCId("UseParallelGC", "PrintGCDetails");
  39 
  40     testGCId("UseG1GC", "PrintGC");
  41     testGCId("UseG1GC", "PrintGCDetails");
  42 
  43     testGCId("UseConcMarkSweepGC", "PrintGC");
  44     testGCId("UseConcMarkSweepGC", "PrintGCDetails");
  45 
  46     testGCId("UseSerialGC", "PrintGC");
  47     testGCId("UseSerialGC", "PrintGCDetails");
  48   }
  49 
  50   private static void verifyContainsGCIDs(OutputAnalyzer output) {
  51     output.shouldMatch("^#0: \\[");
  52     output.shouldMatch("^#1: \\[");
  53     output.shouldHaveExitValue(0);
  54   }
  55 
  56   private static void verifyContainsNoGCIDs(OutputAnalyzer output) {
  57     output.shouldNotMatch("^#[0-9]+: \\[");
  58     output.shouldHaveExitValue(0);
  59   }
  60 
  61   private static void testGCId(String gcFlag, String logFlag) throws Exception {
  62     // GCID logging enabled
  63     ProcessBuilder pb_enabled =
  64       ProcessTools.createJavaProcessBuilder("-XX:+" + gcFlag, "-XX:+" + logFlag, "-Xmx10M", "-XX:+PrintGCID", GCTest.class.getName());
  65     verifyContainsGCIDs(new OutputAnalyzer(pb_enabled.start()));
  66 
  67     // GCID logging disabled
  68     ProcessBuilder pb_disabled =
  69       ProcessTools.createJavaProcessBuilder("-XX:+" + gcFlag, "-XX:+" + logFlag, "-Xmx10M", "-XX:-PrintGCID", GCTest.class.getName());
  70     verifyContainsNoGCIDs(new OutputAnalyzer(pb_disabled.start()));
  71 
  72     // GCID logging default
  73     ProcessBuilder pb_default =
  74       ProcessTools.createJavaProcessBuilder("-XX:+" + gcFlag, "-XX:+" + logFlag, "-Xmx10M", GCTest.class.getName());
  75     verifyContainsGCIDs(new OutputAnalyzer(pb_default.start()));
  76   }
  77 
  78   static class GCTest {
  79     private static byte[] garbage;
  80     public static void main(String [] args) {
  81       System.out.println("Creating garbage");
  82       // create 128MB of garbage. This should result in at least one GC
  83       for (int i = 0; i < 1024; i++) {
  84         garbage = new byte[128 * 1024];
  85       }
  86       // do a system gc to get one more gc
  87       System.gc();
  88       System.out.println("Done");
  89     }
  90   }
  91 }