Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/Metaspace/MaxMetaspaceSizeEnvVarTest.java
32284 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 /testlibrary28* @run driver MaxMetaspaceSizeEnvVarTest29*/3031import java.io.PrintWriter;32import java.lang.management.ManagementFactory;33import java.lang.management.MemoryPoolMXBean;34import java.util.NoSuchElementException;3536import com.oracle.java.testlibrary.ProcessTools;37import com.oracle.java.testlibrary.OutputAnalyzer;3839public class MaxMetaspaceSizeEnvVarTest {4041// This is the test class we exec, passing the MaxMetaspaceSize flag42// by different mechanisms.43static class Main {44public static void main(String[] args) throws Exception {45long expected = Long.parseLong(args[0]);46MemoryPoolMXBean metaspaceMemoryPool =47ManagementFactory.getPlatformMXBeans(MemoryPoolMXBean.class)48.stream()49.filter(pool -> "Metaspace".equals(pool.getName()))50.findFirst()51.orElseThrow(() -> new NoSuchElementException("No value present"));52long max = metaspaceMemoryPool.getUsage().getMax();53System.out.println("Metaspace max usage is " + max);54if (max != expected) {55throw new RuntimeException("Metaspace max " + max +56" != " + expected);57}58}59}6061static void report(String msg) {62System.out.println(msg);63System.err.println(msg);64}6566public static void main(String... args) throws Exception {67final String max = String.valueOf(9 * 1024 * 1024); // 9 MB68final String flagRaw = "MaxMetaspaceSize=" + max;69final String flag = "-XX:" + flagRaw;70final String main = "MaxMetaspaceSizeEnvVarTest$Main";7172ProcessBuilder pb = null;73OutputAnalyzer output = null;7475int test = 1;76report("Test " + test + ": flag not set");7778Main.main(new String[] { "-1" }); // -1 == undefined size79report("------ end Test " + test);80test++;8182report("Test " + test + ": normal command-line flag");83pb = ProcessTools.createJavaProcessBuilder(flag, main, max);84output = new OutputAnalyzer(pb.start());85output.shouldHaveExitValue(0);86output.reportDiagnosticSummary();87report("------ end Test " + test);88test++;8990String[] envVars = {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