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         if (name.contains("%t")) {
  69             name = name.replaceAll("%t", String.valueOf(System.currentTimeMillis()));
  70         }
  71         return name;
  72     }
  73 
  74     /**
  75      * An output stream that redirects to {@link HotSpotJVMCIRuntimeProvider#getLogStream()}. The
  76      * {@link HotSpotJVMCIRuntimeProvider#getLogStream()} value is only accessed the first time an
  77      * IO operation is performed on the stream. This is required to break a deadlock in early JVMCI
  78      * initialization.
  79      */
  80     static class DelayedOutputStream extends OutputStream {
  81         private volatile OutputStream lazy;
  82 
  83         private OutputStream lazy() {
  84             if (lazy == null) {
  85                 synchronized (this) {
  86                     if (lazy == null) {
  87                         lazy = HotSpotJVMCIRuntime.runtime().getLogStream();
  88                     }
  89                 }
  90             }
  91             return lazy;
  92         }
  93 
  94         @Override
  95         public void write(byte[] b, int off, int len) throws IOException {
  96             lazy().write(b, off, len);
  97         }
  98 
  99         @Override
 100         public void write(int b) throws IOException {
 101             lazy().write(b);
 102         }
 103 
 104         @Override
 105         public void flush() throws IOException {
 106             lazy().flush();
 107         }
 108 
 109         @Override
 110         public void close() throws IOException {
 111             lazy().close();
 112         }
 113     }
 114 
 115     /**
 116      * Gets the print stream configured by this option. If no file is configured, the print stream
 117      * will output to HotSpot's {@link HotSpotJVMCIRuntimeProvider#getLogStream() log} stream.
 118      */
 119     public PrintStream getStream(OptionValues options) {
 120         if (getValue(options) != null) {
 121             try {
 122                 final boolean enableAutoflush = true;
 123                 PrintStream ps = new PrintStream(new FileOutputStream(getFilename(options)), enableAutoflush);
 124                 /*
 125                  * Add the JVM and Java arguments to the log file to help identity it.
 126                  */
 127                 String inputArguments = String.join(" ", ManagementFactory.getRuntimeMXBean().getInputArguments());
 128                 ps.println("VM Arguments: " + inputArguments);
 129                 String cmd = System.getProperty("sun.java.command");
 130                 if (cmd != null) {
 131                     ps.println("sun.java.command=" + cmd);
 132                 }
 133                 return ps;
 134             } catch (FileNotFoundException e) {
 135                 throw new RuntimeException("couldn't open file: " + getValue(options), e);
 136             }
 137         } else {
 138             return new PrintStream(new DelayedOutputStream());
 139         }
 140     }
 141 }