comparison graal/com.oracle.jvmci.hotspot/src/com/oracle/jvmci/hotspot/PrintStreamOption.java @ 21551:5324104ac4f3

moved com.oracle.graal.hotspot.jvmci classes to com.oracle.jvmci.hotspot module (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Tue, 26 May 2015 17:13:37 +0200
parents
children
comparison
equal deleted inserted replaced
21550:f48a6cea31eb 21551:5324104ac4f3
1 /*
2 * Copyright (c) 2014, 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 com.oracle.jvmci.hotspot;
24
25 import java.io.*;
26 import java.lang.management.*;
27
28 import com.oracle.graal.options.*;
29
30 /**
31 * An option that encapsulates and configures a print stream.
32 */
33 public class PrintStreamOption extends OptionValue<String> {
34
35 public PrintStreamOption() {
36 super(null);
37 }
38
39 /**
40 * The print stream to which output will be written.
41 *
42 * Declared {@code volatile} to enable safe use of double-checked locking in
43 * {@link #getStream(CompilerToVM)} and {@link #setValue(Object)}.
44 */
45 private volatile PrintStream ps;
46
47 /**
48 * Replace any instance of %p with a an identifying name. Try to get it from the RuntimeMXBean
49 * name.
50 *
51 * @return the name of the file to log to
52 */
53 private String getFilename() {
54 String name = getValue();
55 if (name.contains("%p")) {
56 String runtimeName = ManagementFactory.getRuntimeMXBean().getName();
57 try {
58 int index = runtimeName.indexOf('@');
59 if (index != -1) {
60 long pid = Long.parseLong(runtimeName.substring(0, index));
61 runtimeName = Long.toString(pid);
62 }
63 name = name.replaceAll("%p", runtimeName);
64 } catch (NumberFormatException e) {
65
66 }
67 }
68 return name;
69 }
70
71 /**
72 * Gets the print stream configured by this option. If no file is configured, the print stream
73 * will output to {@link CompilerToVM#writeDebugOutput(byte[], int, int)}.
74 */
75 public PrintStream getStream(final CompilerToVM compilerToVM) {
76 if (ps == null) {
77 if (getValue() != null) {
78 synchronized (this) {
79 if (ps == null) {
80 try {
81 final boolean enableAutoflush = true;
82 ps = new PrintStream(new FileOutputStream(getFilename()), enableAutoflush);
83 /* Add the JVM and Java arguments to the log file to help identity it. */
84 String inputArguments = String.join(" ", ManagementFactory.getRuntimeMXBean().getInputArguments());
85 ps.println("VM Arguments: " + inputArguments);
86 String cmd = System.getProperty("sun.java.command");
87 if (cmd != null) {
88 ps.println("sun.java.command=" + cmd);
89 }
90 } catch (FileNotFoundException e) {
91 throw new RuntimeException("couldn't open file: " + getValue(), e);
92 }
93 }
94 }
95 } else {
96 OutputStream ttyOut = new OutputStream() {
97 @Override
98 public void write(byte[] b, int off, int len) throws IOException {
99 if (b == null) {
100 throw new NullPointerException();
101 } else if (off < 0 || off > b.length || len < 0 || (off + len) > b.length || (off + len) < 0) {
102 throw new IndexOutOfBoundsException();
103 } else if (len == 0) {
104 return;
105 }
106 compilerToVM.writeDebugOutput(b, off, len);
107 }
108
109 @Override
110 public void write(int b) throws IOException {
111 write(new byte[]{(byte) b}, 0, 1);
112 }
113
114 @Override
115 public void flush() throws IOException {
116 compilerToVM.flushDebugOutput();
117 }
118 };
119 ps = new PrintStream(ttyOut);
120 }
121 }
122 return ps;
123 }
124
125 @Override
126 public void setValue(Object v) {
127 if (ps != null) {
128 synchronized (this) {
129 if (ps != null) {
130 ps.close();
131 ps = null;
132 }
133 }
134 }
135 super.setValue(v);
136 }
137 }