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