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