1 /*
   2  * Copyright (c) 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 package requires;
  24 
  25 import java.io.IOException;
  26 import java.nio.file.Files;
  27 import java.nio.file.Path;
  28 import java.nio.file.Paths;
  29 import java.nio.file.StandardOpenOption;
  30 import java.util.ArrayList;
  31 import java.util.HashMap;
  32 import java.util.List;
  33 import java.util.Map;
  34 import java.util.concurrent.Callable;
  35 import java.util.regex.Matcher;
  36 import java.util.regex.Pattern;
  37 
  38 import sun.hotspot.cpuinfo.CPUInfo;
  39 import sun.hotspot.gc.GC;
  40 import sun.hotspot.WhiteBox;
  41 import jdk.test.lib.Platform;
  42 
  43 /**
  44  * The Class to be invoked by jtreg prior Test Suite execution to
  45  * collect information about VM.
  46  * Do not use any API's that may not be available in all target VMs.
  47  * Properties set by this Class will be available in the @requires expressions.
  48  */
  49 public class VMProps implements Callable<Map<String, String>> {
  50 
  51     private static final WhiteBox WB = WhiteBox.getWhiteBox();
  52 
  53     /**
  54      * Collects information about VM properties.
  55      * This method will be invoked by jtreg.
  56      *
  57      * @return Map of property-value pairs.
  58      */
  59     @Override
  60     public Map<String, String> call() {
  61         Map<String, String> map = new HashMap<>();
  62         map.put("vm.flavor", vmFlavor());
  63         map.put("vm.compMode", vmCompMode());
  64         map.put("vm.bits", vmBits());
  65         map.put("vm.flightRecorder", vmFlightRecorder());
  66         map.put("vm.simpleArch", vmArch());
  67         map.put("vm.debug", vmDebug());
  68         map.put("vm.jvmci", vmJvmci());
  69         map.put("vm.emulatedClient", vmEmulatedClient());
  70         map.put("vm.cpu.features", cpuFeatures());
  71         map.put("vm.rtm.cpu", vmRTMCPU());
  72         map.put("vm.rtm.os", vmRTMOS());
  73         map.put("vm.aot", vmAot());
  74         vmGC(map); // vm.gc.X = true/false
  75 
  76         VMProps.dump(map);
  77         return map;
  78     }
  79 
  80     /**
  81      * @return vm.simpleArch value of "os.simpleArch" property of tested JDK.
  82      */
  83     protected String vmArch() {
  84         String arch = System.getProperty("os.arch");
  85         if (arch.equals("x86_64") || arch.equals("amd64")) {
  86             return "x64";
  87         }
  88         else if (arch.contains("86")) {
  89             return "x86";
  90         } else {
  91             return arch;
  92         }
  93     }
  94 
  95 
  96 
  97     /**
  98      * @return VM type value extracted from the "java.vm.name" property.
  99      */
 100     protected String vmFlavor() {
 101         // E.g. "Java HotSpot(TM) 64-Bit Server VM"
 102         String vmName = System.getProperty("java.vm.name");
 103         if (vmName == null) {
 104             return null;
 105         }
 106 
 107         Pattern startP = Pattern.compile(".* (\\S+) VM");
 108         Matcher m = startP.matcher(vmName);
 109         if (m.matches()) {
 110             return m.group(1).toLowerCase();
 111         }
 112         return null;
 113     }
 114 
 115     /**
 116      * @return VM compilation mode extracted from the "java.vm.info" property.
 117      */
 118     protected String vmCompMode() {
 119         // E.g. "mixed mode"
 120         String vmInfo = System.getProperty("java.vm.info");
 121         if (vmInfo == null) {
 122             return null;
 123         }
 124         int k = vmInfo.toLowerCase().indexOf(" mode");
 125         if (k < 0) {
 126             return null;
 127         }
 128         vmInfo = vmInfo.substring(0, k);
 129         switch (vmInfo) {
 130             case "mixed" : return "Xmixed";
 131             case "compiled" : return "Xcomp";
 132             case "interpreted" : return "Xint";
 133             default: return null;
 134         }
 135     }
 136 
 137     /**
 138      * @return VM bitness, the value of the "sun.arch.data.model" property.
 139      */
 140     protected String vmBits() {
 141         return System.getProperty("sun.arch.data.model");
 142     }
 143 
 144     /**
 145      * @return "true" if Flight Recorder is enabled, "false" if is disabled.
 146      */
 147     protected String vmFlightRecorder() {
 148         Boolean isUnlockedCommercialFatures = WB.getBooleanVMFlag("UnlockCommercialFeatures");
 149         Boolean isFlightRecorder = WB.getBooleanVMFlag("FlightRecorder");
 150         String startFROptions = WB.getStringVMFlag("StartFlightRecording");
 151         if (isUnlockedCommercialFatures != null && isUnlockedCommercialFatures) {
 152             if (isFlightRecorder != null && isFlightRecorder) {
 153                 return "true";
 154             }
 155             if (startFROptions != null && !startFROptions.isEmpty()) {
 156                 return "true";
 157             }
 158         }
 159         return "false";
 160     }
 161 
 162     /**
 163      * @return debug level value extracted from the "jdk.debug" property.
 164      */
 165     protected String vmDebug() {
 166         return "" + System.getProperty("jdk.debug").contains("debug");
 167     }
 168 
 169     /**
 170      * @return true if VM supports JVMCI and false otherwise
 171      */
 172     protected String vmJvmci() {
 173         // builds with jvmci have this flag
 174         return "" + (WB.getBooleanVMFlag("EnableJVMCI") != null);
 175     }
 176 
 177     /**
 178      * @return true if VM runs in emulated-client mode and false otherwise.
 179      */
 180     protected String vmEmulatedClient() {
 181         String vmInfo = System.getProperty("java.vm.info");
 182         if (vmInfo == null) {
 183             return "false";
 184         }
 185         return "" + vmInfo.contains(" emulated-client");
 186     }
 187 
 188     /**
 189      * @return supported CPU features
 190      */
 191     protected String cpuFeatures() {
 192         return CPUInfo.getFeatures().toString();
 193     }
 194 
 195     /**
 196      * For all existing GC sets vm.gc.X property.
 197      * Example vm.gc.G1=true means:
 198      *    VM supports G1
 199      *    User either set G1 explicitely (-XX:+UseG1GC) or did not set any GC
 200      * @param map - property-value pairs
 201      */
 202     protected void vmGC(Map<String, String> map){
 203         GC currentGC = GC.current();
 204         boolean isByErgo = GC.currentSetByErgo();
 205         List<GC> supportedGC = GC.allSupported();
 206         for (GC gc: GC.values()) {
 207             boolean isSupported = supportedGC.contains(gc);
 208             boolean isAcceptable = isSupported && (gc == currentGC || isByErgo);
 209             map.put("vm.gc." + gc.name(), "" + isAcceptable);
 210         }
 211     }
 212 
 213     /**
 214      * @return true if VM runs RTM supported OS and false otherwise.
 215      */
 216     protected String vmRTMOS() {
 217         boolean isRTMOS = true;
 218 
 219         if (Platform.isAix()) {
 220             // Actually, this works since AIX 7.1.3.30, but os.version property
 221             // is set to 7.1.
 222             isRTMOS = (Platform.getOsVersionMajor()  > 7) ||
 223                       (Platform.getOsVersionMajor() == 7 && Platform.getOsVersionMinor() > 1);
 224 
 225         } else if (Platform.isLinux()) {
 226             if (Platform.isPPC()) {
 227                 isRTMOS = (Platform.getOsVersionMajor()  > 4) ||
 228                           (Platform.getOsVersionMajor() == 4 && Platform.getOsVersionMinor() > 1);
 229             }
 230         }
 231         return "" + isRTMOS;
 232     }
 233 
 234     /**
 235      * @return true if VM runs RTM supported CPU and false otherwise.
 236      */
 237     protected String vmRTMCPU() {
 238         boolean vmRTMCPU = (Platform.isPPC() ? CPUInfo.hasFeature("tcheck") : CPUInfo.hasFeature("rtm"));
 239 
 240         return "" + vmRTMCPU;
 241     }
 242 
 243     /**
 244      * @return true if VM supports AOT and false otherwise
 245      */
 246     protected String vmAot() {
 247         // builds with aot have jaotc in <JDK>/bin
 248         Path bin = Paths.get(System.getProperty("java.home"))
 249                         .resolve("bin");
 250         Path jaotc;
 251         if (Platform.isWindows()) {
 252             jaotc = bin.resolve("jaotc.exe");
 253         } else {
 254             jaotc = bin.resolve("jaotc");
 255         }
 256         return "" + Files.exists(jaotc);
 257     }
 258 
 259     /**
 260      * Dumps the map to the file if the file name is given as the property.
 261      * This functionality could be helpful to know context in the real
 262      * execution.
 263      *
 264      * @param map
 265      */
 266     protected static void dump(Map<String, String> map) {
 267         String dumpFileName = System.getProperty("vmprops.dump");
 268         if (dumpFileName == null) {
 269             return;
 270         }
 271         List<String> lines = new ArrayList<>();
 272         map.forEach((k, v) -> lines.add(k + ":" + v));
 273         try {
 274             Files.write(Paths.get(dumpFileName), lines, StandardOpenOption.APPEND);
 275         } catch (IOException e) {
 276             throw new RuntimeException("Failed to dump properties into '"
 277                     + dumpFileName + "'", e);
 278         }
 279     }
 280 
 281     /**
 282      * This method is for the testing purpose only.
 283      * @param args
 284      */
 285     public static void main(String args[]) {
 286         Map<String, String> map = new VMProps().call();
 287         map.forEach((k, v) -> System.out.println(k + ": '" + v + "'"));
 288     }
 289 }