VMProps.java revision 2137:bfe45d45e2a1
1169695Skan/*
2169695Skan * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3169695Skan * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4169695Skan *
5169695Skan * This code is free software; you can redistribute it and/or modify it
6169695Skan * under the terms of the GNU General Public License version 2 only, as
7169695Skan * published by the Free Software Foundation.
8169695Skan *
9169695Skan * This code is distributed in the hope that it will be useful, but WITHOUT
10169695Skan * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11169695Skan * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12169695Skan * version 2 for more details (a copy is included in the LICENSE file that
13169695Skan * accompanied this code).
14169695Skan *
15169695Skan * You should have received a copy of the GNU General Public License version
16169695Skan * 2 along with this work; if not, write to the Free Software Foundation,
17169695Skan * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18169695Skan *
19169695Skan * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20169695Skan * or visit www.oracle.com if you need additional information or have any
21169695Skan * questions.
22169695Skan */
23169695Skanpackage requires;
24169695Skan
25169695Skanimport java.io.IOException;
26169695Skanimport java.nio.file.Files;
27169695Skanimport java.nio.file.Paths;
28169695Skanimport java.util.ArrayList;
29169695Skanimport java.util.HashMap;
30169695Skanimport java.util.List;
31169695Skanimport java.util.Map;
32169695Skanimport java.util.concurrent.Callable;
33169695Skanimport java.util.regex.Matcher;
34169695Skanimport java.util.regex.Pattern;
35169695Skan
36169695Skan/**
37169695Skan * The Class to be invoked by jtreg prior Test Suite execution to
38169695Skan * collect information about VM.
39169695Skan * Properties set by this Class will be available in the @requires expressions.
40169695Skan */
41169695Skanpublic class VMProps implements Callable<Map<String, String>> {
42169695Skan
43169695Skan    /**
44169695Skan     * Collects information about VM properties.
45169695Skan     * This method will be invoked by jtreg.
46169695Skan     *
47169695Skan     * @return Map of property-value pairs.
48169695Skan     */
49169695Skan    @Override
50169695Skan    public Map<String, String> call() {
51169695Skan        Map<String, String> map = new HashMap<>();
52169695Skan        map.put("vm.flavor", vmFlavor());
53169695Skan        map.put("vm.compMode", vmCompMode());
54169695Skan        map.put("vm.bits", vmBits());
55169695Skan        map.put("vm.simpleArch", vmArch());
56169695Skan        dump(map);
57169695Skan        return map;
58169695Skan    }
59169695Skan
60169695Skan
61169695Skan    /**
62169695Skan     * @return vm.simpleArch value of "os.simpleArch" property of tested JDK.
63169695Skan     */
64169695Skan    protected String vmArch() {
65169695Skan        String arch = System.getProperty("os.arch");
66169695Skan        if (arch.equals("x86_64") || arch.equals("amd64")) {
67169695Skan            return "x64";
68169695Skan        }
69169695Skan        else if (arch.contains("86")) {
70169695Skan            return "x86";
71169695Skan        } else {
72169695Skan            return arch;
73169695Skan        }
74169695Skan    }
75169695Skan
76169695Skan
77169695Skan
78169695Skan    /**
79169695Skan     * @return VM type value extracted from the "java.vm.name" property.
80169695Skan     */
81169695Skan    protected String vmFlavor() {
82169695Skan        // E.g. "Java HotSpot(TM) 64-Bit Server VM"
83169695Skan        String vmName = System.getProperty("java.vm.name");
84169695Skan        if (vmName == null) {
85169695Skan            return null;
86169695Skan        }
87169695Skan
88169695Skan        Pattern startP = Pattern.compile(".* (\\S+) VM");
89169695Skan        Matcher m = startP.matcher(vmName);
90169695Skan        if (m.matches()) {
91169695Skan            return m.group(1).toLowerCase();
92169695Skan        }
93169695Skan        return null;
94169695Skan    }
95169695Skan
96169695Skan    /**
97169695Skan     * @return VM compilation mode extracted from the "java.vm.info" property.
98169695Skan     */
99169695Skan    protected String vmCompMode() {
100169695Skan        // E.g. "mixed mode"
101169695Skan        String vmInfo = System.getProperty("java.vm.info");
102169695Skan        if (vmInfo == null) {
103169695Skan            return null;
104169695Skan        }
105169695Skan        int k = vmInfo.toLowerCase().indexOf(" mode");
106169695Skan        if (k < 0) {
107169695Skan            return null;
108169695Skan        }
109169695Skan        vmInfo = vmInfo.substring(0, k);
110169695Skan        switch (vmInfo) {
111169695Skan            case "mixed" : return "Xmixed";
112169695Skan            case "compiled" : return "Xcomp";
113169695Skan            case "interpreted" : return "Xint";
114169695Skan            default: return null;
115169695Skan        }
116169695Skan    }
117169695Skan
118169695Skan    /**
119169695Skan     * @return VM bitness, the value of the "sun.arch.data.model" property.
120169695Skan     */
121169695Skan    protected String vmBits() {
122169695Skan        return System.getProperty("sun.arch.data.model");
123169695Skan    }
124169695Skan
125169695Skan    /**
126169695Skan     * Dumps the map to the file if the file name is given as the property.
127169695Skan     * This functionality could be helpful to know context in the real
128169695Skan     * execution.
129169695Skan     *
130169695Skan     * @param map
131169695Skan     */
132169695Skan    protected void dump(Map<String, String> map) {
133169695Skan        String dumpFileName = System.getProperty("vmprops.dump");
134169695Skan        if (dumpFileName == null) {
135169695Skan            return;
136169695Skan        }
137169695Skan        List<String> lines = new ArrayList<>();
138169695Skan        map.forEach((k,v) -> lines.add(k + ":" + v));
139169695Skan        try {
140169695Skan             Files.write(Paths.get(dumpFileName), lines);
141169695Skan        } catch (IOException e) {
142169695Skan            throw new RuntimeException("Failed to dump properties into '"
143169695Skan                    + dumpFileName + "'", e);
144169695Skan        }
145169695Skan    }
146169695Skan
147169695Skan    /**
148169695Skan     * This method is for the testing purpose only.
149169695Skan     * @param args
150169695Skan     */
151169695Skan    public static void main(String args[]) {
152169695Skan        Map<String, String> map = new VMProps().call();
153169695Skan        map.forEach((k,v) -> System.out.println(k + ": '" + v + "'"));
154169695Skan    }
155169695Skan}
156169695Skan