1 /* 2 * Copyright (c) 2017, 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.debug; 24 25 import java.io.IOException; 26 import java.io.InterruptedIOException; 27 import java.net.InetSocketAddress; 28 import java.nio.ByteBuffer; 29 import java.nio.channels.ClosedByInterruptException; 30 import java.nio.channels.FileChannel; 31 import java.nio.channels.SocketChannel; 32 import java.nio.channels.WritableByteChannel; 33 import java.nio.file.Path; 34 import java.nio.file.StandardOpenOption; 35 import java.util.function.Supplier; 36 import static org.graalvm.compiler.debug.DebugOptions.PrintBinaryGraphPort; 37 import static org.graalvm.compiler.debug.DebugOptions.PrintGraphHost; 38 import org.graalvm.compiler.options.OptionValues; 39 40 final class IgvDumpChannel implements WritableByteChannel { 41 private final Supplier<Path> pathProvider; 42 private final OptionValues options; 43 private WritableByteChannel sharedChannel; 44 private boolean closed; 45 46 IgvDumpChannel(Supplier<Path> pathProvider, OptionValues options) { 47 this.pathProvider = pathProvider; 48 this.options = options; 49 } 50 51 @Override 52 public int write(ByteBuffer src) throws IOException { 53 return channel().write(src); 54 } 55 56 @Override 57 public boolean isOpen() { 58 return !closed; 59 } 60 61 @Override 62 public void close() throws IOException { 63 } 64 65 void realClose() throws IOException { 66 closed = true; 67 if (sharedChannel != null) { 68 sharedChannel.close(); 69 sharedChannel = null; 70 } 71 } 72 73 WritableByteChannel channel() throws IOException { 74 if (closed) { 75 throw new IOException(); 76 } 77 if (sharedChannel == null) { 78 if (DebugOptions.PrintGraphFile.getValue(options)) { 79 sharedChannel = createFileChannel(pathProvider); 80 } else { 81 sharedChannel = createNetworkChannel(pathProvider, options); 82 } 83 } 84 return sharedChannel; 85 } 86 87 private static WritableByteChannel createNetworkChannel(Supplier<Path> pathProvider, OptionValues options) throws IOException { 88 String host = PrintGraphHost.getValue(options); 89 int port = PrintBinaryGraphPort.getValue(options); 90 try { 91 WritableByteChannel channel = SocketChannel.open(new InetSocketAddress(host, port)); 92 TTY.println("Connected to the IGV on %s:%d", host, port); 93 return channel; 94 } catch (ClosedByInterruptException | InterruptedIOException e) { 95 /* 96 * Interrupts should not count as errors because they may be caused by a cancelled Graal 97 * compilation. ClosedByInterruptException occurs if the SocketChannel could not be 98 * opened. InterruptedIOException occurs if new Socket(..) was interrupted. 99 */ 100 return null; 101 } catch (IOException e) { 102 if (!DebugOptions.PrintGraphFile.hasBeenSet(options)) { 103 return createFileChannel(pathProvider); 104 } else { 105 throw new IOException(String.format("Could not connect to the IGV on %s:%d", host, port), e); 106 } 107 } 108 } 109 110 private static WritableByteChannel createFileChannel(Supplier<Path> pathProvider) throws IOException { 111 Path path = pathProvider.get(); 112 try { 113 return FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE); 114 } catch (IOException e) { 115 throw new IOException(String.format("Failed to open %s to dump IGV graphs", path), e); 116 } 117 } 118 119 }