hotspot/test/compiler/compilercontrol/share/processors/PrintProcessor.java
author tpivovarova
Tue, 12 Jul 2016 18:24:48 +0300
changeset 40059 c2304140ed64
parent 33452 04815c29859c
child 40631 ed82623d7831
permissions -rw-r--r--
8132919: Put compiler tests in packages Reviewed-by: vlivanov, dpochepk Contributed-by: igor.ignatyev@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33452
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
     1
/*
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
     4
 *
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
     8
 *
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    13
 * accompanied this code).
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    14
 *
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    18
 *
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    21
 * questions.
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    22
 */
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    23
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    24
package compiler.compilercontrol.share.processors;
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    25
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    26
import com.sun.management.HotSpotDiagnosticMXBean;
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    27
import compiler.compilercontrol.share.method.MethodDescriptor;
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    28
import compiler.compilercontrol.share.method.MethodGenerator;
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 33452
diff changeset
    29
import compiler.compilercontrol.share.pool.PoolHelper;
33452
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    30
import compiler.compilercontrol.share.scenario.State;
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    31
import jdk.test.lib.OutputAnalyzer;
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    32
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    33
import java.lang.management.ManagementFactory;
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    34
import java.lang.reflect.Executable;
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    35
import java.util.List;
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    36
import java.util.Map;
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    37
import java.util.function.Consumer;
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    38
import java.util.regex.Matcher;
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    39
import java.util.regex.Pattern;
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    40
import java.util.stream.Collectors;
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    41
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    42
/**
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    43
 * Process output to find compiled methods assemblies printed by print command
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    44
 */
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    45
public class PrintProcessor implements Consumer<OutputAnalyzer> {
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    46
    /**
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    47
     * Compiled method string pattern.
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    48
     * Capturing groups are
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    49
     * 1. Compiler used to compile this method
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    50
     * 2. Time stamp
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    51
     * 3. Compile ID
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    52
     * 4. Method attributes
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    53
     * 5. Compilation level
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    54
     * 6. Method name
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    55
     */
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    56
    private static final Pattern COMPILED_METHOD
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    57
            = Pattern.compile("Compiled method (?<compiler>\\(.*\\))[ ]+"
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    58
            + "(?<time>[0-9]+)[ ]+(?<id>[0-9]+) (?<attr>[ !%sbn]{6})"
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    59
            + "(?<level>[0-9]+)[ ]+(?<name>[^ ]+).*");
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    60
    private final List<String> printMethods;
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    61
    private final List<String> testMethods;
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    62
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    63
    public PrintProcessor(Map<Executable, State> states) {
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    64
        printMethods = states.keySet().stream()
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    65
                .filter(x -> states.get(x).isPrintAssembly())
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    66
                .map(MethodGenerator::logDescriptor)
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    67
                .map(MethodDescriptor::getString)
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    68
                .map(s -> s.replaceFirst("\\(.*", "")) // remove signature
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    69
                .collect(Collectors.toList());
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    70
        testMethods = new PoolHelper().getAllMethods()
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    71
                .stream()
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    72
                .map(pair -> pair.first)
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    73
                .map(MethodGenerator::logDescriptor)
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    74
                .map(MethodDescriptor::getString)
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    75
                .map(s -> s.replaceFirst("\\(.*", "")) // remove signature
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    76
                .collect(Collectors.toList());
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    77
    }
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    78
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    79
    @Override
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    80
    public void accept(OutputAnalyzer outputAnalyzer) {
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    81
        boolean wizardMode = false;
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    82
        try {
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    83
            wizardMode = Boolean.parseBoolean(ManagementFactory
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    84
                    .getPlatformMXBean(HotSpotDiagnosticMXBean.class)
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    85
                    .getVMOption("WizardMode").getValue());
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    86
        } catch (IllegalArgumentException e) {
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    87
            // ignore exception because WizardMode exists in debug only builds
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    88
        }
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    89
        if (wizardMode) {
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    90
            System.out.println("SKIP: WizardMode's output are not supported");
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    91
            return;
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    92
        }
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    93
        for (String line : outputAnalyzer.asLines()) {
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    94
            Matcher matcher = COMPILED_METHOD.matcher(line);
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    95
            if (matcher.matches()) {
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    96
                String method = normalize(matcher.group("name"));
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    97
                if (!printMethods.contains(normalize(method))
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    98
                        && testMethods.contains(method)) {
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
    99
                    System.out.println(outputAnalyzer.getOutput());
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
   100
                    throw new AssertionError("FAILED: wrong method "
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
   101
                            + "was printed: " + method + " LINE: " + line);
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
   102
                }
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
   103
            }
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
   104
        }
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
   105
    }
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
   106
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
   107
    // Normalize given signature to conform regular expression used in tests
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
   108
    private String normalize(String method) {
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
   109
        return method.replaceAll("\\.", "/") // replace dots in a class string
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
   110
                .replaceFirst("::", ".")     // replace :: between class and method
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
   111
                .replace("&lt;", "<")
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
   112
                .replace("&gt;", ">");
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
   113
    }
04815c29859c 8066153: JEP-JDK-8046155: Test task: cover existing
ppunegov
parents:
diff changeset
   114
}