Path: blob/master/test/hotspot/jtreg/gc/logging/TestMetaSpaceLog.java
40942 views
/*1* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2018, Google 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324package gc.logging;2526import java.io.File;27import java.net.URL;28import java.net.URLClassLoader;29import java.util.regex.Pattern;30import java.util.regex.Matcher;3132import jdk.test.lib.Asserts;33import jdk.test.lib.process.OutputAnalyzer;34import jdk.test.lib.process.ProcessTools;35import sun.hotspot.WhiteBox;3637/*38* @test TestMetaSpaceLog39* @bug 821112340* @summary Ensure that the Metaspace is updated in the log41* @library /test/lib42* @modules java.base/jdk.internal.misc43* java.management44* @requires vm.gc != "Epsilon"45* @requires vm.gc != "Z"46* @requires os.maxMemory >= 2G47*48* @compile TestMetaSpaceLog.java49* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox50* @run driver gc.logging.TestMetaSpaceLog51*/5253public class TestMetaSpaceLog {54private static Pattern metaSpaceRegexp;5556static {57// Do this once here.58// Scan for Metaspace update notices as part of the GC log, e.g. in this form:59// [gc,metaspace ] GC(0) Metaspace: 11895K(14208K)->11895K(14208K) NonClass: 10552K(12544K)->10552K(12544K) Class: 1343K(1664K)->1343K(1664K)60metaSpaceRegexp = Pattern.compile(".*Metaspace: ([0-9]+).*->([0-9]+).*");61}6263public static void main(String[] args) throws Exception {64testMetaSpaceUpdate();65}6667private static void verifyContainsMetaSpaceUpdate(OutputAnalyzer output) {68// At least one metaspace line from GC should show GC being collected.69boolean foundCollectedMetaSpace = output.asLines().stream()70.filter(s -> s.contains("[gc,metaspace"))71.anyMatch(TestMetaSpaceLog::check);72Asserts.assertTrue(foundCollectedMetaSpace);73}7475private static boolean check(String line) {76Matcher m = metaSpaceRegexp.matcher(line);77if (m.matches()) {78// Numbers for Metaspace occupation should grow.79long before = Long.parseLong(m.group(1));80long after = Long.parseLong(m.group(2));81return before > after;82}83return false;84}8586private static void testMetaSpaceUpdate() throws Exception {87// Propagate test.src for the jar file.88String testSrc= "-Dtest.src=" + System.getProperty("test.src", ".");8990ProcessBuilder pb =91ProcessTools.createTestJvm(92"-Xlog:gc*",93"-Xbootclasspath/a:.",94"-XX:+UnlockDiagnosticVMOptions",95"-XX:+WhiteBoxAPI",96"-Xmx1000M",97"-Xms1000M",98testSrc, StressMetaSpace.class.getName());99100OutputAnalyzer output = null;101try {102output = new OutputAnalyzer(pb.start());103verifyContainsMetaSpaceUpdate(output);104} catch (Exception e) {105// For error diagnosis: print and throw.106e.printStackTrace();107output.reportDiagnosticSummary();108throw e;109}110}111112static class StressMetaSpace {113private static URL[] urls = new URL[1];114115static {116try {117File jarFile = new File(System.getProperty("test.src") + "/testcases.jar");118urls[0] = jarFile.toURI().toURL();119} catch (Exception e) {120e.printStackTrace();121}122}123124public static void main(String args[]) {125WhiteBox wb = WhiteBox.getWhiteBox();126for(int i = 0; i < 10000; i++) {127loadClass(wb);128}129wb.fullGC();130}131132public static void loadClass(WhiteBox wb) {133try {134URLClassLoader ucl = new URLClassLoader(urls);135Class.forName("case00", false, ucl);136} catch (Exception e) {137e.printStackTrace();138}139}140}141}142143144