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

8346107: Generators: testing utility for random value generation #22941

Open
wants to merge 22 commits into
base: master
Choose a base branch
from

Conversation

theoweidmannoracle
Copy link
Contributor

@theoweidmannoracle theoweidmannoracle commented Jan 7, 2025

This PR is a refactoring and partial rewrite of #22716 by @eme64. The goals remain the same:

For verification testing, it is often critical to generate "interesting" values, to provoke overflows, NaN, etc. And to generate these values in the correct distribution to trigger certain optimizations.

I would like to start a collection of such generators, that can then be used in testing.

The goal is to grow this collection in the future, and add new types. For example byte, char, short, or even Float16.

This will be helpful for the Template framework JDK-8344942, but also other tests.

Related PR, for value verification: #22715

The refactoring makes use of generics, rendering the generators library more flexible by default, by allowing it work with arbitrary types (with special features for Comparable types), improving the composability of different generators and streamlining the client API for simplicity. This allows test authors to quickly compose their own distributions and generators if necessary. An overview of this functionality is provided in the Generators javadoc.


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-8346107: Generators: testing utility for random value generation (Enhancement - P4)

Contributors

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 22941

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Jan 7, 2025

👋 Welcome back tweidmann! 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 Jan 7, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk
Copy link

openjdk bot commented Jan 7, 2025

@theoweidmannoracle 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.

@theoweidmannoracle
Copy link
Contributor Author

/contributor @eme64

@openjdk
Copy link

openjdk bot commented Jan 7, 2025

@theoweidmannoracle Syntax: /contributor (add|remove) [@user | openjdk-user | Full Name <email@address>]. For example:

  • /contributor add @openjdk-bot
  • /contributor add duke
  • /contributor add J. Duke <[email protected]>

User names can only be used for users in the census associated with this repository. For other contributors you need to supply the full name and email address.

@theoweidmannoracle
Copy link
Contributor Author

/contributor add @eme64

@openjdk
Copy link

openjdk bot commented Jan 7, 2025

@theoweidmannoracle
Contributor Emanuel Peter <[email protected]> successfully added.

@theoweidmannoracle theoweidmannoracle marked this pull request as ready for review January 7, 2025 09:13
@openjdk openjdk bot added the rfr Pull request is ready for review label Jan 7, 2025
@mlbridge
Copy link

mlbridge bot commented Jan 7, 2025

Webrevs

Copy link
Contributor

@eme64 eme64 left a comment

Choose a reason for hiding this comment

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

Nice work! I have a few comments in the code.

I am also wondering if we can still do this:

  • pick a random int distribution -> suppose we get mixed special + uniform
  • now sample from different int ranges, all from that same underlying distribution:
    • [0, max]
    • [10, 20]
    • ...

I think it is not possible:

gen = Generators.ints();
// gen is not resrictable, sadly... but I would like to do
gen1 = gen.restrict(0, max);
v1 = gen1.next();
gen2 = gen.restrict(10, 20);
v2 = gen2.next()

If that is indeed not possible:
How can we ensure the continuity of the distribution across different range restrictions, if we want to pick a random distribution?


Edit: let me explain why I need this.

Suppose we will work with the Template Framework, once we have it.

#{g1:int_con(0,int_max)}
#{g2:int_con(0,1000)}
#{g3:int_con}

Basically: I am sampling 3 values from different distributions. Now suppose some thing crazy only happens when all 3 values are close to powers of 2. To make that somewhat likely, I would like to first draw a random distribution (which may be the one with only powers of 2, and neighbors), and then draw all values from that distribution.

/**
* Returns the next value from the stream.
*/
T next();
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this not have a @return?

Why don't you run something like:
/oracle-work/jdk-fork0/build/linux-x64-debug/jdk/bin/javadoc -sourcepath test/hotspot/jtreg:./test/lib compiler.lib.generators
And make sure you don't have any errors/warnings :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What would you like to see here with @return? I don't think there's any more information I can provide? Do you think the entire doc comment should be like below? In my experience that can be detrimental for some IDEs as they will only show the main text (for a lack of a better word) in some circumstances.

  /**
    * @returns the next value from the stream.
    */

To get away with all the warnings would mean documenting every single parameter everywhere (all lo's and hi's). Do you think that's really necessary?

* "interesting" distributions which might trigger various behaviours in
* optimizations.
* <p>
* Normally, clients get the default Generators instance by referring to the static variable {@link #G}.
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to have an example test that uses it as you would expect.

test/hotspot/jtreg/compiler/lib/generators/Generators.java Outdated Show resolved Hide resolved
test/hotspot/jtreg/compiler/lib/generators/Generators.java Outdated Show resolved Hide resolved
Comment on lines 66 to 78
static void testMixed() {
mockRandomness
.checkEmpty()
.enqueueInteger(0, 10, 7)
.enqueueInteger(0, 10, 5)
.enqueueInteger(0, 31, 4)
.enqueueInteger(0, 10, 1)
.enqueueInteger(0, 31, 18);
var g = mockGS.mixed(mockGS.uniformInts(0, 30), mockGS.single(-1), 7, 3);
Asserts.assertEQ(g.next(), -1);
Asserts.assertEQ(g.next(), 4);
Asserts.assertEQ(g.next(), 18);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice if you told us / a future person who extends this, what this mocking does, and how it works.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean specifically how this test here works or how the mocking works? Or both?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe just say what values you feed in, and why it produces the results.
That should hopefully help a future person who tries to extend the test for their own type.

Copy link
Contributor Author

@theoweidmannoracle theoweidmannoracle Jan 8, 2025

Choose a reason for hiding this comment

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

I documented MockRandomnessSource and added comments to this test case. I hope that provides enough clarifications to understand the other tests as it is always the same pattern (and this is definitely the most complicated one).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot-compiler [email protected] rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

2 participants