Path: blob/master/test/jdk/sun/tools/jhsdb/heapconfig/JMapHeapConfigTest.java
41153 views
/*1* Copyright (c) 2015, 2020, 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*/2223import java.io.IOException;24import java.math.BigDecimal;25import java.util.ArrayList;26import java.util.Collections;27import java.util.HashMap;28import java.util.List;29import java.util.Map;3031import jdk.test.lib.apps.LingeredApp;32import jdk.test.lib.SA.SATestUtils;33import jdk.test.lib.Utils;3435/**36* @test37* @bug 804239738* @summary Unit test for jmap utility test heap configuration reader39*40* @requires vm.hasSA41* @library /test/lib42* @modules java.management43* jdk.hotspot.agent/sun.jvm.hotspot44*45* @build JMapHeapConfigTest TmtoolTestScenario46* @run main JMapHeapConfigTest47*/48public class JMapHeapConfigTest {4950static final String expectedJMapValues[] = {51"MinHeapFreeRatio",52"MaxHeapFreeRatio",53"MaxHeapSize",54"NewSize",55"OldSize",56"NewRatio",57"SurvivorRatio",58"MetaspaceSize",59"CompressedClassSpaceSize",60"G1HeapRegionSize"};6162// Test can't deal with negative jlongs:63// ignoring MaxMetaspaceSize64// ignoring MaxNewSize6566static final String desiredMaxHeapSize = "-Xmx128m";6768private static Map<String, String> parseJMapOutput(List<String> jmapOutput) {69Map<String, String> heapConfigMap = new HashMap<String, String>();70boolean shouldParse = false;7172for (String line : jmapOutput) {73line = line.trim();7475if (line.startsWith("Heap Configuration:")) {76shouldParse = true;77continue;78}7980if (line.startsWith("Heap Usage:")) {81shouldParse = false;82continue;83}8485if (shouldParse && !line.equals("")) {86String[] lv = line.split("\\s+");87try {88heapConfigMap.put(lv[0], lv[2]);89} catch (ArrayIndexOutOfBoundsException ex) {90// Ignore mailformed lines91}92}93}94return heapConfigMap;95}9697// Compare stored values98private static void compareValues(Map<String, String> parsedJMapOutput, Map<String, String> parsedVmOutput) {99for (String key : expectedJMapValues) {100try {101String jmapVal = parsedJMapOutput.get(key);102if (jmapVal == null) {103throw new RuntimeException("Key '" + key + "' doesn't exists in jmap output");104}105106String vmVal = parsedVmOutput.get(key);107if (vmVal == null) {108throw new RuntimeException("Key '" + key + "' doesn't exists in vm output");109}110111if (new BigDecimal(jmapVal).compareTo(new BigDecimal(vmVal)) != 0) {112throw new RuntimeException(String.format("Key %s doesn't match %s vs %s", key, vmVal, jmapVal));113}114} catch (NumberFormatException ex) {115throw new RuntimeException("Unexpected key '" + key + "' value", ex);116}117}118}119120public static void main(String[] args) throws Exception {121System.out.println("Starting JMapHeapConfigTest");122SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.123124if (!LingeredApp.isLastModifiedWorking()) {125// Exact behaviour of the test depends to operating system and the test nature,126// so just print the warning and continue127System.err.println("Warning! Last modified time doesn't work.");128}129130boolean mx_found = false;131String[] jvmOptions = Utils.getTestJavaOpts();132for (String option : jvmOptions) {133if (option.startsWith("-Xmx")) {134System.out.println("INFO: maximum heap size set by JTREG as " + option);135mx_found = true;136break;137}138}139140// Forward vm options to LingeredApp141ArrayList<String> cmd = new ArrayList();142Collections.addAll(cmd, Utils.getTestJavaOpts());143if (!mx_found) {144cmd.add(desiredMaxHeapSize);145System.out.println("INFO: maximum heap size set explicitly as " + desiredMaxHeapSize);146}147cmd.add("-XX:+PrintFlagsFinal");148149TmtoolTestScenario tmt = TmtoolTestScenario.create("jmap", "--heap");150int exitcode = tmt.launch(cmd.toArray(new String[0]));151if (exitcode != 0) {152throw new RuntimeException("Test FAILED jmap exits with non zero exit code " + exitcode);153}154155Map<String,String> parsedJmapOutput = parseJMapOutput(tmt.getToolOutput());156Map<String,String> parsedVMOutput = tmt.parseFlagsFinal();157158compareValues(parsedJmapOutput, parsedVMOutput);159160// If test fails it throws RuntimeException161System.out.println("Test PASSED");162}163}164165166