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 /*
  25  * @test
  26  * @bug 8042397
  27  * @summary Unit test for jmap utility test heap configuration reader
  28  * @library /lib/testlibrary
  29  * @modules java.management
  30  * @build jdk.testlibrary.*
  31  * @build JMapHeapConfigTest LingeredApp TmtoolTestScenario
  32  * @run main JMapHeapConfigTest
  33  */
  34 import java.io.IOException;
  35 import java.math.BigDecimal;
  36 import java.util.ArrayList;
  37 import java.util.HashMap;
  38 import java.util.List;
  39 import java.util.Map;
  40 import jdk.testlibrary.Utils;
  41 import jdk.testlibrary.Platform;
  42 
  43 public class JMapHeapConfigTest {
  44 
  45     static final String expectedJMapValues[] = {
  46         "MinHeapFreeRatio",
  47         "MaxHeapFreeRatio",
  48         "MaxHeapSize",
  49         "NewSize",
  50         "MaxNewSize",
  51         "OldSize",
  52         "NewRatio",
  53         "SurvivorRatio",
  54         "MetaspaceSize",
  55         "CompressedClassSpaceSize",
  56         "G1HeapRegionSize"};
  57 
  58     // ignoring MaxMetaspaceSize
  59 
  60     static final String desiredMaxHeapSize = "-Xmx128m";
  61 
  62     private static Map<String, String> parseJMapOutput(List<String> jmapOutput) {
  63         Map<String, String> heapConfigMap = new HashMap<String, String>();
  64         boolean shouldParse = false;
  65 
  66         for (String line : jmapOutput) {
  67             line = line.trim();
  68 
  69             if (line.startsWith("Heap Configuration:")) {
  70                 shouldParse = true;
  71                 continue;
  72             }
  73 
  74             if (line.startsWith("Heap Usage:")) {
  75                 shouldParse = false;
  76                 continue;
  77             }
  78 
  79             if (shouldParse && !line.equals("")) {
  80                 String[] lv = line.split("\\s+");
  81                 try {
  82                     heapConfigMap.put(lv[0], lv[2]);
  83                 } catch (ArrayIndexOutOfBoundsException ex) {
  84                     // Ignore mailformed lines
  85                 }
  86             }
  87         }
  88         return heapConfigMap;
  89     }
  90 
  91     // Compare stored values
  92     private static void compareValues(Map<String, String> parsedJMapOutput, Map<String, String> parsedVmOutput) {
  93         for (String key : expectedJMapValues) {
  94             try {
  95                 String jmapVal = parsedJMapOutput.get(key);
  96                 if (jmapVal == null) {
  97                     throw new RuntimeException("Key '" + key + "' doesn't exists in jmap output");
  98                 }
  99 
 100                 String vmVal = parsedVmOutput.get(key);
 101                 if (vmVal == null) {
 102                     throw new RuntimeException("Key '" + key + "' doesn't exists in vm output");
 103                 }
 104 
 105                 if (new BigDecimal(jmapVal).compareTo(new BigDecimal(vmVal)) != 0) {
 106                     throw new RuntimeException(String.format("Key %s doesn't match %s vs %s", key, vmVal, jmapVal));
 107                 }
 108             } catch (NumberFormatException ex) {
 109                 throw new RuntimeException("Unexpected key '" + key + "' value", ex);
 110             }
 111         }
 112     }
 113 
 114     public static void main(String[] args) throws Exception {
 115         System.out.println("Starting JMapHeapConfigTest");
 116 
 117         if (!Platform.shouldSAAttach()) {
 118             // Silently skip the test if we don't have enough permissions to attach
 119             System.err.println("Error! Insufficient permissions to attach.");
 120             return;
 121         }
 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 }