Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8318306: java/util/Arrays/Sorting.java fails with "Array is not sorted at 8228-th position: 8251.0 and 8153.0" #16230

Closed
wants to merge 7 commits into from

Conversation

vamsi-parasa
Copy link
Contributor

@vamsi-parasa vamsi-parasa commented Oct 17, 2023

The goal of this PR is to address the failure of AVX512 sort test when the following JVM options are enabled (particularly -XX:+DeoptimizeALot) :
-Xcomp -ea -esa -XX:CompileThreshold=100 -XX:+UnlockExperimentalVMOptions -server -XX:-TieredCompilation -XX:+DeoptimizeALot

Description of the error:

The sorting test (test/jdk/java/util/Arrays/Sorting.java) usually fails as shown below (from JBS bug report):

[Arrays.parallelSort] 'Test with check sum' length = 10000, random = C0FFEE, m = 128, CHAR STAGGER
[Arrays.parallelSort] 'Test with check sum' length = 10000, random = C0FFEE, m = 128, SHORT STAGGER
[Arrays.parallelSort] 'Test with check sum' length = 10000, random = C0FFEE, m = 128, FLOAT STAGGER
----------System.err:(25/1026)----------

*** TEST FAILED ***

Array is not sorted at 8228-th position: 8251.0 and 8153.0

java.lang.RuntimeException: Test failed
at Sorting.fail(Sorting.java:644)
at Sorting.checkSorted(Sorting.java:892)
at Sorting.checkSorted(Sorting.java:841)
at Sorting.checkWithCheckSum(Sorting.java:638)
at Sorting.testWithCheckSum(Sorting.java:438)
at Sorting.testBasic(Sorting.java:109)
at Sorting.testCore(Sorting.java:122)
at Sorting.testAll(Sorting.java:140)
at Sorting.testAll(Sorting.java:135)
at Sorting.main(Sorting.java:85)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at com.sun.javatest.regtest.agent.MainWrapper$MainTask.run(MainWrapper.java:138)
at java.base/java.lang.Thread.run(Thread.java:1570)

JavaTest Message: Test threw exception: java.lang.RuntimeException: Test failed
JavaTest Message: shutting down test

STATUS:Failed.`main' threw exception: java.lang.RuntimeException: Test failed

Reproducing the error:

(1) Using test/jdk/java/util/Arrays/Sorting.java:

jtreg -jdk:$JDK_HOME/build/linux-x86_64-server-slowdebug/jdk/ -Xcomp -ea -esa -XX:CompileThreshold=100 -XX:+UnlockExperimentalVMOptions -server -XX:-TieredCompilation -XX:+DeoptimizeALot -timeoutFactor:100 -verbose:all test/jdk/java/util/Arrays/Sorting.java

This failure was also reproduced independently by the author of this PR as follows:
example 1:

[Arrays.parallelSort] 'Test with check sum' length = 10000, random = C0FFEE, m = 2048, DOUBLE REPEATED
[Arrays.parallelSort] 'Test with check sum' length = 10000, random = C0FFEE, m = 2048, INT    DUPLICATED
STDERR:
*** TEST FAILED ***
Array is not sorted at 3536-th position: 1007 and 715

example 2:

[Arrays.parallelSort] 'Test with check sum' length = 10000, random = C0FFEE, m = 16, DOUBLE EQUAL
[Arrays.parallelSort] 'Test with check sum' length = 10000, random = C0FFEE, m = 16, INT    SAW
STDERR:
*** TEST FAILED ***
Array is not sorted at 1787-th position: 1788 and 670

(2) Using a micro:

java -Xcomp -ea -esa -XX:CompileThreshold=25 -XX:+UnlockExperimentalVMOptions -server -XX:-TieredCompilation -XX:+DeoptimizeALot TestDeopt 1e-2 1000 250

import java.util.Arrays;
import java.util.Random;

public class TestDeopt {

    public static void main(String[] args) throws Exception {
        int MAX = 2147483647; // 2^32 - 1
        float fraction = Float.parseFloat(args[0]);
        int size = (int) (fraction * MAX); // size is a fraction of the MAX size
        int iters = Integer.parseInt(args[1]); // number of iterations
        int max = args.length > 2 ? Integer.parseInt(args[2]) : -1 ; // max value for the array elements

        for (int i = 0; i < iters; i++) {
            boolean isSorted = runSort(size, max);
            System.out.println("Iteration " + i + ": is sorted? -> "+ isSorted);
            if (!isSorted) throw new Exception("Test Failed: Array is not properly sorted!");
        }
    }

    static boolean runSort(int size, int max) {
        int[] a = new int[size];
        int[] a_copy = new int[a.length];
        Random rand = new Random();
        for (int i=0; i< a.length; i++) a[i] =  max > 0 ? rand.nextInt(max) : rand.nextInt();
        // call parallel sort
        Arrays.parallelSort(a);
        // check if sorted
        boolean isSorted = true;
        for (int i = 0; i < (a.length -1); i++) isSorted = isSorted && (a[i] <= a[i+1]);
        return isSorted;
    }
}

Suggested Fix:

This appears to be a timing related issue as it's occuring when Arrays.parallelSort() is used along with -XX:+DeoptimizeALot VM option.
Also, when the call to C2 stub happens, there is no guard when deoptimization happens. The partition intrinsic used in the DualPivotQuicksort algorithm returns the pivotIndices (an int[] of size 2). Before the call to the C2 intrinsic stub, the array is being allocated and initialized as shown below:

 // create the pivotIndices array of type int and size = 2
    Node* size = intcon(2);
    Node* klass_node = makecon(TypeKlassPtr::make(ciTypeArrayKlass::make(T_INT)));
    pivotIndices = new_array(klass_node, size, 0);  // no arguments to push
    AllocateArrayNode* alloc = tightly_coupled_allocation(pivotIndices);
    guarantee(alloc != nullptr, "created above");

Thus, the proposed fix is to add the guards for deoptimization. Deoptimization can happen when calling into the runtime to allocate the array and then make sure to re-execute the bytecode if deoptimization happens.

// src/hotspot/share/opto/library_call.cpp
{ PreserveReexecuteState preexecs(this);
    jvms()->set_should_reexecute(true);

  /*.... C2 intrinsic handling ...*/
}

Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8318306: java/util/Arrays/Sorting.java fails with "Array is not sorted at 8228-th position: 8251.0 and 8153.0" (Bug - P2)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/16230/head:pull/16230
$ git checkout pull/16230

Update a local copy of the PR:
$ git checkout pull/16230
$ git pull https://git.openjdk.org/jdk.git pull/16230/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 16230

View PR using the GUI difftool:
$ git pr show -t 16230

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/16230.diff

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Oct 17, 2023

👋 Welcome back vamsi-parasa! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Oct 17, 2023

@vamsi-parasa The following label will be automatically applied to this pull request:

  • hotspot-compiler

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the hotspot-compiler hotspot-compiler-dev@openjdk.org label Oct 17, 2023
@vamsi-parasa
Copy link
Contributor Author

Hello Tobias (@TobiHartmann ),

Could you please have a look at my changes to library_call.cpp in order to address this issue?

Thanks,
Vamsi

@TobiHartmann
Copy link
Member

TobiHartmann commented Oct 18, 2023

Thanks for quickly jumping on this, Vamsi. Could you please add a description to the PR of what the problem is and what your fix does?

Are you able to reproduce the issue? A targeted regression test would be great.

@vamsi-parasa vamsi-parasa changed the title 8318306: Fix for AVX512 sort test failure when DeoptimizeALot is enabled 8318306: java/util/Arrays/Sorting.java fails with "Array is not sorted at 8228-th position: 8251.0 and 8153.0" Oct 18, 2023
@vamsi-parasa
Copy link
Contributor Author

Are you able to reproduce the issue? A targeted regression test would be great.

@TobiHartmann
Thanks Tobias!
I was able to reproduce the issue when -XX:+DeoptimizeALot is used.
Please see the updated description of the issue along with the summary of the suggested fix.

@vamsi-parasa
Copy link
Contributor Author

@TobiHartmann

A targeted regression test would be great.

Also added a micro to reproduce the error in the description.

@TobiHartmann
Copy link
Member

TobiHartmann commented Oct 19, 2023

@vamsi-parasa Thanks for the updates! Please add the micro as jtreg test.

@TobiHartmann
Copy link
Member

Thus the proposed fix is to add the guards for deoptimization to ensure that the correct pivotIndices is returned even when deoptimization happen

Isn't the problem that we can deopt when calling into the runtime to allocate the array and that we then need to make sure that we re-execute the bytecode? Anyway, the fix looks good to me and is in-line with other places like LibraryCallKit::inline_multiplyToLen where we perform array allocation.

@vamsi-parasa
Copy link
Contributor Author

Isn't the problem that we can deopt when calling into the runtime to allocate the array and that we then need to make sure that we re-execute the bytecode?

Please see the description updated to be technically correct, as suggested.

@vamsi-parasa
Copy link
Contributor Author

@vamsi-parasa Thanks for the updates! Please add the micro as jtreg test.

@TobiHartmann
Thank you for your suggestions!
Please see the micro added as a jtreg test.

@vamsi-parasa vamsi-parasa marked this pull request as ready for review October 19, 2023 17:17
@openjdk openjdk bot added the rfr Pull request is ready for review label Oct 19, 2023
@mlbridge
Copy link

mlbridge bot commented Oct 19, 2023

Webrevs

Copy link
Member

@TobiHartmann TobiHartmann left a comment

Choose a reason for hiding this comment

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

I think the test should go into test/hotspot/jtreg/compiler/intrinsics/ because it's a compiler test.

test/jdk/java/util/Arrays/SortingDeoptimizationTest.java Outdated Show resolved Hide resolved
@vamsi-parasa
Copy link
Contributor Author

I think the test should go into test/hotspot/jtreg/compiler/intrinsics/ because it's a compiler test.

Please see the test was moved to test/hotspot/jtreg/compiler/intrinsics/ as suggested.

/*
* @test
* @bug 8318306
* @run main/othervm/timeout=200 -XX:+IgnoreUnrecognizedVMOptions -Xcomp -ea -esa -XX:CompileThreshold=100 -XX:+UnlockExperimentalVMOptions -server -XX:-TieredCompilation -XX:+DeoptimizeALot SortingDeoptimizationTest 1e-2 100 50
Copy link
Member

Choose a reason for hiding this comment

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

Hi @vamsi-parasa, you are already passing -Xcomp which will trigger method compilation on first invocation, CompileThreshold may not be needed, You can pass -Xbatch instead to make compilation a blocking operation.

Copy link
Member

Choose a reason for hiding this comment

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

But please verify that the issue still triggers. It may well be that -Xcomp is required to trigger one early allocation + deopt, followed by additional allocations once we reach the lowered threshold. Also, -Xcomp implies -Xbatch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @vamsi-parasa, you are already passing -Xcomp which will trigger method compilation on first invocation, CompileThreshold may not be needed, You can pass -Xbatch instead to make compilation a blocking operation.

Hi @jatin-bhateja , these flags are actually being used by the later tier tests and were given to me. Hence, they are being retained without any modifications.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, BTW I am consistently able to reproduce the issue with following JVM options.

  • @run main/othervm/timeout=200 -Xbatch -ea -esa -XX:-TieredCompilation -XX:+DeoptimizeALot SortingDeoptimizationTest .1 10 50000

Copy link
Member

@TobiHartmann TobiHartmann left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@openjdk
Copy link

openjdk bot commented Oct 24, 2023

⚠️ @vamsi-parasa the full name on your profile does not match the author name in this pull requests' HEAD commit. If this pull request gets integrated then the author name from this pull requests' HEAD commit will be used for the resulting commit. If you wish to push a new commit with a different author name, then please run the following commands in a local repository of your personal fork:

$ git checkout deopt_simdsort
$ git commit --author='Preferred Full Name <you@example.com>' --allow-empty -m 'Update full name'
$ git push

@openjdk
Copy link

openjdk bot commented Oct 24, 2023

@vamsi-parasa This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8318306: java/util/Arrays/Sorting.java fails with "Array is not sorted at 8228-th position: 8251.0 and 8153.0"

Reviewed-by: thartmann, jbhateja

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 113 new commits pushed to the master branch:

  • 1165037: 8318569: Add getter methods for Locale and Patterns in ListFormat
  • 6f35274: 8318702: G1: Fix nonstandard indentation in g1HeapTransition.cpp
  • e272098: 8318160: javac does not reject private method reference with type-variable receiver
  • 54c613a: 8318693: Fix rendering for code blocks nested under list items in building.md
  • e67550c: 8318509: x86 count_positives intrinsic broken for -XX:AVX3Threshold=0
  • 8879c78: 8317689: [JVMCI] include error message when CreateJavaVM in libgraal fails
  • f9795d0: 8318222: RISC-V: C2 CmpU3
  • d1077d6: 8316046: x64 platforms unecessarily save xmm16-31 when UseAVX >= 3
  • 21d8a47: 8318701: Fix copyright year
  • d4b7612: 8318240: [AIX] Cleaners.java test failure
  • ... and 103 more: https://git.openjdk.org/jdk/compare/5145e5a40a8e9a87b3bc9f236dbf9e4b89094e46...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@TobiHartmann, @jatin-bhateja) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 24, 2023
@vamsi-parasa
Copy link
Contributor Author

Looks good to me.

@TobiHartmann
Thank you Tobias for your suggestions!

@vamsi-parasa
Copy link
Contributor Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Oct 24, 2023
@openjdk
Copy link

openjdk bot commented Oct 24, 2023

@vamsi-parasa
Your change (at version d6ba70f) is now ready to be sponsored by a Committer.

@sviswa7
Copy link

sviswa7 commented Oct 24, 2023

/sponsor

@openjdk
Copy link

openjdk bot commented Oct 24, 2023

Going to push as commit 1f2a80b.
Since your change was applied there have been 113 commits pushed to the master branch:

  • 1165037: 8318569: Add getter methods for Locale and Patterns in ListFormat
  • 6f35274: 8318702: G1: Fix nonstandard indentation in g1HeapTransition.cpp
  • e272098: 8318160: javac does not reject private method reference with type-variable receiver
  • 54c613a: 8318693: Fix rendering for code blocks nested under list items in building.md
  • e67550c: 8318509: x86 count_positives intrinsic broken for -XX:AVX3Threshold=0
  • 8879c78: 8317689: [JVMCI] include error message when CreateJavaVM in libgraal fails
  • f9795d0: 8318222: RISC-V: C2 CmpU3
  • d1077d6: 8316046: x64 platforms unecessarily save xmm16-31 when UseAVX >= 3
  • 21d8a47: 8318701: Fix copyright year
  • d4b7612: 8318240: [AIX] Cleaners.java test failure
  • ... and 103 more: https://git.openjdk.org/jdk/compare/5145e5a40a8e9a87b3bc9f236dbf9e4b89094e46...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Oct 24, 2023
@openjdk openjdk bot closed this Oct 24, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels Oct 24, 2023
@openjdk
Copy link

openjdk bot commented Oct 24, 2023

@sviswa7 @vamsi-parasa Pushed as commit 1f2a80b.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot-compiler hotspot-compiler-dev@openjdk.org integrated Pull request has been integrated
4 participants