VMProps.java revision 2138:4a042dae129c
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.lang.management.ManagementFactory; 27import java.lang.management.RuntimeMXBean; 28import java.nio.file.Files; 29import java.nio.file.Paths; 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 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 */ 43public 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 map.put("vm.simpleArch", vmArch()); 59 dump(map); 60 return map; 61 } 62 63 64 /** 65 * @return vm.simpleArch value of "os.simpleArch" property of tested JDK. 66 */ 67 protected String vmArch() { 68 String arch = System.getProperty("os.arch"); 69 if (arch.equals("x86_64") || arch.equals("amd64")) { 70 return "x64"; 71 } 72 else if (arch.contains("86")) { 73 return "x86"; 74 } else { 75 return arch; 76 } 77 } 78 79 80 81 /** 82 * @return VM type value extracted from the "java.vm.name" property. 83 */ 84 protected String vmFlavor() { 85 // E.g. "Java HotSpot(TM) 64-Bit Server VM" 86 String vmName = System.getProperty("java.vm.name"); 87 if (vmName == null) { 88 return null; 89 } 90 91 Pattern startP = Pattern.compile(".* (\\S+) VM"); 92 Matcher m = startP.matcher(vmName); 93 if (m.matches()) { 94 return m.group(1).toLowerCase(); 95 } 96 return null; 97 } 98 99 /** 100 * @return VM compilation mode extracted from the "java.vm.info" property. 101 */ 102 protected String vmCompMode() { 103 // E.g. "mixed mode" 104 String vmInfo = System.getProperty("java.vm.info"); 105 if (vmInfo == null) { 106 return null; 107 } 108 int k = vmInfo.toLowerCase().indexOf(" mode"); 109 if (k < 0) { 110 return null; 111 } 112 vmInfo = vmInfo.substring(0, k); 113 switch (vmInfo) { 114 case "mixed" : return "Xmixed"; 115 case "compiled" : return "Xcomp"; 116 case "interpreted" : return "Xint"; 117 default: return null; 118 } 119 } 120 121 /** 122 * @return VM bitness, the value of the "sun.arch.data.model" property. 123 */ 124 protected String vmBits() { 125 return System.getProperty("sun.arch.data.model"); 126 } 127 128 /** 129 * @return "true" if Flight Recorder is enabled, "false" if is disabled. 130 */ 131 protected String vmFlightRecorder() { 132 RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean(); 133 List<String> arguments = runtimeMxBean.getInputArguments(); 134 if (arguments.contains("-XX:+UnlockCommercialFeatures")) { 135 if (arguments.contains("-XX:+FlightRecorder")) { 136 return "true"; 137 } 138 if (arguments.contains("-XX:-FlightRecorder")) { 139 return "false"; 140 } 141 if (arguments.stream() 142 .anyMatch(option -> option.startsWith("-XX:StartFlightRecording"))) { 143 return "true"; 144 } 145 } 146 return "false"; 147 } 148 149 /** 150 * Dumps the map to the file if the file name is given as the property. 151 * This functionality could be helpful to know context in the real 152 * execution. 153 * 154 * @param map 155 */ 156 protected void dump(Map<String, String> map) { 157 String dumpFileName = System.getProperty("vmprops.dump"); 158 if (dumpFileName == null) { 159 return; 160 } 161 List<String> lines = new ArrayList<>(); 162 map.forEach((k, v) -> lines.add(k + ":" + v)); 163 try { 164 Files.write(Paths.get(dumpFileName), lines); 165 } catch (IOException e) { 166 throw new RuntimeException("Failed to dump properties into '" 167 + dumpFileName + "'", e); 168 } 169 } 170 171 /** 172 * This method is for the testing purpose only. 173 * @param args 174 */ 175 public static void main(String args[]) { 176 Map<String, String> map = new VMProps().call(); 177 map.forEach((k, v) -> System.out.println(k + ": '" + v + "'")); 178 } 179} 180