Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/compiler/complog/share/LogCompilationTest.java
41161 views
/*1* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package vm.compiler.complog.share;2324import jdk.test.lib.Utils;25import nsk.share.TestBug;26import nsk.share.TestFailure;27import nsk.share.log.Log;28import nsk.share.log.LogSupport;29import vm.share.options.Option;30import vm.share.options.OptionSupport;31import vm.share.process.ProcessExecutor;3233import java.io.File;34import java.io.FileNotFoundException;35import java.io.FileOutputStream;36import java.io.PrintStream;37import java.lang.reflect.Constructor;38import java.util.ArrayList;39import java.util.Collections;40import java.util.List;4142/**43* Test executor for all tests that require compilation log analysis.44* Next options should be passed to LogCompilationTest:45* <b>-testClass</b> test to be executed;46* <b>-testedJava</b> path to testing java binary;47* <b>-parserClass</b> parser that will be used for compilation log analysis;48* <b>-timeout</b> timeout in secoonds.49*/5051public class LogCompilationTest extends OptionSupport implements Runnable {5253@Option(name="testClass", description="Test to be executed.")54protected String testClass;5556@Option(name="parserClass", description="Parser for compilation log.")57protected String parserClass;5859@Option(name="timeout", description="Timeout value in seconds.", default_value="1800")60protected int timeout;6162@Option(name="testedJava", description="Java binary to be tested.")63protected String testedJava;6465@Option(name="parserOptions", description="Options that will be passed to compilation log parser.", default_value="")66protected String parserOptions;6768protected Log log = new LogSupport(System.out);69protected Log testLog;7071public static final String compilationLog = "hotspot.log";7273public static void main(String[] args) {74LogCompilationTest.setupAndRun(new LogCompilationTest(), args);75}7677public void run() {78execute();79parse();80}8182private String[] getJVMOptions() {83List<String> options = new ArrayList<>();84Collections.addAll(options, Utils.getTestJavaOpts());85options.add("-XX:+UnlockDiagnosticVMOptions");86options.add("-XX:+LogCompilation");87options.add("-XX:LogFile=" + compilationLog);88return options.toArray(new String[0]);89}9091private LogCompilationParser getParser() {92try {93Class<?> parser = Class.forName(parserClass);94Constructor<?> ctor = parser.getConstructor();95return (LogCompilationParser) ctor.newInstance();96} catch (Throwable e) {97throw new TestBug("Parser could not be instantiated.", e);98}99}100101private void execute() {102String[] options = getJVMOptions();103ProcessExecutor executor = new ProcessExecutor();104try {105testLog = new LogSupport(new PrintStream(new FileOutputStream("test.log")));106} catch (FileNotFoundException e) {107throw new TestFailure("Can't create test log file.", e);108}109110executor.logStdOutErr("Test>>", testLog);111executor.addArg(testedJava);112executor.addArgs(options);113executor.addArg(testClass);114executor.start();115int exitCode = executor.waitFor(timeout * 1000);116117if (exitCode != 0) {118if (new File("hs_err_pid" + executor.getPid() + ".log").exists()) {119throw new TestFailure("Test crashed. Exit code: " + exitCode);120} else {121throw new TestFailure("Test exited with non-zero code: " + exitCode);122}123}124}125126private void parse() {127File hotspotLog = new File(compilationLog);128LogCompilationParser parser = getParser();129parser.setOptions(parserOptions);130parser.setLog(log);131try {132parser.parse(hotspotLog);133} catch (Throwable e) {134throw new TestFailure("Error occurred during compilation log parsing.",e);135}136}137138}139140141