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     testGCId("UseShenandoahGC", "PrintGC");
  50     testGCId("UseShenandoahGC", "PrintGCDetails");
  51   }
  52 
  53   private static void verifyContainsGCIDs(OutputAnalyzer output) {
  54     output.shouldMatch("^#0: \\[");
  55     output.shouldMatch("^#1: \\[");
  56     output.shouldHaveExitValue(0);
  57   }
  58 
  59   private static void verifyContainsNoGCIDs(OutputAnalyzer output) {
  60     output.shouldNotMatch("^#[0-9]+: \\[");
  61     output.shouldHaveExitValue(0);
  62   }
  63 
  64   private static void testGCId(String gcFlag, String logFlag) throws Exception {
  65     // GCID logging enabled
  66     ProcessBuilder pb_enabled =
  67       ProcessTools.createJavaProcessBuilder("-XX:+UnlockExperimentalVMOptions", "-XX:+" + gcFlag, "-XX:+" + logFlag, "-Xmx10M", "-XX:+PrintGCID", GCTest.class.getName());
  68     verifyContainsGCIDs(new OutputAnalyzer(pb_enabled.start()));
  69 
  70     // GCID logging disabled
  71     ProcessBuilder pb_disabled =
  72       ProcessTools.createJavaProcessBuilder("-XX:+UnlockExperimentalVMOptions", "-XX:+" + gcFlag, "-XX:+" + logFlag, "-Xmx10M", "-XX:-PrintGCID", GCTest.class.getName());
  73     verifyContainsNoGCIDs(new OutputAnalyzer(pb_disabled.start()));
  74 
  75     // GCID logging default
  76     ProcessBuilder pb_default =
  77       ProcessTools.createJavaProcessBuilder("-XX:+UnlockExperimentalVMOptions", "-XX:+" + gcFlag, "-XX:+" + logFlag, "-Xmx10M", GCTest.class.getName());
  78     verifyContainsNoGCIDs(new OutputAnalyzer(pb_default.start()));
  79   }
  80 
  81   static class GCTest {
  82     private static byte[] garbage;
  83     public static void main(String [] args) {
  84       System.out.println("Creating garbage");
  85       // create 128MB of garbage. This should result in at least one GC
  86       for (int i = 0; i < 1024; i++) {
  87         garbage = new byte[128 * 1024];
  88       }
  89       // do a system gc to get one more gc
  90       System.gc();
  91       System.out.println("Done");
  92     }
  93   }
  94 }