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 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; 75 } 76 77 /** 78 * @return VM compilation mode extracted from the "java.vm.info" property. 79 */ 80 protected String vmCompMode() { 81 // E.g. "mixed mode" 82 String vmInfo = System.getProperty("java.vm.info"); 83 if (vmInfo == null) { 84 return null; 85 } 86 int k = vmInfo.toLowerCase().indexOf(" mode"); 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 }