Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/class_unloading/TestCMSClassUnloadingEnabledHWM.java
32283 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key gc26* @bug 804983127* @library /testlibrary /testlibrary/whitebox28* @build TestCMSClassUnloadingEnabledHWM29* @run main ClassFileInstaller sun.hotspot.WhiteBox30* @run driver TestCMSClassUnloadingEnabledHWM31* @summary Test that -XX:-CMSClassUnloadingEnabled will trigger a Full GC when more than MetaspaceSize metadata is allocated.32*/3334import com.oracle.java.testlibrary.OutputAnalyzer;35import com.oracle.java.testlibrary.ProcessTools;36import java.lang.management.GarbageCollectorMXBean;37import java.lang.management.ManagementFactory;38import java.util.ArrayList;39import java.util.Arrays;40import sun.hotspot.WhiteBox;4142public class TestCMSClassUnloadingEnabledHWM {43private static long MetaspaceSize = 32 * 1024 * 1024;44private static long YoungGenSize = 32 * 1024 * 1024;4546private static OutputAnalyzer run(boolean enableUnloading) throws Exception {47ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(48"-Xbootclasspath/a:.",49"-XX:+UnlockDiagnosticVMOptions",50"-XX:+WhiteBoxAPI",51"-Xmx128m",52"-XX:CMSMaxAbortablePrecleanTime=1",53"-XX:CMSWaitDuration=50",54"-XX:MetaspaceSize=" + MetaspaceSize,55"-Xmn" + YoungGenSize,56"-XX:+UseConcMarkSweepGC",57"-XX:" + (enableUnloading ? "+" : "-") + "CMSClassUnloadingEnabled",58"-XX:+PrintHeapAtGC",59"-XX:+PrintGCDetails",60"-XX:+PrintGCTimeStamps",61TestCMSClassUnloadingEnabledHWM.AllocateBeyondMetaspaceSize.class.getName(),62"" + MetaspaceSize);63return new OutputAnalyzer(pb.start());64}6566public static OutputAnalyzer runWithCMSClassUnloading() throws Exception {67return run(true);68}6970public static OutputAnalyzer runWithoutCMSClassUnloading() throws Exception {71return run(false);72}7374public static void testWithoutCMSClassUnloading() throws Exception {75// -XX:-CMSClassUnloadingEnabled is used, so we expect a full GC instead of a concurrent cycle.76OutputAnalyzer out = runWithoutCMSClassUnloading();7778out.shouldMatch(".*Full GC.*");79out.shouldNotMatch(".*CMS Initial Mark.*");80}8182public static void testWithCMSClassUnloading() throws Exception {83// -XX:+CMSClassUnloadingEnabled is used, so we expect a concurrent cycle instead of a full GC.84OutputAnalyzer out = runWithCMSClassUnloading();8586out.shouldMatch(".*CMS Initial Mark.*");87out.shouldNotMatch(".*Full GC.*");88}8990public static void main(String args[]) throws Exception {91testWithCMSClassUnloading();92testWithoutCMSClassUnloading();93}9495public static class AllocateBeyondMetaspaceSize {96public static void main(String [] args) throws Exception {97if (args.length != 1) {98throw new IllegalArgumentException("Usage: <MetaspaceSize>");99}100101WhiteBox wb = WhiteBox.getWhiteBox();102103// Allocate past the MetaspaceSize limit.104long metaspaceSize = Long.parseLong(args[0]);105long allocationBeyondMetaspaceSize = metaspaceSize * 2;106long metaspace = wb.allocateMetaspace(null, allocationBeyondMetaspaceSize);107108// Wait for at least one GC to occur. The caller will parse the log files produced.109GarbageCollectorMXBean cmsGCBean = getCMSGCBean();110while (cmsGCBean.getCollectionCount() == 0) {111Thread.sleep(100);112}113114wb.freeMetaspace(null, metaspace, metaspace);115}116117private static GarbageCollectorMXBean getCMSGCBean() {118for (GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) {119if (gcBean.getObjectName().toString().equals("java.lang:type=GarbageCollector,name=ConcurrentMarkSweep")) {120return gcBean;121}122}123return null;124}125}126}127128129130