PrintProcessor.java revision 11833:1cbffa2beba6
1276789Sdim/*
2276789Sdim * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3276789Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4276789Sdim *
5276789Sdim * This code is free software; you can redistribute it and/or modify it
6276789Sdim * under the terms of the GNU General Public License version 2 only, as
7276789Sdim * published by the Free Software Foundation.
8276789Sdim *
9276789Sdim * This code is distributed in the hope that it will be useful, but WITHOUT
10276789Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11276789Sdim * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12276789Sdim * version 2 for more details (a copy is included in the LICENSE file that
13276789Sdim * accompanied this code).
14276789Sdim *
15276789Sdim * You should have received a copy of the GNU General Public License version
16276789Sdim * 2 along with this work; if not, write to the Free Software Foundation,
17276789Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18276789Sdim *
19276789Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20276789Sdim * or visit www.oracle.com if you need additional information or have any
21276789Sdim * questions.
22276789Sdim */
23276789Sdim
24276789Sdimpackage compiler.compilercontrol.share.processors;
25276789Sdim
26276789Sdimimport com.sun.management.HotSpotDiagnosticMXBean;
27276789Sdimimport compiler.compilercontrol.share.method.MethodDescriptor;
28276789Sdimimport compiler.compilercontrol.share.method.MethodGenerator;
29276789Sdimimport compiler.compilercontrol.share.pool.PoolHelper;
30276789Sdimimport compiler.compilercontrol.share.scenario.State;
31276789Sdimimport jdk.test.lib.process.OutputAnalyzer;
32276789Sdim
33276789Sdimimport java.lang.management.ManagementFactory;
34276789Sdimimport java.lang.reflect.Executable;
35276789Sdimimport java.util.List;
36276789Sdimimport java.util.Map;
37276789Sdimimport java.util.function.Consumer;
38276789Sdimimport java.util.regex.Matcher;
39276789Sdimimport java.util.regex.Pattern;
40276789Sdimimport java.util.stream.Collectors;
41276789Sdim
42276789Sdim/**
43276789Sdim * Process output to find compiled methods assemblies printed by print command
44276789Sdim */
45276789Sdimpublic class PrintProcessor implements Consumer<OutputAnalyzer> {
46276789Sdim    /**
47276789Sdim     * Compiled method string pattern.
48276789Sdim     * Capturing groups are
49276789Sdim     * 1. Compiler used to compile this method
50276789Sdim     * 2. Time stamp
51276789Sdim     * 3. Compile ID
52276789Sdim     * 4. Method attributes
53276789Sdim     * 5. Compilation level
54276789Sdim     * 6. Method name
55276789Sdim     */
56276789Sdim    private static final Pattern COMPILED_METHOD
57276789Sdim            = Pattern.compile("Compiled method (?<compiler>\\(.*\\))[ ]+"
58276789Sdim            + "(?<time>[0-9]+)[ ]+(?<id>[0-9]+) (?<attr>[ !%sbn]{6})"
59276789Sdim            + "(?<level>[0-9]+)[ ]+(?<name>[^ ]+).*");
60309124Sdim    private final List<String> printMethods;
61309124Sdim    private final List<String> testMethods;
62309124Sdim
63    public PrintProcessor(Map<Executable, State> states) {
64        printMethods = states.keySet().stream()
65                .filter(x -> states.get(x).isPrintAssembly())
66                .map(MethodGenerator::logDescriptor)
67                .map(MethodDescriptor::getString)
68                .map(s -> s.replaceFirst("\\(.*", "")) // remove signature
69                .collect(Collectors.toList());
70        testMethods = new PoolHelper().getAllMethods()
71                .stream()
72                .map(pair -> pair.first)
73                .map(MethodGenerator::logDescriptor)
74                .map(MethodDescriptor::getString)
75                .map(s -> s.replaceFirst("\\(.*", "")) // remove signature
76                .collect(Collectors.toList());
77    }
78
79    @Override
80    public void accept(OutputAnalyzer outputAnalyzer) {
81        boolean wizardMode = false;
82        try {
83            wizardMode = Boolean.parseBoolean(ManagementFactory
84                    .getPlatformMXBean(HotSpotDiagnosticMXBean.class)
85                    .getVMOption("WizardMode").getValue());
86        } catch (IllegalArgumentException e) {
87            // ignore exception because WizardMode exists in debug only builds
88        }
89        if (wizardMode) {
90            System.out.println("SKIP: WizardMode's output are not supported");
91            return;
92        }
93        for (String line : outputAnalyzer.asLines()) {
94            Matcher matcher = COMPILED_METHOD.matcher(line);
95            if (matcher.matches()) {
96                String method = normalize(matcher.group("name"));
97                if (!printMethods.contains(normalize(method))
98                        && testMethods.contains(method)) {
99                    System.out.println(outputAnalyzer.getOutput());
100                    throw new AssertionError("FAILED: wrong method "
101                            + "was printed: " + method + " LINE: " + line);
102                }
103            }
104        }
105    }
106
107    // Normalize given signature to conform regular expression used in tests
108    private String normalize(String method) {
109        return method.replaceAll("\\.", "/") // replace dots in a class string
110                .replaceFirst("::", ".")     // replace :: between class and method
111                .replace("&lt;", "<")
112                .replace("&gt;", ">");
113    }
114}
115