TestCMSClassUnloadingEnabledHWM.java revision 9727:f944761a3ce3
148391Speter/*
248391Speter * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
348391Speter * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
448391Speter *
548391Speter * This code is free software; you can redistribute it and/or modify it
648391Speter * under the terms of the GNU General Public License version 2 only, as
748391Speter * published by the Free Software Foundation.
848391Speter *
948391Speter * This code is distributed in the hope that it will be useful, but WITHOUT
1048391Speter * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1148391Speter * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1248391Speter * version 2 for more details (a copy is included in the LICENSE file that
1348391Speter * accompanied this code).
1448391Speter *
1548391Speter * You should have received a copy of the GNU General Public License version
1648391Speter * 2 along with this work; if not, write to the Free Software Foundation,
1748391Speter * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1848391Speter *
1948391Speter * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2048391Speter * or visit www.oracle.com if you need additional information or have any
2148391Speter * questions.
2248391Speter */
2348391Speter
2448391Speter/*
2548391Speter * @test
2648391Speter * @key gc
2748391Speter * @bug 8049831
2848391Speter * @library /testlibrary /test/lib
2948391Speter * @modules java.base/sun.misc
3048391Speter *          java.management
3148391Speter * @build TestCMSClassUnloadingEnabledHWM
3270317Sjake * @run main ClassFileInstaller sun.hotspot.WhiteBox
3374927Sjhb *                              sun.hotspot.WhiteBox$WhiteBoxPermission
3474927Sjhb * @run driver TestCMSClassUnloadingEnabledHWM
3555722Simp * @summary Test that -XX:-CMSClassUnloadingEnabled will trigger a Full GC when more than MetaspaceSize metadata is allocated.
3655539Sluoqi */
3774927Sjhb
3848391Speterimport jdk.test.lib.OutputAnalyzer;
3948391Speterimport jdk.test.lib.ProcessTools;
4048391Speterimport java.lang.management.GarbageCollectorMXBean;
4148391Speterimport java.lang.management.ManagementFactory;
4248391Speterimport java.util.ArrayList;
4348391Speterimport java.util.Arrays;
4448391Speterimport sun.hotspot.WhiteBox;
4548391Speter
4648391Speterpublic class TestCMSClassUnloadingEnabledHWM {
4748391Speter  private static long MetaspaceSize = 32 * 1024 * 1024;
4848391Speter  private static long YoungGenSize  = 32 * 1024 * 1024;
4948391Speter
5048391Speter  private static OutputAnalyzer run(boolean enableUnloading) throws Exception {
5148391Speter    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
5248391Speter      "-Xbootclasspath/a:.",
5348391Speter      "-XX:+UnlockDiagnosticVMOptions",
5448391Speter      "-XX:+WhiteBoxAPI",
5548391Speter      "-Xmx128m",
5648391Speter      "-XX:CMSMaxAbortablePrecleanTime=1",
5748391Speter      "-XX:CMSWaitDuration=50",
58104354Sscottl      "-XX:MetaspaceSize=" + MetaspaceSize,
5948391Speter      "-Xmn" + YoungGenSize,
6048391Speter      "-XX:+UseConcMarkSweepGC",
6148391Speter      "-XX:" + (enableUnloading ? "+" : "-") + "CMSClassUnloadingEnabled",
6248391Speter      "-Xlog:gc",
6348391Speter      TestCMSClassUnloadingEnabledHWM.AllocateBeyondMetaspaceSize.class.getName(),
6465557Sjasone      "" + MetaspaceSize);
6548391Speter    return new OutputAnalyzer(pb.start());
6665557Sjasone  }
6765557Sjasone
6865557Sjasone  public static OutputAnalyzer runWithCMSClassUnloading() throws Exception {
6965557Sjasone    return run(true);
7065557Sjasone  }
7165557Sjasone
7248391Speter  public static OutputAnalyzer runWithoutCMSClassUnloading() throws Exception {
7348391Speter    return run(false);
7448391Speter  }
75104354Sscottl
7648391Speter  public static void testWithoutCMSClassUnloading() throws Exception {
7748391Speter    // -XX:-CMSClassUnloadingEnabled is used, so we expect a full GC instead of a concurrent cycle.
7848391Speter    OutputAnalyzer out = runWithoutCMSClassUnloading();
79103216Sjulian
8048391Speter    out.shouldMatch(".*Pause Full.*");
8148391Speter    out.shouldNotMatch(".*Pause Initial Mark.*");
82114434Sdes  }
8365557Sjasone
8465557Sjasone  public static void testWithCMSClassUnloading() throws Exception {
8590375Speter    // -XX:+CMSClassUnloadingEnabled is used, so we expect a concurrent cycle instead of a full GC.
86104354Sscottl    OutputAnalyzer out = runWithCMSClassUnloading();
8748391Speter
8848391Speter    out.shouldMatch(".*Pause Initial Mark.*");
8948391Speter    out.shouldNotMatch(".*Pause Full.*");
9048391Speter  }
9148391Speter
9248391Speter  public static void main(String args[]) throws Exception {
9348391Speter    testWithCMSClassUnloading();
9448391Speter    testWithoutCMSClassUnloading();
9571559Sjhb  }
9671559Sjhb
97114983Sjhb  public static class AllocateBeyondMetaspaceSize {
98114983Sjhb    public static void main(String [] args) throws Exception {
99114983Sjhb      if (args.length != 1) {
10073911Sjhb        throw new IllegalArgumentException("Usage: <MetaspaceSize>");
10171559Sjhb      }
10248391Speter
10348391Speter      WhiteBox wb = WhiteBox.getWhiteBox();
10448391Speter
10548391Speter      // Allocate past the MetaspaceSize limit.
10648391Speter      long metaspaceSize = Long.parseLong(args[0]);
10748391Speter      long allocationBeyondMetaspaceSize  = metaspaceSize * 2;
10848391Speter      long metaspace = wb.allocateMetaspace(null, allocationBeyondMetaspaceSize);
109103216Sjulian
110103216Sjulian      // Wait for at least one GC to occur. The caller will parse the log files produced.
111103216Sjulian      GarbageCollectorMXBean cmsGCBean = getCMSGCBean();
11248391Speter      while (cmsGCBean.getCollectionCount() == 0) {
11369657Sjhb        Thread.sleep(100);
11469657Sjhb      }
115113631Sjhb
116103216Sjulian      wb.freeMetaspace(null, metaspace, metaspace);
117113631Sjhb    }
11869657Sjhb
11969657Sjhb    private static GarbageCollectorMXBean getCMSGCBean() {
12048391Speter      for (GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) {
12148391Speter        if (gcBean.getObjectName().toString().equals("java.lang:type=GarbageCollector,name=ConcurrentMarkSweep")) {
12248391Speter          return gcBean;
12348391Speter        }
12448391Speter      }
12548391Speter      return null;
12686293Speter    }
12786293Speter  }
12870317Sjake}
12986293Speter
13086293Speter