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