1 /* 2 * Copyright (c) 2015, 2016, Oracle 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 it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 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 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.io.IOException; 25 import java.math.BigDecimal; 26 import java.util.ArrayList; 27 import java.util.HashMap; 28 import java.util.List; 29 import java.util.Map; 30 31 import jdk.test.lib.apps.LingeredApp; 32 import jdk.testlibrary.Utils; 33 import jdk.test.lib.Platform; 34 35 /* 36 * @test 37 * @bug 8042397 38 * @summary Unit test for jmap utility test heap configuration reader 39 * 40 * @library /test/lib 41 * @library /lib/testlibrary 42 * @modules jdk.hotspot.agent/sun.jvm.hotspot 43 * java.management 44 * 45 * @build JMapHeapConfigTest TmtoolTestScenario 46 * @run main JMapHeapConfigTest 47 */ 48 public class JMapHeapConfigTest { 49 50 static final String expectedJMapValues[] = { 51 "MinHeapFreeRatio", 52 "MaxHeapFreeRatio", 53 "MaxHeapSize", 54 "NewSize", 55 "MaxNewSize", 56 "OldSize", 57 "NewRatio", 58 "SurvivorRatio", 59 "MetaspaceSize", 60 "CompressedClassSpaceSize", 61 "G1HeapRegionSize"}; 62 63 // ignoring MaxMetaspaceSize 64 65 static final String desiredMaxHeapSize = "-Xmx128m"; 66 67 private static Map<String, String> parseJMapOutput(List<String> jmapOutput) { 68 Map<String, String> heapConfigMap = new HashMap<String, String>(); 69 boolean shouldParse = false; 70 71 for (String line : jmapOutput) { 72 line = line.trim(); 73 74 if (line.startsWith("Heap Configuration:")) { 75 shouldParse = true; 76 continue; 77 } 78 79 if (line.startsWith("Heap Usage:")) { 80 shouldParse = false; 81 continue; 82 } 83 84 if (shouldParse && !line.equals("")) { 85 String[] lv = line.split("\\s+"); 86 try { 87 heapConfigMap.put(lv[0], lv[2]); 88 } catch (ArrayIndexOutOfBoundsException ex) { 89 // Ignore mailformed lines 90 } 91 } 92 } 93 return heapConfigMap; 94 } 95 96 // Compare stored values 97 private static void compareValues(Map<String, String> parsedJMapOutput, Map<String, String> parsedVmOutput) { 98 for (String key : expectedJMapValues) { 99 try { 100 String jmapVal = parsedJMapOutput.get(key); 101 if (jmapVal == null) { 102 throw new RuntimeException("Key '" + key + "' doesn't exists in jmap output"); 103 } 104 105 String vmVal = parsedVmOutput.get(key); 106 if (vmVal == null) { 107 throw new RuntimeException("Key '" + key + "' doesn't exists in vm output"); 108 } 109 110 if (new BigDecimal(jmapVal).compareTo(new BigDecimal(vmVal)) != 0) { 111 throw new RuntimeException(String.format("Key %s doesn't match %s vs %s", key, vmVal, jmapVal)); 112 } 113 } catch (NumberFormatException ex) { 114 throw new RuntimeException("Unexpected key '" + key + "' value", ex); 115 } 116 } 117 } 118 119 public static void main(String[] args) throws Exception { 120 System.out.println("Starting JMapHeapConfigTest"); 121 122 if (!Platform.shouldSAAttach()) { 123 // Silently skip the test if we don't have enough permissions to attach 124 System.err.println("Error! Insufficient permissions to attach."); 125 return; 126 } 127 128 if (!LingeredApp.isLastModifiedWorking()) { 129 // Exact behaviour of the test depends to operating system and the test nature, 130 // so just print the warning and continue 131 System.err.println("Warning! Last modified time doesn't work."); 132 } 133 134 boolean mx_found = false; 135 List<String> jvmOptions = Utils.getVmOptions(); 136 for (String option : jvmOptions) { 137 if (option.startsWith("-Xmx")) { 138 System.out.println("INFO: maximum heap size set by JTREG as " + option); 139 mx_found = true; 140 break; 141 } 142 } 143 144 // Forward vm options to LingeredApp 145 ArrayList<String> cmd = new ArrayList(); 146 cmd.addAll(Utils.getVmOptions()); 147 if (!mx_found) { 148 cmd.add(desiredMaxHeapSize); 149 System.out.println("INFO: maximum heap size set explicitly as " + desiredMaxHeapSize); 150 } 151 cmd.add("-XX:+PrintFlagsFinal"); 152 153 TmtoolTestScenario tmt = TmtoolTestScenario.create("jmap", "--heap"); 154 int exitcode = tmt.launch(cmd); 155 if (exitcode != 0) { 156 throw new RuntimeException("Test FAILED jmap exits with non zero exit code " + exitcode); 157 } 158 159 Map<String,String> parsedJmapOutput = parseJMapOutput(tmt.getToolOutput()); 160 Map<String,String> parsedVMOutput = tmt.parseFlagsFinal(); 161 162 compareValues(parsedJmapOutput, parsedVMOutput); 163 164 // If test fails it throws RuntimeException 165 System.out.println("Test PASSED"); 166 } 167 }