Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8271471: [IR Framework] Rare occurrence of "<!-- safepoint while prin…
…ting -->" in PrintIdeal/PrintOptoAssembly can let tests fail

Reviewed-by: kvn, thartmann
  • Loading branch information
chhagedorn committed Aug 16, 2021
1 parent 0a03481 commit 3677734
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 24 deletions.
Expand Up @@ -39,6 +39,8 @@
* Parse the hotspot pid file of the test VM to match all @IR rules.
*/
public class IRMatcher {
public static final String SAFEPOINT_WHILE_PRINTING_MESSAGE = "<!-- safepoint while printing -->";

private static final boolean PRINT_IR_ENCODING = Boolean.parseBoolean(System.getProperty("PrintIREncoding", "false"));
private static final Pattern IR_ENCODING_PATTERN =
Pattern.compile("(?<=" + IREncodingPrinter.START + "\r?\n)[\\s\\S]*(?=" + IREncodingPrinter.END + ")");
Expand Down Expand Up @@ -487,7 +489,16 @@ private void reportFailuresIfAny() {
+ "-".repeat(String.valueOf(failures).length()) + System.lineSeparator());
failuresBuilder.append(">>> Check stdout for compilation output of the failed methods")
.append(System.lineSeparator()).append(System.lineSeparator());
throw new IRViolationException(failuresBuilder.toString(), compilationsBuilder.toString());

// In some very rare cases, the VM output to regex match on contains "<!-- safepoint while printing -->"
// (emitted by ttyLocker::break_tty_for_safepoint) which might be the reason for a matching error.
// Do not throw an exception in this case (i.e. bailout).
String compilations = compilationsBuilder.toString();
if (!compilations.contains(SAFEPOINT_WHILE_PRINTING_MESSAGE)) {
throw new IRViolationException(failuresBuilder.toString(), compilationsBuilder.toString());
} else {
System.out.println("Found " + SAFEPOINT_WHILE_PRINTING_MESSAGE + ", bail out of IR matching");
}
}
}
}
Expand Up @@ -42,7 +42,7 @@
* @requires vm.debug == true & vm.compMode != "Xint" & vm.compiler2.enabled & vm.flagless
* @summary Test IR matcher with different default IR node regexes. Use -DPrintIREncoding.
* Normally, the framework should be called with driver.
* @library /test/lib /
* @library /test/lib /testlibrary_tests /
* @build sun.hotspot.WhiteBox
* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox
* @run main/othervm/timeout=240 -Xbootclasspath/a:. -DSkipWhiteBoxInstall=true -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
Expand Down Expand Up @@ -224,7 +224,7 @@ public static void main(String[] args) {

try {
runWithArgumentsFail(CompilationOutputOfFails.class);
shouldNotReach();
Utils.shouldHaveThrownException();
} catch (IRViolationException e) {
try {
boolean failed = false;
Expand Down Expand Up @@ -321,7 +321,7 @@ private static void runWithArgumentsFail(Class<?> clazz, String... args) {
private static void runCheck(String[] args , Constraint... constraints) {
try {
new TestFramework(constraints[0].getKlass()).addFlags(args).start(); // All constraints have the same class.
shouldNotReach();
Utils.shouldHaveThrownException();
} catch (IRViolationException e) {
checkConstraints(e, constraints);
} catch (Exception e) {
Expand All @@ -332,7 +332,7 @@ private static void runCheck(String[] args , Constraint... constraints) {
private static void runCheck(Constraint... constraints) {
try {
TestFramework.run(constraints[0].getKlass()); // All constraints have the same class.
shouldNotReach();
Utils.shouldHaveThrownException();
} catch (IRViolationException e) {
checkConstraints(e, constraints);
} catch (Exception e) {
Expand All @@ -357,7 +357,7 @@ private static void checkConstraints(IRViolationException e, Constraint[] constr
private static void runFailOnTestsArgs(Constraint constraint, String... args) {
try {
new TestFramework(constraint.getKlass()).addFlags(args).start(); // All constraints have the same class.
shouldNotReach();
Utils.shouldHaveThrownException();
} catch (IRViolationException e) {
try {
constraint.checkConstraint(e);
Expand All @@ -369,10 +369,6 @@ private static void runFailOnTestsArgs(Constraint constraint, String... args) {
}
}

public static void shouldNotReach() {
throw new ShouldNotReachException("Framework did not fail but it should have!");
}

public static void findIrIds(String output, String method, int... numbers) {
StringBuilder builder = new StringBuilder();
builder.append(method);
Expand Down Expand Up @@ -1412,12 +1408,6 @@ class MyClassSub extends MyClass {
static int iFldStatic;
}

class ShouldNotReachException extends RuntimeException {
ShouldNotReachException(String s) {
super(s);
}
}


// Base class for any kind of constraint that is used to verify if the framework reports the correct IR failures.
abstract class Constraint {
Expand Down
Expand Up @@ -34,7 +34,7 @@
* @test
* @requires vm.debug == true & vm.compMode != "Xint" & vm.compiler2.enabled & vm.flagless
* @summary Test different custom run tests.
* @library /test/lib /
* @library /test/lib /testlibrary_tests /
* @run driver ir_framework.tests.TestRunTests
*/

Expand All @@ -44,7 +44,7 @@ public static void main(String[] args) {
TestFramework.run();
try {
TestFramework.run(BadStandalone.class);
throw new RuntimeException("Should not reach");
Utils.shouldHaveThrownException();
} catch (IRViolationException e) {
String[] matches = { "test(int)", "test2(int)", "Failed IR Rules (2)"};
Arrays.stream(matches).forEach(m -> Asserts.assertTrue(e.getExceptionInfo().contains(m)));
Expand Down
Expand Up @@ -31,7 +31,7 @@
* @test
* @requires vm.debug == true & vm.compMode != "Xint" & vm.compiler2.enabled & vm.flagless
* @summary Test scenarios with the framework.
* @library /test/lib /
* @library /test/lib /testlibrary_tests /
* @run driver ir_framework.tests.TestScenarios
*/

Expand All @@ -44,15 +44,27 @@ public static void main(String[] args) {
Scenario s3dup = new Scenario(3, "-XX:TLABRefillWasteFraction=53");
try {
new TestFramework().addScenarios(sDefault, s1, s2, s3).start();
Asserts.fail("Should not reach");
if (Utils.notAllBailedOut(sDefault, s1, s3)) {
// Not all scenarios had a bailout which means that at least one exception should have been thrown.
Asserts.fail("Should have thrown an exception");
}
} catch (TestRunException e) {
Asserts.assertTrue(e.getMessage().contains("The following scenarios have failed: #0, #1, #3"), e.getMessage());
if (!e.getMessage().contains("The following scenarios have failed: #0, #1, #3")) {
// Was there a bailout in a scenario? If not fail.
Asserts.assertTrue(Utils.anyBailedOut(sDefault, s1, s3), e.getMessage());
}
}
try {
new TestFramework().addScenarios(s1, s2, s3).start();
Asserts.fail("Should not reach");
if (Utils.notAllBailedOut(s1, s3)) {
// Not all scenarios had a bailout which means that at least one exception should have been thrown.
Asserts.fail("Should have thrown an exception");
}
} catch (TestRunException e) {
Asserts.assertTrue(e.getMessage().contains("The following scenarios have failed: #1, #3"), e.getMessage());
if (!e.getMessage().contains("The following scenarios have failed: #1, #3")) {
// Was there a bailout in a scenario? If not fail.
Asserts.assertTrue(Utils.anyBailedOut(sDefault, s1, s3), e.getMessage());
}
}
new TestFramework(ScenarioTest.class).addScenarios(s1, s2, s3).start();
try {
Expand All @@ -71,7 +83,6 @@ public static void main(String[] args) {
} catch (Exception e) {
Asserts.fail("Should not catch other exceptions");
}

}

@Test
Expand Down
55 changes: 55 additions & 0 deletions test/hotspot/jtreg/testlibrary_tests/ir_framework/tests/Utils.java
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2021, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package ir_framework.tests;

import compiler.lib.ir_framework.Scenario;
import compiler.lib.ir_framework.driver.IRMatcher;
import compiler.lib.ir_framework.driver.TestVMProcess;
import jdk.test.lib.Asserts;

import java.util.Arrays;

public class Utils {
public static void shouldHaveThrownException() {
// Do not throw an exception if we hit a safepoint while printing which could possibly let the IR matching fail.
// This happens very rarely. If there is a problem with the test, then we will catch that on the next test invocation.
if (!TestVMProcess.getLastTestVMOutput().contains(IRMatcher.SAFEPOINT_WHILE_PRINTING_MESSAGE)) {
Asserts.fail("Should have thrown exception");
}
}

/**
* Is there at least one scenario which hit a safepoint while printing (i.e. a bailout)?
*/
public static boolean anyBailedOut(Scenario... scenarios) {
return Arrays.stream(scenarios).anyMatch(s -> s.getTestVMOutput().contains(IRMatcher.SAFEPOINT_WHILE_PRINTING_MESSAGE));
}

/**
* Is there at least one scenario which did not hit a safepoint while printing (i.e. a bailout)?
*/
public static boolean notAllBailedOut(Scenario... scenarios) {
return Arrays.stream(scenarios).anyMatch(s -> !s.getTestVMOutput().contains(IRMatcher.SAFEPOINT_WHILE_PRINTING_MESSAGE));
}
}

3 comments on commit 3677734

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 3677734 Dec 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 3677734 Dec 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin the backport was successfully created on the branch GoeLin-backport-36777345 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 36777345 from the openjdk/jdk repository.

The commit being backported was authored by Christian Hagedorn on 16 Aug 2021 and was reviewed by Vladimir Kozlov and Tobias Hartmann.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev GoeLin-backport-36777345:GoeLin-backport-36777345
$ git checkout GoeLin-backport-36777345
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev GoeLin-backport-36777345

Please sign in to comment.