1 /* 2 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. 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 */ 23 24 25 package org.graalvm.compiler.core; 26 27 import static org.graalvm.compiler.core.GraalCompilerOptions.PrintCompilation; 28 import static org.graalvm.compiler.serviceprovider.GraalServices.getCurrentThreadAllocatedBytes; 29 import static org.graalvm.compiler.serviceprovider.GraalServices.isThreadAllocatedMemorySupported; 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(); 85 allocatedBytesBefore = isThreadAllocatedMemorySupported() ? getCurrentThreadAllocatedBytes() : -1; 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(); 102 final long duration = (stop - start) / 1000000; 103 final int targetCodeSize = result != null ? result.getTargetCodeSize() : -1; 104 final int bytecodeSize = result != null ? result.getBytecodeSize() : 0; 105 if (allocatedBytesBefore == -1) { 106 TTY.println(getMethodDescription() + String.format(" | %4dms %5dB %5dB", duration, bytecodeSize, targetCodeSize)); 107 } else { 108 final long allocatedBytesAfter = getCurrentThreadAllocatedBytes(); 109 final long allocatedKBytes = (allocatedBytesAfter - allocatedBytesBefore) / 1024; 110 TTY.println(getMethodDescription() + String.format(" | %4dms %5dB %5dB %5dkB", duration, bytecodeSize, targetCodeSize, allocatedKBytes)); 111 } 112 } 113 } 114 }