src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/DumpPathTest.java
author dlong
Sat, 08 Dec 2018 00:56:10 -0800
changeset 52910 583fd71c47d6
parent 52578 7dd81e82d083
child 54084 84f10bbf993f
permissions -rw-r--r--
8214023: Update Graal Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     1
/*
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 52578
diff changeset
     2
 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     4
 *
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     8
 *
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    13
 * accompanied this code).
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    14
 *
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    18
 *
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    21
 * questions.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    22
 */
50858
2d3e99a72541 8205824: Update Graal
never
parents: 49873
diff changeset
    23
2d3e99a72541 8205824: Update Graal
never
parents: 49873
diff changeset
    24
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    25
package org.graalvm.compiler.core.test;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    26
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    27
import java.io.IOException;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    28
import java.nio.file.DirectoryStream;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    29
import java.nio.file.Files;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    30
import java.nio.file.Path;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    31
49873
26ebfe8ce852 8199755: Update Graal
dlong
parents: 48861
diff changeset
    32
import jdk.internal.vm.compiler.collections.EconomicMap;
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    33
import org.graalvm.compiler.debug.DebugOptions;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    34
import org.graalvm.compiler.options.OptionKey;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    35
import org.graalvm.compiler.options.OptionValues;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    36
import org.junit.Test;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    37
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    38
/**
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    39
 * Check that setting the dump path results in files ending up in the right directory with matching
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    40
 * names.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    41
 */
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    42
public class DumpPathTest extends GraalCompilerTest {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    43
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    44
    public static Object snippet() {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    45
        return new String("snippet");
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    46
    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    47
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    48
    @Test
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    49
    public void testDump() throws IOException {
52578
7dd81e82d083 8210777: Update Graal
dlong
parents: 50858
diff changeset
    50
        assumeManagementLibraryIsLoadable();
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    51
        Path dumpDirectoryPath = Files.createTempDirectory("DumpPathTest");
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    52
        String[] extensions = new String[]{".cfg", ".bgv", ".graph-strings"};
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    53
        EconomicMap<OptionKey<?>, Object> overrides = OptionValues.newOptionMap();
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    54
        overrides.put(DebugOptions.DumpPath, dumpDirectoryPath.toString());
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    55
        overrides.put(DebugOptions.PrintGraphFile, true);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    56
        overrides.put(DebugOptions.PrintCanonicalGraphStrings, true);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    57
        overrides.put(DebugOptions.Dump, "*");
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    58
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    59
        // Generate dump files.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    60
        test(new OptionValues(getInitialOptions(), overrides), "snippet");
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    61
        // Check that Ideal files got created, in the right place.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    62
        checkForFiles(dumpDirectoryPath, extensions);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    63
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    64
        // Clean up the generated files.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    65
        scrubDirectory(dumpDirectoryPath);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    66
    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    67
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    68
    /**
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    69
     * Check that the given directory contains file or directory names with all the given
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    70
     * extensions.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    71
     */
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    72
    private static void checkForFiles(Path directoryPath, String[] extensions) throws IOException {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    73
        String[] paths = new String[extensions.length];
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    74
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(directoryPath)) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    75
            for (Path filePath : stream) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    76
                String fileName = filePath.getFileName().toString();
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    77
                for (int i = 0; i < extensions.length; i++) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    78
                    String extension = extensions[i];
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    79
                    if (fileName.endsWith(extensions[i])) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    80
                        assertTrue(paths[i] == null, "multiple files found for %s in %s", extension, directoryPath);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    81
                        paths[i] = fileName.replace(extensions[i], "");
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    82
                    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    83
                }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    84
            }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    85
        }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    86
        for (int i = 0; i < paths.length; i++) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    87
            assertTrue(paths[i] != null, "missing file for extension %s in %s", extensions[i], directoryPath);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    88
        }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    89
        // Ensure that all file names are the same.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    90
        for (int i = 1; i < paths.length; i++) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    91
            assertTrue(paths[0].equals(paths[i]), paths[0] + " != " + paths[i]);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    92
        }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    93
    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    94
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    95
    /**
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    96
     * Remove the temporary directory.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    97
     */
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    98
    private static void scrubDirectory(Path directoryPath) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    99
        try {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   100
            try (DirectoryStream<Path> stream = Files.newDirectoryStream(directoryPath)) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   101
                for (Path filePath : stream) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   102
                    if (Files.isRegularFile(filePath)) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   103
                        Files.delete(filePath);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   104
                    } else if (Files.isDirectory(filePath)) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   105
                        scrubDirectory(filePath);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   106
                    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   107
                }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   108
            }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   109
            Files.delete(directoryPath);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   110
        } catch (IOException ioe) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   111
            ioe.printStackTrace();
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   112
        }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   113
    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   114
}