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 |
|
24 |
|
25 package org.graalvm.compiler.hotspot; |
|
26 |
|
27 import java.io.FileNotFoundException; |
|
28 import java.io.FileOutputStream; |
|
29 import java.io.IOException; |
|
30 import java.io.OutputStream; |
|
31 import java.io.PrintStream; |
|
32 import java.util.List; |
|
33 |
|
34 import org.graalvm.compiler.core.common.SuppressFBWarnings; |
|
35 import org.graalvm.compiler.options.OptionKey; |
|
36 import org.graalvm.compiler.options.OptionValues; |
|
37 import org.graalvm.compiler.serviceprovider.GraalServices; |
|
38 |
|
39 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; |
|
40 |
|
41 /** |
|
42 * An option for a configurable file name that can also open a {@link PrintStream} on the file. If |
|
43 * no value is given for the option, the stream will output to HotSpot's |
|
44 * {@link HotSpotJVMCIRuntime#getLogStream() log} stream |
|
45 */ |
|
46 public class PrintStreamOptionKey extends OptionKey<String> { |
|
47 |
|
48 public PrintStreamOptionKey() { |
|
49 super(null); |
|
50 } |
|
51 |
|
52 /** |
|
53 * @return {@code nameTemplate} with all instances of %p replaced by |
|
54 * {@link GraalServices#getExecutionID()} and %t by {@link System#currentTimeMillis()} |
|
55 */ |
|
56 private static String makeFilename(String nameTemplate) { |
|
57 String name = nameTemplate; |
|
58 if (name.contains("%p")) { |
|
59 name = name.replaceAll("%p", GraalServices.getExecutionID()); |
|
60 } |
|
61 if (name.contains("%t")) { |
|
62 name = name.replaceAll("%t", String.valueOf(System.currentTimeMillis())); |
|
63 } |
|
64 return name; |
|
65 } |
|
66 |
|
67 /** |
|
68 * An output stream that redirects to {@link HotSpotJVMCIRuntime#getLogStream()}. The |
|
69 * {@link HotSpotJVMCIRuntime#getLogStream()} value is only accessed the first time an IO |
|
70 * operation is performed on the stream. This is required to break a deadlock in early JVMCI |
|
71 * initialization. |
|
72 */ |
|
73 static class DelayedOutputStream extends OutputStream { |
|
74 private volatile OutputStream lazy; |
|
75 |
|
76 private OutputStream lazy() { |
|
77 if (lazy == null) { |
|
78 synchronized (this) { |
|
79 if (lazy == null) { |
|
80 lazy = HotSpotJVMCIRuntime.runtime().getLogStream(); |
|
81 } |
|
82 } |
|
83 } |
|
84 return lazy; |
|
85 } |
|
86 |
|
87 @Override |
|
88 public void write(byte[] b, int off, int len) throws IOException { |
|
89 lazy().write(b, off, len); |
|
90 } |
|
91 |
|
92 @Override |
|
93 public void write(int b) throws IOException { |
|
94 lazy().write(b); |
|
95 } |
|
96 |
|
97 @Override |
|
98 public void flush() throws IOException { |
|
99 lazy().flush(); |
|
100 } |
|
101 |
|
102 @Override |
|
103 public void close() throws IOException { |
|
104 lazy().close(); |
|
105 } |
|
106 } |
|
107 |
|
108 /** |
|
109 * Gets the print stream configured by this option. If no file is configured, the print stream |
|
110 * will output to HotSpot's {@link HotSpotJVMCIRuntime#getLogStream() log} stream. |
|
111 */ |
|
112 @SuppressFBWarnings(value = "DLS_DEAD_LOCAL_STORE", justification = "false positive on dead store to `ps`") |
|
113 public PrintStream getStream(OptionValues options) { |
|
114 String nameTemplate = getValue(options); |
|
115 if (nameTemplate != null) { |
|
116 String name = makeFilename(nameTemplate); |
|
117 try { |
|
118 final boolean enableAutoflush = true; |
|
119 PrintStream ps = new PrintStream(new FileOutputStream(name), enableAutoflush); |
|
120 /* |
|
121 * Add the JVM and Java arguments to the log file to help identity it. |
|
122 */ |
|
123 List<String> inputArguments = GraalServices.getInputArguments(); |
|
124 if (inputArguments != null) { |
|
125 ps.println("VM Arguments: " + String.join(" ", inputArguments)); |
|
126 } |
|
127 String cmd = System.getProperty("sun.java.command"); |
|
128 if (cmd != null) { |
|
129 ps.println("sun.java.command=" + cmd); |
|
130 } |
|
131 return ps; |
|
132 } catch (FileNotFoundException e) { |
|
133 throw new RuntimeException("couldn't open file: " + name, e); |
|
134 } |
|
135 } else { |
|
136 return new PrintStream(new DelayedOutputStream()); |
|
137 } |
|
138 } |
|
139 } |
|