1 /* 2 * Copyright (c) 2015, 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.testlibrary.Platform; 34 35 /* 36 * @test 37 * @bug 8042397 38 * @summary Unit test for jmap utility test heap configuration reader 39 * @library /test/lib/share/classes 40 * @library /lib/testlibrary 41 * @modules java.management 42 * @build jdk.testlibrary.* 43 * @build jdk.test.lib.apps.* 44 * @build JMapHeapConfigTest TmtoolTestScenario 45 * @run main JMapHeapConfigTest 46 */ 47 public class JMapHeapConfigTest { 48 49 static final String expectedJMapValues[] = { 50 "MinHeapFreeRatio", 51 "MaxHeapFreeRatio", 52 "MaxHeapSize", 53 "NewSize", 54 "MaxNewSize", 55 "OldSize", 56 "NewRatio", 57 "SurvivorRatio", 58 "MetaspaceSize", 59 "CompressedClassSpaceSize", 60 "G1HeapRegionSize"}; 61 62 // ignoring MaxMetaspaceSize 63 64 static final String desiredMaxHeapSize = "-Xmx128m"; 65 66 private static Map<String, String> parseJMapOutput(List<String> jmapOutput) { 67 Map<String, String> heapConfigMap = new HashMap<String, String>(); 68 boolean shouldParse = false; 69 70 for (String line : jmapOutput) { 71 line = line.trim(); 72 73 if (line.startsWith("Heap Configuration:")) { 74 shouldParse = true; 75 continue; 76 } 77 78 if (line.startsWith("Heap Usage:")) { 79 shouldParse = false; 80 continue; 81 } 82 83 if (shouldParse && !line.equals("")) { 84 String[] lv = line.split("\\s+"); 85 try { 86 heapConfigMap.put(lv[0], lv[2]); 87 } catch (ArrayIndexOutOfBoundsException ex) { 88 // Ignore mailformed lines 89 } 90 } 91 } 92 return heapConfigMap; 93 } 94 95 // Compare stored values 96 private static void compareValues(Map<String, String> parsedJMapOutput, Map<String, String> parsedVmOutput) { 97 for (String key : expectedJMapValues) { 98 try { 99 String jmapVal = parsedJMapOutput.get(key); 100 if (jmapVal == null) { 101 throw new RuntimeException("Key '" + key + "' doesn't exists in jmap output"); 102 } 103 104 String vmVal = parsedVmOutput.get(key); 105 if (vmVal == null) { 106 throw new RuntimeException("Key '" + key + "' doesn't exists in vm output"); 107 } 108 109 if (new BigDecimal(jmapVal).compareTo(new BigDecimal(vmVal)) != 0) { 110 throw new RuntimeException(String.format("Key %s doesn't match %s vs %s", key, vmVal, jmapVal)); 111 } 112 } catch (NumberFormatException ex) { 113 throw new RuntimeException("Unexpected key '" + key + "' value", ex); 114 } 115 } 116 } 117 118 public static void main(String[] args) throws Exception { 119 System.out.println("Starting JMapHeapConfigTest"); 120 121 if (!Platform.shouldSAAttach()) { 122 // Silently skip the test if we don't have enough permissions to attach 123 System.err.println("Error! Insufficient permissions to attach."); 124 return; 125 } 126 127 if (!LingeredApp.isLastModifiedWorking()) { 128 // Exact behaviour of the test depends to operating system and the test nature, 129 // so just print the warning and continue 130 System.err.println("Warning! Last modified time doesn't work."); 131 } 132 133 boolean mx_found = false; 134 List<String> jvmOptions = Utils.getVmOptions(); 135 for (String option : jvmOptions) { 136 if (option.startsWith("-Xmx")) { 137 System.out.println("INFO: maximum heap size set by JTREG as " + option); 138 mx_found = true; 139 break; 140 } 141 } 142 143 // Forward vm options to LingeredApp 144 ArrayList<String> cmd = new ArrayList(); 145 cmd.addAll(Utils.getVmOptions()); 146 if (!mx_found) { 147 cmd.add(desiredMaxHeapSize); 148 System.out.println("INFO: maximum heap size set explicitly as " + desiredMaxHeapSize); 149 } 150 cmd.add("-XX:+PrintFlagsFinal"); 151 152 TmtoolTestScenario tmt = TmtoolTestScenario.create("jmap", "-heap"); 153 int exitcode = tmt.launch(cmd); 154 if (exitcode != 0) { 155 throw new RuntimeException("Test FAILED jmap exits with non zero exit code " + exitcode); 156 } 157 158 Map<String,String> parsedJmapOutput = parseJMapOutput(tmt.getToolOutput()); 159 Map<String,String> parsedVMOutput = tmt.parseFlagsFinal(); 160 161 compareValues(parsedJmapOutput, parsedVMOutput); 162 163 // If test fails it throws RuntimeException 164 System.out.println("Test PASSED"); 165 } 166 }