test/hotspot/jtreg/gc/class_unloading/TestCMSClassUnloadingEnabledHWM.java
changeset 59053 ba6c248cae19
parent 59051 f0312c7d5b37
child 59055 57ad70bcf06c
equal deleted inserted replaced
59051:f0312c7d5b37 59053:ba6c248cae19
     1 /*
       
     2  * Copyright (c) 2014, 2019, 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 package gc.class_unloading;
       
    25 
       
    26 /*
       
    27  * @test
       
    28  * @key gc
       
    29  * @bug 8049831
       
    30  * @requires vm.gc.ConcMarkSweep & !vm.graal.enabled
       
    31  * @library /test/lib
       
    32  * @modules java.base/jdk.internal.misc
       
    33  *          java.management
       
    34  * @build sun.hotspot.WhiteBox
       
    35  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
       
    36  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
       
    37  * @run driver gc.class_unloading.TestCMSClassUnloadingEnabledHWM
       
    38  * @summary Test that -XX:-CMSClassUnloadingEnabled will trigger a Full GC when more than MetaspaceSize metadata is allocated.
       
    39  */
       
    40 
       
    41 import jdk.test.lib.process.OutputAnalyzer;
       
    42 import jdk.test.lib.process.ProcessTools;
       
    43 import java.lang.management.GarbageCollectorMXBean;
       
    44 import java.lang.management.ManagementFactory;
       
    45 import sun.hotspot.WhiteBox;
       
    46 
       
    47 public class TestCMSClassUnloadingEnabledHWM {
       
    48   private static long MetaspaceSize = 32 * 1024 * 1024;
       
    49   private static long YoungGenSize  = 32 * 1024 * 1024;
       
    50 
       
    51   private static OutputAnalyzer run(boolean enableUnloading) throws Exception {
       
    52     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
       
    53       "-Xbootclasspath/a:.",
       
    54       "-XX:+UnlockDiagnosticVMOptions",
       
    55       "-XX:+WhiteBoxAPI",
       
    56       "-Xmx128m",
       
    57       "-XX:CMSMaxAbortablePrecleanTime=1",
       
    58       "-XX:CMSWaitDuration=50",
       
    59       "-XX:MetaspaceSize=" + MetaspaceSize,
       
    60       "-Xmn" + YoungGenSize,
       
    61       "-XX:+UseConcMarkSweepGC",
       
    62       "-XX:" + (enableUnloading ? "+" : "-") + "CMSClassUnloadingEnabled",
       
    63       "-Xlog:gc",
       
    64       TestCMSClassUnloadingEnabledHWM.AllocateBeyondMetaspaceSize.class.getName(),
       
    65       "" + MetaspaceSize);
       
    66     return new OutputAnalyzer(pb.start());
       
    67   }
       
    68 
       
    69   public static OutputAnalyzer runWithCMSClassUnloading() throws Exception {
       
    70     return run(true);
       
    71   }
       
    72 
       
    73   public static OutputAnalyzer runWithoutCMSClassUnloading() throws Exception {
       
    74     return run(false);
       
    75   }
       
    76 
       
    77   public static void testWithoutCMSClassUnloading() throws Exception {
       
    78     // -XX:-CMSClassUnloadingEnabled is used, so we expect a full GC instead of a concurrent cycle.
       
    79     OutputAnalyzer out = runWithoutCMSClassUnloading();
       
    80 
       
    81     out.shouldMatch(".*Pause Full.*");
       
    82     out.shouldNotMatch(".*Pause Initial Mark.*");
       
    83   }
       
    84 
       
    85   public static void testWithCMSClassUnloading() throws Exception {
       
    86     // -XX:+CMSClassUnloadingEnabled is used, so we expect a concurrent cycle instead of a full GC.
       
    87     OutputAnalyzer out = runWithCMSClassUnloading();
       
    88 
       
    89     out.shouldMatch(".*Pause Initial Mark.*");
       
    90     out.shouldNotMatch(".*Pause Full.*");
       
    91   }
       
    92 
       
    93   public static void main(String args[]) throws Exception {
       
    94     testWithCMSClassUnloading();
       
    95     testWithoutCMSClassUnloading();
       
    96   }
       
    97 
       
    98   public static class AllocateBeyondMetaspaceSize {
       
    99     public static void main(String [] args) throws Exception {
       
   100       if (args.length != 1) {
       
   101         throw new IllegalArgumentException("Usage: <MetaspaceSize>");
       
   102       }
       
   103 
       
   104       WhiteBox wb = WhiteBox.getWhiteBox();
       
   105 
       
   106       // Allocate past the MetaspaceSize limit.
       
   107       long metaspaceSize = Long.parseLong(args[0]);
       
   108       long allocationBeyondMetaspaceSize  = metaspaceSize * 2;
       
   109       long metaspace = wb.allocateMetaspace(null, allocationBeyondMetaspaceSize);
       
   110 
       
   111       // Wait for at least one GC to occur. The caller will parse the log files produced.
       
   112       GarbageCollectorMXBean cmsGCBean = getCMSGCBean();
       
   113       while (cmsGCBean.getCollectionCount() == 0) {
       
   114         Thread.sleep(100);
       
   115       }
       
   116 
       
   117       wb.freeMetaspace(null, metaspace, metaspace);
       
   118     }
       
   119 
       
   120     private static GarbageCollectorMXBean getCMSGCBean() {
       
   121       for (GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) {
       
   122         if (gcBean.getObjectName().toString().equals("java.lang:type=GarbageCollector,name=ConcurrentMarkSweep")) {
       
   123           return gcBean;
       
   124         }
       
   125       }
       
   126       return null;
       
   127     }
       
   128   }
       
   129 }
       
   130