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 */ 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 map.put("vm.arch", vmArch()); 56 dump(map); 57 return map; 58 } 59 60 61 /** 62 * @return vm.arch value extracted from the "os.arch" property of tested JDK. 63 */ 64 protected String vmArch() { 65 String arch = System.getProperty("os.arch"); 66 if (arch.equals("x86_64") || arch.equals("amd64")) { 67 return "x64"; 68 } 69 else if (arch.contains("86")) { 70 return "x86"; 71 } 72 else if (arch.equals("aarch64")) { 73 return "arm64"; 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 * Dumps the map to the file if the file name is given as the property. 130 * This functionality could be helpful to know context in the real 131 * execution. 132 * 133 * @param map 134 */ 135 protected void dump(Map<String, String> map) { 136 String dumpFileName = System.getProperty("vmprops.dump"); 137 if (dumpFileName == null) { 138 return; 139 } 140 List<String> lines = new ArrayList<>(); 141 map.forEach((k,v) -> lines.add(k + ":" + v)); 142 try { 143 Files.write(Paths.get(dumpFileName), lines); 144 } catch (IOException e) { 145 throw new RuntimeException("Failed to dump properties into '" 146 + dumpFileName + "'", e); 147 } 148 } 149 150 /** 151 * This method is for the testing purpose only. 152 * @param args 153 */ 154 public static void main(String args[]) { 155 Map<String, String> map = new VMProps().call(); 156 map.forEach((k,v) -> System.out.println(k + ": '" + v + "'")); 157 } 158 }