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