From 91220287fcada7943e09099022b5745af491e328 Mon Sep 17 00:00:00 2001
From: Evgeny Nikitin <enikitin@openjdk.org>
Date: Thu, 23 Apr 2020 16:20:17 +0200
Subject: [PATCH] 8147018: CompilerControl: Improve handling of timeouts and
 failures for tests

Dump expected method states, improve compile commands dumping in CompilerControl tests

Reviewed-by: iignatyev, rbackman
---
 .../jcmd/AddAndRemoveTest.java                |  1 -
 .../jcmd/ClearDirectivesFileStackTest.java    |  1 -
 .../jcmd/ClearDirectivesStackTest.java        |  1 -
 .../jcmd/PrintDirectivesTest.java             |  1 -
 .../compilercontrol/share/MultiCommand.java   |  3 +--
 .../compilercontrol/share/SingleCommand.java  |  3 +--
 .../share/actions/CompileAction.java          |  9 ++++++-
 .../share/scenario/CompileCommand.java        | 25 +++++++++++++++----
 .../share/scenario/JcmdCommand.java           | 11 +++++++-
 .../share/scenario/Scenario.java              |  3 ++-
 10 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/test/hotspot/jtreg/compiler/compilercontrol/jcmd/AddAndRemoveTest.java b/test/hotspot/jtreg/compiler/compilercontrol/jcmd/AddAndRemoveTest.java
index 898fa82410c..5ff3bd830f9 100644
--- a/test/hotspot/jtreg/compiler/compilercontrol/jcmd/AddAndRemoveTest.java
+++ b/test/hotspot/jtreg/compiler/compilercontrol/jcmd/AddAndRemoveTest.java
@@ -63,7 +63,6 @@ public class AddAndRemoveTest extends AbstractTestBase {
             MethodDescriptor md = getValidMethodDescriptor(exec);
             CompileCommand compileCommand = new JcmdCommand(Command.COMPILEONLY,
                     md, null, Scenario.Type.JCMD, Scenario.JcmdType.ADD);
-            compileCommand.print();
             builder.add(compileCommand);
         }
         // Remove half of them
diff --git a/test/hotspot/jtreg/compiler/compilercontrol/jcmd/ClearDirectivesFileStackTest.java b/test/hotspot/jtreg/compiler/compilercontrol/jcmd/ClearDirectivesFileStackTest.java
index 6a18953c745..cbdafb63e65 100644
--- a/test/hotspot/jtreg/compiler/compilercontrol/jcmd/ClearDirectivesFileStackTest.java
+++ b/test/hotspot/jtreg/compiler/compilercontrol/jcmd/ClearDirectivesFileStackTest.java
@@ -76,7 +76,6 @@ public class ClearDirectivesFileStackTest extends AbstractTestBase {
             CompileCommand compileCommand = new CompileCommand(command,
                     methodDescriptor, cmdGen.generateCompiler(),
                     Scenario.Type.DIRECTIVE);
-            compileCommand.print();
             builder.add(compileCommand);
         }
         // clear the stack
diff --git a/test/hotspot/jtreg/compiler/compilercontrol/jcmd/ClearDirectivesStackTest.java b/test/hotspot/jtreg/compiler/compilercontrol/jcmd/ClearDirectivesStackTest.java
index a20ed6a53e5..c046812f730 100644
--- a/test/hotspot/jtreg/compiler/compilercontrol/jcmd/ClearDirectivesStackTest.java
+++ b/test/hotspot/jtreg/compiler/compilercontrol/jcmd/ClearDirectivesStackTest.java
@@ -66,7 +66,6 @@ public class ClearDirectivesStackTest extends AbstractTestBase {
                     cmdGen.generateCommand(), methodDescriptor,
                     cmdGen.generateCompiler(), Scenario.Type.JCMD,
                     Scenario.JcmdType.ADD);
-            compileCommand.print();
             builder.add(compileCommand);
         }
         // clear the stack
diff --git a/test/hotspot/jtreg/compiler/compilercontrol/jcmd/PrintDirectivesTest.java b/test/hotspot/jtreg/compiler/compilercontrol/jcmd/PrintDirectivesTest.java
index 8c1ea162423..70df3e95a1b 100644
--- a/test/hotspot/jtreg/compiler/compilercontrol/jcmd/PrintDirectivesTest.java
+++ b/test/hotspot/jtreg/compiler/compilercontrol/jcmd/PrintDirectivesTest.java
@@ -73,7 +73,6 @@ public class PrintDirectivesTest extends AbstractTestBase {
             CompileCommand compileCommand = new CompileCommand(command,
                     methodDescriptor, cmdGen.generateCompiler(),
                     Scenario.Type.DIRECTIVE);
-            compileCommand.print();
             builder.add(compileCommand);
         }
         // print all directives
diff --git a/test/hotspot/jtreg/compiler/compilercontrol/share/MultiCommand.java b/test/hotspot/jtreg/compiler/compilercontrol/share/MultiCommand.java
index ab51daa1520..2683c356708 100644
--- a/test/hotspot/jtreg/compiler/compilercontrol/share/MultiCommand.java
+++ b/test/hotspot/jtreg/compiler/compilercontrol/share/MultiCommand.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -76,7 +76,6 @@ public class MultiCommand extends AbstractTestBase {
         builder.addFlag("-XX:+UnlockDiagnosticVMOptions");
         builder.addFlag("-XX:CompilerDirectivesLimit=101");
         for (CompileCommand cc : testCases) {
-            cc.print();
             builder.add(cc);
         }
         Scenario scenario = builder.build();
diff --git a/test/hotspot/jtreg/compiler/compilercontrol/share/SingleCommand.java b/test/hotspot/jtreg/compiler/compilercontrol/share/SingleCommand.java
index 1ca86054bc5..a7a1bac1b3a 100644
--- a/test/hotspot/jtreg/compiler/compilercontrol/share/SingleCommand.java
+++ b/test/hotspot/jtreg/compiler/compilercontrol/share/SingleCommand.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -49,7 +49,6 @@ public class SingleCommand extends AbstractTestBase {
         CommandGenerator cmdGen = new CommandGenerator();
         CompileCommand compileCommand = cmdGen.generateCompileCommand(command,
                 md, type);
-        compileCommand.print();
         builder.add(compileCommand);
         Scenario scenario = builder.build();
         scenario.execute();
diff --git a/test/hotspot/jtreg/compiler/compilercontrol/share/actions/CompileAction.java b/test/hotspot/jtreg/compiler/compilercontrol/share/actions/CompileAction.java
index 515aced38ba..464952d715d 100644
--- a/test/hotspot/jtreg/compiler/compilercontrol/share/actions/CompileAction.java
+++ b/test/hotspot/jtreg/compiler/compilercontrol/share/actions/CompileAction.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -58,6 +58,13 @@ public class CompileAction {
      */
     public static void checkCompiled(Executable executable,
                                      State state) {
+        { // Dumping the state being checked
+            System.out.println("Checking expected compilation state: {");
+            System.out.println("  method: " + executable);
+            state.toString().lines()
+                    .map(line -> "  " + line).forEach(System.out::println);
+            System.out.println("}");
+        }
         int first = COMP_LEVELS[0];
         if (first < 4) {
             checkCompilation(executable, first, state.isC1Compilable());
diff --git a/test/hotspot/jtreg/compiler/compilercontrol/share/scenario/CompileCommand.java b/test/hotspot/jtreg/compiler/compilercontrol/share/scenario/CompileCommand.java
index 72e6c25cdc9..a22a45191a1 100644
--- a/test/hotspot/jtreg/compiler/compilercontrol/share/scenario/CompileCommand.java
+++ b/test/hotspot/jtreg/compiler/compilercontrol/share/scenario/CompileCommand.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -57,10 +57,25 @@ public class CompileCommand {
     }
 
     /**
-     * Prints compile command to the system output
+     * Formats the command according to the following pattern:
+     * {@code <command_name> Type: <type> Compiler: <compiler> MethodDescriptor: <method_descriptor> IsValid: <true/false>}
+     * Sample output:
+     * COMPILEONLY Type: OPTION Compiler: C1 MethodDescriptor: *Klass.method* IsValid: true
      */
-    public void print() {
-        System.out.printf("%s (type: %s): %s (valid: %b)%n", command.name(),
-                type.name(), methodDescriptor.getString(), isValid());
+    protected String formatFields() {
+        return command.name() +
+               " Type: " + type +
+               " Compiler: " + compiler +
+               " MethodDescriptor: " + (methodDescriptor == null ? "null" : methodDescriptor.getString()) +
+               " IsValid: " + isValid();
+    }
+
+    /**
+     * Returns formatted string representation in the form
+     * {@code "(CompileCommand Field1: <field1> Field2: <field2> ...)}
+     * The fields are formatted by {@link #formatFields()}.
+     */
+    public String toString() {
+        return "(CompileCommand " + formatFields() + ")";
     }
 }
diff --git a/test/hotspot/jtreg/compiler/compilercontrol/share/scenario/JcmdCommand.java b/test/hotspot/jtreg/compiler/compilercontrol/share/scenario/JcmdCommand.java
index 21452c83844..4e38ab5f1a5 100644
--- a/test/hotspot/jtreg/compiler/compilercontrol/share/scenario/JcmdCommand.java
+++ b/test/hotspot/jtreg/compiler/compilercontrol/share/scenario/JcmdCommand.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -36,4 +36,13 @@ public class JcmdCommand extends CompileCommand {
         super(command, methodDescriptor, compiler, type);
         this.jcmdType = jcmdType;
     }
+
+    /**
+     * Enchances parent's class method with the the JCMDtype printing:
+     * {@code ... JCMDType: <jcmd_type>}
+     */
+    protected String formatFields() {
+        return super.formatFields() + " JCMDType: " + jcmdType;
+    }
+
 }
diff --git a/test/hotspot/jtreg/compiler/compilercontrol/share/scenario/Scenario.java b/test/hotspot/jtreg/compiler/compilercontrol/share/scenario/Scenario.java
index 8c860de787a..f10b74ad1b6 100644
--- a/test/hotspot/jtreg/compiler/compilercontrol/share/scenario/Scenario.java
+++ b/test/hotspot/jtreg/compiler/compilercontrol/share/scenario/Scenario.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -214,6 +214,7 @@ public final class Scenario {
         }
 
         public void add(CompileCommand compileCommand) {
+            System.out.println(compileCommand);
             String[] vmOptions = compileCommand.command.vmOpts;
             Collections.addAll(vmopts, vmOptions);
             if (compileCommand.command == Command.LOG) {