1 /*
   2  * Copyright (c) 2015, 2016, 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 TestVerifyBeforeAndAfterGCFlags
  26  * @key gc
  27  * @bug 8000831
  28  * @summary Runs an simple application (GarbageProducer) with various
  29          combinations of -XX:{+|-}Verify{After|Before}GC flags and checks that
  30          output contain or doesn't contain expected patterns
  31  * @modules java.base/jdk.internal.misc
  32  * @modules java.management
  33  * @library /test/lib
  34  * @run driver TestVerifyBeforeAndAfterGCFlags
  35  */
  36 
  37 import java.util.ArrayList;
  38 import java.util.Collections;
  39 
  40 import jdk.test.lib.Utils;
  41 import jdk.test.lib.process.OutputAnalyzer;
  42 import jdk.test.lib.process.ProcessTools;
  43 
  44 public class TestVerifyBeforeAndAfterGCFlags {
  45 
  46     // VerifyBeforeGC:[Verifying threads heap tenured eden syms strs zone dict metaspace chunks hand code cache ]
  47     public static final String VERIFY_BEFORE_GC_PATTERN = "Verifying Before GC";
  48     // VerifyBeforeGC: VerifyBeforeGC: VerifyBeforeGC:
  49     public static final String VERIFY_BEFORE_GC_CORRUPTED_PATTERN = "VerifyBeforeGC:(?!\\[Verifying[^]]+\\])";
  50 
  51     // VerifyAfterGC:[Verifying threads heap tenured eden syms strs zone dict metaspace chunks hand code cache ]
  52     public static final String VERIFY_AFTER_GC_PATTERN = "Verifying After GC";
  53     // VerifyAfterGC: VerifyAfterGC: VerifyAfterGC:
  54     public static final String VERIFY_AFTER_GC_CORRUPTED_PATTERN = "VerifyAfterGC:(?!\\[Verifying[^]]+\\])";
  55 
  56     public static void main(String args[]) throws Exception {
  57         String[] filteredOpts = Utils.getFilteredTestJavaOpts(
  58                                     new String[] { "-Xlog:gc+verify=debug",
  59                                                    "-XX:+UseGCLogFileRotation",
  60                                                    "-XX:-DisplayVMOutput",
  61                                                    "VerifyBeforeGC",
  62                                                    "VerifyAfterGC" });
  63         testVerifyFlags(false, false, filteredOpts);
  64         testVerifyFlags(true,  true,  filteredOpts);
  65         testVerifyFlags(true,  false, filteredOpts);
  66         testVerifyFlags(false, true,  filteredOpts);
  67     }
  68 
  69     public static void testVerifyFlags(boolean verifyBeforeGC,
  70                                        boolean verifyAfterGC,
  71                                        String[] opts) throws Exception {
  72         ArrayList<String> vmOpts = new ArrayList<>();
  73         if (opts != null && (opts.length > 0)) {
  74             Collections.addAll(vmOpts, opts);
  75         }
  76 
  77         Collections.addAll(vmOpts, new String[] {
  78                                        "-Xlog:gc+verify=debug",
  79                                        "-Xmx5m",
  80                                        "-Xms5m",
  81                                        "-Xmn3m",
  82                                        "-XX:+UnlockDiagnosticVMOptions",
  83                                        (verifyBeforeGC ? "-XX:+VerifyBeforeGC"
  84                                                        : "-XX:-VerifyBeforeGC"),
  85                                        (verifyAfterGC ? "-XX:+VerifyAfterGC"
  86                                                       : "-XX:-VerifyAfterGC"),
  87                                        GarbageProducer.class.getName() });
  88         ProcessBuilder procBuilder =
  89             ProcessTools.createJavaProcessBuilder(vmOpts.toArray(
  90                                                    new String[vmOpts.size()]));
  91         OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start());
  92 
  93         analyzer.shouldHaveExitValue(0);
  94         analyzer.shouldNotMatch(VERIFY_BEFORE_GC_CORRUPTED_PATTERN);
  95         analyzer.shouldNotMatch(VERIFY_AFTER_GC_CORRUPTED_PATTERN);
  96 
  97         if (verifyBeforeGC) {
  98             analyzer.shouldMatch(VERIFY_BEFORE_GC_PATTERN);
  99         } else {
 100             analyzer.shouldNotMatch(VERIFY_BEFORE_GC_PATTERN);
 101         }
 102 
 103         if (verifyAfterGC) {
 104             analyzer.shouldMatch(VERIFY_AFTER_GC_PATTERN);
 105         } else {
 106             analyzer.shouldNotMatch(VERIFY_AFTER_GC_PATTERN);
 107         }
 108     }
 109 
 110     public static class GarbageProducer {
 111         static long[][] garbage = new long[10][];
 112 
 113         public static void main(String args[]) {
 114             int j = 0;
 115             for(int i = 0; i<1000; i++) {
 116                 garbage[j] = new long[10000];
 117                 j = (j+1)%garbage.length;
 118             }
 119         }
 120     }
 121 }