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