25
26 import java.io.File;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.util.concurrent.TimeUnit;
33 import java.util.function.BiConsumer;
34 import java.util.function.Function;
35 import java.util.stream.Collectors;
36 import jdk.test.lib.jittester.types.TypeKlass;
37 import jdk.test.lib.jittester.utils.PseudoRandom;
38
39 public abstract class TestsGenerator implements BiConsumer<IRNode, IRNode> {
40 private static final int DEFAULT_JTREG_TIMEOUT = 120;
41 protected static final String JAVA_BIN = getJavaPath();
42 protected static final String JAVAC = Paths.get(JAVA_BIN, "javac").toString();
43 protected static final String JAVA = Paths.get(JAVA_BIN, "java").toString();
44 protected final Path generatorDir;
45 protected final Function<String, String[]> preRunActions;
46 protected final String jtDriverOptions;
47 private static final String DISABLE_WARNINGS = "-XX:-PrintWarnings";
48
49 protected TestsGenerator(String suffix) {
50 this(suffix, s -> new String[0], "");
51 }
52
53 protected TestsGenerator(String suffix, Function<String, String[]> preRunActions,
54 String jtDriverOptions) {
55 generatorDir = getRoot().resolve(suffix);
56 this.preRunActions = preRunActions;
57 this.jtDriverOptions = jtDriverOptions;
58 }
59
60 protected void generateGoldenOut(String mainClassName) {
61 String classPath = getRoot().resolve(generatorDir)
62 .toAbsolutePath()
63 .toString();
64 ProcessBuilder pb = new ProcessBuilder(JAVA, "-Xint", DISABLE_WARNINGS, "-Xverify",
65 "-cp", classPath, mainClassName);
66 String goldFile = mainClassName + ".gold";
67 try {
68 runProcess(pb, generatorDir.resolve(goldFile).toString());
69 } catch (IOException | InterruptedException e) {
70 throw new Error("Can't run generated test ", e);
71 }
72 }
73
74 protected static int runProcess(ProcessBuilder pb, String name)
75 throws IOException, InterruptedException {
76 pb.redirectError(new File(name + ".err"));
77 pb.redirectOutput(new File(name + ".out"));
78 Process process = pb.start();
79 try {
80 if (process.waitFor(DEFAULT_JTREG_TIMEOUT, TimeUnit.SECONDS)) {
81 try (FileWriter file = new FileWriter(name + ".exit")) {
82 file.write(Integer.toString(process.exitValue()));
83 }
84 return process.exitValue();
85 }
86 } finally {
87 process.destroyForcibly();
88 }
89 return -1;
90 }
91
92 protected static void compilePrinter() {
93 Path root = getRoot();
94 ProcessBuilder pbPrinter = new ProcessBuilder(JAVAC,
95 root.resolve("jdk")
96 .resolve("test")
97 .resolve("lib")
98 .resolve("jittester")
99 .resolve("jtreg")
100 .resolve("Printer.java")
101 .toString());
102 try {
103 int exitCode = runProcess(pbPrinter, root.resolve("Printer").toString());
104 if (exitCode != 0) {
105 throw new Error("Printer compilation returned exit code " + exitCode);
106 }
107 } catch (IOException | InterruptedException e) {
108 throw new Error("Can't compile printer", e);
109 }
110 }
111
112 protected static void ensureExisting(Path path) {
113 if (Files.notExists(path)) {
114 try {
|
25
26 import java.io.File;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.util.concurrent.TimeUnit;
33 import java.util.function.BiConsumer;
34 import java.util.function.Function;
35 import java.util.stream.Collectors;
36 import jdk.test.lib.jittester.types.TypeKlass;
37 import jdk.test.lib.jittester.utils.PseudoRandom;
38
39 public abstract class TestsGenerator implements BiConsumer<IRNode, IRNode> {
40 private static final int DEFAULT_JTREG_TIMEOUT = 120;
41 protected static final String JAVA_BIN = getJavaPath();
42 protected static final String JAVAC = Paths.get(JAVA_BIN, "javac").toString();
43 protected static final String JAVA = Paths.get(JAVA_BIN, "java").toString();
44 protected final Path generatorDir;
45 protected final Path tmpDir;
46 protected final Function<String, String[]> preRunActions;
47 protected final String jtDriverOptions;
48 private static final String DISABLE_WARNINGS = "-XX:-PrintWarnings";
49
50 protected TestsGenerator(String suffix) {
51 this(suffix, s -> new String[0], "");
52 }
53
54 protected TestsGenerator(String suffix, Function<String, String[]> preRunActions,
55 String jtDriverOptions) {
56 generatorDir = getRoot().resolve(suffix).toAbsolutePath();
57 try {
58 tmpDir = Files.createTempDirectory(suffix).toAbsolutePath();
59 } catch (IOException e) {
60 throw new Error("Can't get a tmp dir for " + suffix, e);
61 }
62 this.preRunActions = preRunActions;
63 this.jtDriverOptions = jtDriverOptions;
64 }
65
66 protected void generateGoldenOut(String mainClassName) {
67 String classPath = tmpDir.toString() + File.pathSeparator
68 + generatorDir.toString();
69 ProcessBuilder pb = new ProcessBuilder(JAVA, "-Xint", DISABLE_WARNINGS, "-Xverify",
70 "-cp", classPath, mainClassName);
71 String goldFile = mainClassName + ".gold";
72 try {
73 runProcess(pb, generatorDir.resolve(goldFile).toString());
74 } catch (IOException | InterruptedException e) {
75 throw new Error("Can't run generated test ", e);
76 }
77 }
78
79 protected static int runProcess(ProcessBuilder pb, String name)
80 throws IOException, InterruptedException {
81 pb.redirectError(new File(name + ".err"));
82 pb.redirectOutput(new File(name + ".out"));
83 Process process = pb.start();
84 try {
85 if (process.waitFor(DEFAULT_JTREG_TIMEOUT, TimeUnit.SECONDS)) {
86 try (FileWriter file = new FileWriter(name + ".exit")) {
87 file.write(Integer.toString(process.exitValue()));
88 }
89 return process.exitValue();
90 }
91 } finally {
92 process.destroyForcibly();
93 }
94 return -1;
95 }
96
97 protected void compilePrinter() {
98 Path root = getRoot();
99 ProcessBuilder pbPrinter = new ProcessBuilder(JAVAC,
100 "-d", tmpDir.toString(),
101 root.resolve("jdk")
102 .resolve("test")
103 .resolve("lib")
104 .resolve("jittester")
105 .resolve("jtreg")
106 .resolve("Printer.java")
107 .toString());
108 try {
109 int exitCode = runProcess(pbPrinter, root.resolve("Printer").toString());
110 if (exitCode != 0) {
111 throw new Error("Printer compilation returned exit code " + exitCode);
112 }
113 } catch (IOException | InterruptedException e) {
114 throw new Error("Can't compile printer", e);
115 }
116 }
117
118 protected static void ensureExisting(Path path) {
119 if (Files.notExists(path)) {
120 try {
|