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