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.Paths; 28 import java.util.ArrayList; 29 import java.util.HashMap; 30 import java.util.List; 31 import java.util.Map; 32 import java.util.concurrent.Callable; 33 import java.util.regex.Matcher; 34 import java.util.regex.Pattern; 35 36 /** 37 * The Class to be invoked by jtreg prior Test Suite execution to 38 * collect information about VM. 39 * Properties set by this Class will be available in the @requires expressions. 40 */ 41 public class VMProps implements Callable<Map<String, String>> { 42 43 /** 44 * Collects information about VM properties. 45 * This method will be invoked by jtreg. 46 * 47 * @return Map of property-value pairs. 48 */ 49 @Override 50 public Map<String, String> call() { 51 Map<String, String> map = new HashMap<>(); 52 map.put("vm.flavor", vmFlavor()); 53 map.put("vm.compMode", vmCompMode()); 54 map.put("vm.bits", vmBits()); 55 dump(map); 56 return map; 57 } 58 59 /** 60 * @return VM type value extracted from the "java.vm.name" property. 61 */ 62 protected String vmFlavor() { 63 // E.g. "Java HotSpot(TM) 64-Bit Server VM" 64 String vmName = System.getProperty("java.vm.name"); 65 if (vmName == null) { 66 return null; 67 } 68 69 Pattern startP = Pattern.compile(".* (\\S+) VM"); 70 Matcher m = startP.matcher(vmName); 71 if (m.matches()) { 72 return m.group(1).toLowerCase(); 73 } 74 return null; 87 if (k < 0) { 88 return null; 89 } 90 vmInfo = vmInfo.substring(0, k); 91 switch (vmInfo) { 92 case "mixed" : return "Xmixed"; 93 case "compiled" : return "Xcomp"; 94 case "interpreted" : return "Xint"; 95 default: return null; 96 } 97 } 98 99 /** 100 * @return VM bitness, the value of the "sun.arch.data.model" property. 101 */ 102 protected String vmBits() { 103 return System.getProperty("sun.arch.data.model"); 104 } 105 106 /** 107 * Dumps the map to the file if the file name is given as the property. 108 * This functionality could be helpful to know context in the real 109 * execution. 110 * 111 * @param map 112 */ 113 protected void dump(Map<String, String> map) { 114 String dumpFileName = System.getProperty("vmprops.dump"); 115 if (dumpFileName == null) { 116 return; 117 } 118 List<String> lines = new ArrayList<>(); 119 map.forEach((k,v) -> lines.add(k + ":" + v)); 120 try { 121 Files.write(Paths.get(dumpFileName), lines); 122 } catch (IOException e) { 123 throw new RuntimeException("Failed to dump properties into '" 124 + dumpFileName + "'", e); 125 } 126 } 127 128 /** 129 * This method is for the testing purpose only. 130 * @param args 131 */ 132 public static void main(String args[]) { 133 Map<String, String> map = new VMProps().call(); 134 map.forEach((k,v) -> System.out.println(k + ": '" + v + "'")); 135 } 136 } | 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.lang.management.ManagementFactory; 27 import java.lang.management.RuntimeMXBean; 28 import java.nio.file.Files; 29 import java.nio.file.Paths; 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 /** 39 * The Class to be invoked by jtreg prior Test Suite execution to 40 * collect information about VM. 41 * Properties set by this Class will be available in the @requires expressions. 42 */ 43 public class VMProps implements Callable<Map<String, String>> { 44 45 /** 46 * Collects information about VM properties. 47 * This method will be invoked by jtreg. 48 * 49 * @return Map of property-value pairs. 50 */ 51 @Override 52 public Map<String, String> call() { 53 Map<String, String> map = new HashMap<>(); 54 map.put("vm.flavor", vmFlavor()); 55 map.put("vm.compMode", vmCompMode()); 56 map.put("vm.bits", vmBits()); 57 map.put("vm.flightRecorder", vmFlightRecorder()); 58 dump(map); 59 return map; 60 } 61 62 /** 63 * @return VM type value extracted from the "java.vm.name" property. 64 */ 65 protected String vmFlavor() { 66 // E.g. "Java HotSpot(TM) 64-Bit Server VM" 67 String vmName = System.getProperty("java.vm.name"); 68 if (vmName == null) { 69 return null; 70 } 71 72 Pattern startP = Pattern.compile(".* (\\S+) VM"); 73 Matcher m = startP.matcher(vmName); 74 if (m.matches()) { 75 return m.group(1).toLowerCase(); 76 } 77 return null; 90 if (k < 0) { 91 return null; 92 } 93 vmInfo = vmInfo.substring(0, k); 94 switch (vmInfo) { 95 case "mixed" : return "Xmixed"; 96 case "compiled" : return "Xcomp"; 97 case "interpreted" : return "Xint"; 98 default: return null; 99 } 100 } 101 102 /** 103 * @return VM bitness, the value of the "sun.arch.data.model" property. 104 */ 105 protected String vmBits() { 106 return System.getProperty("sun.arch.data.model"); 107 } 108 109 /** 110 * @return "true" if Flight Recorder is enabled, "false" if is disabled. 111 */ 112 protected String vmFlightRecorder() { 113 RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean(); 114 List<String> arguments = runtimeMxBean.getInputArguments(); 115 if (arguments.contains("-XX:+UnlockCommercialFeatures")) { 116 if (arguments.contains("-XX:+FlightRecorder")) { 117 return "true"; 118 } 119 if (arguments.contains("-XX:-FlightRecorder")) { 120 return "false"; 121 } 122 if (arguments.stream() 123 .anyMatch(option -> option.startsWith("-XX:StartFlightRecording"))) { 124 return "true"; 125 } 126 } 127 return "false"; 128 } 129 130 /** 131 * Dumps the map to the file if the file name is given as the property. 132 * This functionality could be helpful to know context in the real 133 * execution. 134 * 135 * @param map 136 */ 137 protected void dump(Map<String, String> map) { 138 String dumpFileName = System.getProperty("vmprops.dump"); 139 if (dumpFileName == null) { 140 return; 141 } 142 List<String> lines = new ArrayList<>(); 143 map.forEach((k, v) -> lines.add(k + ":" + v)); 144 try { 145 Files.write(Paths.get(dumpFileName), lines); 146 } catch (IOException e) { 147 throw new RuntimeException("Failed to dump properties into '" 148 + dumpFileName + "'", e); 149 } 150 } 151 152 /** 153 * This method is for the testing purpose only. 154 * @param args 155 */ 156 public static void main(String args[]) { 157 Map<String, String> map = new VMProps().call(); 158 map.forEach((k, v) -> System.out.println(k + ": '" + v + "'")); 159 } 160 } |