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