1 /* 2 * Copyright (c) 2016, 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 26 package jdk.tools.jaotc; 27 28 import java.io.FileWriter; 29 import java.io.IOException; 30 import java.io.PrintWriter; 31 import java.lang.management.ManagementFactory; 32 import java.lang.management.MemoryUsage; 33 import java.nio.file.Path; 34 import java.nio.file.Paths; 35 import java.text.MessageFormat; 36 import java.util.Date; 37 38 import jdk.tools.jaotc.binformat.ByteContainer; 39 import jdk.tools.jaotc.binformat.BinaryContainer; 40 41 final class LogPrinter { 42 43 private static FileWriter logFile = null; 44 private final Options options; 45 private final PrintWriter log; 46 LogPrinter(Main main, PrintWriter log)47 LogPrinter(Main main, PrintWriter log) { 48 this.options = main.options; 49 this.log = log; 50 } 51 printInfo(String message)52 void printInfo(String message) { 53 if (options.info) { 54 log.print(message); 55 log.flush(); 56 } 57 } 58 printlnInfo(String message)59 void printlnInfo(String message) { 60 if (options.info) { 61 log.println(message); 62 log.flush(); 63 } 64 } 65 printVerbose(String message)66 void printVerbose(String message) { 67 if (options.verbose) { 68 log.print(message); 69 log.flush(); 70 } 71 } 72 printlnVerbose(String message)73 void printlnVerbose(String message) { 74 if (options.verbose) { 75 log.println(message); 76 log.flush(); 77 } 78 } 79 printDebug(String message)80 void printDebug(String message) { 81 if (options.debug) { 82 log.print(message); 83 log.flush(); 84 } 85 } 86 printlnDebug(String message)87 void printlnDebug(String message) { 88 if (options.debug) { 89 log.println(message); 90 log.flush(); 91 } 92 } 93 printError(String message)94 void printError(String message) { 95 log.println("Error: " + message); 96 log.flush(); 97 } 98 reportError(Throwable e)99 void reportError(Throwable e) { 100 log.println("Error: " + e.getMessage()); 101 if (options.info) { 102 e.printStackTrace(log); 103 } 104 log.flush(); 105 } 106 reportError(String key, Object... args)107 void reportError(String key, Object... args) { 108 printError(MessageFormat.format(key, args)); 109 } 110 humanReadableByteCount(long bytes)111 private static String humanReadableByteCount(long bytes) { 112 int unit = 1024; 113 114 if (bytes < unit) { 115 return bytes + " B"; 116 } 117 118 int exp = (int) (Math.log(bytes) / Math.log(unit)); 119 char pre = "KMGTPE".charAt(exp - 1); 120 return String.format("%.1f %cB", bytes / Math.pow(unit, exp), pre); 121 } 122 printMemoryUsage()123 void printMemoryUsage() { 124 if (options.verbose) { 125 MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); 126 float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted(); 127 log.format(" [used: %-7s, comm: %-7s, freeRatio ~= %.1f%%]", 128 humanReadableByteCount(memusage.getUsed()), 129 humanReadableByteCount(memusage.getCommitted()), 130 freeratio * 100); 131 } 132 } 133 printContainerInfo(ByteContainer container)134 private void printContainerInfo(ByteContainer container) { 135 printlnVerbose(container.getContainerName() + ": " + container.getByteStreamSize() + " bytes"); 136 } 137 containersInfo(BinaryContainer binaryContainer)138 void containersInfo(BinaryContainer binaryContainer) { 139 printContainerInfo(binaryContainer.getHeaderContainer().getContainer()); 140 printContainerInfo(binaryContainer.getConfigContainer()); 141 printContainerInfo(binaryContainer.getKlassesOffsetsContainer()); 142 printContainerInfo(binaryContainer.getMethodsOffsetsContainer()); 143 printContainerInfo(binaryContainer.getKlassesDependenciesContainer()); 144 printContainerInfo(binaryContainer.getStubsOffsetsContainer()); 145 printContainerInfo(binaryContainer.getMethodMetadataContainer()); 146 printContainerInfo(binaryContainer.getCodeContainer()); 147 printContainerInfo(binaryContainer.getCodeSegmentsContainer()); 148 printContainerInfo(binaryContainer.getConstantDataContainer()); 149 printContainerInfo(binaryContainer.getKlassesGotContainer()); 150 printContainerInfo(binaryContainer.getCountersGotContainer()); 151 printContainerInfo(binaryContainer.getMetadataGotContainer()); 152 printContainerInfo(binaryContainer.getMethodStateContainer()); 153 printContainerInfo(binaryContainer.getOopGotContainer()); 154 printContainerInfo(binaryContainer.getMetaspaceNamesContainer()); 155 } 156 openLog()157 static void openLog() { 158 int v = Integer.getInteger("jdk.tools.jaotc.logCompilation", 0); 159 if (v == 0) { 160 logFile = null; 161 return; 162 } 163 // Create log file in current directory 164 String fileName = "aot_compilation" + new Date().getTime() + ".log"; 165 Path logFilePath = Paths.get("./", fileName); 166 String logFileName = logFilePath.toString(); 167 try { 168 // Create file to which we do not append 169 logFile = new FileWriter(logFileName, false); 170 } catch (IOException e) { 171 System.out.println("Unable to open logfile :" + logFileName + "\nNo logs will be created"); 172 logFile = null; 173 } 174 } 175 writeLog(String str)176 static void writeLog(String str) { 177 if (logFile != null) { 178 try { 179 logFile.write(str + "\n"); 180 logFile.flush(); 181 } catch (IOException e) { 182 // Print to console 183 System.out.println(str + "\n"); 184 } 185 } 186 } 187 closeLog()188 static void closeLog() { 189 if (logFile != null) { 190 try { 191 logFile.close(); 192 } catch (IOException e) { 193 // Do nothing 194 } 195 } 196 } 197 198 } 199