1 /*
   2  * Copyright (c) 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 TestNumWorkerOutput
  26  * @bug 8165292
  27  * @summary Check that when PrintGCDetails is enabled, gc,task output is printed only once per collection.
  28  * @key gc
  29  * @requires vm.gc=="null"
  30  * @modules java.base/jdk.internal.misc
  31  * @library /test/lib
  32  * @build sun.hotspot.WhiteBox
  33  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  34  * @run main/othervm -XX:+UseConcMarkSweepGC TestNumWorkerOutput UseConcMarkSweepGC
  35  * @run main/othervm -XX:+UseG1GC TestNumWorkerOutput UseG1GC
  36  */
  37 
  38 import sun.hotspot.WhiteBox;
  39 
  40 import java.util.regex.Matcher;
  41 import java.util.regex.Pattern;
  42 
  43 import jdk.test.lib.Platform;
  44 import jdk.test.lib.process.OutputAnalyzer;
  45 import jdk.test.lib.process.ProcessTools;
  46 
  47 import static jdk.test.lib.Asserts.*;
  48 
  49 public class TestNumWorkerOutput {
  50 
  51     public static void checkPatternOnce(String pattern, String what) throws Exception {
  52         Pattern r = Pattern.compile(pattern);
  53         Matcher m = r.matcher(what);
  54 
  55         if (!m.find()) {
  56             throw new RuntimeException("Could not find pattern " + pattern + " in output");
  57         }
  58         if (m.find()) {
  59             throw new RuntimeException("Could find pattern " + pattern + " in output more than once");
  60         }
  61     }
  62 
  63     public static void runTest(String gcArg) throws Exception {
  64         final String[] arguments = {
  65             "-Xbootclasspath/a:.",
  66             "-XX:+UnlockExperimentalVMOptions",
  67             "-XX:+UnlockDiagnosticVMOptions",
  68             "-XX:+WhiteBoxAPI",
  69             "-XX:+" + gcArg,
  70             "-Xmx10M",
  71             "-XX:+PrintGCDetails",
  72             GCTest.class.getName()
  73             };
  74 
  75         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(arguments);
  76         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  77 
  78         output.shouldHaveExitValue(0);
  79 
  80         System.out.println(output.getStdout());
  81 
  82         String stdout = output.getStdout();
  83 
  84         checkPatternOnce(".*[info.*].*[gc,task.*].*GC\\(0\\) .*Using \\d+ workers of \\d+ for evacuation.*", stdout);
  85     }
  86 
  87     public static void main(String[] args) throws Exception {
  88         runTest(args[0]);
  89     }
  90 
  91     static class GCTest {
  92         private static final WhiteBox WB = WhiteBox.getWhiteBox();
  93 
  94         public static Object holder;
  95 
  96         public static void main(String [] args) {
  97             holder = new byte[100];
  98             WB.youngGC();
  99             System.out.println(holder);
 100         }
 101     }
 102 }
 103