VMProps.java revision 2470:65a40732289f
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.nio.file.Files;
27import java.nio.file.Paths;
28import java.nio.file.StandardOpenOption;
29import java.util.ArrayList;
30import java.util.HashMap;
31import java.util.List;
32import java.util.Map;
33import java.util.concurrent.Callable;
34import java.util.regex.Matcher;
35import java.util.regex.Pattern;
36
37import sun.hotspot.cpuinfo.CPUInfo;
38import sun.hotspot.gc.GC;
39import sun.hotspot.WhiteBox;
40
41/**
42 * The Class to be invoked by jtreg prior Test Suite execution to
43 * collect information about VM.
44 * Do not use any API's that may not be available in all target VMs.
45 * Properties set by this Class will be available in the @requires expressions.
46 */
47public class VMProps implements Callable<Map<String, String>> {
48
49    private static final WhiteBox WB = WhiteBox.getWhiteBox();
50
51    /**
52     * Collects information about VM properties.
53     * This method will be invoked by jtreg.
54     *
55     * @return Map of property-value pairs.
56     */
57    @Override
58    public Map<String, String> call() {
59        Map<String, String> map = new HashMap<>();
60        map.put("vm.flavor", vmFlavor());
61        map.put("vm.compMode", vmCompMode());
62        map.put("vm.bits", vmBits());
63        map.put("vm.flightRecorder", vmFlightRecorder());
64        map.put("vm.simpleArch", vmArch());
65        map.put("vm.debug", vmDebug());
66        map.put("vm.jvmci", vmJvmci());
67        map.put("vm.cpu.features", cpuFeatures());
68        vmGC(map); // vm.gc.X = true/false
69
70        VMProps.dump(map);
71        return map;
72    }
73
74    /**
75     * @return vm.simpleArch value of "os.simpleArch" property of tested JDK.
76     */
77    protected String vmArch() {
78        String arch = System.getProperty("os.arch");
79        if (arch.equals("x86_64") || arch.equals("amd64")) {
80            return "x64";
81        }
82        else if (arch.contains("86")) {
83            return "x86";
84        } else {
85            return arch;
86        }
87    }
88
89
90
91    /**
92     * @return VM type value extracted from the "java.vm.name" property.
93     */
94    protected String vmFlavor() {
95        // E.g. "Java HotSpot(TM) 64-Bit Server VM"
96        String vmName = System.getProperty("java.vm.name");
97        if (vmName == null) {
98            return null;
99        }
100
101        Pattern startP = Pattern.compile(".* (\\S+) VM");
102        Matcher m = startP.matcher(vmName);
103        if (m.matches()) {
104            return m.group(1).toLowerCase();
105        }
106        return null;
107    }
108
109    /**
110     * @return VM compilation mode extracted from the "java.vm.info" property.
111     */
112    protected String vmCompMode() {
113        // E.g. "mixed mode"
114        String vmInfo = System.getProperty("java.vm.info");
115        if (vmInfo == null) {
116            return null;
117        }
118        int k = vmInfo.toLowerCase().indexOf(" mode");
119        if (k < 0) {
120            return null;
121        }
122        vmInfo = vmInfo.substring(0, k);
123        switch (vmInfo) {
124            case "mixed" : return "Xmixed";
125            case "compiled" : return "Xcomp";
126            case "interpreted" : return "Xint";
127            default: return null;
128        }
129    }
130
131    /**
132     * @return VM bitness, the value of the "sun.arch.data.model" property.
133     */
134    protected String vmBits() {
135        return System.getProperty("sun.arch.data.model");
136    }
137
138    /**
139     * @return "true" if Flight Recorder is enabled, "false" if is disabled.
140     */
141    protected String vmFlightRecorder() {
142        Boolean isUnlockedCommercialFatures = WB.getBooleanVMFlag("UnlockCommercialFeatures");
143        Boolean isFlightRecorder = WB.getBooleanVMFlag("FlightRecorder");
144        String startFROptions = WB.getStringVMFlag("StartFlightRecording");
145        if (isUnlockedCommercialFatures != null && isUnlockedCommercialFatures) {
146            if (isFlightRecorder != null && isFlightRecorder) {
147                return "true";
148            }
149            if (startFROptions != null && !startFROptions.isEmpty()) {
150                return "true";
151            }
152        }
153        return "false";
154    }
155
156    /**
157     * @return debug level value extracted from the "jdk.debug" property.
158     */
159    protected String vmDebug() {
160        return "" + System.getProperty("jdk.debug").contains("debug");
161    }
162
163    /**
164     * @return true if VM supports JVMCI and false otherwise
165     */
166    protected String vmJvmci() {
167        // builds with jvmci have this flag
168        return "" + (WB.getBooleanVMFlag("EnableJVMCI") != null);
169    }
170
171    /**
172     * @return supported CPU features
173     */
174    protected String cpuFeatures() {
175        return CPUInfo.getFeatures().toString();
176    }
177
178    /**
179     * For all existing GC sets vm.gc.X property.
180     * Example vm.gc.G1=true means:
181     *    VM supports G1
182     *    User either set G1 explicitely (-XX:+UseG1GC) or did not set any GC
183     * @param map - property-value pairs
184     */
185    protected void vmGC(Map<String, String> map){
186        GC currentGC = GC.current();
187        boolean isByErgo = GC.currentSetByErgo();
188        List<GC> supportedGC = GC.allSupported();
189        for (GC gc: GC.values()) {
190            boolean isSupported = supportedGC.contains(gc);
191            boolean isAcceptable = isSupported && (gc == currentGC || isByErgo);
192            map.put("vm.gc." + gc.name(), "" + isAcceptable);
193        }
194    }
195
196    /**
197     * Dumps the map to the file if the file name is given as the property.
198     * This functionality could be helpful to know context in the real
199     * execution.
200     *
201     * @param map
202     */
203    protected static void dump(Map<String, String> map) {
204        String dumpFileName = System.getProperty("vmprops.dump");
205        if (dumpFileName == null) {
206            return;
207        }
208        List<String> lines = new ArrayList<>();
209        map.forEach((k, v) -> lines.add(k + ":" + v));
210        try {
211            Files.write(Paths.get(dumpFileName), lines, StandardOpenOption.APPEND);
212        } catch (IOException e) {
213            throw new RuntimeException("Failed to dump properties into '"
214                    + dumpFileName + "'", e);
215        }
216    }
217
218    /**
219     * This method is for the testing purpose only.
220     * @param args
221     */
222    public static void main(String args[]) {
223        Map<String, String> map = new VMProps().call();
224        map.forEach((k, v) -> System.out.println(k + ": '" + v + "'"));
225    }
226}
227