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