src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.debug/src/org/graalvm/compiler/debug/IgvDumpChannel.java
author dlong
Tue, 24 Sep 2019 12:47:15 -0400
changeset 58299 6df94ce3ab2f
parent 54084 84f10bbf993f
permissions -rw-r--r--
8229201: Update Graal Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     1
/*
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 54084
diff changeset
     2
 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     4
 *
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     8
 *
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    13
 * accompanied this code).
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    14
 *
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    18
 *
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    21
 * questions.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    22
 */
50858
2d3e99a72541 8205824: Update Graal
never
parents: 47798
diff changeset
    23
2d3e99a72541 8205824: Update Graal
never
parents: 47798
diff changeset
    24
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    25
package org.graalvm.compiler.debug;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    26
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    27
import static org.graalvm.compiler.debug.DebugOptions.PrintGraphHost;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    28
import static org.graalvm.compiler.debug.DebugOptions.PrintGraphPort;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    29
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    30
import java.io.File;
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    31
import java.io.IOException;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    32
import java.io.InterruptedIOException;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    33
import java.net.InetSocketAddress;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    34
import java.nio.ByteBuffer;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    35
import java.nio.channels.ClosedByInterruptException;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    36
import java.nio.channels.FileChannel;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    37
import java.nio.channels.SocketChannel;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    38
import java.nio.channels.WritableByteChannel;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    39
import java.nio.file.Path;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    40
import java.nio.file.StandardOpenOption;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    41
import java.util.function.Supplier;
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    42
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    43
import org.graalvm.compiler.debug.DebugOptions.PrintGraphTarget;
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    44
import org.graalvm.compiler.options.OptionValues;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    45
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    46
import jdk.vm.ci.common.NativeImageReinitialize;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    47
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    48
final class IgvDumpChannel implements WritableByteChannel {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    49
    private final Supplier<Path> pathProvider;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    50
    private final OptionValues options;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    51
    private WritableByteChannel sharedChannel;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    52
    private boolean closed;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    53
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    54
    IgvDumpChannel(Supplier<Path> pathProvider, OptionValues options) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    55
        this.pathProvider = pathProvider;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    56
        this.options = options;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    57
    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    58
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    59
    @Override
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    60
    public int write(ByteBuffer src) throws IOException {
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    61
        WritableByteChannel channel = channel();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    62
        return channel == null ? 0 : channel.write(src);
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    63
    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    64
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    65
    @Override
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    66
    public boolean isOpen() {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    67
        return !closed;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    68
    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    69
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    70
    @Override
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    71
    public void close() throws IOException {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    72
    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    73
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    74
    void realClose() throws IOException {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    75
        closed = true;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    76
        if (sharedChannel != null) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    77
            sharedChannel.close();
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    78
            sharedChannel = null;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    79
        }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    80
    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    81
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    82
    WritableByteChannel channel() throws IOException {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    83
        if (closed) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    84
            throw new IOException();
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    85
        }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    86
        if (sharedChannel == null) {
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    87
            PrintGraphTarget target = DebugOptions.PrintGraph.getValue(options);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    88
            if (target == PrintGraphTarget.File) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    89
                sharedChannel = createFileChannel(pathProvider, null);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    90
            } else if (target == PrintGraphTarget.Network) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    91
                sharedChannel = createNetworkChannel(pathProvider, options);
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    92
            } else {
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    93
                TTY.println("WARNING: Graph dumping requested but value of %s option is %s", DebugOptions.PrintGraph.getName(), PrintGraphTarget.Disable);
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    94
            }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    95
        }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    96
        return sharedChannel;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    97
    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    98
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    99
    private static WritableByteChannel createNetworkChannel(Supplier<Path> pathProvider, OptionValues options) throws IOException {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   100
        String host = PrintGraphHost.getValue(options);
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   101
        int port = PrintGraphPort.getValue(options);
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   102
        try {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   103
            WritableByteChannel channel = SocketChannel.open(new InetSocketAddress(host, port));
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   104
            String targetAnnouncement = String.format("Connected to the IGV on %s:%d", host, port);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   105
            maybeAnnounceTarget(targetAnnouncement);
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   106
            return channel;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   107
        } catch (ClosedByInterruptException | InterruptedIOException e) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   108
            /*
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   109
             * Interrupts should not count as errors because they may be caused by a cancelled Graal
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   110
             * compilation. ClosedByInterruptException occurs if the SocketChannel could not be
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   111
             * opened. InterruptedIOException occurs if new Socket(..) was interrupted.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   112
             */
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   113
            return null;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   114
        } catch (IOException e) {
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   115
            String networkFailure = String.format("Could not connect to the IGV on %s:%d", host, port);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   116
            if (pathProvider != null) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   117
                return createFileChannel(pathProvider, networkFailure);
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   118
            } else {
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   119
                throw new IOException(networkFailure, e);
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   120
            }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   121
        }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   122
    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   123
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   124
    @NativeImageReinitialize private static String lastTargetAnnouncement;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   125
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   126
    private static void maybeAnnounceTarget(String targetAnnouncement) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   127
        if (!targetAnnouncement.equals(lastTargetAnnouncement)) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   128
            // Ignore races - an extra announcement is ok
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   129
            lastTargetAnnouncement = targetAnnouncement;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   130
            TTY.println(targetAnnouncement);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   131
        }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   132
    }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   133
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   134
    private static WritableByteChannel createFileChannel(Supplier<Path> pathProvider, String networkFailure) throws IOException {
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   135
        Path path = pathProvider.get();
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   136
        try {
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   137
            FileChannel channel = FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   138
            File dir = path.toFile();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   139
            if (!dir.isDirectory()) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   140
                dir = dir.getParentFile();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   141
            }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   142
            if (networkFailure == null) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   143
                maybeAnnounceTarget("Dumping IGV graphs in " + dir);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   144
            } else {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   145
                maybeAnnounceTarget(networkFailure + ". Dumping IGV graphs in " + dir);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   146
            }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   147
            return channel;
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   148
        } catch (IOException e) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   149
            throw new IOException(String.format("Failed to open %s to dump IGV graphs", path), e);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   150
        }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   151
    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   152
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   153
}