Path: blob/master/test/hotspot/jtreg/runtime/Metaspace/MaxMetaspaceSizeEnvVarTest.java
40942 views
/*1* Copyright (c) 2021, 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* @bug 826034926* @summary test that setting via the env-var and options file shows up as expected27* @library /test/lib28* @run driver MaxMetaspaceSizeEnvVarTest29*/3031import java.io.PrintWriter;32import java.lang.management.ManagementFactory;33import java.lang.management.MemoryPoolMXBean;3435import jdk.test.lib.process.ProcessTools;36import jdk.test.lib.process.OutputAnalyzer;3738public class MaxMetaspaceSizeEnvVarTest {3940// This is the test class we exec, passing the MaxMetaspaceSize flag41// by different mechanisms.42static class Main {43public static void main(String[] args) throws Exception {44long expected = Long.parseLong(args[0]);45MemoryPoolMXBean metaspaceMemoryPool =46ManagementFactory.getPlatformMXBeans(MemoryPoolMXBean.class)47.stream()48.filter(pool -> "Metaspace".equals(pool.getName()))49.findFirst()50.orElseThrow();51long max = metaspaceMemoryPool.getUsage().getMax();52System.out.println("Metaspace max usage is " + max);53if (max != expected) {54throw new RuntimeException("Metaspace max " + max +55" != " + expected);56}57}58}5960static void report(String msg) {61System.out.println(msg);62System.err.println(msg);63}6465public static void main(String... args) throws Exception {66final String max = String.valueOf(9 * 1024 * 1024); // 9 MB67final String flagRaw = "MaxMetaspaceSize=" + max;68final String flag = "-XX:" + flagRaw;69final String main = "MaxMetaspaceSizeEnvVarTest$Main";7071ProcessBuilder pb = null;72OutputAnalyzer output = null;7374int test = 1;75report("Test " + test + ": flag not set");7677Main.main(new String[] { "-1" }); // -1 == undefined size78report("------ end Test " + test);79test++;8081report("Test " + test + ": normal command-line flag");82pb = ProcessTools.createJavaProcessBuilder(flag, main, max);83output = new OutputAnalyzer(pb.start());84output.shouldHaveExitValue(0);85output.reportDiagnosticSummary();86report("------ end Test " + test);87test++;8889String[] envVars = {90"JDK_JAVA_OPTIONS",91"_JAVA_OPTIONS",92"JAVA_TOOL_OPTIONS"93};9495for (String envVar : envVars) {96report("Test " + test + ": " + envVar + " env-var");97pb = ProcessTools.createJavaProcessBuilder(main, max);98pb.environment().put(envVar, flag);99output = new OutputAnalyzer(pb.start());100output.shouldHaveExitValue(0);101output.reportDiagnosticSummary();102report("------ end Test " + test);103test++;104}105106report("Test " + test + ": .hotspotrc file");107final String rcFile = ".hotspotrc";108final String rcFileFlag = "-XX:Flags=" + rcFile;109110PrintWriter pw = new PrintWriter(rcFile);111pw.println(flagRaw);112pw.close();113pb = ProcessTools.createJavaProcessBuilder(rcFileFlag, main, max);114output = new OutputAnalyzer(pb.start());115output.shouldHaveExitValue(0);116output.reportDiagnosticSummary();117report("------ end Test " + test);118}119}120121122