VMProps.java revision 2759:d16c61dfcaf6
1/* 2 * Copyright (c) 2016, 2017, 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 */ 23package requires; 24 25import java.io.IOException; 26import java.nio.file.Files; 27import java.nio.file.Path; 28import java.nio.file.Paths; 29import java.nio.file.StandardOpenOption; 30import java.util.ArrayList; 31import java.util.HashMap; 32import java.util.List; 33import java.util.Map; 34import java.util.concurrent.Callable; 35import java.util.regex.Matcher; 36import java.util.regex.Pattern; 37 38import sun.hotspot.cpuinfo.CPUInfo; 39import sun.hotspot.gc.GC; 40import sun.hotspot.WhiteBox; 41import 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 */ 49public 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 * Prints a stack trace before returning null. 82 * Used by the various helper functions which parse information from 83 * VM properties in the case where they don't find an expected property 84 * or a propoerty doesn't conform to an expected format. 85 * 86 * @return null 87 */ 88 private String nullWithException(String message) { 89 new Exception(message).printStackTrace(); 90 return null; 91 } 92 93 /** 94 * @return vm.simpleArch value of "os.simpleArch" property of tested JDK. 95 */ 96 protected String vmArch() { 97 String arch = System.getProperty("os.arch"); 98 if (arch.equals("x86_64") || arch.equals("amd64")) { 99 return "x64"; 100 } 101 else if (arch.contains("86")) { 102 return "x86"; 103 } else { 104 return arch; 105 } 106 } 107 108 109 110 /** 111 * @return VM type value extracted from the "java.vm.name" property. 112 */ 113 protected String vmFlavor() { 114 // E.g. "Java HotSpot(TM) 64-Bit Server VM" 115 String vmName = System.getProperty("java.vm.name"); 116 if (vmName == null) { 117 return nullWithException("Can't get 'java.vm.name' property"); 118 } 119 120 Pattern startP = Pattern.compile(".* (\\S+) VM"); 121 Matcher m = startP.matcher(vmName); 122 if (m.matches()) { 123 return m.group(1).toLowerCase(); 124 } 125 return nullWithException("Can't get VM flavor from 'java.vm.name'"); 126 } 127 128 /** 129 * @return VM compilation mode extracted from the "java.vm.info" property. 130 */ 131 protected String vmCompMode() { 132 // E.g. "mixed mode" 133 String vmInfo = System.getProperty("java.vm.info"); 134 if (vmInfo == null) { 135 return nullWithException("Can't get 'java.vm.info' property"); 136 } 137 if (vmInfo.toLowerCase().indexOf("mixed mode") != -1) { 138 return "Xmixed"; 139 } else if (vmInfo.toLowerCase().indexOf("compiled mode") != -1) { 140 return "Xcomp"; 141 } else if (vmInfo.toLowerCase().indexOf("interpreted mode") != -1) { 142 return "Xint"; 143 } else { 144 return nullWithException("Can't get compilation mode from 'java.vm.info'"); 145 } 146 } 147 148 /** 149 * @return VM bitness, the value of the "sun.arch.data.model" property. 150 */ 151 protected String vmBits() { 152 String dataModel = System.getProperty("sun.arch.data.model"); 153 if (dataModel != null) { 154 return dataModel; 155 } else { 156 return nullWithException("Can't get 'sun.arch.data.model' property"); 157 } 158 } 159 160 /** 161 * @return "true" if Flight Recorder is enabled, "false" if is disabled. 162 */ 163 protected String vmFlightRecorder() { 164 Boolean isUnlockedCommercialFatures = WB.getBooleanVMFlag("UnlockCommercialFeatures"); 165 Boolean isFlightRecorder = WB.getBooleanVMFlag("FlightRecorder"); 166 String startFROptions = WB.getStringVMFlag("StartFlightRecording"); 167 if (isUnlockedCommercialFatures != null && isUnlockedCommercialFatures) { 168 if (isFlightRecorder != null && isFlightRecorder) { 169 return "true"; 170 } 171 if (startFROptions != null && !startFROptions.isEmpty()) { 172 return "true"; 173 } 174 } 175 return "false"; 176 } 177 178 /** 179 * @return debug level value extracted from the "jdk.debug" property. 180 */ 181 protected String vmDebug() { 182 String debug = System.getProperty("jdk.debug"); 183 if (debug != null) { 184 return "" + debug.contains("debug"); 185 } else { 186 return nullWithException("Can't get 'jdk.debug' property"); 187 } 188 } 189 190 /** 191 * @return true if VM supports JVMCI and false otherwise 192 */ 193 protected String vmJvmci() { 194 // builds with jvmci have this flag 195 return "" + (WB.getBooleanVMFlag("EnableJVMCI") != null); 196 } 197 198 /** 199 * @return true if VM runs in emulated-client mode and false otherwise. 200 */ 201 protected String vmEmulatedClient() { 202 String vmInfo = System.getProperty("java.vm.info"); 203 if (vmInfo == null) { 204 return "false"; 205 } 206 return "" + vmInfo.contains(" emulated-client"); 207 } 208 209 /** 210 * @return supported CPU features 211 */ 212 protected String cpuFeatures() { 213 return CPUInfo.getFeatures().toString(); 214 } 215 216 /** 217 * For all existing GC sets vm.gc.X property. 218 * Example vm.gc.G1=true means: 219 * VM supports G1 220 * User either set G1 explicitely (-XX:+UseG1GC) or did not set any GC 221 * @param map - property-value pairs 222 */ 223 protected void vmGC(Map<String, String> map){ 224 GC currentGC = GC.current(); 225 boolean isByErgo = GC.currentSetByErgo(); 226 List<GC> supportedGC = GC.allSupported(); 227 for (GC gc: GC.values()) { 228 boolean isSupported = supportedGC.contains(gc); 229 boolean isAcceptable = isSupported && (gc == currentGC || isByErgo); 230 map.put("vm.gc." + gc.name(), "" + isAcceptable); 231 } 232 } 233 234 /** 235 * @return true if VM runs RTM supported OS and false otherwise. 236 */ 237 protected String vmRTMOS() { 238 boolean isRTMOS = true; 239 240 if (Platform.isAix()) { 241 // Actually, this works since AIX 7.1.3.30, but os.version property 242 // is set to 7.1. 243 isRTMOS = (Platform.getOsVersionMajor() > 7) || 244 (Platform.getOsVersionMajor() == 7 && Platform.getOsVersionMinor() > 1); 245 246 } else if (Platform.isLinux()) { 247 if (Platform.isPPC()) { 248 isRTMOS = (Platform.getOsVersionMajor() > 4) || 249 (Platform.getOsVersionMajor() == 4 && Platform.getOsVersionMinor() > 1); 250 } 251 } 252 return "" + isRTMOS; 253 } 254 255 /** 256 * @return true if VM runs RTM supported CPU and false otherwise. 257 */ 258 protected String vmRTMCPU() { 259 boolean vmRTMCPU = (Platform.isPPC() ? CPUInfo.hasFeature("tcheck") : CPUInfo.hasFeature("rtm")); 260 261 return "" + vmRTMCPU; 262 } 263 264 /** 265 * @return true if VM supports AOT and false otherwise 266 */ 267 protected String vmAOT() { 268 // builds with aot have jaotc in <JDK>/bin 269 Path bin = Paths.get(System.getProperty("java.home")) 270 .resolve("bin"); 271 Path jaotc; 272 if (Platform.isWindows()) { 273 jaotc = bin.resolve("jaotc.exe"); 274 } else { 275 jaotc = bin.resolve("jaotc"); 276 } 277 return "" + Files.exists(jaotc); 278 } 279 280 /** 281 * Dumps the map to the file if the file name is given as the property. 282 * This functionality could be helpful to know context in the real 283 * execution. 284 * 285 * @param map 286 */ 287 protected static void dump(Map<String, String> map) { 288 String dumpFileName = System.getProperty("vmprops.dump"); 289 if (dumpFileName == null) { 290 return; 291 } 292 List<String> lines = new ArrayList<>(); 293 map.forEach((k, v) -> lines.add(k + ":" + v)); 294 try { 295 Files.write(Paths.get(dumpFileName), lines, StandardOpenOption.APPEND); 296 } catch (IOException e) { 297 throw new RuntimeException("Failed to dump properties into '" 298 + dumpFileName + "'", e); 299 } 300 } 301 302 /** 303 * This method is for the testing purpose only. 304 * @param args 305 */ 306 public static void main(String args[]) { 307 Map<String, String> map = new VMProps().call(); 308 map.forEach((k, v) -> System.out.println(k + ": '" + v + "'")); 309 } 310} 311