VMProps.java revision 2137:bfe45d45e2a1
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.util.ArrayList;
29import java.util.HashMap;
30import java.util.List;
31import java.util.Map;
32import java.util.concurrent.Callable;
33import java.util.regex.Matcher;
34import 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 */
41public 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        map.put("vm.simpleArch", vmArch());
56        dump(map);
57        return map;
58    }
59
60
61    /**
62     * @return vm.simpleArch value of "os.simpleArch" property of tested JDK.
63     */
64    protected String vmArch() {
65        String arch = System.getProperty("os.arch");
66        if (arch.equals("x86_64") || arch.equals("amd64")) {
67            return "x64";
68        }
69        else if (arch.contains("86")) {
70            return "x86";
71        } else {
72            return arch;
73        }
74    }
75
76
77
78    /**
79     * @return VM type value extracted from the "java.vm.name" property.
80     */
81    protected String vmFlavor() {
82        // E.g. "Java HotSpot(TM) 64-Bit Server VM"
83        String vmName = System.getProperty("java.vm.name");
84        if (vmName == null) {
85            return null;
86        }
87
88        Pattern startP = Pattern.compile(".* (\\S+) VM");
89        Matcher m = startP.matcher(vmName);
90        if (m.matches()) {
91            return m.group(1).toLowerCase();
92        }
93        return null;
94    }
95
96    /**
97     * @return VM compilation mode extracted from the "java.vm.info" property.
98     */
99    protected String vmCompMode() {
100        // E.g. "mixed mode"
101        String vmInfo = System.getProperty("java.vm.info");
102        if (vmInfo == null) {
103            return null;
104        }
105        int k = vmInfo.toLowerCase().indexOf(" mode");
106        if (k < 0) {
107            return null;
108        }
109        vmInfo = vmInfo.substring(0, k);
110        switch (vmInfo) {
111            case "mixed" : return "Xmixed";
112            case "compiled" : return "Xcomp";
113            case "interpreted" : return "Xint";
114            default: return null;
115        }
116    }
117
118    /**
119     * @return VM bitness, the value of the "sun.arch.data.model" property.
120     */
121    protected String vmBits() {
122        return System.getProperty("sun.arch.data.model");
123    }
124
125    /**
126     * Dumps the map to the file if the file name is given as the property.
127     * This functionality could be helpful to know context in the real
128     * execution.
129     *
130     * @param map
131     */
132    protected void dump(Map<String, String> map) {
133        String dumpFileName = System.getProperty("vmprops.dump");
134        if (dumpFileName == null) {
135            return;
136        }
137        List<String> lines = new ArrayList<>();
138        map.forEach((k,v) -> lines.add(k + ":" + v));
139        try {
140             Files.write(Paths.get(dumpFileName), lines);
141        } catch (IOException e) {
142            throw new RuntimeException("Failed to dump properties into '"
143                    + dumpFileName + "'", e);
144        }
145    }
146
147    /**
148     * This method is for the testing purpose only.
149     * @param args
150     */
151    public static void main(String args[]) {
152        Map<String, String> map = new VMProps().call();
153        map.forEach((k,v) -> System.out.println(k + ": '" + v + "'"));
154    }
155}
156