VMProps.java revision 2464:65a40732289f
176195Sbrian/* 276195Sbrian * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 376195Sbrian * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 476195Sbrian * 576195Sbrian * This code is free software; you can redistribute it and/or modify it 676195Sbrian * under the terms of the GNU General Public License version 2 only, as 776195Sbrian * published by the Free Software Foundation. 876195Sbrian * 976195Sbrian * This code is distributed in the hope that it will be useful, but WITHOUT 1076195Sbrian * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1176195Sbrian * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1276195Sbrian * version 2 for more details (a copy is included in the LICENSE file that 1376195Sbrian * accompanied this code). 1476195Sbrian * 1576195Sbrian * You should have received a copy of the GNU General Public License version 1676195Sbrian * 2 along with this work; if not, write to the Free Software Foundation, 1776195Sbrian * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1876195Sbrian * 1976195Sbrian * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2076195Sbrian * or visit www.oracle.com if you need additional information or have any 2176195Sbrian * questions. 2276195Sbrian */ 2376195Sbrianpackage requires; 2476195Sbrian 2576195Sbrianimport java.io.IOException; 2676195Sbrianimport java.nio.file.Files; 2776195Sbrianimport java.nio.file.Paths; 2876195Sbrianimport java.nio.file.StandardOpenOption; 2976195Sbrianimport java.util.ArrayList; 30145975Sanholtimport java.util.HashMap; 3176195Sbrianimport java.util.List; 3276195Sbrianimport java.util.Map; 3376195Sbrianimport java.util.concurrent.Callable; 3476195Sbrianimport java.util.regex.Matcher; 3576195Sbrianimport java.util.regex.Pattern; 3676195Sbrian 3776195Sbrianimport sun.hotspot.cpuinfo.CPUInfo; 3876195Sbrianimport sun.hotspot.gc.GC; 3976195Sbrianimport sun.hotspot.WhiteBox; 4076195Sbrian 4176195Sbrian/** 4276195Sbrian * The Class to be invoked by jtreg prior Test Suite execution to 4376195Sbrian * collect information about VM. 4476195Sbrian * Do not use any API's that may not be available in all target VMs. 4576195Sbrian * Properties set by this Class will be available in the @requires expressions. 4676195Sbrian */ 4776195Sbrianpublic class VMProps implements Callable<Map<String, String>> { 4876195Sbrian 4976195Sbrian private static final WhiteBox WB = WhiteBox.getWhiteBox(); 5076195Sbrian 5176195Sbrian /** 5276195Sbrian * Collects information about VM properties. 5376195Sbrian * This method will be invoked by jtreg. 5476195Sbrian * 5576195Sbrian * @return Map of property-value pairs. 5676195Sbrian */ 5776195Sbrian @Override 5876195Sbrian public Map<String, String> call() { 5976195Sbrian Map<String, String> map = new HashMap<>(); 6076195Sbrian map.put("vm.flavor", vmFlavor()); 6176195Sbrian map.put("vm.compMode", vmCompMode()); 6276195Sbrian map.put("vm.bits", vmBits()); 6376195Sbrian map.put("vm.flightRecorder", vmFlightRecorder()); 6476195Sbrian map.put("vm.simpleArch", vmArch()); 6576195Sbrian map.put("vm.debug", vmDebug()); 6676195Sbrian map.put("vm.jvmci", vmJvmci()); 6776195Sbrian map.put("vm.cpu.features", cpuFeatures()); 6876195Sbrian vmGC(map); // vm.gc.X = true/false 6976195Sbrian 7076195Sbrian VMProps.dump(map); 7176195Sbrian return map; 7276195Sbrian } 7376195Sbrian 7476195Sbrian /** 7576195Sbrian * @return vm.simpleArch value of "os.simpleArch" property of tested JDK. 7676195Sbrian */ 7776195Sbrian protected String vmArch() { 7876195Sbrian String arch = System.getProperty("os.arch"); 7976195Sbrian if (arch.equals("x86_64") || arch.equals("amd64")) { 8076195Sbrian return "x64"; 8176195Sbrian } 8276195Sbrian else if (arch.contains("86")) { 8376195Sbrian return "x86"; 8476195Sbrian } else { 8576195Sbrian return arch; 8676195Sbrian } 8776195Sbrian } 8876195Sbrian 8976195Sbrian 9076195Sbrian 9176195Sbrian /** 9276195Sbrian * @return VM type value extracted from the "java.vm.name" property. 9376195Sbrian */ 9476195Sbrian protected String vmFlavor() { 9576195Sbrian // E.g. "Java HotSpot(TM) 64-Bit Server VM" 9676195Sbrian String vmName = System.getProperty("java.vm.name"); 9776195Sbrian if (vmName == null) { 9876195Sbrian return null; 9976195Sbrian } 10076195Sbrian 10176195Sbrian Pattern startP = Pattern.compile(".* (\\S+) VM"); 10276195Sbrian Matcher m = startP.matcher(vmName); 10376195Sbrian if (m.matches()) { 10476195Sbrian return m.group(1).toLowerCase(); 10576195Sbrian } 10676195Sbrian return null; 10776195Sbrian } 10876195Sbrian 10976195Sbrian /** 11076195Sbrian * @return VM compilation mode extracted from the "java.vm.info" property. 11176195Sbrian */ 11276195Sbrian protected String vmCompMode() { 11376195Sbrian // E.g. "mixed mode" 11476195Sbrian String vmInfo = System.getProperty("java.vm.info"); 11576195Sbrian if (vmInfo == null) { 11676195Sbrian return null; 11776195Sbrian } 11876195Sbrian int k = vmInfo.toLowerCase().indexOf(" mode"); 11976195Sbrian if (k < 0) { 12076195Sbrian return null; 12176195Sbrian } 12276195Sbrian vmInfo = vmInfo.substring(0, k); 12376195Sbrian switch (vmInfo) { 12476195Sbrian case "mixed" : return "Xmixed"; 12576195Sbrian case "compiled" : return "Xcomp"; 12676195Sbrian case "interpreted" : return "Xint"; 12776195Sbrian default: return null; 12876195Sbrian } 12976195Sbrian } 13076195Sbrian 13176195Sbrian /** 13276195Sbrian * @return VM bitness, the value of the "sun.arch.data.model" property. 13376195Sbrian */ 13476195Sbrian protected String vmBits() { 13576195Sbrian return System.getProperty("sun.arch.data.model"); 13676195Sbrian } 13776195Sbrian 13876195Sbrian /** 13976195Sbrian * @return "true" if Flight Recorder is enabled, "false" if is disabled. 14076195Sbrian */ 14176195Sbrian protected String vmFlightRecorder() { 14276195Sbrian Boolean isUnlockedCommercialFatures = WB.getBooleanVMFlag("UnlockCommercialFeatures"); 14376195Sbrian Boolean isFlightRecorder = WB.getBooleanVMFlag("FlightRecorder"); 14476195Sbrian String startFROptions = WB.getStringVMFlag("StartFlightRecording"); 14576195Sbrian if (isUnlockedCommercialFatures != null && isUnlockedCommercialFatures) { 14676195Sbrian if (isFlightRecorder != null && isFlightRecorder) { 14776195Sbrian return "true"; 14876195Sbrian } 14976195Sbrian if (startFROptions != null && !startFROptions.isEmpty()) { 15076195Sbrian return "true"; 15176195Sbrian } 15276195Sbrian } 15376195Sbrian return "false"; 15476195Sbrian } 15576195Sbrian 15676195Sbrian /** 15776195Sbrian * @return debug level value extracted from the "jdk.debug" property. 15876195Sbrian */ 15976195Sbrian protected String vmDebug() { 16076195Sbrian return "" + System.getProperty("jdk.debug").contains("debug"); 16176195Sbrian } 16276195Sbrian 16376195Sbrian /** 16476195Sbrian * @return true if VM supports JVMCI and false otherwise 16576195Sbrian */ 16676195Sbrian protected String vmJvmci() { 16776195Sbrian // builds with jvmci have this flag 16876195Sbrian return "" + (WB.getBooleanVMFlag("EnableJVMCI") != null); 16976195Sbrian } 17076195Sbrian 17176195Sbrian /** 17276195Sbrian * @return supported CPU features 17376195Sbrian */ 17476195Sbrian protected String cpuFeatures() { 17576195Sbrian return CPUInfo.getFeatures().toString(); 17676195Sbrian } 17776195Sbrian 17876195Sbrian /** 17976195Sbrian * For all existing GC sets vm.gc.X property. 18076195Sbrian * Example vm.gc.G1=true means: 18176195Sbrian * VM supports G1 18276195Sbrian * User either set G1 explicitely (-XX:+UseG1GC) or did not set any GC 18376195Sbrian * @param map - property-value pairs 18476195Sbrian */ 18576195Sbrian protected void vmGC(Map<String, String> map){ 18676195Sbrian GC currentGC = GC.current(); 18776195Sbrian boolean isByErgo = GC.currentSetByErgo(); 18876195Sbrian List<GC> supportedGC = GC.allSupported(); 18976195Sbrian for (GC gc: GC.values()) { 19076195Sbrian boolean isSupported = supportedGC.contains(gc); 19176195Sbrian boolean isAcceptable = isSupported && (gc == currentGC || isByErgo); 19276195Sbrian map.put("vm.gc." + gc.name(), "" + isAcceptable); 19376195Sbrian } 19476195Sbrian } 19576195Sbrian 19676195Sbrian /** 19776195Sbrian * Dumps the map to the file if the file name is given as the property. 19876195Sbrian * This functionality could be helpful to know context in the real 19976195Sbrian * execution. 20076195Sbrian * 20176195Sbrian * @param map 20276195Sbrian */ 20376195Sbrian protected static void dump(Map<String, String> map) { 20476195Sbrian String dumpFileName = System.getProperty("vmprops.dump"); 20576195Sbrian if (dumpFileName == null) { 20676195Sbrian return; 20776195Sbrian } 20876195Sbrian List<String> lines = new ArrayList<>(); 20976195Sbrian map.forEach((k, v) -> lines.add(k + ":" + v)); 21076195Sbrian try { 21176195Sbrian Files.write(Paths.get(dumpFileName), lines, StandardOpenOption.APPEND); 21276195Sbrian } catch (IOException e) { 21376195Sbrian throw new RuntimeException("Failed to dump properties into '" 21476195Sbrian + dumpFileName + "'", e); 21576195Sbrian } 21676195Sbrian } 21776195Sbrian 21876195Sbrian /** 21976195Sbrian * This method is for the testing purpose only. 22076195Sbrian * @param args 22176195Sbrian */ 22276195Sbrian public static void main(String args[]) { 22376195Sbrian Map<String, String> map = new VMProps().call(); 22476195Sbrian map.forEach((k, v) -> System.out.println(k + ": '" + v + "'")); 22576195Sbrian } 22676195Sbrian} 22776195Sbrian