1 /*
   2  * Copyright (c) 2013, 2018, 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 package vm.compiler.complog.share;
  24 
  25 import java.io.*;
  26 import java.util.*;
  27 import java.util.regex.*;
  28 import java.lang.reflect.*;
  29 import java.lang.management.*;
  30 
  31 import nsk.share.TestFailure;
  32 import nsk.share.TestBug;
  33 import nsk.share.log.*;
  34 import vm.share.options.*;
  35 import vm.share.process.*;
  36 
  37 /**
  38  * Test executor for all tests that require compilation log analysis.
  39  * Next options should be passed to LogCompilationTest:
  40  * <b>-testClass</b> test to be executed;
  41  * <b>-testedJava</b> path to testing java binary;
  42  * <b>-options</b> options that should be passed to testing java;
  43  * <b>-parserClass</b> parser that will be used for compilation log analysis;
  44  * <b>-timeout</b> timeout in secoonds.
  45  */
  46 
  47 public class LogCompilationTest extends OptionSupport implements Runnable {
  48 
  49     @Option(name="testClass", description="Test to be executed.")
  50     protected String testClass;
  51 
  52     @Option(name="parserClass", description="Parser for compilation log.")
  53     protected String parserClass;
  54 
  55     @Option(name="timeout", description="Timeout value in seconds.", default_value="1800")
  56     protected int timeout;
  57 
  58     @Option(name="testedJava", description="Java binary to be tested.")
  59     protected String testedJava;
  60 
  61     @Option(name="parserOptions", description="Options that will be passed to compilation log parser.", default_value="")
  62     protected String parserOptions;
  63 
  64     @Option(name="options", description="Options for tested java.")
  65     protected String jvmOptions;
  66 
  67     protected Log log = new LogSupport(System.out);
  68     protected Log testLog;
  69 
  70     public static final String compilationLog = "hotspot.log";
  71 
  72     public static void main(String args[]) {
  73         LogCompilationTest.setupAndRun(new LogCompilationTest(), args);
  74     }
  75 
  76     public void run() {
  77         execute();
  78         parse();
  79     }
  80 
  81     private String[] getJVMOptions() {
  82         Matcher matcher = Pattern.compile("(\"[^\"]*\")|([^\\s\"]+(\"[^\"]*\")?)").
  83             matcher(jvmOptions);
  84         List<String> options = new LinkedList<String>();
  85         while(matcher.find()) {
  86             options.add(matcher.group());
  87         }
  88         options.add("-XX:+UnlockDiagnosticVMOptions");
  89         options.add("-XX:+LogCompilation");
  90         options.add("-XX:LogFile="+compilationLog);
  91         return options.toArray(new String[0]);
  92     }
  93 
  94     private LogCompilationParser getParser() {
  95         try {
  96             Class parser = Class.forName(parserClass);
  97             Constructor ctor = parser.getConstructor();
  98             return (LogCompilationParser)ctor.newInstance();
  99         } catch (Throwable e) {
 100             throw new TestBug("Parser could not be instantiated.",e);
 101         }
 102     }
 103 
 104     private void execute() {
 105         String options[] = getJVMOptions();
 106         ProcessExecutor executor = new ProcessExecutor();
 107         try {
 108             testLog = new LogSupport(new PrintStream(new FileOutputStream("test.log")));
 109         } catch (FileNotFoundException e) {
 110             throw new TestFailure("Can't create test log file.", e);
 111         }
 112         executor.logStdOutErr("Test>>",testLog);
 113         executor.addArg(testedJava);
 114         executor.addArgs(options);
 115         executor.addArg(testClass);
 116         executor.start();
 117         executor.waitFor(timeout*1000);
 118 
 119         if(executor.getResult()!=0) {
 120             if(new File("hs_err_pid"+executor.getPid()+".log").exists()) {
 121                 throw new TestFailure("Test crashed.");
 122             } else {
 123                 throw new TestFailure("Test exited with non-zero code.");
 124             }
 125         }
 126     }
 127 
 128     private void parse() {
 129         File hotspotLog = new File(compilationLog);
 130         LogCompilationParser parser = getParser();
 131         parser.setOptions(parserOptions);
 132         parser.setLog(log);
 133         try {
 134             parser.parse(hotspotLog);
 135         } catch (Throwable e) {
 136             throw new TestFailure("Error occurred during compilation log parsing.",e);
 137         }
 138     }
 139 
 140 }