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