src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.hotspot/src/org/graalvm/compiler/hotspot/HotSpotTTYStreamProvider.java
author dlong
Sat, 08 Dec 2018 00:56:10 -0800
changeset 52910 583fd71c47d6
parent 50858 2d3e99a72541
child 54084 84f10bbf993f
permissions -rw-r--r--
8214023: Update Graal Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     1
/*
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     4
 *
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     8
 *
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    13
 * accompanied this code).
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    14
 *
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    18
 *
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    21
 * questions.
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    22
 */
50858
2d3e99a72541 8205824: Update Graal
never
parents: 47216
diff changeset
    23
2d3e99a72541 8205824: Update Graal
never
parents: 47216
diff changeset
    24
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    25
package org.graalvm.compiler.hotspot;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    26
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    27
import static org.graalvm.compiler.hotspot.HotSpotGraalOptionValues.GRAAL_OPTION_PROPERTY_PREFIX;
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
    28
import static org.graalvm.compiler.hotspot.HotSpotGraalOptionValues.HOTSPOT_OPTIONS;
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
    29
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    30
import java.io.FileNotFoundException;
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    31
import java.io.FileOutputStream;
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    32
import java.io.IOException;
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    33
import java.io.OutputStream;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    34
import java.io.PrintStream;
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    35
import java.util.List;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    36
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    37
import org.graalvm.compiler.core.common.SuppressFBWarnings;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    38
import org.graalvm.compiler.debug.TTYStreamProvider;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    39
import org.graalvm.compiler.options.Option;
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    40
import org.graalvm.compiler.options.OptionKey;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    41
import org.graalvm.compiler.options.OptionType;
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    42
import org.graalvm.compiler.options.OptionValues;
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    43
import org.graalvm.compiler.serviceprovider.GraalServices;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    44
import org.graalvm.compiler.serviceprovider.ServiceProvider;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    45
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    46
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    47
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    48
@ServiceProvider(TTYStreamProvider.class)
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    49
public class HotSpotTTYStreamProvider implements TTYStreamProvider {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    50
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    51
    public static class Options {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    52
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    53
        // @formatter:off
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    54
        @Option(help = "File to which logging is sent.  A %p in the name will be replaced with a string identifying " +
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
    55
                       "the process, usually the process id and %t will be replaced by System.currentTimeMillis().", type = OptionType.Expert)
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    56
        public static final LogStreamOptionKey LogFile = new LogStreamOptionKey();
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    57
        // @formatter:on
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    58
    }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    59
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    60
    @Override
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    61
    public PrintStream getStream() {
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
    62
        return Options.LogFile.getStream(HOTSPOT_OPTIONS);
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    63
    }
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    64
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    65
    /**
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    66
     * An option for a configurable file name that can also open a {@link PrintStream} on the file.
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    67
     * If no value is given for the option, the stream will output to HotSpot's
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    68
     * {@link HotSpotJVMCIRuntime#getLogStream() log} stream
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    69
     */
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    70
    private static class LogStreamOptionKey extends OptionKey<String> {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    71
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    72
        LogStreamOptionKey() {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    73
            super(null);
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    74
        }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    75
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    76
        /**
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    77
         * @return {@code nameTemplate} with all instances of %p replaced by
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    78
         *         {@link GraalServices#getExecutionID()} and %t by
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    79
         *         {@link System#currentTimeMillis()}
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    80
         */
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    81
        private static String makeFilename(String nameTemplate) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    82
            String name = nameTemplate;
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    83
            if (name.contains("%p")) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    84
                name = name.replaceAll("%p", GraalServices.getExecutionID());
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    85
            }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    86
            if (name.contains("%t")) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    87
                name = name.replaceAll("%t", String.valueOf(System.currentTimeMillis()));
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    88
            }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    89
            return name;
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    90
        }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    91
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    92
        /**
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    93
         * An output stream that redirects to {@link HotSpotJVMCIRuntime#getLogStream()}. The
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    94
         * {@link HotSpotJVMCIRuntime#getLogStream()} value is only accessed the first time an IO
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    95
         * operation is performed on the stream. This is required to break a deadlock in early JVMCI
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    96
         * initialization.
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    97
         */
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    98
        static class DelayedOutputStream extends OutputStream {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
    99
            private volatile OutputStream lazy;
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   100
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   101
            private OutputStream lazy() {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   102
                if (lazy == null) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   103
                    synchronized (this) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   104
                        if (lazy == null) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   105
                            lazy = HotSpotJVMCIRuntime.runtime().getLogStream();
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   106
                            PrintStream ps = new PrintStream(lazy);
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   107
                            ps.printf("[Use -D%sLogFile=<path> to redirect Graal log output to a file.]%n", GRAAL_OPTION_PROPERTY_PREFIX);
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   108
                        }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   109
                    }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   110
                }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   111
                return lazy;
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   112
            }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   113
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   114
            @Override
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   115
            public void write(byte[] b, int off, int len) throws IOException {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   116
                lazy().write(b, off, len);
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   117
            }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   118
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   119
            @Override
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   120
            public void write(int b) throws IOException {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   121
                lazy().write(b);
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   122
            }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   123
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   124
            @Override
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   125
            public void flush() throws IOException {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   126
                lazy().flush();
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   127
            }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   128
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   129
            @Override
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   130
            public void close() throws IOException {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   131
                lazy().close();
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   132
            }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   133
        }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   134
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   135
        /**
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   136
         * Gets the print stream configured by this option. If no file is configured, the print
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   137
         * stream will output to HotSpot's {@link HotSpotJVMCIRuntime#getLogStream() log} stream.
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   138
         */
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   139
        @SuppressFBWarnings(value = "DLS_DEAD_LOCAL_STORE", justification = "false positive on dead store to `ps`")
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   140
        public PrintStream getStream(OptionValues options) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   141
            String nameTemplate = getValue(options);
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   142
            if (nameTemplate != null) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   143
                String name = makeFilename(nameTemplate);
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   144
                try {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   145
                    final boolean enableAutoflush = true;
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   146
                    PrintStream ps = new PrintStream(new FileOutputStream(name), enableAutoflush);
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   147
                    /*
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   148
                     * Add the JVM and Java arguments to the log file to help identity it.
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   149
                     */
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   150
                    List<String> inputArguments = GraalServices.getInputArguments();
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   151
                    if (inputArguments != null) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   152
                        ps.println("VM Arguments: " + String.join(" ", inputArguments));
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   153
                    }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   154
                    String cmd = System.getProperty("sun.java.command");
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   155
                    if (cmd != null) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   156
                        ps.println("sun.java.command=" + cmd);
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   157
                    }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   158
                    return ps;
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   159
                } catch (FileNotFoundException e) {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   160
                    throw new RuntimeException("couldn't open file: " + name, e);
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   161
                }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   162
            } else {
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   163
                return new PrintStream(new DelayedOutputStream());
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   164
            }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   165
        }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   166
    }
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
   167
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   168
}