diff graal/GraalCompiler/src/com/sun/c1x/debug/IdealGraphPrinterObserver.java @ 2698:b179be22a3db

Allow to submit graphs to IdealGraphVisualizer remotely with new options named after their counterparts in C2
author Peter Hofer <peter.hofer@jku.at>
date Wed, 18 May 2011 15:01:43 +0200
parents 79590d6b4a7c
children a51ef0310dad
line wrap: on
line diff
--- a/graal/GraalCompiler/src/com/sun/c1x/debug/IdealGraphPrinterObserver.java	Wed May 18 15:14:55 2011 +0200
+++ b/graal/GraalCompiler/src/com/sun/c1x/debug/IdealGraphPrinterObserver.java	Wed May 18 15:01:43 2011 +0200
@@ -23,6 +23,7 @@
 package com.sun.c1x.debug;
 
 import java.io.*;
+import java.net.*;
 import java.util.regex.*;
 
 import com.oracle.graal.graph.*;
@@ -40,8 +41,21 @@
 
     private static final Pattern INVALID_CHAR = Pattern.compile("[^A-Za-z0-9_.-]");
 
+    private final String host;
+    private final int port;
+
     private IdealGraphPrinter printer;
     private OutputStream stream;
+    private Socket socket;
+
+    public IdealGraphPrinterObserver() {
+        this(null, -1);
+    }
+
+    public IdealGraphPrinterObserver(String host, int port) {
+        this.host = host;
+        this.port = port;
+    }
 
     @Override
     public void compilationStarted(CompilationEvent event) {
@@ -52,22 +66,69 @@
             name = name.substring(1, name.length() - 1).replace('/', '.');
             name = name + "." + event.getMethod().name();
 
-            String filename = name + ".igv.xml";
-            filename = INVALID_CHAR.matcher(filename).replaceAll("_");
+            if (host != null) {
+                openNetworkPrinter(name);
+            } else {
+                openFilePrinter(name);
+            }
+        }
+    }
+
+    private void openFilePrinter(String name) {
+        String filename = name + ".igv.xml";
+        filename = INVALID_CHAR.matcher(filename).replaceAll("_");
 
-            try {
-                stream = new FileOutputStream(filename);
-                printer = new IdealGraphPrinter(stream);
+        try {
+            stream = new FileOutputStream(filename);
+            printer = new IdealGraphPrinter(stream);
+            if (C1XOptions.OmitDOTFrameStates) {
+                printer.addOmittedClass(FrameState.class);
+            }
+            printer.begin();
+            printer.beginGroup(name, name, -1);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
 
-                if (C1XOptions.OmitDOTFrameStates) {
-                    printer.addOmittedClass(FrameState.class);
-                }
+    private void openNetworkPrinter(String name) {
+        try {
+            socket = new Socket(host, port);
+            if (socket.getInputStream().read() == 'y') {
+                stream = socket.getOutputStream();
+            } else {
+                // server currently does not accept any input
+                socket.close();
+                socket = null;
+                return;
+            }
 
-                printer.begin();
-                printer.beginGroup(name, name, -1);
-            } catch (IOException e) {
-                e.printStackTrace();
+            printer = new IdealGraphPrinter(stream);
+            if (C1XOptions.OmitDOTFrameStates) {
+                printer.addOmittedClass(FrameState.class);
             }
+            printer.begin();
+            printer.beginGroup(name, name, -1);
+            printer.flush();
+            if (socket.getInputStream().read() != 'y') {
+                // server declines input for this method
+                socket.close();
+                socket = null;
+                stream = null;
+                printer = null;
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+
+            if (socket != null) {
+                try {
+                    socket.close();
+                } catch (IOException ioe) {
+                }
+                socket = null;
+            }
+            stream = null;
+            printer = null;
         }
     }
 
@@ -85,12 +146,18 @@
             try {
                 printer.endGroup();
                 printer.end();
-                stream.close();
+
+                if (socket != null) {
+                    socket.close(); // also closes stream
+                } else {
+                    stream.close();
+                }
             } catch (IOException e) {
                 e.printStackTrace();
             } finally {
                 printer = null;
                 stream = null;
+                socket = null;
             }
         }
     }