< prev index next >

test/jtreg-ext/requires/VMProps.java

Print this page




  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;


  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         }


  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 import sun.hotspot.WhiteBox;
  36 import sun.hotspot.gc.GC;
  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     private static final WhiteBox WB = WhiteBox.getWhiteBox();    
  46 
  47     /**
  48      * Collects information about VM properties.
  49      * This method will be invoked by jtreg.
  50      *
  51      * @return Map of property-value pairs.
  52      */
  53     @Override
  54     public Map<String, String> call() {
  55         Map<String, String> map = new HashMap<>();
  56         map.put("vm.flavor", vmFlavor());
  57         map.put("vm.compMode", vmCompMode());
  58         map.put("vm.bits", vmBits());
  59         vmGC(map); // vm.gc.X.acceptable = true/false
  60         dump(map);
  61         return map;
  62     }
  63 
  64     /**
  65      * @return VM type value extracted from the "java.vm.name" property.
  66      */
  67     protected String vmFlavor() {
  68         // E.g. "Java HotSpot(TM) 64-Bit Server VM"
  69         String vmName = System.getProperty("java.vm.name");
  70         if (vmName == null) {
  71             return null;
  72         }
  73 
  74         Pattern startP = Pattern.compile(".* (\\S+) VM");
  75         Matcher m = startP.matcher(vmName);
  76         if (m.matches()) {
  77             return m.group(1).toLowerCase();
  78         }
  79         return null;


  91         int k = vmInfo.toLowerCase().indexOf(" mode");
  92         if (k < 0) {
  93             return null;
  94         }
  95         vmInfo = vmInfo.substring(0, k);
  96         switch (vmInfo) {
  97             case "mixed" : return "Xmixed";
  98             case "compiled" : return "Xcomp";
  99             case "interpreted" : return "Xint";
 100             default: return null;
 101         }
 102     }
 103 
 104     /**
 105      * @return VM bitness, the value of the "sun.arch.data.model" property.
 106      */
 107     protected String vmBits() {
 108         return System.getProperty("sun.arch.data.model");
 109     }
 110 
 111     /**
 112      * For all existing GC sets vm.gc.XXX.acceptable property.
 113      * Example vm.gc.G1.acceptable=true means: 
 114      *    VM supports G1
 115      *    User either set G1 explicitely (-XX:+UseG1GC) or did not set any GC
 116      * @param map - requires context
 117      */    
 118     protected void vmGC(Map<String, String> map){
 119         GC currentGC = GC.current();
 120         boolean isByErgo = GC.currentSetByErgo();
 121         List<GC> supportedGC = GC.allSupported();
 122         for (GC gc: GC.values()) {
 123             boolean isSupported = supportedGC.contains(gc);
 124             boolean isAcceptable = isSupported && (gc == currentGC || isByErgo);
 125             map.put("vm.gc." + gc.name() + ".acceptable", "" + isAcceptable);
 126         }
 127     }
 128 
 129     /**
 130      * Dumps the map to the file if the file name is given as the property.
 131      * This functionality could be helpful to know context in the real
 132      * execution.
 133      *
 134      * @param map
 135      */
 136     protected void dump(Map<String, String> map) {
 137         String dumpFileName = System.getProperty("vmprops.dump");
 138         if (dumpFileName == null) {
 139             return;
 140         }
 141         List<String> lines = new ArrayList<>();
 142         map.forEach((k,v) -> lines.add(k + ":" + v));
 143         try {
 144              Files.write(Paths.get(dumpFileName), lines);
 145         } catch (IOException e) {
 146             throw new RuntimeException("Failed to dump properties into '"
 147                     + dumpFileName + "'", e);
 148         }
< prev index next >