VMProps.java revision 2324:a26dbefcc658
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.util.ArrayList; 29import java.util.HashMap; 30import java.util.List; 31import java.util.Map; 32import java.util.concurrent.Callable; 33import java.util.regex.Matcher; 34import java.util.regex.Pattern; 35import sun.hotspot.gc.GC; 36import sun.hotspot.WhiteBox; 37 38/** 39 * The Class to be invoked by jtreg prior Test Suite execution to 40 * collect information about VM. 41 * Do not use any API's that may not be available in all target VMs. 42 * Properties set by this Class will be available in the @requires expressions. 43 */ 44public class VMProps implements Callable<Map<String, String>> { 45 46 private static final WhiteBox WB = WhiteBox.getWhiteBox(); 47 48 /** 49 * Collects information about VM properties. 50 * This method will be invoked by jtreg. 51 * 52 * @return Map of property-value pairs. 53 */ 54 @Override 55 public Map<String, String> call() { 56 Map<String, String> map = new HashMap<>(); 57 map.put("vm.flavor", vmFlavor()); 58 map.put("vm.compMode", vmCompMode()); 59 map.put("vm.bits", vmBits()); 60 map.put("vm.flightRecorder", vmFlightRecorder()); 61 map.put("vm.simpleArch", vmArch()); 62 map.put("vm.debug", vmDebug()); 63 vmGC(map); // vm.gc.X = true/false 64 65 dump(map); 66 return map; 67 } 68 69 /** 70 * @return vm.simpleArch value of "os.simpleArch" property of tested JDK. 71 */ 72 protected String vmArch() { 73 String arch = System.getProperty("os.arch"); 74 if (arch.equals("x86_64") || arch.equals("amd64")) { 75 return "x64"; 76 } 77 else if (arch.contains("86")) { 78 return "x86"; 79 } else { 80 return arch; 81 } 82 } 83 84 85 86 /** 87 * @return VM type value extracted from the "java.vm.name" property. 88 */ 89 protected String vmFlavor() { 90 // E.g. "Java HotSpot(TM) 64-Bit Server VM" 91 String vmName = System.getProperty("java.vm.name"); 92 if (vmName == null) { 93 return null; 94 } 95 96 Pattern startP = Pattern.compile(".* (\\S+) VM"); 97 Matcher m = startP.matcher(vmName); 98 if (m.matches()) { 99 return m.group(1).toLowerCase(); 100 } 101 return null; 102 } 103 104 /** 105 * @return VM compilation mode extracted from the "java.vm.info" property. 106 */ 107 protected String vmCompMode() { 108 // E.g. "mixed mode" 109 String vmInfo = System.getProperty("java.vm.info"); 110 if (vmInfo == null) { 111 return null; 112 } 113 int k = vmInfo.toLowerCase().indexOf(" mode"); 114 if (k < 0) { 115 return null; 116 } 117 vmInfo = vmInfo.substring(0, k); 118 switch (vmInfo) { 119 case "mixed" : return "Xmixed"; 120 case "compiled" : return "Xcomp"; 121 case "interpreted" : return "Xint"; 122 default: return null; 123 } 124 } 125 126 /** 127 * @return VM bitness, the value of the "sun.arch.data.model" property. 128 */ 129 protected String vmBits() { 130 return System.getProperty("sun.arch.data.model"); 131 } 132 133 /** 134 * @return "true" if Flight Recorder is enabled, "false" if is disabled. 135 */ 136 protected String vmFlightRecorder() { 137 Boolean isUnlockedCommercialFatures = WB.getBooleanVMFlag("UnlockCommercialFeatures"); 138 Boolean isFlightRecorder = WB.getBooleanVMFlag("FlightRecorder"); 139 String startFROptions = WB.getStringVMFlag("StartFlightRecording"); 140 if (isUnlockedCommercialFatures != null && isUnlockedCommercialFatures) { 141 if (isFlightRecorder != null && isFlightRecorder) { 142 return "true"; 143 } 144 if (startFROptions != null && !startFROptions.isEmpty()) { 145 return "true"; 146 } 147 } 148 return "false"; 149 } 150 151 /** 152 * @return debug level value extracted from the "jdk.debug" property. 153 */ 154 protected String vmDebug() { 155 return "" + System.getProperty("jdk.debug").contains("debug"); 156 } 157 158 /** 159 * For all existing GC sets vm.gc.X property. 160 * Example vm.gc.G1=true means: 161 * VM supports G1 162 * User either set G1 explicitely (-XX:+UseG1GC) or did not set any GC 163 * @param map - property-value pairs 164 */ 165 protected void vmGC(Map<String, String> map){ 166 GC currentGC = GC.current(); 167 boolean isByErgo = GC.currentSetByErgo(); 168 List<GC> supportedGC = GC.allSupported(); 169 for (GC gc: GC.values()) { 170 boolean isSupported = supportedGC.contains(gc); 171 boolean isAcceptable = isSupported && (gc == currentGC || isByErgo); 172 map.put("vm.gc." + gc.name(), "" + isAcceptable); 173 } 174 } 175 176 /** 177 * Dumps the map to the file if the file name is given as the property. 178 * This functionality could be helpful to know context in the real 179 * execution. 180 * 181 * @param map 182 */ 183 protected void dump(Map<String, String> map) { 184 String dumpFileName = System.getProperty("vmprops.dump"); 185 if (dumpFileName == null) { 186 return; 187 } 188 List<String> lines = new ArrayList<>(); 189 map.forEach((k, v) -> lines.add(k + ":" + v)); 190 try { 191 Files.write(Paths.get(dumpFileName), lines); 192 } catch (IOException e) { 193 throw new RuntimeException("Failed to dump properties into '" 194 + dumpFileName + "'", e); 195 } 196 } 197 198 /** 199 * This method is for the testing purpose only. 200 * @param args 201 */ 202 public static void main(String args[]) { 203 Map<String, String> map = new VMProps().call(); 204 map.forEach((k, v) -> System.out.println(k + ": '" + v + "'")); 205 } 206} 207