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 */
23package org.graalvm.compiler.options;
24
25import java.nio.file.Path;
26import java.nio.file.Paths;
27import java.util.concurrent.atomic.AtomicInteger;
28import java.util.concurrent.atomic.AtomicLong;
29
30public class UniquePathUtilities {
31
32    private static final AtomicLong globalTimeStamp = new AtomicLong();
33    /**
34     * This generates a per thread persistent id to aid mapping related dump files with each other.
35     */
36    private static final ThreadLocal<Integer> threadDumpId = new ThreadLocal<>();
37    private static final AtomicInteger dumpId = new AtomicInteger();
38
39    private static int getThreadDumpId() {
40        Integer id = threadDumpId.get();
41        if (id == null) {
42            id = dumpId.incrementAndGet();
43            threadDumpId.set(id);
44        }
45        return id;
46    }
47
48    private static String formatExtension(String ext) {
49        if (ext == null || ext.length() == 0) {
50            return "";
51        }
52        return "." + ext;
53    }
54
55    public static long getGlobalTimeStamp() {
56        if (globalTimeStamp.get() == 0) {
57            globalTimeStamp.compareAndSet(0, System.currentTimeMillis());
58        }
59        return globalTimeStamp.get();
60    }
61
62    /**
63     * Generate a {@link Path} using the format "%s-%d_%d%s" with the {@link OptionValue#getValue()
64     * base filename}, a {@link #globalTimeStamp global timestamp}, {@link #getThreadDumpId a per
65     * thread unique id} and an optional {@code extension}.
66     *
67     * @return the output file path or null if the flag is null
68     */
69    public static Path getPath(OptionValue<String> option, OptionValue<String> defaultDirectory, String extension) {
70        return getPath(option, defaultDirectory, extension, true);
71    }
72
73    /**
74     * Generate a {@link Path} using the format "%s-%d_%s" with the {@link OptionValue#getValue()
75     * base filename}, a {@link #globalTimeStamp global timestamp} and an optional {@code extension}
76     * .
77     *
78     * @return the output file path or null if the flag is null
79     */
80    public static Path getPathGlobal(OptionValue<String> option, OptionValue<String> defaultDirectory, String extension) {
81        return getPath(option, defaultDirectory, extension, false);
82    }
83
84    private static Path getPath(OptionValue<String> option, OptionValue<String> defaultDirectory, String extension, boolean includeThreadId) {
85        if (option.getValue() == null) {
86            return null;
87        }
88        final String name = includeThreadId
89                        ? String.format("%s-%d_%d%s", option.getValue(), getGlobalTimeStamp(), getThreadDumpId(), formatExtension(extension))
90                        : String.format("%s-%d%s", option.getValue(), getGlobalTimeStamp(), formatExtension(extension));
91        Path result = Paths.get(name);
92        if (result.isAbsolute() || defaultDirectory == null) {
93            return result;
94        }
95        return Paths.get(defaultDirectory.getValue(), name);
96    }
97
98}
99