src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/DumpPathTest.java
author jwilhelm
Tue, 12 Mar 2019 19:17:42 +0100
changeset 54084 84f10bbf993f
parent 52910 583fd71c47d6
child 57537 ecc6e394475f
child 58678 9cf78a70fa4f
permissions -rw-r--r--
8218074: 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;
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    34
import org.graalvm.compiler.debug.DebugOptions.PrintGraphTarget;
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    35
import org.graalvm.compiler.options.OptionKey;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    36
import org.graalvm.compiler.options.OptionValues;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    37
import org.junit.Test;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    38
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    39
/**
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    40
 * 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
    41
 * names.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    42
 */
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    43
public class DumpPathTest extends GraalCompilerTest {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    44
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    45
    public static Object snippet() {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    46
        return new String("snippet");
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    47
    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    48
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    49
    @Test
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    50
    public void testDump() throws IOException {
52578
7dd81e82d083 8210777: Update Graal
dlong
parents: 50858
diff changeset
    51
        assumeManagementLibraryIsLoadable();
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    52
        Path dumpDirectoryPath = Files.createTempDirectory("DumpPathTest");
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    53
        String[] extensions = new String[]{".cfg", ".bgv", ".graph-strings"};
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    54
        EconomicMap<OptionKey<?>, Object> overrides = OptionValues.newOptionMap();
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    55
        overrides.put(DebugOptions.DumpPath, dumpDirectoryPath.toString());
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    56
        overrides.put(DebugOptions.PrintGraph, PrintGraphTarget.File);
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    57
        overrides.put(DebugOptions.PrintCanonicalGraphStrings, true);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    58
        overrides.put(DebugOptions.Dump, "*");
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    59
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    60
        // Generate dump files.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    61
        test(new OptionValues(getInitialOptions(), overrides), "snippet");
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    62
        // Check that Ideal files got created, in the right place.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    63
        checkForFiles(dumpDirectoryPath, extensions);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    64
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    65
        // Clean up the generated files.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    66
        scrubDirectory(dumpDirectoryPath);
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
    /**
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    70
     * Check that the given directory contains file or directory names with all the given
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    71
     * extensions.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    72
     */
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    73
    private static void checkForFiles(Path directoryPath, String[] extensions) throws IOException {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    74
        String[] paths = new String[extensions.length];
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    75
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(directoryPath)) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    76
            for (Path filePath : stream) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    77
                String fileName = filePath.getFileName().toString();
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    78
                for (int i = 0; i < extensions.length; i++) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    79
                    String extension = extensions[i];
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    80
                    if (fileName.endsWith(extensions[i])) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    81
                        assertTrue(paths[i] == null, "multiple files found for %s in %s", extension, directoryPath);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    82
                        paths[i] = fileName.replace(extensions[i], "");
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
        }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    87
        for (int i = 0; i < paths.length; i++) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    88
            assertTrue(paths[i] != null, "missing file for extension %s in %s", extensions[i], directoryPath);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    89
        }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    90
        // Ensure that all file names are the same.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    91
        for (int i = 1; i < paths.length; i++) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    92
            assertTrue(paths[0].equals(paths[i]), paths[0] + " != " + paths[i]);
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
    /**
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    97
     * Remove the temporary directory.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    98
     */
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    99
    private static void scrubDirectory(Path directoryPath) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   100
        try {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   101
            try (DirectoryStream<Path> stream = Files.newDirectoryStream(directoryPath)) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   102
                for (Path filePath : stream) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   103
                    if (Files.isRegularFile(filePath)) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   104
                        Files.delete(filePath);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   105
                    } else if (Files.isDirectory(filePath)) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   106
                        scrubDirectory(filePath);
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
            }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   110
            Files.delete(directoryPath);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   111
        } catch (IOException ioe) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   112
            ioe.printStackTrace();
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   113
        }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   114
    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   115
}