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 
  24 package compiler.ciReplay;
  25 
  26 import compiler.whitebox.CompilerWhiteBoxTest;
  27 import java.io.IOException;
  28 import java.io.File;
  29 import java.io.BufferedReader;
  30 import java.io.FileReader;
  31 import java.nio.file.Files;
  32 import java.nio.file.Path;
  33 import java.nio.file.Paths;
  34 import java.nio.file.StandardOpenOption;
  35 import java.util.ArrayList;
  36 import java.util.Arrays;
  37 import java.util.List;
  38 import java.util.Optional;
  39 import java.util.regex.Pattern;
  40 import java.util.regex.Matcher;
  41 import jdk.test.lib.Platform;
  42 import jdk.test.lib.process.ProcessTools;
  43 import jdk.test.lib.process.OutputAnalyzer;
  44 import jdk.test.lib.Asserts;
  45 import jdk.test.lib.Utils;
  46 
  47 public abstract class CiReplayBase {
  48     public static final String REPLAY_FILE_NAME = "test_replay.txt";
  49     public static final boolean CLIENT_VM_AVAILABLE;
  50     public static final boolean SERVER_VM_AVAILABLE;
  51     public static final String TIERED_ENABLED_VM_OPTION = "-XX:+TieredCompilation";
  52     public static final String TIERED_DISABLED_VM_OPTION = "-XX:-TieredCompilation";
  53     public static final String ENABLE_COREDUMP_ON_CRASH = "-XX:+CreateCoredumpOnCrash";
  54     public static final String DISABLE_COREDUMP_ON_CRASH = "-XX:-CreateCoredumpOnCrash";
  55     public static final String CLIENT_VM_OPTION = "-client";
  56     public static final String SERVER_VM_OPTION = "-server";
  57     public static final String TEST_CORE_FILE_NAME = "test_core";
  58     public static final String RUN_SHELL_NO_LIMIT = "ulimit -c unlimited && ";
  59     private static final String REPLAY_FILE_OPTION = "-XX:ReplayDataFile=" + REPLAY_FILE_NAME;
  60     private static final String LOCATIONS_STRING = "location: ";
  61     private static final String HS_ERR_NAME = "hs_err_pid";
  62     private static final String RUN_SHELL_ZERO_LIMIT = "ulimit -S -c 0 && ";
  63     private static final String VERSION_OPTION = "-version";
  64     private static final String[] REPLAY_GENERATION_OPTIONS = new String[]{"-Xms8m", "-Xmx32m",
  65         "-XX:MetaspaceSize=4m", "-XX:MaxMetaspaceSize=16m", "-XX:InitialCodeCacheSize=512k",
  66         "-XX:ReservedCodeCacheSize=4m", "-XX:ThreadStackSize=512", "-XX:VMThreadStackSize=512",
  67         "-XX:CompilerThreadStackSize=512", "-XX:ParallelGCThreads=1", "-XX:CICompilerCount=2",
  68         "-Xcomp", "-XX:CICrashAt=1", "-XX:+DumpReplayDataOnError", "-XX:-TransmitErrorReport",
  69         "-XX:+PreferInterpreterNativeStubs", "-XX:+PrintCompilation", REPLAY_FILE_OPTION};
  70     private static final String[] REPLAY_OPTIONS = new String[]{DISABLE_COREDUMP_ON_CRASH,
  71         "-XX:+ReplayCompiles", REPLAY_FILE_OPTION};
  72     protected final Optional<Boolean> runServer;
  73 
  74     static {
  75         try {
  76             CLIENT_VM_AVAILABLE = ProcessTools.executeTestJvm(CLIENT_VM_OPTION, VERSION_OPTION)
  77                     .getOutput().contains("Client");
  78             SERVER_VM_AVAILABLE = ProcessTools.executeTestJvm(SERVER_VM_OPTION, VERSION_OPTION)
  79                     .getOutput().contains("Server");
  80         } catch(Throwable t) {
  81             throw new Error("Initialization failed: " + t, t);
  82         }
  83     }
  84 
  85     public CiReplayBase() {
  86         runServer = Optional.empty();
  87     }
  88 
  89     public CiReplayBase(String args[]) {
  90         if (args.length != 1 || (!"server".equals(args[0]) && !"client".equals(args[0]))) {
  91             throw new Error("Expected 1 argument: [server|client]");
  92         }
  93         runServer = Optional.of("server".equals(args[0]));
  94     }
  95 
  96     public void runTest(boolean needCoreDump, String... args) {
  97         cleanup();
  98         if (generateReplay(needCoreDump)) {
  99             testAction();
 100             cleanup();
 101         } else {
 102             throw new Error("Host is not configured to generate cores");
 103         }
 104     }
 105 
 106     public abstract void testAction();
 107 
 108     private static void remove(String item) {
 109         File toDelete = new File(item);
 110         toDelete.delete();
 111         if (Platform.isWindows()) {
 112             Utils.waitForCondition(() -> !toDelete.exists());
 113         }
 114     }
 115 
 116     private static void removeFromCurrentDirectoryStartingWith(String prefix) {
 117         Arrays.stream(new File(".").listFiles())
 118                 .filter(f -> f.getName().startsWith(prefix))
 119                 .forEach(File::delete);
 120     }
 121 
 122     public static void cleanup() {
 123         removeFromCurrentDirectoryStartingWith("core");
 124         removeFromCurrentDirectoryStartingWith("replay");
 125         removeFromCurrentDirectoryStartingWith(HS_ERR_NAME);
 126         remove(TEST_CORE_FILE_NAME);
 127         remove(REPLAY_FILE_NAME);
 128     }
 129 
 130     public boolean generateReplay(boolean needCoreDump, String... vmopts) {
 131         OutputAnalyzer crashOut;
 132         String crashOutputString;
 133         try {
 134             List<String> options = new ArrayList<>();
 135             options.addAll(Arrays.asList(REPLAY_GENERATION_OPTIONS));
 136             options.addAll(Arrays.asList(vmopts));
 137             options.add(needCoreDump ? ENABLE_COREDUMP_ON_CRASH : DISABLE_COREDUMP_ON_CRASH);
 138             options.add(VERSION_OPTION);
 139             if (needCoreDump) {
 140                 crashOut = ProcessTools.executeProcess(getTestJavaCommandlineWithPrefix(
 141                         RUN_SHELL_NO_LIMIT, options.toArray(new String[0])));
 142             } else {
 143                 crashOut = ProcessTools.executeProcess(ProcessTools.createJavaProcessBuilder(true,
 144                         options.toArray(new String[0])));
 145             }
 146             crashOutputString = crashOut.getOutput();
 147             Asserts.assertNotEquals(crashOut.getExitValue(), 0, "Crash JVM exits gracefully");
 148             Files.write(Paths.get("crash.out"), crashOutputString.getBytes(),
 149                     StandardOpenOption.CREATE, StandardOpenOption.WRITE,
 150                     StandardOpenOption.TRUNCATE_EXISTING);
 151         } catch (Throwable t) {
 152             throw new Error("Can't create replay: " + t, t);
 153         }
 154         if (needCoreDump) {
 155             String coreFileLocation = getCoreFileLocation(crashOutputString);
 156             if (coreFileLocation == null) {
 157                 if (Platform.isOSX()) {
 158                     File coresDir = new File("/cores");
 159                     if (!coresDir.isDirectory() || !coresDir.canWrite()) {
 160                         return false;
 161                     }
 162                 }
 163                 throw new Error("Couldn't find core file location in: '" + crashOutputString + "'");
 164             }
 165             try {
 166                 Asserts.assertGT(new File(coreFileLocation).length(), 0L, "Unexpected core size");
 167                 Files.move(Paths.get(coreFileLocation), Paths.get(TEST_CORE_FILE_NAME));
 168             } catch (IOException ioe) {
 169                 throw new Error("Can't move core file: " + ioe, ioe);
 170             }
 171         }
 172         removeFromCurrentDirectoryStartingWith(HS_ERR_NAME);
 173         return true;
 174     }
 175 
 176     public void commonTests() {
 177         positiveTest();
 178         if (Platform.isTieredSupported()) {
 179             positiveTest(TIERED_ENABLED_VM_OPTION);
 180         }
 181     }
 182 
 183     public int startTest(String... additionalVmOpts) {
 184         try {
 185             List<String> allAdditionalOpts = new ArrayList<>();
 186             allAdditionalOpts.addAll(Arrays.asList(REPLAY_OPTIONS));
 187             allAdditionalOpts.addAll(Arrays.asList(additionalVmOpts));
 188             OutputAnalyzer oa = ProcessTools.executeProcess(getTestJavaCommandlineWithPrefix(
 189                     RUN_SHELL_ZERO_LIMIT, allAdditionalOpts.toArray(new String[0])));
 190             return oa.getExitValue();
 191         } catch (Throwable t) {
 192             throw new Error("Can't run replay process: " + t, t);
 193         }
 194     }
 195 
 196     public void runVmTests() {
 197         boolean runServerValue = runServer.orElseThrow(() -> new Error("runServer must be set"));
 198         if (runServerValue) {
 199             if (CLIENT_VM_AVAILABLE) {
 200                 negativeTest(CLIENT_VM_OPTION);
 201             }
 202         } else {
 203             if (SERVER_VM_AVAILABLE) {
 204                 negativeTest(TIERED_DISABLED_VM_OPTION, SERVER_VM_OPTION);
 205                 if (Platform.isTieredSupported()) {
 206                     positiveTest(TIERED_ENABLED_VM_OPTION, SERVER_VM_OPTION);
 207                 }
 208             }
 209         }
 210         nonTieredTests(runServerValue ? CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION
 211                 : CompilerWhiteBoxTest.COMP_LEVEL_SIMPLE);
 212     }
 213 
 214     public int getCompLevelFromReplay() {
 215         try(BufferedReader br = new BufferedReader(new FileReader(REPLAY_FILE_NAME))) {
 216             return br.lines()
 217                     .filter(s -> s.startsWith("compile "))
 218                     .map(s -> s.substring(s.lastIndexOf(' ') + 1))
 219                     .map(Integer::parseInt)
 220                     .findAny()
 221                     .get();
 222         } catch (IOException ioe) {
 223             throw new Error("Failed to read replay data: " + ioe, ioe);
 224         }
 225     }
 226 
 227     public void positiveTest(String... additionalVmOpts) {
 228         Asserts.assertEQ(startTest(additionalVmOpts), 0, "Unexpected exit code for positive case: "
 229                 + Arrays.toString(additionalVmOpts));
 230     }
 231 
 232     public void negativeTest(String... additionalVmOpts) {
 233         Asserts.assertNE(startTest(additionalVmOpts), 0, "Unexpected exit code for negative case: "
 234                 + Arrays.toString(additionalVmOpts));
 235     }
 236 
 237     public void nonTieredTests(int compLevel) {
 238         int replayDataCompLevel = getCompLevelFromReplay();
 239         if (replayDataCompLevel == compLevel) {
 240             positiveTest(TIERED_DISABLED_VM_OPTION);
 241         } else {
 242             negativeTest(TIERED_DISABLED_VM_OPTION);
 243         }
 244     }
 245 
 246     // lets search few possible locations using process output and return existing location
 247     private String getCoreFileLocation(String crashOutputString) {
 248         Asserts.assertTrue(crashOutputString.contains(LOCATIONS_STRING),
 249                 "Output doesn't contain the location of core file, see crash.out");
 250         String stringWithLocation = Arrays.stream(crashOutputString.split("\\r?\\n"))
 251                 .filter(str -> str.contains(LOCATIONS_STRING))
 252                 .findFirst()
 253                 .get();
 254         stringWithLocation = stringWithLocation.substring(stringWithLocation
 255                 .indexOf(LOCATIONS_STRING) + LOCATIONS_STRING.length());
 256         String coreWithPid;
 257         if (stringWithLocation.contains("or ") && !Platform.isWindows()) {
 258             Matcher m = Pattern.compile("or.* ([^ ]+[^\\)])\\)?").matcher(stringWithLocation);
 259             if (!m.find()) {
 260                 throw new Error("Couldn't find path to core inside location string");
 261             }
 262             coreWithPid = m.group(1);
 263         } else {
 264             coreWithPid = stringWithLocation.trim();
 265         }
 266         if (new File(coreWithPid).exists()) {
 267             return coreWithPid;
 268         }
 269         String justCore = Paths.get("core").toString();
 270         if (new File(justCore).exists()) {
 271             return justCore;
 272         }
 273         Path coreWithPidPath = Paths.get(coreWithPid);
 274         String justFile = coreWithPidPath.getFileName().toString();
 275         if (new File(justFile).exists()) {
 276             return justFile;
 277         }
 278         Path parent = coreWithPidPath.getParent();
 279         if (parent != null) {
 280             String coreWithoutPid = parent.resolve("core").toString();
 281             if (new File(coreWithoutPid).exists()) {
 282                 return coreWithoutPid;
 283             }
 284         }
 285         return null;
 286     }
 287 
 288     private String[] getTestJavaCommandlineWithPrefix(String prefix, String... args) {
 289         try {
 290             String cmd = ProcessTools.getCommandLine(ProcessTools.createJavaProcessBuilder(true, args));
 291             return new String[]{"sh", "-c", prefix
 292                     + (Platform.isWindows() ? cmd.replace('\\', '/').replace(";", "\\;") : cmd)};
 293         } catch(Throwable t) {
 294             throw new Error("Can't create process builder: " + t, t);
 295         }
 296     }
 297 }