Path: blob/master/test/functional/VM_Test/src/j9vm/runner/Runner.java
6004 views
/*******************************************************************************1* Copyright (c) 2001, 2020 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package j9vm.runner;22import java.io.*;23import java.util.Properties;24import java.util.Iterator;25import java.util.Map;26import java.lang.reflect.Method;2728public class Runner {2930protected enum OSName {31AIX,32LINUX,33WINDOWS,34ZOS,35MAC,36UNKNOWN37}3839protected enum OSArch {40PPC,41S390X,42X86,43UNKNOWN44}4546protected enum AddrMode {47BIT31,48BIT32,49BIT64,50UNKNOWN51}5253public static final String systemPropertyPrefix = "j9vm.";5455protected String className;56protected String exeName;57protected String bootClassPath;58protected String userClassPath;59protected String javaVersion;60protected OutputCollector inCollector;61protected OutputCollector errCollector;62protected OSName osName = OSName.UNKNOWN;63protected OSArch osArch = OSArch.UNKNOWN;64protected AddrMode addrMode = AddrMode.UNKNOWN;6566private final String heapOptions = "-Xms64m -Xmx64m";6768private void setPlatform() {6970String OSSpec = System.getProperty("os.name").toLowerCase();71if (OSSpec != null) {72/* Get OS from the spec string */73if (OSSpec.contains("aix")) {74osName = OSName.AIX;75} else if (OSSpec.contains("linux")) {76osName = OSName.LINUX;77} else if (OSSpec.contains("windows")) {78osName = OSName.WINDOWS;79} else if (OSSpec.contains("z/os")) {80osName = OSName.ZOS;81} else if (OSSpec.contains("mac")) {82osName = OSName.MAC;83} else {84System.out.println("Runner couldn't determine underlying OS. Got OS Name:" + OSSpec);85osName = OSName.UNKNOWN;86}87}88String archSpec = System.getProperty("os.arch").toLowerCase();89if (archSpec != null) {90/* Get arch from spec string */91if (archSpec.contains("ppc")) {92osArch = OSArch.PPC;93} else if (archSpec.contains("s390")) {94osArch = OSArch.S390X;95} else if (archSpec.contains("amd64") || archSpec.contains("x86")) {96osArch = OSArch.X86;97} else {98System.out.println("Runner couldn't determine underlying architecture. Got OS Arch:" + archSpec);99osArch = OSArch.UNKNOWN;100}101}102103String addressingMode = System.getProperty("sun.arch.data.model");104if (addressingMode != null) {105/* Get address mode. S390 31-Bit addressing mode should return 32. */106if ((osArch == OSArch.S390X) && (addressingMode.contains("32"))) {107addrMode = AddrMode.BIT31;108} else if (addressingMode.contains("32")) {109addrMode = AddrMode.BIT32;110} else if (addressingMode.contains("64")) {111addrMode = AddrMode.BIT64;112} else {113System.out.println("Runner couldn't determine underlying addressing mode. Got addressingMode:" + addressingMode);114addrMode = AddrMode.UNKNOWN;115}116}117}118119public Runner(String className, String exeName, String bootClassPath, String userClassPath, String javaVersion) {120super();121this.className = className;122this.exeName = exeName;123this.bootClassPath = bootClassPath;124this.userClassPath = userClassPath;125this.javaVersion = javaVersion;126setPlatform();127}128129public String getBootClassPathOption () {130if (bootClassPath == null) return "";131return "-Xbootclasspath:" + bootClassPath;132}133134public String getUserClassPathOption () {135if (userClassPath == null) return "";136return "-classpath " + userClassPath;137}138139public String getJ9VMSystemPropertiesString() {140String result = "";141Properties systemProperties = System.getProperties();142Iterator it = systemProperties.entrySet().iterator();143while(it.hasNext()) {144Map.Entry entry = (Map.Entry) it.next();145String key = (String) entry.getKey();146if(key.startsWith(systemPropertyPrefix)) {147String value = (String) entry.getValue();148result += "-D" + key + "=" + value + " ";149}150151}152return result;153}154155public String getCustomCommandLineOptions() {156/* For sub-classes to override, if desired. */157return "";158}159160public String getCommandLine() {161return exeName + " " + heapOptions + " " + getCustomCommandLineOptions() + " "162+ getJ9VMSystemPropertiesString() + " " + getBootClassPathOption() + " "163+ getUserClassPathOption() + " ";164}165166public String getTestClassArguments() {167/* For sub-classes to override, if desired. */168return "";169}170171public int runCommandLine(String commandLine) {172System.out.println("command: " + commandLine);173System.out.println();174Process process;175try {176process = Runtime.getRuntime().exec(commandLine);177} catch (Throwable e) {178System.out.println("Exception starting process!");179System.out.println("(" + e.getMessage() + ")");180e.printStackTrace();181return 99999;182}183184BufferedInputStream inStream = new BufferedInputStream(process.getInputStream());185BufferedInputStream errStream = new BufferedInputStream(process.getErrorStream());186inCollector = new OutputCollector(inStream);187errCollector = new OutputCollector(errStream);188inCollector.start();189errCollector.start();190try {191process.waitFor();192inCollector.join();193errCollector.join();194} catch (InterruptedException e) {195/* Nothing. */196}197/* Must release process resources here, or wimpy platforms198like Neutrino will run out of handles! */199int retval = process.exitValue();200process.destroy(); process = null;201System.gc();202return retval;203}204205public boolean run() {206int retval = runCommandLine(getCommandLine() + " " + className + " " + getTestClassArguments());207if ( 0 != retval ) {208System.out.println("no-zero exit value: " + retval);209return false;210}211return true;212}213214}215216217