changeset 16405:9bfc4247262f

send log output to native tty
author Lukas Stadler <lukas.stadler@oracle.com>
date Fri, 04 Jul 2014 16:06:44 +0200
parents fe985eebfcd9
children ed91068c8af5
files graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugScope.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/CompilationTask.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotGraalRuntime.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/PrintStreamOption.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/bridge/CompilerToVM.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/bridge/CompilerToVMImpl.java src/share/vm/graal/graalCompilerToVM.cpp
diffstat 8 files changed, 57 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java	Fri Jul 04 13:47:37 2014 +0200
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java	Fri Jul 04 16:06:44 2014 +0200
@@ -845,7 +845,7 @@
     }
 
     public static DebugConfig silentConfig() {
-        return fixedConfig(false, false, false, false, false, false, Collections.<DebugDumpHandler> emptyList(), Collections.<DebugVerifyHandler> emptyList(), System.out);
+        return fixedConfig(false, false, false, false, false, false, Collections.<DebugDumpHandler> emptyList(), Collections.<DebugVerifyHandler> emptyList(), null);
     }
 
     public static DebugConfig fixedConfig(final boolean isLogEnabled, final boolean isDumpEnabled, final boolean isMeterEnabled, final boolean isMemUseTrackingEnabled, final boolean isTimerEnabled,
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugScope.java	Fri Jul 04 13:47:37 2014 +0200
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugScope.java	Fri Jul 04 16:06:44 2014 +0200
@@ -42,20 +42,28 @@
             this.indent = (parentIndent == null ? "" : parentIndent.indent + INDENTATION_INCREMENT);
         }
 
-        private void printScopeName() {
+        private void printScopeName(StringBuilder str) {
             if (logScopeName) {
                 if (parentIndent != null) {
-                    parentIndent.printScopeName();
+                    parentIndent.printScopeName(str);
                 }
-                output.println(indent + "[thread:" + Thread.currentThread().getId() + "] scope: " + qualifiedName);
+                str.append(indent).append("[thread:").append(Thread.currentThread().getId()).append("] scope: ").append(qualifiedName).append(System.lineSeparator());
                 logScopeName = false;
             }
         }
 
         public void log(String msg, Object... args) {
             if (isLogEnabled()) {
-                printScopeName();
-                output.println(indent + String.format(msg, args));
+                StringBuilder str = new StringBuilder();
+                printScopeName(str);
+                str.append(indent);
+                if (args.length == 0) {
+                    str.append(msg);
+                } else {
+                    str.append(String.format(msg, args));
+                }
+                str.append(System.lineSeparator());
+                output.append(str);
                 lastUsedIndent = this;
             }
         }
@@ -132,7 +140,7 @@
 
         // Be pragmatic: provide a default log stream to prevent a crash if the stream is not
         // set while logging
-        this.output = System.out;
+        this.output = TTY.cachedOut;
         assert context != null;
 
         if (parent != null) {
@@ -183,13 +191,6 @@
         lastUsedIndent.log(msg, args);
     }
 
-    public void printf(String msg, Object... args) {
-        if (isLogEnabled()) {
-            lastUsedIndent.printScopeName();
-            output.printf(msg, args);
-        }
-    }
-
     public void dump(Object object, String formatString, Object... args) {
         if (isDumpEnabled()) {
             DebugConfig config = getConfig();
@@ -293,7 +294,7 @@
 
             // Be pragmatic: provide a default log stream to prevent a crash if the stream is not
             // set while logging
-            output = System.out;
+            output = TTY.cachedOut;
         } else {
             meterEnabled = config.isMeterEnabled();
             memUseTrackingEnabled = config.isMemUseTrackingEnabled();
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/CompilationTask.java	Fri Jul 04 13:47:37 2014 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/CompilationTask.java	Fri Jul 04 16:06:44 2014 +0200
@@ -349,7 +349,7 @@
 
         // Ensure a debug configuration for this thread is initialized
         if (Debug.isEnabled() && DebugScope.getConfig() == null) {
-            DebugEnvironment.initialize(System.out);
+            DebugEnvironment.initialize(TTY.cachedOut);
         }
 
         HotSpotResolvedJavaMethod method = HotSpotResolvedJavaMethod.fromMetaspace(metaspaceMethod);
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotGraalRuntime.java	Fri Jul 04 13:47:37 2014 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/HotSpotGraalRuntime.java	Fri Jul 04 16:06:44 2014 +0200
@@ -109,7 +109,7 @@
 
         this.compilerToVm = toVM;
 
-        TTY.initialize(Options.LogFile.getStream());
+        TTY.initialize(Options.LogFile.getStream(compilerToVm));
 
         if (Log.getValue() == null && Meter.getValue() == null && Time.getValue() == null && Dump.getValue() == null && Verify.getValue() == null) {
             if (MethodFilter.getValue() != null) {
@@ -118,7 +118,7 @@
         }
 
         if (Debug.isEnabled()) {
-            DebugEnvironment.initialize(LogFile.getStream());
+            DebugEnvironment.initialize(TTY.cachedOut);
 
             String summary = DebugValueSummary.getValue();
             if (summary != null) {
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/PrintStreamOption.java	Fri Jul 04 13:47:37 2014 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/PrintStreamOption.java	Fri Jul 04 16:06:44 2014 +0200
@@ -25,6 +25,7 @@
 import java.io.*;
 import java.lang.management.*;
 
+import com.oracle.graal.hotspot.bridge.*;
 import com.oracle.graal.options.*;
 
 /**
@@ -40,14 +41,14 @@
      * The print stream to which output will be written.
      *
      * Declared {@code volatile} to enable safe use of double-checked locking in
-     * {@link #getStream()} and {@link #setValue(Object)}.
+     * {@link #getStream(CompilerToVM)} and {@link #setValue(Object)}.
      */
     private volatile PrintStream ps;
 
     /**
      * Replace any instance of %p with a an identifying name. Try to get it from the RuntimeMXBean
      * name.
-     * 
+     *
      * @return the name of the file to log to
      */
     private String getFilename() {
@@ -69,9 +70,10 @@
     }
 
     /**
-     * Gets the print stream configured by this option.
+     * Gets the print stream configured by this option. If no file is configured, the print stream
+     * will output to {@link CompilerToVM#writeDebugOutput(byte[], int, int)}.
      */
-    public PrintStream getStream() {
+    public PrintStream getStream(final CompilerToVM compilerToVM) {
         if (ps == null) {
             if (getValue() != null) {
                 synchronized (this) {
@@ -85,7 +87,25 @@
                     }
                 }
             } else {
-                ps = System.out;
+                OutputStream ttyOut = new OutputStream() {
+                    @Override
+                    public void write(byte[] b, int off, int len) throws IOException {
+                        if (b == null) {
+                            throw new NullPointerException();
+                        } else if (off < 0 || off > b.length || len < 0 || (off + len) > b.length || (off + len) < 0) {
+                            throw new IndexOutOfBoundsException();
+                        } else if (len == 0) {
+                            return;
+                        }
+                        compilerToVM.writeDebugOutput(b, off, len);
+                    }
+
+                    @Override
+                    public void write(int b) throws IOException {
+                        write(new byte[]{(byte) b}, 0, 1);
+                    }
+                };
+                ps = new PrintStream(ttyOut);
             }
         }
         return ps;
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/bridge/CompilerToVM.java	Fri Jul 04 13:47:37 2014 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/bridge/CompilerToVM.java	Fri Jul 04 16:06:44 2014 +0200
@@ -361,4 +361,6 @@
     int getVtableIndexForInterface(long metaspaceKlass, long metaspaceMethod);
 
     boolean shouldDebugNonSafepoints();
+
+    void writeDebugOutput(byte[] bytes, int offset, int length);
 }
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/bridge/CompilerToVMImpl.java	Fri Jul 04 13:47:37 2014 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/bridge/CompilerToVMImpl.java	Fri Jul 04 16:06:44 2014 +0200
@@ -199,4 +199,6 @@
     public native int getVtableIndexForInterface(long metaspaceKlass, long metaspaceMethod);
 
     public native boolean shouldDebugNonSafepoints();
+
+    public native void writeDebugOutput(byte[] bytes, int offset, int length);
 }
--- a/src/share/vm/graal/graalCompilerToVM.cpp	Fri Jul 04 13:47:37 2014 +0200
+++ b/src/share/vm/graal/graalCompilerToVM.cpp	Fri Jul 04 16:06:44 2014 +0200
@@ -997,6 +997,14 @@
   }
 C2V_END
 
+C2V_VMENTRY(void, writeDebugOutput, (JNIEnv*, jobject, jbyteArray bytes, jint offset, jint length))
+  while (length > 0) {
+    jbyte* start = ((typeArrayOop) JNIHandles::resolve(bytes))->byte_at_addr(offset);
+    tty->write((char*) start, MIN2(length, O_BUFLEN));
+    length -= O_BUFLEN;
+    offset += O_BUFLEN;
+  }
+C2V_END
 
 
 #define CC (char*)  /*cast a literal from (const char*)*/
@@ -1077,6 +1085,7 @@
   {CC"getNextStackFrame",                            CC"("HS_STACK_FRAME_REF "[JI)"HS_STACK_FRAME_REF,                         FN_PTR(getNextStackFrame)},
   {CC"materializeVirtualObjects",                    CC"("HS_STACK_FRAME_REF"Z)V",                                             FN_PTR(materializeVirtualObjects)},
   {CC"shouldDebugNonSafepoints",                     CC"()Z",                                                                  FN_PTR(shouldDebugNonSafepoints)},
+  {CC"writeDebugOutput",                             CC"([BII)V",                                                              FN_PTR(writeDebugOutput)},
 };
 
 int CompilerToVM_methods_count() {