1 /*
   2  * Copyright (c) 2017, 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 TestVerifyGCType
  26  * @summary Test the VerifyGCType flag to ensure basic functionality.
  27  * @key gc
  28  * @requires vm.gc.G1
  29  * @library /test/lib
  30  * @build sun.hotspot.WhiteBox
  31  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  32  * @run driver TestVerifyGCType
  33  */
  34 
  35 import java.util.ArrayList;
  36 import java.util.Collections;
  37 
  38 import jdk.test.lib.Asserts;
  39 import jdk.test.lib.Utils;
  40 import jdk.test.lib.process.OutputAnalyzer;
  41 import jdk.test.lib.process.ProcessTools;
  42 import sun.hotspot.WhiteBox;
  43 
  44 public class TestVerifyGCType {
  45     public static final String VERIFY_TAG    = "[gc,verify]";
  46     public static final String VERIFY_BEFORE = "Verifying Before GC";
  47     public static final String VERIFY_DURING = "Verifying During GC";
  48     public static final String VERIFY_AFTER  = "Verifying After GC";
  49 
  50     public static void main(String args[]) throws Exception {
  51         testAllVerificationEnabled();
  52         testAllExplicitlyEnabled();
  53         testFullAndRemark();
  54         testConcurrentMark();
  55         testBadVerificationType();
  56         testUnsupportedCollector();
  57     }
  58 
  59     private static void testAllVerificationEnabled() throws Exception {
  60         // Test with all verification enabled
  61         OutputAnalyzer output = testWithVerificationType(new String[0]);
  62         output.shouldHaveExitValue(0);
  63 
  64         verifyCollection("Pause Young", true, false, true, output.getStdout());
  65         verifyCollection("Pause Initial Mark", true, false, true, output.getStdout());
  66         verifyCollection("Pause Mixed", true, false, true, output.getStdout());
  67         verifyCollection("Pause Remark", false, true, false, output.getStdout());
  68         verifyCollection("Pause Cleanup", false, true, false, output.getStdout());
  69         verifyCollection("Pause Full", true, true, true, output.getStdout());
  70     }
  71 
  72     private static void testAllExplicitlyEnabled() throws Exception {
  73         OutputAnalyzer output;
  74         // Test with all explicitly enabled
  75         output = testWithVerificationType(new String[] {
  76                 "young-only", "initial-mark", "mixed", "remark", "cleanup", "full"});
  77         output.shouldHaveExitValue(0);
  78 
  79         verifyCollection("Pause Young", true, false, true, output.getStdout());
  80         verifyCollection("Pause Initial Mark", true, false, true, output.getStdout());
  81         verifyCollection("Pause Mixed", true, false, true, output.getStdout());
  82         verifyCollection("Pause Remark", false, true, false, output.getStdout());
  83         verifyCollection("Pause Cleanup", false, true, false, output.getStdout());
  84         verifyCollection("Pause Full", true, true, true, output.getStdout());
  85     }
  86 
  87     private static void testFullAndRemark() throws Exception {
  88         OutputAnalyzer output;
  89         // Test with full and remark
  90         output = testWithVerificationType(new String[] {"remark", "full"});
  91         output.shouldHaveExitValue(0);
  92 
  93         verifyCollection("Pause Young", false, false, false, output.getStdout());
  94         verifyCollection("Pause Initial Mark", false, false, false, output.getStdout());
  95         verifyCollection("Pause Mixed", false, false, false, output.getStdout());
  96         verifyCollection("Pause Remark", false, true, false, output.getStdout());
  97         verifyCollection("Pause Cleanup", false, false, false, output.getStdout());
  98         verifyCollection("Pause Full", true, true, true, output.getStdout());
  99     }
 100 
 101     private static void testConcurrentMark() throws Exception {
 102         OutputAnalyzer output;
 103         // Test with full and remark
 104         output = testWithVerificationType(new String[] {"initial-mark", "cleanup", "remark"});
 105         output.shouldHaveExitValue(0);
 106 
 107         verifyCollection("Pause Young", false, false, false, output.getStdout());
 108         verifyCollection("Pause Initial Mark", true, false, true, output.getStdout());
 109         verifyCollection("Pause Mixed", false, false, false, output.getStdout());
 110         verifyCollection("Pause Remark", false, true, false, output.getStdout());
 111         verifyCollection("Pause Cleanup", false, true, false, output.getStdout());
 112         verifyCollection("Pause Full", false, false, false, output.getStdout());
 113     }
 114 
 115     private static void testBadVerificationType() throws Exception {
 116         OutputAnalyzer output;
 117         // Test bad type
 118         output = testWithVerificationType(new String[] {"old"});
 119         output.shouldHaveExitValue(0);
 120 
 121         output.shouldMatch("VerifyGCType: '.*' is unknown. Available types are: young-only, initial-mark, mixed, remark, cleanup and full");
 122         verifyCollection("Pause Young", true, false, true, output.getStdout());
 123         verifyCollection("Pause Initial Mark", true, false, true, output.getStdout());
 124         verifyCollection("Pause Mixed", true, false, true, output.getStdout());
 125         verifyCollection("Pause Remark", false, true, false, output.getStdout());
 126         verifyCollection("Pause Cleanup", false, true, false, output.getStdout());
 127         verifyCollection("Pause Full", true, true, true, output.getStdout());
 128     }
 129 
 130     private static void testUnsupportedCollector() throws Exception {
 131         OutputAnalyzer output;
 132         // Test bad gc
 133         output = testWithBadGC();
 134         output.shouldHaveExitValue(0);
 135         output.shouldMatch("VerifyGCType is not supported by this collector.");
 136     }
 137 
 138     private static OutputAnalyzer testWithVerificationType(String[] types) throws Exception {
 139         ArrayList<String> basicOpts = new ArrayList<>();
 140         Collections.addAll(basicOpts, new String[] {
 141                                        "-Xbootclasspath/a:.",
 142                                        "-XX:+UnlockDiagnosticVMOptions",
 143                                        "-XX:+UseG1GC",
 144                                        "-XX:+WhiteBoxAPI",
 145                                        "-XX:+ExplicitGCInvokesConcurrent",
 146                                        "-Xlog:gc,gc+start,gc+verify=info",
 147                                        "-XX:+VerifyBeforeGC",
 148                                        "-XX:+VerifyAfterGC",
 149                                        "-XX:+VerifyDuringGC"});
 150 
 151         for(String verifyType : types) {
 152             basicOpts.add("-XX:VerifyGCType="+verifyType);
 153         }
 154 
 155         basicOpts.add(TriggerGCs.class.getName());
 156 
 157         ProcessBuilder procBuilder =  ProcessTools.createJavaProcessBuilder(basicOpts.toArray(
 158                                                                             new String[basicOpts.size()]));
 159         OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start());
 160         return analyzer;
 161     }
 162 
 163     private static OutputAnalyzer testWithBadGC() throws Exception {
 164         ProcessBuilder procBuilder =  ProcessTools.createJavaProcessBuilder(new String[] {
 165                 "-XX:+UseParallelGC",
 166                 "-XX:+UnlockDiagnosticVMOptions",
 167                 "-XX:VerifyGCType=full",
 168                 "-version"});
 169 
 170         OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start());
 171         return analyzer;
 172     }
 173 
 174     private static void verifyCollection(String name, boolean expectBefore, boolean expectDuring, boolean expectAfter, String data) {
 175         CollectionInfo ci = CollectionInfo.parseFirst(name, data);
 176         Asserts.assertTrue(ci != null, "Expected GC not found: " + name);
 177 
 178         // Verify Before
 179         verifyType(ci, expectBefore, VERIFY_BEFORE);
 180         // Verify During
 181         verifyType(ci, expectDuring, VERIFY_DURING);
 182         // Verify After
 183         verifyType(ci, expectAfter, VERIFY_AFTER);
 184     }
 185 
 186     private static void verifyType(CollectionInfo ci, boolean shouldExist, String pattern) {
 187         if (shouldExist) {
 188             Asserts.assertTrue(ci.containsVerification(pattern), "Missing expected verification for: " + ci.getName());
 189         } else {
 190             Asserts.assertFalse(ci.containsVerification(pattern), "Found unexpected verification for: " + ci.getName());
 191         }
 192     }
 193 
 194     public static class CollectionInfo {
 195         String name;
 196         ArrayList<String> verification;
 197         public CollectionInfo(String name) {
 198             this.name = name;
 199             this.verification = new ArrayList<>();
 200             System.out.println("Created CollectionInfo: " + name);
 201         }
 202 
 203         public String getName() {
 204             return name;
 205         }
 206 
 207         public void addVerification(String verify) {
 208             System.out.println("Adding: " + verify);
 209             verification.add(verify);
 210         }
 211 
 212         public boolean containsVerification(String contains) {
 213             for (String entry : verification) {
 214                 if (entry.contains(contains)) {
 215                     return true;
 216                 }
 217             }
 218             return false;
 219         }
 220 
 221         static CollectionInfo parseFirst(String name, String data) {
 222             CollectionInfo result = null;
 223             int firstIndex = data.indexOf(name);
 224             if (firstIndex == -1) {
 225                 return result;
 226             }
 227             int nextIndex = data.indexOf(name, firstIndex + 1);
 228             if (nextIndex == -1) {
 229                 return result;
 230             }
 231             // Found an entry for this name
 232             result = new CollectionInfo(name);
 233             String collectionData = data.substring(firstIndex, nextIndex + name.length());
 234             for (String line : collectionData.split(System.getProperty("line.separator"))) {
 235                 if (line.contains(VERIFY_TAG)) {
 236                     result.addVerification(line);
 237                 }
 238             }
 239             return result;
 240         }
 241     }
 242 
 243     public static class TriggerGCs {
 244         public static void main(String args[]) throws Exception {
 245             WhiteBox wb = WhiteBox.getWhiteBox();
 246             // Trigger the different GCs using the WhiteBox API and System.gc()
 247             // to start a concurrent cycle with -XX:+ExplicitGCInvokesConcurrent.
 248             wb.fullGC();  // full
 249             System.gc();  // initial-mark, remark and cleanup
 250             // Sleep to make sure concurrent cycle is done
 251             Thread.sleep(1000);
 252             wb.youngGC(); // young-only
 253             wb.youngGC(); // mixed
 254         }
 255     }
 256 }