1 /*
   2  * Copyright (c) 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.salver.handler;
  24 
  25 import java.io.File;
  26 import java.io.IOException;
  27 import java.net.InetSocketAddress;
  28 import java.nio.channels.ClosedByInterruptException;
  29 import java.text.SimpleDateFormat;
  30 import java.util.Date;
  31 
  32 import org.graalvm.compiler.debug.DebugDumpHandler;
  33 import org.graalvm.compiler.debug.TTY;
  34 import org.graalvm.compiler.salver.Salver;
  35 import org.graalvm.compiler.salver.SalverOptions;
  36 import org.graalvm.compiler.salver.dumper.AbstractGraalDumper;
  37 import org.graalvm.compiler.salver.serialize.JSONSerializer;
  38 import org.graalvm.compiler.salver.serialize.Serializer;
  39 
  40 public abstract class AbstractGraalDumpHandler<D extends AbstractGraalDumper> extends AbstractDumpHandler<D> implements DebugDumpHandler {
  41 
  42     private Serializer serializer;
  43 
  44     private static final int MAX_FAILURES = 7;
  45     private int failures;
  46 
  47     public static final class NotInitializedException extends IOException {
  48 
  49         private static final long serialVersionUID = 1L;
  50     }
  51 
  52     protected void ensureInitialized() throws IOException {
  53         if (writer == null) {
  54             if (failures < MAX_FAILURES) {
  55                 if (SalverOptions.SalverToFile.getValue()) {
  56                     initializeFileChannelWriter();
  57                 } else {
  58                     initializeSocketChannelWriter();
  59                 }
  60             }
  61             if (writer == null) {
  62                 throw new NotInitializedException();
  63             }
  64         }
  65         if (dumper == null) {
  66             dumper = createDumper();
  67             if (dumper == null) {
  68                 throw new NotInitializedException();
  69             }
  70             if (serializer == null) {
  71                 serializer = createSerializer();
  72             }
  73             if (serializer.getWriter() != writer) {
  74                 serializer.setWriter(writer);
  75             }
  76             dumper.setSerializer(serializer);
  77             dumper.beginDump();
  78         }
  79     }
  80 
  81     protected abstract D createDumper();
  82 
  83     protected Serializer createSerializer() {
  84         return new JSONSerializer();
  85     }
  86 
  87     protected abstract void handle(Object obj, String msg) throws IOException;
  88 
  89     protected void initializeSocketChannelWriter() {
  90         InetSocketAddress socketAddress = Salver.getSocketAddress();
  91         try {
  92             setWriter(socketAddress);
  93             printlnTTY("Connected to %s:%d (ECID = %s)", socketAddress.getHostName(), socketAddress.getPort(), Salver.ECID);
  94         } catch (ClosedByInterruptException e) {
  95             // May be caused by a cancelled Graal compilation
  96         } catch (IOException e) {
  97             printlnTTY("Couldn't connect to %s:%d (%s)", socketAddress.getHostName(), socketAddress.getPort(), e);
  98             failures++;
  99         }
 100     }
 101 
 102     private static final ThreadLocal<SimpleDateFormat> sdf = new ThreadLocal<SimpleDateFormat>() {
 103         @Override
 104         protected SimpleDateFormat initialValue() {
 105             return new SimpleDateFormat("YYYY-MM-dd_HH-mm");
 106         }
 107     };
 108 
 109     protected void initializeFileChannelWriter() {
 110         String filename = sdf.get().format(new Date());
 111         if (label != null) {
 112             filename += "_" + Salver.ECID + "_" + label.replaceAll("(?i)[^a-z0-9-]", "-");
 113         }
 114         String fileExt = JSONSerializer.getFileExtension();
 115         File file = new File(filename + "." + fileExt);
 116         try {
 117             for (int i = 1; file.exists(); i++) {
 118                 if (i < 1 << 7) {
 119                     file = new File(filename + "_" + i + "." + fileExt);
 120                 } else {
 121                     throw new IOException();
 122                 }
 123             }
 124             setWriter(file.toPath());
 125             printlnTTY("Dumping to \"%s\"", file.getName());
 126         } catch (ClosedByInterruptException e) {
 127             // May be caused by a cancelled Graal compilation
 128         } catch (IOException e) {
 129             printlnTTY("Failed to open %s for dumping (%s)", file.getName(), e);
 130             failures++;
 131         }
 132     }
 133 
 134     public void dump(Object obj) {
 135         dump(obj, null);
 136     }
 137 
 138     @Override
 139     public void dump(Object obj, String msg) {
 140         try {
 141             handle(obj, msg);
 142         } catch (NotInitializedException e) {
 143             // Ignore
 144         } catch (IOException e) {
 145             printlnTTY("%s", e);
 146             if (failures < MAX_FAILURES) {
 147                 failures++;
 148             } else {
 149                 close();
 150             }
 151         }
 152     }
 153 
 154     @Override
 155     public void close() {
 156         try {
 157             super.close();
 158         } catch (IOException e) {
 159             printlnTTY("%s", e);
 160         } finally {
 161             failures = 0;
 162         }
 163     }
 164 
 165     protected void printlnTTY(String format, Object... args) {
 166         if (label != null) {
 167             TTY.println("[" + label + "] " + format, args);
 168         } else {
 169             TTY.println(format, args);
 170         }
 171     }
 172 }