src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core/src/org/graalvm/compiler/core/CompilationPrinter.java
author dlong
Tue, 24 Sep 2019 12:47:15 -0400
changeset 58299 6df94ce3ab2f
parent 52910 583fd71c47d6
permissions -rw-r--r--
8229201: Update Graal Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
     1
/*
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 52910
diff changeset
     2
 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
     4
 *
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
     8
 *
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    13
 * accompanied this code).
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    14
 *
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    18
 *
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    21
 * questions.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    22
 */
50858
2d3e99a72541 8205824: Update Graal
never
parents: 49873
diff changeset
    23
2d3e99a72541 8205824: Update Graal
never
parents: 49873
diff changeset
    24
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    25
package org.graalvm.compiler.core;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    26
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    27
import static org.graalvm.compiler.core.GraalCompilerOptions.PrintCompilation;
49873
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
    28
import static org.graalvm.compiler.serviceprovider.GraalServices.getCurrentThreadAllocatedBytes;
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
    29
import static org.graalvm.compiler.serviceprovider.GraalServices.isThreadAllocatedMemorySupported;
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    30
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    31
import org.graalvm.compiler.code.CompilationResult;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    32
import org.graalvm.compiler.core.common.CompilationIdentifier;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    33
import org.graalvm.compiler.debug.TTY;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    34
import org.graalvm.compiler.options.OptionValues;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    35
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    36
import jdk.vm.ci.meta.JavaMethod;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    37
import jdk.vm.ci.runtime.JVMCICompiler;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    38
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    39
/**
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    40
 * Utility for printing an informational line to {@link TTY} upon completion of compiling a method.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    41
 */
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    42
public final class CompilationPrinter {
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    43
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    44
    private final CompilationIdentifier id;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    45
    private final JavaMethod method;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    46
    private final int entryBCI;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    47
    private final long start;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    48
    private final long allocatedBytesBefore;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    49
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    50
    /**
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    51
     * Gets an object that will report statistics for a compilation if
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    52
     * {@link GraalCompilerOptions#PrintCompilation} is enabled and {@link TTY} is not suppressed.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    53
     * This method should be called just before a compilation starts as it captures pre-compilation
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    54
     * data for the purpose of {@linkplain #finish(CompilationResult) printing} the post-compilation
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    55
     * statistics.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    56
     *
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    57
     * @param options used to get the value of {@link GraalCompilerOptions#PrintCompilation}
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    58
     * @param id the identifier for the compilation
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    59
     * @param method the method for which code is being compiled
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    60
     * @param entryBCI the BCI at which compilation starts
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    61
     */
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    62
    public static CompilationPrinter begin(OptionValues options, CompilationIdentifier id, JavaMethod method, int entryBCI) {
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    63
        if (PrintCompilation.getValue(options) && !TTY.isSuppressed()) {
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    64
            return new CompilationPrinter(id, method, entryBCI);
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    65
        }
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    66
        return DISABLED;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    67
    }
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    68
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    69
    private static final CompilationPrinter DISABLED = new CompilationPrinter();
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    70
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    71
    private CompilationPrinter() {
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    72
        this.method = null;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    73
        this.id = null;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    74
        this.entryBCI = -1;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    75
        this.start = -1;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    76
        this.allocatedBytesBefore = -1;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    77
    }
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    78
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    79
    private CompilationPrinter(CompilationIdentifier id, JavaMethod method, int entryBCI) {
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    80
        this.method = method;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    81
        this.id = id;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    82
        this.entryBCI = entryBCI;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    83
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    84
        start = System.nanoTime();
49873
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
    85
        allocatedBytesBefore = isThreadAllocatedMemorySupported() ? getCurrentThreadAllocatedBytes() : -1;
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    86
    }
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    87
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    88
    private String getMethodDescription() {
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    89
        return String.format("%-30s %-70s %-45s %-50s %s", id.toString(CompilationIdentifier.Verbosity.ID),
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    90
                        method.getDeclaringClass().getName(), method.getName(),
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    91
                        method.getSignature().toMethodDescriptor(),
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    92
                        entryBCI == JVMCICompiler.INVOCATION_ENTRY_BCI ? "" : "(OSR@" + entryBCI + ") ");
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    93
    }
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    94
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    95
    /**
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    96
     * Notifies this object that the compilation finished and the informational line should be
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    97
     * printed to {@link TTY}.
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    98
     */
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
    99
    public void finish(CompilationResult result) {
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   100
        if (id != null) {
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   101
            final long stop = System.nanoTime();
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 52910
diff changeset
   102
            final long duration = (stop - start) / 1000;
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   103
            final int targetCodeSize = result != null ? result.getTargetCodeSize() : -1;
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   104
            final int bytecodeSize = result != null ? result.getBytecodeSize() : 0;
49873
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
   105
            if (allocatedBytesBefore == -1) {
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 52910
diff changeset
   106
                TTY.println(getMethodDescription() + String.format(" | %4dus %5dB bytecodes %5dB codesize", duration, bytecodeSize, targetCodeSize));
49873
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
   107
            } else {
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
   108
                final long allocatedBytesAfter = getCurrentThreadAllocatedBytes();
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
   109
                final long allocatedKBytes = (allocatedBytesAfter - allocatedBytesBefore) / 1024;
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 52910
diff changeset
   110
                TTY.println(getMethodDescription() + String.format(" | %4dus %5dB bytecodes %5dB codesize %5dkB allocated", duration, bytecodeSize, targetCodeSize, allocatedKBytes));
49873
26ebfe8ce852 8199755: Update Graal
dlong
parents: 47216
diff changeset
   111
            }
46680
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   112
        }
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   113
    }
2894e4262fd6 8184768: Update Graal
iveresov
parents:
diff changeset
   114
}