1 /*
   2  * Copyright (c) 2016, 2020, 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
  26  * @bug 8149991
  27  * @summary safepoint+cleanup=info should have output from the code
  28  * @library /test/lib
  29  * @modules java.base/jdk.internal.misc
  30  *          java.management
  31  * @run driver SafepointCleanupTest
  32  * @run driver SafepointCleanupTest -XX:+AsyncDeflateIdleMonitors
  33  */
  34 
  35 import jdk.test.lib.process.OutputAnalyzer;
  36 import jdk.test.lib.process.ProcessTools;
  37 
  38 public class SafepointCleanupTest {
  39     static final String ASYNC_DISABLE_OPTION = "-XX:-AsyncDeflateIdleMonitors";
  40     static final String ASYNC_ENABLE_OPTION = "-XX:+AsyncDeflateIdleMonitors";
  41     static final String UNLOCK_DIAG_OPTION = "-XX:+UnlockDiagnosticVMOptions";
  42 
  43     static void analyzeOutputOn(ProcessBuilder pb) throws Exception {
  44         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  45         output.shouldContain("[safepoint,cleanup]");
  46         output.shouldContain("safepoint cleanup tasks");
  47         output.shouldContain("deflating global idle monitors");
  48         output.shouldContain("deflating per-thread idle monitors");
  49         output.shouldContain("updating inline caches");
  50         output.shouldContain("compilation policy safepoint handler");
  51         output.shouldHaveExitValue(0);
  52     }
  53 
  54     static void analyzeOutputOff(ProcessBuilder pb) throws Exception {
  55         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  56         output.shouldNotContain("[safepoint,cleanup]");
  57         output.shouldHaveExitValue(0);
  58     }
  59 
  60     public static void main(String[] args) throws Exception {
  61         String async_option;
  62         if (args.length == 0) {
  63             // By default test deflating idle monitors at a safepoint.
  64             async_option = ASYNC_DISABLE_OPTION;
  65         } else {
  66             async_option = args[0];
  67         }
  68         if (!async_option.equals(ASYNC_DISABLE_OPTION) &&
  69             !async_option.equals(ASYNC_ENABLE_OPTION)) {
  70             throw new RuntimeException("Unknown async_option value: '"
  71                                        + async_option + "'");
  72         }
  73 
  74         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xlog:safepoint+cleanup=info",
  75                                                                   UNLOCK_DIAG_OPTION,
  76                                                                   async_option,
  77                                                                   InnerClass.class.getName());
  78         analyzeOutputOn(pb);
  79 
  80         pb = ProcessTools.createJavaProcessBuilder("-XX:+TraceSafepointCleanupTime",
  81                                                    UNLOCK_DIAG_OPTION,
  82                                                    async_option,
  83                                                    InnerClass.class.getName());
  84         analyzeOutputOn(pb);
  85 
  86         pb = ProcessTools.createJavaProcessBuilder("-Xlog:safepoint+cleanup=off",
  87                                                    UNLOCK_DIAG_OPTION,
  88                                                    async_option,
  89                                                    InnerClass.class.getName());
  90         analyzeOutputOff(pb);
  91 
  92         pb = ProcessTools.createJavaProcessBuilder("-XX:-TraceSafepointCleanupTime",
  93                                                    UNLOCK_DIAG_OPTION,
  94                                                    async_option,
  95                                                    InnerClass.class.getName());
  96         analyzeOutputOff(pb);
  97     }
  98 
  99     public static class InnerClass {
 100         public static void main(String[] args) throws Exception {
 101             System.out.println("Safepoint Cleanup test");
 102         }
 103     }
 104 }