1 /*
   2  * Copyright (c) 2016, 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.debug;
  24 
  25 import java.io.IOException;
  26 import java.nio.file.Path;
  27 import java.nio.file.Paths;
  28 import java.util.HashMap;
  29 import java.util.concurrent.atomic.AtomicInteger;
  30 import java.util.concurrent.atomic.AtomicLong;
  31 
  32 import org.graalvm.compiler.options.OptionKey;
  33 import org.graalvm.compiler.options.OptionValues;
  34 
  35 public class UniquePathUtilities {
  36 
  37     private static final AtomicLong globalTimeStamp = new AtomicLong();
  38     /**
  39      * This generates a per thread persistent id to aid mapping related dump files with each other.
  40      */
  41     private static final ThreadLocal<PerThreadSequence> threadDumpId = new ThreadLocal<>();
  42     private static final AtomicInteger dumpId = new AtomicInteger();
  43 
  44     static class PerThreadSequence {
  45         final int threadID;
  46         HashMap<String, Integer> sequences = new HashMap<>(2);
  47 
  48         PerThreadSequence(int threadID) {
  49             this.threadID = threadID;
  50         }
  51 
  52         String generateID(String extension) {
  53             Integer box = sequences.get(extension);
  54             if (box == null) {
  55                 sequences.put(extension, 1);
  56                 return Integer.toString(threadID);
  57             } else {
  58                 sequences.put(extension, box + 1);
  59                 return Integer.toString(threadID) + '-' + box;
  60             }
  61         }
  62     }
  63 
  64     private static String getThreadDumpId(String extension) {
  65         PerThreadSequence id = threadDumpId.get();
  66         if (id == null) {
  67             id = new PerThreadSequence(dumpId.incrementAndGet());
  68             threadDumpId.set(id);
  69         }
  70         return id.generateID(extension);
  71     }
  72 
  73     public static String formatExtension(String ext) {
  74         if (ext == null || ext.length() == 0) {
  75             return "";
  76         }
  77         return "." + ext;
  78     }
  79 
  80     public static long getGlobalTimeStamp() {
  81         if (globalTimeStamp.get() == 0) {
  82             globalTimeStamp.compareAndSet(0, System.currentTimeMillis());
  83         }
  84         return globalTimeStamp.get();
  85     }
  86 
  87     /**
  88      * Generates a {@link Path} using the format "%s-%d_%d%s" with the {@code baseNameOption}, a
  89      * {@link #getGlobalTimeStamp() global timestamp} , {@link #getThreadDumpId a per thread unique
  90      * id} and an optional {@code extension}.
  91      *
  92      * @return the output file path or null if the flag is null
  93      */
  94     public static Path getPath(OptionValues options, OptionKey<String> option, String extension) throws IOException {
  95         return getPath(options, option, extension, true);
  96     }
  97 
  98     /**
  99      * Generate a {@link Path} using the format "%s-%d_%s" with the {@code baseNameOption}, a
 100      * {@link #getGlobalTimeStamp() global timestamp} and an optional {@code extension} .
 101      *
 102      * @return the output file path or null if the flag is null
 103      */
 104     public static Path getPathGlobal(OptionValues options, OptionKey<String> baseNameOption, String extension) throws IOException {
 105         return getPath(options, baseNameOption, extension, false);
 106     }
 107 
 108     private static Path getPath(OptionValues options, OptionKey<String> baseNameOption, String extension, boolean includeThreadId) throws IOException {
 109         if (baseNameOption.getValue(options) == null) {
 110             return null;
 111         }
 112         String ext = formatExtension(extension);
 113         final String name = includeThreadId
 114                         ? String.format("%s-%d_%s%s", baseNameOption.getValue(options), getGlobalTimeStamp(), getThreadDumpId(ext), ext)
 115                         : String.format("%s-%d%s", baseNameOption.getValue(options), getGlobalTimeStamp(), ext);
 116         Path result = Paths.get(name);
 117         if (result.isAbsolute()) {
 118             return result;
 119         }
 120         Path dumpDir = DebugOptions.getDumpDirectory(options);
 121         return dumpDir.resolve(name).normalize();
 122     }
 123 }