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 = "Verifying Before GC";
  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 = "Verifying After GC";
  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[] { "-Xlog:gc+verify=debug",
  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                                        "-Xlog:gc+verify=debug",
  78                                        "-Xmx5m",
  79                                        "-Xms5m",
  80                                        "-Xmn3m",
  81                                        "-XX:+UnlockDiagnosticVMOptions",
  82                                        (verifyBeforeGC ? "-XX:+VerifyBeforeGC"
  83                                                        : "-XX:-VerifyBeforeGC"),
  84                                        (verifyAfterGC ? "-XX:+VerifyAfterGC"
  85                                                       : "-XX:-VerifyAfterGC"),
  86                                        GarbageProducer.class.getName() });
  87         ProcessBuilder procBuilder =
  88             ProcessTools.createJavaProcessBuilder(vmOpts.toArray(
  89                                                    new String[vmOpts.size()]));
  90         OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start());
  91 
  92         analyzer.shouldHaveExitValue(0);
  93         analyzer.shouldNotMatch(VERIFY_BEFORE_GC_CORRUPTED_PATTERN);
  94         analyzer.shouldNotMatch(VERIFY_AFTER_GC_CORRUPTED_PATTERN);
  95 
  96         if (verifyBeforeGC) {
  97             analyzer.shouldMatch(VERIFY_BEFORE_GC_PATTERN);
  98         } else {
  99             analyzer.shouldNotMatch(VERIFY_BEFORE_GC_PATTERN);
 100         }
 101 
 102         if (verifyAfterGC) {
 103             analyzer.shouldMatch(VERIFY_AFTER_GC_PATTERN);
 104         } else {
 105             analyzer.shouldNotMatch(VERIFY_AFTER_GC_PATTERN);
 106         }
 107     }
 108 
 109     public static class GarbageProducer {
 110         static long[][] garbage = new long[10][];
 111 
 112         public static void main(String args[]) {
 113             int j = 0;
 114             for(int i = 0; i<1000; i++) {
 115                 garbage[j] = new long[10000];
 116                 j = (j+1)%garbage.length;
 117             }
 118         }
 119     }
 120 }
--- EOF ---