1 /* 2 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2018, Google and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 package gc.logging; 26 27 import java.io.File; 28 import java.net.URL; 29 import java.net.URLClassLoader; 30 import java.util.regex.Pattern; 31 import java.util.regex.Matcher; 32 33 import jdk.test.lib.Asserts; 34 import jdk.test.lib.process.OutputAnalyzer; 35 import jdk.test.lib.process.ProcessTools; 36 import sun.hotspot.WhiteBox; 37 38 /* 39 * @test TestMetaSpaceLog 40 * @bug 8211123 41 * @summary Ensure that the Metaspace is updated in the log 42 * @key gc 43 * @library /test/lib 44 * @modules java.base/jdk.internal.misc 45 * java.management 46 * @requires vm.gc != "Epsilon" 47 * @requires vm.gc != "Z" 48 * @requires vm.gc != "Shenandoah" 49 * 50 * @compile TestMetaSpaceLog.java 51 * @run driver ClassFileInstaller sun.hotspot.WhiteBox 52 * @run driver gc.logging.TestMetaSpaceLog 53 */ 54 55 public class TestMetaSpaceLog { 56 private static Pattern metaSpaceRegexp; 57 58 static { 59 // Do this once here. 60 metaSpaceRegexp = Pattern.compile(".*Metaspace: ([0-9]+).*->([0-9]+).*"); 61 } 62 63 public static void main(String[] args) throws Exception { 64 testMetaSpaceUpdate(); 65 } 66 67 private static void verifyContainsMetaSpaceUpdate(OutputAnalyzer output) { 68 // At least one metaspace line from GC should show GC being collected. 69 boolean foundCollectedMetaSpace = output.asLines().stream() 70 .filter(s -> s.contains("[gc,metaspace")) 71 .anyMatch(TestMetaSpaceLog::check); 72 Asserts.assertTrue(foundCollectedMetaSpace); 73 } 74 75 private static boolean check(String line) { 76 Matcher m = metaSpaceRegexp.matcher(line); 77 Asserts.assertTrue(m.matches(), "Unexpected line for metaspace logging: " + line); 78 long before = Long.parseLong(m.group(1)); 79 long after = Long.parseLong(m.group(2)); 80 return before > after; 81 } 82 83 private static void testMetaSpaceUpdate() throws Exception { 84 // Propagate test.src for the jar file. 85 String testSrc= "-Dtest.src=" + System.getProperty("test.src", "."); 86 87 ProcessBuilder pb = 88 ProcessTools.createJavaProcessBuilder( 89 true, 90 "-Xlog:gc*", 91 "-Xbootclasspath/a:.", 92 "-XX:+UnlockDiagnosticVMOptions", 93 "-XX:+WhiteBoxAPI", 94 "-Xmx1000M", 95 "-Xms1000M", 96 testSrc, StressMetaSpace.class.getName()); 97 98 OutputAnalyzer output = null; 99 try { 100 output = new OutputAnalyzer(pb.start()); 101 verifyContainsMetaSpaceUpdate(output); 102 } catch (Exception e) { 103 // For error diagnosis: print and throw. 104 e.printStackTrace(); 105 output.reportDiagnosticSummary(); 106 throw e; 107 } 108 } 109 110 static class StressMetaSpace { 111 private static URL[] urls = new URL[1]; 112 113 static { 114 try { 115 File jarFile = new File(System.getProperty("test.src") + "/testcases.jar"); 116 urls[0] = jarFile.toURI().toURL(); 117 } catch (Exception e) { 118 e.printStackTrace(); 119 } 120 } 121 122 public static void main(String args[]) { 123 WhiteBox wb = WhiteBox.getWhiteBox(); 124 for(int i = 0; i < 10000; i++) { 125 loadClass(wb); 126 } 127 wb.fullGC(); 128 } 129 130 public static void loadClass(WhiteBox wb) { 131 try { 132 URLClassLoader ucl = new URLClassLoader(urls); 133 Class.forName("case00", false, ucl); 134 } catch (Exception e) { 135 e.printStackTrace(); 136 } 137 } 138 } 139 }