1 /* 2 * Copyright (c) 2014, 2015, 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 package org.graalvm.compiler.hotspot; 24 25 import java.io.FileNotFoundException; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 import java.io.OutputStream; 29 import java.io.PrintStream; 30 import java.lang.management.ManagementFactory; 31 import java.lang.management.RuntimeMXBean; 32 33 import org.graalvm.compiler.options.OptionValue; 34 import org.graalvm.compiler.options.UniquePathUtilities; 35 36 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; 37 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider; 38 39 /** 40 * An option that encapsulates and configures a print stream. 41 */ 42 public class PrintStreamOption extends OptionValue<String> { 43 44 public PrintStreamOption() { 45 super(null); 46 } 47 48 /** 49 * The print stream to which output will be written. 50 * 51 * Declared {@code volatile} to enable safe use of double-checked locking in 52 * {@link #getStream()} and {@link #setValue(Object)}. 53 */ 54 private volatile PrintStream ps; 55 56 /** 57 * Replaces any instance of %p with an identifying name such as a process ID extracted from 58 * {@link RuntimeMXBean#getName()} and any instance of %t with the value of 59 * {@link System#currentTimeMillis()}. 60 * 61 * @return the name of the file to log to 62 */ 63 private String getFilename() { 64 String name = getValue(); 65 if (name.contains("%t")) { 66 name = name.replaceAll("%t", String.valueOf(UniquePathUtilities.getGlobalTimeStamp())); 67 } 68 if (name.contains("%p")) { 69 String runtimeName = ManagementFactory.getRuntimeMXBean().getName(); 70 try { 71 int index = runtimeName.indexOf('@'); 72 if (index != -1) { 73 long pid = Long.parseLong(runtimeName.substring(0, index)); 74 runtimeName = Long.toString(pid); 75 } 76 name = name.replaceAll("%p", runtimeName); 77 } catch (NumberFormatException e) { 78 79 } 80 } 81 return name; 82 } 83 84 /** 85 * An output stream that redirects to {@link HotSpotJVMCIRuntimeProvider#getLogStream()}. The 86 * {@link HotSpotJVMCIRuntimeProvider#getLogStream()} value is only accessed the first time an 87 * IO operation is performed on the stream. This is required to break a deadlock in early JVMCI 88 * initialization. 89 */ 90 static class DelayedOutputStream extends OutputStream { 91 private volatile OutputStream lazy; 92 93 private OutputStream lazy() { 94 if (lazy == null) { 95 synchronized (this) { 96 if (lazy == null) { 97 lazy = HotSpotJVMCIRuntime.runtime().getLogStream(); 98 } 99 } 100 } 101 return lazy; 102 } 103 104 @Override 105 public void write(byte[] b, int off, int len) throws IOException { 106 lazy().write(b, off, len); 107 } 108 109 @Override 110 public void write(int b) throws IOException { 111 lazy().write(b); 112 } 113 114 @Override 115 public void flush() throws IOException { 116 lazy().flush(); 117 } 118 119 @Override 120 public void close() throws IOException { 121 lazy().close(); 122 } 123 } 124 125 /** 126 * Gets the print stream configured by this option. If no file is configured, the print stream 127 * will output to HotSpot's {@link HotSpotJVMCIRuntimeProvider#getLogStream() log} stream. 128 */ 129 public PrintStream getStream() { 130 if (ps == null) { 131 if (getValue() != null) { 132 synchronized (this) { 133 if (ps == null) { 134 try { 135 final boolean enableAutoflush = true; 136 ps = new PrintStream(new FileOutputStream(getFilename()), enableAutoflush); 137 /* 138 * Add the JVM and Java arguments to the log file to help identity it. 139 */ 140 String inputArguments = String.join(" ", ManagementFactory.getRuntimeMXBean().getInputArguments()); 141 ps.println("VM Arguments: " + inputArguments); 142 String cmd = System.getProperty("sun.java.command"); 143 if (cmd != null) { 144 ps.println("sun.java.command=" + cmd); 145 } 146 } catch (FileNotFoundException e) { 147 throw new RuntimeException("couldn't open file: " + getValue(), e); 148 } 149 } 150 } 151 } else { 152 ps = new PrintStream(new DelayedOutputStream()); 153 } 154 } 155 return ps; 156 } 157 158 @Override 159 public void setValue(Object v) { 160 if (ps != null) { 161 synchronized (this) { 162 if (ps != null) { 163 ps.close(); 164 ps = null; 165 } 166 } 167 } 168 super.setValue(v); 169 } 170 }