VMProps.java revision 2194:4fe0143c0a15
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 vmGC(map); // vm.gc.X = true/false 63 64 dump(map); 65 return map; 66 } 67 68 /** 69 * @return vm.simpleArch value of "os.simpleArch" property of tested JDK. 70 */ 71 protected String vmArch() { 72 String arch = System.getProperty("os.arch"); 73 if (arch.equals("x86_64") || arch.equals("amd64")) { 74 return "x64"; 75 } 76 else if (arch.contains("86")) { 77 return "x86"; 78 } else { 79 return arch; 80 } 81 } 82 83 84 85 /** 86 * @return VM type value extracted from the "java.vm.name" property. 87 */ 88 protected String vmFlavor() { 89 // E.g. "Java HotSpot(TM) 64-Bit Server VM" 90 String vmName = System.getProperty("java.vm.name"); 91 if (vmName == null) { 92 return null; 93 } 94 95 Pattern startP = Pattern.compile(".* (\\S+) VM"); 96 Matcher m = startP.matcher(vmName); 97 if (m.matches()) { 98 return m.group(1).toLowerCase(); 99 } 100 return null; 101 } 102 103 /** 104 * @return VM compilation mode extracted from the "java.vm.info" property. 105 */ 106 protected String vmCompMode() { 107 // E.g. "mixed mode" 108 String vmInfo = System.getProperty("java.vm.info"); 109 if (vmInfo == null) { 110 return null; 111 } 112 int k = vmInfo.toLowerCase().indexOf(" mode"); 113 if (k < 0) { 114 return null; 115 } 116 vmInfo = vmInfo.substring(0, k); 117 switch (vmInfo) { 118 case "mixed" : return "Xmixed"; 119 case "compiled" : return "Xcomp"; 120 case "interpreted" : return "Xint"; 121 default: return null; 122 } 123 } 124 125 /** 126 * @return VM bitness, the value of the "sun.arch.data.model" property. 127 */ 128 protected String vmBits() { 129 return System.getProperty("sun.arch.data.model"); 130 } 131 132 /** 133 * @return "true" if Flight Recorder is enabled, "false" if is disabled. 134 */ 135 protected String vmFlightRecorder() { 136 Boolean isUnlockedCommercialFatures = WB.getBooleanVMFlag("UnlockCommercialFeatures"); 137 Boolean isFlightRecorder = WB.getBooleanVMFlag("FlightRecorder"); 138 String startFROptions = WB.getStringVMFlag("StartFlightRecording"); 139 if (isUnlockedCommercialFatures != null && isUnlockedCommercialFatures) { 140 if (isFlightRecorder != null && isFlightRecorder) { 141 return "true"; 142 } 143 if (startFROptions != null && !startFROptions.isEmpty()) { 144 return "true"; 145 } 146 } 147 return "false"; 148 } 149 150 /** 151 * For all existing GC sets vm.gc.X property. 152 * Example vm.gc.G1=true means: 153 * VM supports G1 154 * User either set G1 explicitely (-XX:+UseG1GC) or did not set any GC 155 * @param map - property-value pairs 156 */ 157 protected void vmGC(Map<String, String> map){ 158 GC currentGC = GC.current(); 159 boolean isByErgo = GC.currentSetByErgo(); 160 List<GC> supportedGC = GC.allSupported(); 161 for (GC gc: GC.values()) { 162 boolean isSupported = supportedGC.contains(gc); 163 boolean isAcceptable = isSupported && (gc == currentGC || isByErgo); 164 map.put("vm.gc." + gc.name(), "" + isAcceptable); 165 } 166 } 167 168 /** 169 * Dumps the map to the file if the file name is given as the property. 170 * This functionality could be helpful to know context in the real 171 * execution. 172 * 173 * @param map 174 */ 175 protected void dump(Map<String, String> map) { 176 String dumpFileName = System.getProperty("vmprops.dump"); 177 if (dumpFileName == null) { 178 return; 179 } 180 List<String> lines = new ArrayList<>(); 181 map.forEach((k, v) -> lines.add(k + ":" + v)); 182 try { 183 Files.write(Paths.get(dumpFileName), lines); 184 } catch (IOException e) { 185 throw new RuntimeException("Failed to dump properties into '" 186 + dumpFileName + "'", e); 187 } 188 } 189 190 /** 191 * This method is for the testing purpose only. 192 * @param args 193 */ 194 public static void main(String args[]) { 195 Map<String, String> map = new VMProps().call(); 196 map.forEach((k, v) -> System.out.println(k + ": '" + v + "'")); 197 } 198} 199