1 /*
   2  * Copyright (c) 2014, 2015, 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.hotspot;
  24 
  25 import java.io.FileNotFoundException;
  26 import java.io.FileOutputStream;
  27 import java.io.IOException;
  28 import java.io.OutputStream;
  29 import java.io.PrintStream;
  30 import java.lang.management.ManagementFactory;
  31 
  32 import org.graalvm.compiler.options.OptionKey;
  33 import org.graalvm.compiler.options.OptionValues;
  34 
  35 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
  36 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider;
  37 
  38 /**
  39  * An option that encapsulates and configures a print stream.
  40  */
  41 public class PrintStreamOptionKey extends OptionKey<String> {
  42 
  43     public PrintStreamOptionKey() {
  44         super(null);
  45     }
  46 
  47     /**
  48      * Replace any instance of %p with an identifying name. Try to get it from the RuntimeMXBean
  49      * name.
  50      *
  51      * @return the name of the file to log to
  52      */
  53     private String getFilename(OptionValues options) {
  54         String name = getValue(options);
  55         if (name.contains("%p")) {
  56             String runtimeName = ManagementFactory.getRuntimeMXBean().getName();
  57             try {
  58                 int index = runtimeName.indexOf('@');
  59                 if (index != -1) {
  60                     long pid = Long.parseLong(runtimeName.substring(0, index));
  61                     runtimeName = Long.toString(pid);
  62                 }
  63                 name = name.replaceAll("%p", runtimeName);
  64             } catch (NumberFormatException e) {
  65 
  66             }
  67         }
  68         return name;
  69     }
  70 
  71     /**
  72      * An output stream that redirects to {@link HotSpotJVMCIRuntimeProvider#getLogStream()}. The
  73      * {@link HotSpotJVMCIRuntimeProvider#getLogStream()} value is only accessed the first time an
  74      * IO operation is performed on the stream. This is required to break a deadlock in early JVMCI
  75      * initialization.
  76      */
  77     static class DelayedOutputStream extends OutputStream {
  78         private volatile OutputStream lazy;
  79 
  80         private OutputStream lazy() {
  81             if (lazy == null) {
  82                 synchronized (this) {
  83                     if (lazy == null) {
  84                         lazy = HotSpotJVMCIRuntime.runtime().getLogStream();
  85                     }
  86                 }
  87             }
  88             return lazy;
  89         }
  90 
  91         @Override
  92         public void write(byte[] b, int off, int len) throws IOException {
  93             lazy().write(b, off, len);
  94         }
  95 
  96         @Override
  97         public void write(int b) throws IOException {
  98             lazy().write(b);
  99         }
 100 
 101         @Override
 102         public void flush() throws IOException {
 103             lazy().flush();
 104         }
 105 
 106         @Override
 107         public void close() throws IOException {
 108             lazy().close();
 109         }
 110     }
 111 
 112     /**
 113      * Gets the print stream configured by this option. If no file is configured, the print stream
 114      * will output to HotSpot's {@link HotSpotJVMCIRuntimeProvider#getLogStream() log} stream.
 115      */
 116     public PrintStream getStream(OptionValues options) {
 117         if (getValue(options) != null) {
 118             try {
 119                 final boolean enableAutoflush = true;
 120                 PrintStream ps = new PrintStream(new FileOutputStream(getFilename(options)), enableAutoflush);
 121                 /*
 122                  * Add the JVM and Java arguments to the log file to help identity it.
 123                  */
 124                 String inputArguments = String.join(" ", ManagementFactory.getRuntimeMXBean().getInputArguments());
 125                 ps.println("VM Arguments: " + inputArguments);
 126                 String cmd = System.getProperty("sun.java.command");
 127                 if (cmd != null) {
 128                     ps.println("sun.java.command=" + cmd);
 129                 }
 130                 return ps;
 131             } catch (FileNotFoundException e) {
 132                 throw new RuntimeException("couldn't open file: " + getValue(options), e);
 133             }
 134         } else {
 135             return new PrintStream(new DelayedOutputStream());
 136         }
 137     }
 138 }