46680
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
|
46680
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
46680
|
25 |
package org.graalvm.compiler.core;
|
|
26 |
|
|
27 |
import static org.graalvm.compiler.core.GraalCompilerOptions.PrintCompilation;
|
49873
|
28 |
import static org.graalvm.compiler.serviceprovider.GraalServices.getCurrentThreadAllocatedBytes;
|
|
29 |
import static org.graalvm.compiler.serviceprovider.GraalServices.isThreadAllocatedMemorySupported;
|
46680
|
30 |
|
|
31 |
import org.graalvm.compiler.code.CompilationResult;
|
|
32 |
import org.graalvm.compiler.core.common.CompilationIdentifier;
|
|
33 |
import org.graalvm.compiler.debug.TTY;
|
|
34 |
import org.graalvm.compiler.options.OptionValues;
|
|
35 |
|
|
36 |
import jdk.vm.ci.meta.JavaMethod;
|
|
37 |
import jdk.vm.ci.runtime.JVMCICompiler;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* Utility for printing an informational line to {@link TTY} upon completion of compiling a method.
|
|
41 |
*/
|
|
42 |
public final class CompilationPrinter {
|
|
43 |
|
|
44 |
private final CompilationIdentifier id;
|
|
45 |
private final JavaMethod method;
|
|
46 |
private final int entryBCI;
|
|
47 |
private final long start;
|
|
48 |
private final long allocatedBytesBefore;
|
|
49 |
|
|
50 |
/**
|
|
51 |
* Gets an object that will report statistics for a compilation if
|
|
52 |
* {@link GraalCompilerOptions#PrintCompilation} is enabled and {@link TTY} is not suppressed.
|
|
53 |
* This method should be called just before a compilation starts as it captures pre-compilation
|
|
54 |
* data for the purpose of {@linkplain #finish(CompilationResult) printing} the post-compilation
|
|
55 |
* statistics.
|
|
56 |
*
|
|
57 |
* @param options used to get the value of {@link GraalCompilerOptions#PrintCompilation}
|
|
58 |
* @param id the identifier for the compilation
|
|
59 |
* @param method the method for which code is being compiled
|
|
60 |
* @param entryBCI the BCI at which compilation starts
|
|
61 |
*/
|
|
62 |
public static CompilationPrinter begin(OptionValues options, CompilationIdentifier id, JavaMethod method, int entryBCI) {
|
|
63 |
if (PrintCompilation.getValue(options) && !TTY.isSuppressed()) {
|
|
64 |
return new CompilationPrinter(id, method, entryBCI);
|
|
65 |
}
|
|
66 |
return DISABLED;
|
|
67 |
}
|
|
68 |
|
|
69 |
private static final CompilationPrinter DISABLED = new CompilationPrinter();
|
|
70 |
|
|
71 |
private CompilationPrinter() {
|
|
72 |
this.method = null;
|
|
73 |
this.id = null;
|
|
74 |
this.entryBCI = -1;
|
|
75 |
this.start = -1;
|
|
76 |
this.allocatedBytesBefore = -1;
|
|
77 |
}
|
|
78 |
|
|
79 |
private CompilationPrinter(CompilationIdentifier id, JavaMethod method, int entryBCI) {
|
|
80 |
this.method = method;
|
|
81 |
this.id = id;
|
|
82 |
this.entryBCI = entryBCI;
|
|
83 |
|
|
84 |
start = System.nanoTime();
|
49873
|
85 |
allocatedBytesBefore = isThreadAllocatedMemorySupported() ? getCurrentThreadAllocatedBytes() : -1;
|
46680
|
86 |
}
|
|
87 |
|
|
88 |
private String getMethodDescription() {
|
|
89 |
return String.format("%-30s %-70s %-45s %-50s %s", id.toString(CompilationIdentifier.Verbosity.ID),
|
|
90 |
method.getDeclaringClass().getName(), method.getName(),
|
|
91 |
method.getSignature().toMethodDescriptor(),
|
|
92 |
entryBCI == JVMCICompiler.INVOCATION_ENTRY_BCI ? "" : "(OSR@" + entryBCI + ") ");
|
|
93 |
}
|
|
94 |
|
|
95 |
/**
|
|
96 |
* Notifies this object that the compilation finished and the informational line should be
|
|
97 |
* printed to {@link TTY}.
|
|
98 |
*/
|
|
99 |
public void finish(CompilationResult result) {
|
|
100 |
if (id != null) {
|
|
101 |
final long stop = System.nanoTime();
|
58299
|
102 |
final long duration = (stop - start) / 1000;
|
46680
|
103 |
final int targetCodeSize = result != null ? result.getTargetCodeSize() : -1;
|
|
104 |
final int bytecodeSize = result != null ? result.getBytecodeSize() : 0;
|
49873
|
105 |
if (allocatedBytesBefore == -1) {
|
58299
|
106 |
TTY.println(getMethodDescription() + String.format(" | %4dus %5dB bytecodes %5dB codesize", duration, bytecodeSize, targetCodeSize));
|
49873
|
107 |
} else {
|
|
108 |
final long allocatedBytesAfter = getCurrentThreadAllocatedBytes();
|
|
109 |
final long allocatedKBytes = (allocatedBytesAfter - allocatedBytesBefore) / 1024;
|
58299
|
110 |
TTY.println(getMethodDescription() + String.format(" | %4dus %5dB bytecodes %5dB codesize %5dkB allocated", duration, bytecodeSize, targetCodeSize, allocatedKBytes));
|
49873
|
111 |
}
|
46680
|
112 |
}
|
|
113 |
}
|
|
114 |
}
|